Skip to content

Commit

Permalink
Make ColorScheme a WinRT object
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-zamora committed Aug 10, 2020
1 parent e6c71cb commit c60f366
Show file tree
Hide file tree
Showing 12 changed files with 122 additions and 60 deletions.
6 changes: 4 additions & 2 deletions src/cascadia/TerminalApp/CascadiaSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ Author(s):
#include "Profile.h"
#include "IDynamicProfileGenerator.h"

#include "ColorScheme.h"

// fwdecl unittest classes
namespace TerminalAppLocalTests
{
Expand Down Expand Up @@ -70,7 +72,7 @@ class TerminalApp::CascadiaSettings final
static std::filesystem::path GetDefaultSettingsPath();

const Profile* FindProfile(GUID profileGuid) const noexcept;
const ColorScheme* GetColorSchemeForProfile(const GUID profileGuid) const;
const winrt::TerminalApp::ColorScheme* GetColorSchemeForProfile(const GUID profileGuid) const;

std::vector<TerminalApp::SettingsLoadWarnings>& GetWarnings();

Expand All @@ -91,7 +93,7 @@ class TerminalApp::CascadiaSettings final
void _LayerOrCreateProfile(const Json::Value& profileJson);
Profile* _FindMatchingProfile(const Json::Value& profileJson);
void _LayerOrCreateColorScheme(const Json::Value& schemeJson);
ColorScheme* _FindMatchingColorScheme(const Json::Value& schemeJson);
winrt::TerminalApp::implementation::ColorScheme* _FindMatchingColorScheme(const Json::Value& schemeJson);
void _ParseJsonString(std::string_view fileData, const bool isDefaultSettings);
static const Json::Value& _GetProfilesJsonObject(const Json::Value& json);
static const Json::Value& _GetDisabledProfileSourcesJsonObject(const Json::Value& json);
Expand Down
9 changes: 5 additions & 4 deletions src/cascadia/TerminalApp/CascadiaSettingsSerialization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -654,7 +654,8 @@ void CascadiaSettings::_LayerOrCreateColorScheme(const Json::Value& schemeJson)
}
else
{
_globals.AddColorScheme(ColorScheme::FromJson(schemeJson));
const auto scheme = implementation::ColorScheme::FromJson(schemeJson);
_globals.AddColorScheme(*scheme);
}
}

Expand All @@ -669,9 +670,9 @@ void CascadiaSettings::_LayerOrCreateColorScheme(const Json::Value& schemeJson)
// Return Value:
// - a ColorScheme that can be layered with the given json object, iff such a
// color scheme exists.
ColorScheme* CascadiaSettings::_FindMatchingColorScheme(const Json::Value& schemeJson)
implementation::ColorScheme* CascadiaSettings::_FindMatchingColorScheme(const Json::Value& schemeJson)
{
if (auto schemeName = ColorScheme::GetNameFromJson(schemeJson))
if (auto schemeName = implementation::ColorScheme::GetNameFromJson(schemeJson))
{
auto& schemes = _globals.GetColorSchemes();
auto iterator = schemes.find(*schemeName);
Expand All @@ -681,7 +682,7 @@ ColorScheme* CascadiaSettings::_FindMatchingColorScheme(const Json::Value& schem
// maybe not the _safest_ thing, but we have a mind to make Profile
// and ColorScheme winrt types in the future, so this will be safer
// then.
return &iterator->second;
return winrt::get_self<implementation::ColorScheme>(iterator->second);
}
}
return nullptr;
Expand Down
34 changes: 21 additions & 13 deletions src/cascadia/TerminalApp/ColorScheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@
#include "Utils.h"
#include "JsonUtils.h"

#include "ColorScheme.g.cpp"

using namespace ::Microsoft::Console;
using namespace TerminalApp;
using namespace winrt::TerminalApp;
using namespace winrt::TerminalApp::implementation;

static constexpr std::string_view NameKey{ "name" };
static constexpr std::string_view ForegroundKey{ "foreground" };
Expand Down Expand Up @@ -46,7 +48,7 @@ ColorScheme::ColorScheme() :
{
}

ColorScheme::ColorScheme(std::wstring name, til::color defaultFg, til::color defaultBg, til::color cursorColor) :
ColorScheme::ColorScheme(winrt::hstring name, uint32_t defaultFg, uint32_t defaultBg, uint32_t cursorColor) :
_schemeName{ name },
_table{},
_defaultForeground{ defaultFg },
Expand Down Expand Up @@ -87,10 +89,10 @@ void ColorScheme::ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl:
// - json: an object which should be a serialization of a ColorScheme object.
// Return Value:
// - a new ColorScheme instance created from the values in `json`
ColorScheme ColorScheme::FromJson(const Json::Value& json)
winrt::com_ptr<winrt::TerminalApp::ColorScheme> ColorScheme::FromJson(const Json::Value& json)
{
ColorScheme result;
result.LayerJson(json);
auto result = winrt::make_self<ColorScheme>();
result->LayerJson(json);
return result;
}

Expand Down Expand Up @@ -138,32 +140,38 @@ void ColorScheme::LayerJson(const Json::Value& json)
}
}

std::wstring_view ColorScheme::GetName() const noexcept
winrt::hstring ColorScheme::Name() const noexcept
{
return { _schemeName };
return { _schemeName.c_str() };
}

std::array<til::color, COLOR_TABLE_SIZE>& ColorScheme::GetTable() noexcept
winrt::com_array<uint32_t> ColorScheme::Table() noexcept
{
return _table;
winrt::com_array<uint32_t> result = { COLOR_TABLE_SIZE };
for (int i = 0; i < _table.size(); i++)
{
result[i] = _table[i];
}
return result;
//return _table;
}

til::color ColorScheme::GetForeground() const noexcept
uint32_t ColorScheme::Foreground() const noexcept
{
return _defaultForeground;
}

til::color ColorScheme::GetBackground() const noexcept
uint32_t ColorScheme::Background() const noexcept
{
return _defaultBackground;
}

til::color ColorScheme::GetSelectionBackground() const noexcept
uint32_t ColorScheme::SelectionBackground() const noexcept
{
return _selectionBackground;
}

til::color ColorScheme::GetCursorColor() const noexcept
uint32_t ColorScheme::CursorColor() const noexcept
{
return _cursorColor;
}
Expand Down
66 changes: 36 additions & 30 deletions src/cascadia/TerminalApp/ColorScheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,48 +18,54 @@ Author(s):
#include "TerminalSettings.h"
#include "../../inc/conattrs.hpp"

#include "ColorScheme.g.h"
#include "til.h"

// fwdecl unittest classes
namespace TerminalAppLocalTests
{
class SettingsTests;
class ColorSchemeTests;
};

namespace TerminalApp
namespace winrt::TerminalApp::implementation
{
class ColorScheme;
};
struct ColorScheme : ColorSchemeT<ColorScheme>
{
public:
ColorScheme();
ColorScheme(hstring name, uint32_t defaultFg, uint32_t defaultBg, uint32_t cursorColor);
~ColorScheme();

class TerminalApp::ColorScheme
{
public:
ColorScheme();
ColorScheme(std::wstring name, til::color defaultFg, til::color defaultBg, til::color cursorColor);
~ColorScheme();
void ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const;

void ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const;
static com_ptr<TerminalApp::ColorScheme> FromJson(const Json::Value& json);
bool ShouldBeLayered(const Json::Value& json) const;
void LayerJson(const Json::Value& json);

static ColorScheme FromJson(const Json::Value& json);
bool ShouldBeLayered(const Json::Value& json) const;
void LayerJson(const Json::Value& json);
hstring Name() const noexcept;
com_array<uint32_t> Table() noexcept;
uint32_t Foreground() const noexcept;
uint32_t Background() const noexcept;
uint32_t SelectionBackground() const noexcept;
uint32_t CursorColor() const noexcept;

std::wstring_view GetName() const noexcept;
std::array<til::color, COLOR_TABLE_SIZE>& GetTable() noexcept;
til::color GetForeground() const noexcept;
til::color GetBackground() const noexcept;
til::color GetSelectionBackground() const noexcept;
til::color GetCursorColor() const noexcept;
static std::optional<std::wstring> GetNameFromJson(const Json::Value& json);

static std::optional<std::wstring> GetNameFromJson(const Json::Value& json);
private:
std::wstring _schemeName;
std::array<uint32_t, COLOR_TABLE_SIZE> _table;
til::color _defaultForeground;
til::color _defaultBackground;
til::color _selectionBackground;
til::color _cursorColor;

private:
std::wstring _schemeName;
std::array<til::color, COLOR_TABLE_SIZE> _table;
til::color _defaultForeground;
til::color _defaultBackground;
til::color _selectionBackground;
til::color _cursorColor;
friend class TerminalAppLocalTests::SettingsTests;
friend class TerminalAppLocalTests::ColorSchemeTests;
};
}

friend class TerminalAppLocalTests::SettingsTests;
friend class TerminalAppLocalTests::ColorSchemeTests;
};
namespace winrt::TerminalApp::factory_implementation
{
BASIC_FACTORY(ColorScheme);
}
23 changes: 23 additions & 0 deletions src/cascadia/TerminalApp/ColorScheme.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import "../TerminalSettings.idl";

namespace TerminalApp
{
[default_interface] runtimeclass ColorScheme {
ColorScheme();
ColorScheme(String name, UInt32 defaultFg, UInt32 defaultBg, UInt32 cursorColor);

void ApplyScheme(TerminalSettings terminalSettings);

String Name { get; };

UInt32 Foreground { get; };
UInt32 Background { get; };
UInt32 SelectionBackground { get; };
UInt32 CursorColor { get; };

UInt32[] Table { get; };
}
}
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/GlobalAppSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,7 @@ void GlobalAppSettings::LayerJson(const Json::Value& json)
// - <none>
void GlobalAppSettings::AddColorScheme(ColorScheme scheme)
{
std::wstring name{ scheme.GetName() };
std::wstring name{ scheme.Name() };
_colorSchemes[name] = std::move(scheme);
}

Expand Down
11 changes: 6 additions & 5 deletions src/cascadia/TerminalApp/GlobalAppSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,11 @@ Author(s):
--*/
#pragma once
#include "AppKeyBindings.h"
#include "ColorScheme.h"
#include "Command.h"
#include "SettingsTypes.h"

#include "ColorScheme.g.h"

// fwdecl unittest classes
namespace TerminalAppLocalTests
{
Expand All @@ -37,9 +38,9 @@ class TerminalApp::GlobalAppSettings final
GlobalAppSettings();
~GlobalAppSettings();

std::unordered_map<std::wstring, ColorScheme>& GetColorSchemes() noexcept;
const std::unordered_map<std::wstring, ColorScheme>& GetColorSchemes() const noexcept;
void AddColorScheme(ColorScheme scheme);
std::unordered_map<std::wstring, winrt::TerminalApp::ColorScheme>& GetColorSchemes() noexcept;
const std::unordered_map<std::wstring, winrt::TerminalApp::ColorScheme>& GetColorSchemes() const noexcept;
void AddColorScheme(winrt::TerminalApp::ColorScheme scheme);

winrt::TerminalApp::AppKeyBindings GetKeybindings() const noexcept;

Expand Down Expand Up @@ -88,7 +89,7 @@ class TerminalApp::GlobalAppSettings final
winrt::com_ptr<winrt::TerminalApp::implementation::AppKeyBindings> _keybindings;
std::vector<::TerminalApp::SettingsLoadWarnings> _keybindingsWarnings;

std::unordered_map<std::wstring, ColorScheme> _colorSchemes;
std::unordered_map<std::wstring, winrt::TerminalApp::ColorScheme> _colorSchemes;
std::unordered_map<winrt::hstring, winrt::TerminalApp::Command> _commands;

friend class TerminalAppLocalTests::SettingsTests;
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalApp/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ Author(s):
--*/
#pragma once
#include "ColorScheme.h"
#include "ColorScheme.g.h"
#include "SettingsTypes.h"

// fwdecl unittest classes
Expand Down Expand Up @@ -46,7 +46,7 @@ class TerminalApp::Profile final

~Profile();

winrt::TerminalApp::TerminalSettings CreateTerminalSettings(const std::unordered_map<std::wstring, ColorScheme>& schemes) const;
winrt::TerminalApp::TerminalSettings CreateTerminalSettings(const std::unordered_map<std::wstring, winrt::TerminalApp::ColorScheme>& schemes) const;

Json::Value GenerateStub() const;
static Profile FromJson(const Json::Value& json);
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/TerminalPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -594,7 +594,7 @@ namespace winrt::TerminalApp::implementation
const auto* scheme = _settings->GetColorSchemeForProfile(profileGuid);
// If they explicitly specified `null` as the scheme (indicating _no_ scheme), log
// that as the empty string.
const auto schemeName = scheme ? scheme->GetName() : L"\0";
const auto schemeName = scheme ? scheme->Name() : L"\0";

TraceLoggingWrite(
g_hTerminalAppProvider, // handle to TerminalApp tracelogging provider
Expand Down
9 changes: 7 additions & 2 deletions src/cascadia/TerminalApp/lib/TerminalAppLib.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,9 @@
<DependentUpon>../Tab.idl</DependentUpon>
</ClInclude>
<ClInclude Include="../Pane.h" />
<ClInclude Include="../ColorScheme.h" />
<ClInclude Include="../ColorScheme.h">
<DependentUpon>../ColorScheme.idl</DependentUpon>
</ClInclude>
<ClInclude Include="../GlobalAppSettings.h" />
<ClInclude Include="../Profile.h" />
<ClInclude Include="../CascadiaSettings.h" />
Expand Down Expand Up @@ -175,7 +177,9 @@
<DependentUpon>../Tab.idl</DependentUpon>
</ClCompile>
<ClCompile Include="../Pane.cpp" />
<ClCompile Include="../ColorScheme.cpp" />
<ClCompile Include="../ColorScheme.cpp">
<DependentUpon>../ColorScheme.idl</DependentUpon>
</ClCompile>
<ClCompile Include="../GlobalAppSettings.cpp" />
<ClCompile Include="../Profile.cpp" />
<ClCompile Include="../CascadiaSettings.cpp" />
Expand Down Expand Up @@ -263,6 +267,7 @@
<Midl Include="../CommandKeyChordVisibilityConverter.idl" />
<Midl Include="../Tab.idl" />
<Midl Include="../TerminalSettings.idl" />
<Midl Include="..\ColorScheme.idl" />
</ItemGroup>
<!-- ========================= Misc Files ======================== -->
<ItemGroup>
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalApp/lib/TerminalAppLib.vcxproj.filters
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,9 @@
<Midl Include="../TerminalSettings.idl">
<Filter>settings</Filter>
</Midl>
<Midl Include="..\ColorScheme.idl">
<Filter>settings</Filter>
</Midl>
</ItemGroup>
<ItemGroup>
<None Include="../packages.config" />
Expand Down
13 changes: 13 additions & 0 deletions src/inc/til/color.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,19 @@ namespace til // Terminal Implementation Library. Also: "Today I Learned"
color& operator=(color&&) = default;
~color() = default;

constexpr color(uint32_t c) :
r{ static_cast<uint8_t>(c & 0xFF) },
g{ static_cast<uint8_t>((c & 0xFF00) >> 8) },
b{ static_cast<uint8_t>((c & 0xFF0000) >> 16) },
a{ 255 }
{
}

operator uint32_t() const noexcept
{
return static_cast<uint32_t>(r) | (static_cast<uint32_t>(g) << 8) | (static_cast<uint32_t>(b) << 16);
}

#ifdef _WINDEF_
constexpr color(COLORREF c) :
r{ static_cast<uint8_t>(c & 0xFF) },
Expand Down

0 comments on commit c60f366

Please sign in to comment.