Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update ColorScheme with Json Serializer and color table API #7609

Merged
merged 12 commits into from
Sep 17, 2020
22 changes: 22 additions & 0 deletions src/cascadia/TerminalApp/ColorScheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,19 @@ static constexpr std::array<std::string_view, 16> TableColors = {
BrightWhiteKey,
};

ColorScheme::ColorScheme() :
_Foreground{ DEFAULT_FOREGROUND_WITH_ALPHA },
_Background{ DEFAULT_BACKGROUND_WITH_ALPHA },
_SelectionBackground{ DEFAULT_FOREGROUND },
_CursorColor{ DEFAULT_CURSOR_COLOR }
{
}

ColorScheme::ColorScheme(winrt::hstring name, Color defaultFg, Color defaultBg, Color cursorColor) :
_Name{ name },
_Foreground{ defaultFg },
_Background{ defaultBg },
_SelectionBackground{ DEFAULT_FOREGROUND },
_CursorColor{ cursorColor }
{
}
Expand Down Expand Up @@ -165,6 +174,19 @@ winrt::com_array<Color> ColorScheme::Table() const noexcept
return result;
}

// Method Description:
// - Set a color in the color table
// Arguments:
// - index: the index of the desired color within the table
// - value: the color value we are setting the color table color to
// Return Value:
// - none
void ColorScheme::SetColorTable(uint8_t index, const winrt::Windows::UI::Color& value) noexcept
{
THROW_HR_IF(E_INVALIDARG, index > COLOR_TABLE_SIZE - 1);
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
_table[index] = value;
}

// Method Description:
// - Parse the name from the JSON representation of a ColorScheme.
// Arguments:
Expand Down
54 changes: 6 additions & 48 deletions src/cascadia/TerminalApp/ColorScheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,10 @@ 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() = default;
ColorScheme();
ColorScheme(hstring name, Windows::UI::Color defaultFg, Windows::UI::Color defaultBg, Windows::UI::Color cursorColor);

static com_ptr<ColorScheme> FromJson(const Json::Value& json);
Expand All @@ -54,50 +45,17 @@ public:
static std::optional<std::wstring> GetNameFromJson(const Json::Value& json);

com_array<Windows::UI::Color> Table() const noexcept;
void SetColorTable(uint8_t index, const winrt::Windows::UI::Color& value) noexcept;

GETSET_PROPERTY(winrt::hstring, Name, L"");
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
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);
GETSET_COLORPROPERTY(Foreground); // defined in constructor
GETSET_COLORPROPERTY(Background); // defined in constructor
GETSET_COLORPROPERTY(SelectionBackground); // defined in constructor
GETSET_COLORPROPERTY(CursorColor); // defined in constructor

private:
std::array<til::color, COLOR_TABLE_SIZE> _table;

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
21 changes: 2 additions & 19 deletions src/cascadia/TerminalApp/ColorScheme.idl
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,15 @@ namespace TerminalApp
{
[default_interface] runtimeclass ColorScheme {
ColorScheme();
ColorScheme(String name, Windows.UI.Color defaultFg, Windows.UI.Color defaultBg, Windows.UI.Color cursorColor);

String Name { get; };
String Name;

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; };
void SetColorTable(UInt8 index, Windows.UI.Color value);
carlos-zamora marked this conversation as resolved.
Show resolved Hide resolved
}
}