Skip to content

Commit

Permalink
polish ColorScheme
Browse files Browse the repository at this point in the history
  • Loading branch information
carlos-zamora committed Sep 11, 2020
1 parent a5fccc6 commit c69572b
Show file tree
Hide file tree
Showing 4 changed files with 137 additions and 93 deletions.
129 changes: 53 additions & 76 deletions src/cascadia/TerminalApp/ColorScheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,46 +20,48 @@ static constexpr std::string_view ForegroundKey{ "foreground" };
static constexpr std::string_view BackgroundKey{ "background" };
static constexpr std::string_view SelectionBackgroundKey{ "selectionBackground" };
static constexpr std::string_view CursorColorKey{ "cursorColor" };

static constexpr std::string_view BlackKey{ "black" };
static constexpr std::string_view RedKey{ "red" };
static constexpr std::string_view GreenKey{ "green" };
static constexpr std::string_view YellowKey{ "yellow" };
static constexpr std::string_view BlueKey{ "blue" };
static constexpr std::string_view PurpleKey{ "purple" };
static constexpr std::string_view CyanKey{ "cyan" };
static constexpr std::string_view WhiteKey{ "white" };
static constexpr std::string_view BrightBlackKey{ "brightBlack" };
static constexpr std::string_view BrightRedKey{ "brightRed" };
static constexpr std::string_view BrightGreenKey{ "brightGreen" };
static constexpr std::string_view BrightYellowKey{ "brightYellow" };
static constexpr std::string_view BrightBlueKey{ "brightBlue" };
static constexpr std::string_view BrightPurpleKey{ "brightPurple" };
static constexpr std::string_view BrightCyanKey{ "brightCyan" };
static constexpr std::string_view BrightWhiteKey{ "brightWhite" };

static constexpr std::array<std::string_view, 16> TableColors = {
"black",
"red",
"green",
"yellow",
"blue",
"purple",
"cyan",
"white",
"brightBlack",
"brightRed",
"brightGreen",
"brightYellow",
"brightBlue",
"brightPurple",
"brightCyan",
"brightWhite"
BlackKey,
RedKey,
GreenKey,
YellowKey,
BlueKey,
PurpleKey,
CyanKey,
WhiteKey,
BrightBlackKey,
BrightRedKey,
BrightGreenKey,
BrightYellowKey,
BrightBlueKey,
BrightPurpleKey,
BrightCyanKey,
BrightWhiteKey,
};

ColorScheme::ColorScheme() :
_schemeName{ L"" },
_table{},
_defaultForeground{ DEFAULT_FOREGROUND_WITH_ALPHA },
_defaultBackground{ DEFAULT_BACKGROUND_WITH_ALPHA },
_selectionBackground{ DEFAULT_FOREGROUND },
_cursorColor{ DEFAULT_CURSOR_COLOR }
{
}

ColorScheme::ColorScheme(winrt::hstring name, Color defaultFg, Color defaultBg, Color cursorColor) :
_schemeName{ name },
_table{},
_defaultForeground{ defaultFg },
_defaultBackground{ defaultBg },
_selectionBackground{ DEFAULT_FOREGROUND },
_cursorColor{ cursorColor }
{
}

ColorScheme::~ColorScheme()
_Name{ name },
_Foreground{ defaultFg },
_Background{ defaultBg },
_CursorColor{ cursorColor }
{
}

Expand All @@ -72,10 +74,10 @@ ColorScheme::~ColorScheme()
// - <none>
void ColorScheme::ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const
{
terminalSettings.DefaultForeground(static_cast<COLORREF>(_defaultForeground));
terminalSettings.DefaultBackground(static_cast<COLORREF>(_defaultBackground));
terminalSettings.SelectionBackground(static_cast<COLORREF>(_selectionBackground));
terminalSettings.CursorColor(static_cast<COLORREF>(_cursorColor));
terminalSettings.DefaultForeground(static_cast<COLORREF>(_Foreground));
terminalSettings.DefaultBackground(static_cast<COLORREF>(_Background));
terminalSettings.SelectionBackground(static_cast<COLORREF>(_SelectionBackground));
terminalSettings.CursorColor(static_cast<COLORREF>(_CursorColor));

auto const tableCount = gsl::narrow_cast<int>(_table.size());
for (int i = 0; i < tableCount; i++)
Expand Down Expand Up @@ -110,7 +112,7 @@ bool ColorScheme::ShouldBeLayered(const Json::Value& json) const
std::wstring nameFromJson{};
if (JsonUtils::GetValueForKey(json, NameKey, nameFromJson))
{
return nameFromJson == _schemeName;
return nameFromJson == _Name;
}
return false;
}
Expand All @@ -127,11 +129,11 @@ bool ColorScheme::ShouldBeLayered(const Json::Value& json) const
// <none>
void ColorScheme::LayerJson(const Json::Value& json)
{
JsonUtils::GetValueForKey(json, NameKey, _schemeName);
JsonUtils::GetValueForKey(json, ForegroundKey, _defaultForeground);
JsonUtils::GetValueForKey(json, BackgroundKey, _defaultBackground);
JsonUtils::GetValueForKey(json, SelectionBackgroundKey, _selectionBackground);
JsonUtils::GetValueForKey(json, CursorColorKey, _cursorColor);
JsonUtils::GetValueForKey(json, NameKey, _Name);
JsonUtils::GetValueForKey(json, ForegroundKey, _Foreground);
JsonUtils::GetValueForKey(json, BackgroundKey, _Background);
JsonUtils::GetValueForKey(json, SelectionBackgroundKey, _SelectionBackground);
JsonUtils::GetValueForKey(json, CursorColorKey, _CursorColor);

int i = 0;
for (const auto& current : TableColors)
Expand Down Expand Up @@ -163,11 +165,11 @@ Json::Value ColorScheme::ToJson(const TerminalApp::ColorScheme& scheme)
// <none>
void ColorScheme::UpdateJson(Json::Value& json)
{
JsonUtils::SetValueForKey(json, NameKey, _schemeName);
JsonUtils::SetValueForKey(json, ForegroundKey, _defaultForeground);
JsonUtils::SetValueForKey(json, BackgroundKey, _defaultBackground);
JsonUtils::SetValueForKey(json, SelectionBackgroundKey, _selectionBackground);
JsonUtils::SetValueForKey(json, CursorColorKey, _cursorColor);
JsonUtils::SetValueForKey(json, NameKey, _Name);
JsonUtils::SetValueForKey(json, ForegroundKey, _Foreground);
JsonUtils::SetValueForKey(json, BackgroundKey, _Background);
JsonUtils::SetValueForKey(json, SelectionBackgroundKey, _SelectionBackground);
JsonUtils::SetValueForKey(json, CursorColorKey, _CursorColor);

int i = 0;
for (const auto& current : TableColors)
Expand All @@ -177,38 +179,13 @@ void ColorScheme::UpdateJson(Json::Value& json)
}
}

winrt::hstring ColorScheme::Name() const noexcept
{
return _schemeName;
}

winrt::com_array<Color> ColorScheme::Table() const noexcept
{
winrt::com_array<Color> result{ COLOR_TABLE_SIZE };
std::transform(_table.begin(), _table.end(), result.begin(), [](til::color c) -> Color { return c; });
return result;
}

Color ColorScheme::Foreground() const noexcept
{
return _defaultForeground;
}

Color ColorScheme::Background() const noexcept
{
return _defaultBackground;
}

Color ColorScheme::SelectionBackground() const noexcept
{
return _selectionBackground;
}

Color ColorScheme::CursorColor() const noexcept
{
return _cursorColor;
}

// Method Description:
// - Parse the name from the JSON representation of a ColorScheme.
// Arguments:
Expand Down
64 changes: 51 additions & 13 deletions src/cascadia/TerminalApp/ColorScheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,20 @@ namespace TerminalAppLocalTests

namespace winrt::TerminalApp::implementation
{
// Use this macro to quick implement both the getter and setter for a color property.
// This should only be used for color types where there's no logic in the
// getter/setter beyond just accessing/updating the value.
// This takes advantage of til::color
#define GETSET_COLORTABLEPROPERTY(name) \
public: \
winrt::Windows::UI::Color name() const noexcept { return _table[##name##Index]; } \
void name(const winrt::Windows::UI::Color& value) noexcept { _table[##name##Index] = value; }

struct ColorScheme : ColorSchemeT<ColorScheme>
{
public:
ColorScheme();
ColorScheme() = default;
ColorScheme(hstring name, Windows::UI::Color defaultFg, Windows::UI::Color defaultBg, Windows::UI::Color cursorColor);
~ColorScheme();

void ApplyScheme(const winrt::Microsoft::Terminal::TerminalControl::IControlSettings& terminalSettings) const;

Expand All @@ -45,22 +53,52 @@ namespace winrt::TerminalApp::implementation
static Json::Value ToJson(const TerminalApp::ColorScheme& scheme);
void UpdateJson(Json::Value& json);

hstring Name() const noexcept;
static std::optional<std::wstring> GetNameFromJson(const Json::Value& json);

com_array<Windows::UI::Color> Table() const noexcept;
Windows::UI::Color Foreground() const noexcept;
Windows::UI::Color Background() const noexcept;
Windows::UI::Color SelectionBackground() const noexcept;
Windows::UI::Color CursorColor() const noexcept;

static std::optional<std::wstring> GetNameFromJson(const Json::Value& json);
GETSET_PROPERTY(winrt::hstring, Name, L"");
GETSET_COLORPROPERTY(Foreground, DEFAULT_FOREGROUND_WITH_ALPHA);
GETSET_COLORPROPERTY(Background, DEFAULT_BACKGROUND_WITH_ALPHA);
GETSET_COLORPROPERTY(SelectionBackground, DEFAULT_FOREGROUND);
GETSET_COLORPROPERTY(CursorColor, DEFAULT_CURSOR_COLOR);

GETSET_COLORTABLEPROPERTY(Black);
GETSET_COLORTABLEPROPERTY(Red);
GETSET_COLORTABLEPROPERTY(Green);
GETSET_COLORTABLEPROPERTY(Yellow);
GETSET_COLORTABLEPROPERTY(Blue);
GETSET_COLORTABLEPROPERTY(Purple);
GETSET_COLORTABLEPROPERTY(Cyan);
GETSET_COLORTABLEPROPERTY(White);
GETSET_COLORTABLEPROPERTY(BrightBlack);
GETSET_COLORTABLEPROPERTY(BrightRed);
GETSET_COLORTABLEPROPERTY(BrightGreen);
GETSET_COLORTABLEPROPERTY(BrightYellow);
GETSET_COLORTABLEPROPERTY(BrightBlue);
GETSET_COLORTABLEPROPERTY(BrightPurple);
GETSET_COLORTABLEPROPERTY(BrightCyan);
GETSET_COLORTABLEPROPERTY(BrightWhite);

private:
hstring _schemeName;
std::array<til::color, COLOR_TABLE_SIZE> _table;
til::color _defaultForeground;
til::color _defaultBackground;
til::color _selectionBackground;
til::color _cursorColor;

static constexpr unsigned int BlackIndex{ 0 };
static constexpr unsigned int RedIndex{ 1 };
static constexpr unsigned int GreenIndex{ 2 };
static constexpr unsigned int YellowIndex{ 3 };
static constexpr unsigned int BlueIndex{ 4 };
static constexpr unsigned int PurpleIndex{ 5 };
static constexpr unsigned int CyanIndex{ 6 };
static constexpr unsigned int WhiteIndex{ 7 };
static constexpr unsigned int BrightBlackIndex{ 8 };
static constexpr unsigned int BrightRedIndex{ 9 };
static constexpr unsigned int BrightGreenIndex{ 10 };
static constexpr unsigned int BrightYellowIndex{ 11 };
static constexpr unsigned int BrightBlueIndex{ 12 };
static constexpr unsigned int BrightPurpleIndex{ 13 };
static constexpr unsigned int BrightCyanIndex{ 14 };
static constexpr unsigned int BrightWhiteIndex{ 15 };

friend class TerminalAppLocalTests::SettingsTests;
friend class TerminalAppLocalTests::ColorSchemeTests;
Expand Down
25 changes: 21 additions & 4 deletions src/cascadia/TerminalApp/ColorScheme.idl
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,27 @@ namespace TerminalApp

String Name { get; };

Windows.UI.Color Foreground { get; };
Windows.UI.Color Background { get; };
Windows.UI.Color SelectionBackground { get; };
Windows.UI.Color CursorColor { get; };
Windows.UI.Color Foreground;
Windows.UI.Color Background;
Windows.UI.Color SelectionBackground;
Windows.UI.Color CursorColor;

Windows.UI.Color Black;
Windows.UI.Color Red;
Windows.UI.Color Green;
Windows.UI.Color Yellow;
Windows.UI.Color Blue;
Windows.UI.Color Purple;
Windows.UI.Color Cyan;
Windows.UI.Color White;
Windows.UI.Color BrightBlack;
Windows.UI.Color BrightRed;
Windows.UI.Color BrightGreen;
Windows.UI.Color BrightYellow;
Windows.UI.Color BrightBlue;
Windows.UI.Color BrightPurple;
Windows.UI.Color BrightCyan;
Windows.UI.Color BrightWhite;

Windows.UI.Color[] Table { get; };
}
Expand Down
12 changes: 12 additions & 0 deletions src/cascadia/inc/cppwinrt_utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -107,6 +107,18 @@ public: \
private: \
type _##name{ __VA_ARGS__ };

// Use this macro to quick implement both the getter and setter for a color property.
// This should only be used for color types where there's no logic in the
// getter/setter beyond just accessing/updating the value.
// This takes advantage of til::color
#define GETSET_COLORPROPERTY(name, ...) \
public: \
winrt::Windows::UI::Color name() const noexcept { return _##name; } \
void name(const winrt::Windows::UI::Color& value) noexcept { _##name = value; } \
\
private: \
til::color _##name{ __VA_ARGS__ };

// Use this macro to quickly implement both the getter and setter for an
// observable property. This is similar to the GETSET_PROPERTY macro above,
// except this will also raise a PropertyChanged event with the name of the
Expand Down

1 comment on commit c69572b

@github-actions

This comment was marked as resolved.

Please sign in to comment.