Skip to content

Commit

Permalink
Add Selection Background Color as a setting to Profiles and Col… (#3471)
Browse files Browse the repository at this point in the history
<!-- Enter a brief description/summary of your PR here. What does it fix/what does it change/how was it tested (even manually, if necessary)? -->
## Summary of the Pull Request
This introduces a setting to both Profiles and ColorSchemes called <code>selectionBackground</code> that allows you to change the selection background color to what's specified. If <code>selectionBackground</code> isn't set in either the profile or color scheme, it'll default to what it was before - white.

<!-- Please review the items on the PR checklist before submitting-->
## PR Checklist
* [x] Closes #3326
* [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA
* [x] Tests added/passed
* [x] Requires documentation to be updated

<!-- Describe how you validated the behavior. Add automated tests wherever possible, but list manual validation steps taken as well -->
## Validation Steps Performed
- Added selectionBackground to existing profile and colorscheme tests.
- Verified that the color does change to what I expect it to be when I add "selectionBackground" to either/both a profile and a color scheme.


<hr>

* adding selectionBackground to ColorScheme and TerminalSettings

* Changing PaintSelection inside the renderers to take a SelectionBackground COLORREF

* changes to conhost and terminal renderdata, and to terminal settings and core

* IT WORKS

* modification of unit tests, json schemas, reordering of functions

* more movement

* changed a couple of unit tests to add selectionBackground, added the setting to schemas, also added the optional setting to profiles

* default selection background should be slightly offwhite like the default foreground is

* reverting changes to .sln

* cleaning up

* adding comment

* oops

* added clangformat to my vs hehe

* moving selectionBackground to IControlSettings and removing from ICoreSettings

* trying to figure out why the WHOLE FILE LOOKS LIKE ITS CHANGED

* here it goes again

* pls

* adding default foreground as the default for selection background in dx
  • Loading branch information
leonMSFT authored and zadjii-msft committed Nov 13, 2019
1 parent c9f148c commit a404778
Show file tree
Hide file tree
Showing 16 changed files with 117 additions and 10 deletions.
2 changes: 2 additions & 0 deletions doc/cascadia/SettingsSchema.md
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ Properties listed below are specific to each unique profile.
| `icon` | Optional | String | | Image file location of the icon used in the profile. Displays within the tab and the dropdown menu. |
| `padding` | Optional | String | `8, 8, 8, 8` | Sets the padding around the text within the window. Can have three different formats: `"#"` sets the same padding for all sides, `"#, #"` sets the same padding for left-right and top-bottom, and `"#, #, #, #"` sets the padding individually for left, top, right, and bottom. |
| `scrollbarState` | Optional | String | | Defines the visibility of the scrollbar. Possible values: `"visible"`, `"hidden"` |
| `selectionBackground` | Optional | String | Sets the selection background color of the profile. Overrides `selectionBackground` set in color scheme if `colorscheme` is set. Uses hex color format: `"#rrggbb"`. |
| `snapOnInput` | Optional | Boolean | `true` | When set to `true`, the window will scroll to the command input line when typing. When set to `false`, the window will not scroll when you start typing. |
| `source` | Optional | String | | Stores the name of the profile generator that originated this profile. _There are no discoverable values for this field._ |
| `startingDirectory` | Optional | String | `%USERPROFILE%` | The directory the shell starts in when it is loaded. |
Expand All @@ -57,6 +58,7 @@ Properties listed below are specific to each color scheme. [ColorTool](https://g
| `name` | _Required_ | String | Name of the color scheme. |
| `foreground` | _Required_ | String | Sets the foreground color of the color scheme. |
| `background` | _Required_ | String | Sets the background color of the color scheme. |
| `selectionBackground` | Optional | String | Sets the selection background color of the color scheme. |
| `black` | _Required_ | String | Sets the color used as ANSI black. |
| `blue` | _Required_ | String | Sets the color used as ANSI blue. |
| `brightBlack` | _Required_ | String | Sets the color used as ANSI bright black. |
Expand Down
8 changes: 8 additions & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,10 @@
],
"type": "string"
},
"selectionBackground": {
"$ref": "#/definitions/Color",
"description": "Sets the selection background color of the profile. Overrides selection background set in color scheme if colorscheme is set. Uses hex color format: \"#rrggbb\"."
},
"snapOnInput": {
"default": true,
"description": "When set to true, the window will scroll to the command input line when typing. When set to false, the window will not scroll when you start typing.",
Expand Down Expand Up @@ -492,6 +496,10 @@
"$ref": "#/definitions/Color",
"description": "Sets the color used as ANSI red."
},
"selectionBackground": {
"$ref": "#/definitions/Color",
"description": "Sets the selection background color of the color scheme."
},
"white": {
"$ref": "#/definitions/Color",
"description": "Sets the color used as ANSI white."
Expand Down
7 changes: 7 additions & 0 deletions src/cascadia/LocalTests_TerminalApp/ColorSchemeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ namespace TerminalAppLocalTests
"name": "scheme0",
"foreground": "#000000",
"background": "#010101",
"selectionBackground": "#010100",
"red": "#010000",
"green": "#000100",
"blue": "#000001"
Expand All @@ -106,6 +107,7 @@ namespace TerminalAppLocalTests
"name": "scheme1",
"foreground": "#020202",
"background": "#030303",
"selectionBackground": "#020200",
"red": "#020000",
"blue": "#000002"
Expand All @@ -114,6 +116,7 @@ namespace TerminalAppLocalTests
"name": "scheme0",
"foreground": "#040404",
"background": "#050505",
"selectionBackground": "#030300",
"red": "#030000",
"green": "#000300"
})" };
Expand All @@ -126,6 +129,8 @@ namespace TerminalAppLocalTests
VERIFY_ARE_EQUAL(L"scheme0", scheme0._schemeName);
VERIFY_ARE_EQUAL(ARGB(0, 0, 0, 0), scheme0._defaultForeground);
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 1), scheme0._defaultBackground);
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 1), scheme0._defaultBackground);
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 0), scheme0._selectionBackground);
VERIFY_ARE_EQUAL(ARGB(0, 1, 0, 0), scheme0._table[XTERM_RED_ATTR]);
VERIFY_ARE_EQUAL(ARGB(0, 0, 1, 0), scheme0._table[XTERM_GREEN_ATTR]);
VERIFY_ARE_EQUAL(ARGB(0, 0, 0, 1), scheme0._table[XTERM_BLUE_ATTR]);
Expand All @@ -136,6 +141,7 @@ namespace TerminalAppLocalTests

VERIFY_ARE_EQUAL(ARGB(0, 2, 2, 2), scheme0._defaultForeground);
VERIFY_ARE_EQUAL(ARGB(0, 3, 3, 3), scheme0._defaultBackground);
VERIFY_ARE_EQUAL(ARGB(0, 2, 2, 0), scheme0._selectionBackground);
VERIFY_ARE_EQUAL(ARGB(0, 2, 0, 0), scheme0._table[XTERM_RED_ATTR]);
VERIFY_ARE_EQUAL(ARGB(0, 0, 1, 0), scheme0._table[XTERM_GREEN_ATTR]);
VERIFY_ARE_EQUAL(ARGB(0, 0, 0, 2), scheme0._table[XTERM_BLUE_ATTR]);
Expand All @@ -146,6 +152,7 @@ namespace TerminalAppLocalTests

VERIFY_ARE_EQUAL(ARGB(0, 4, 4, 4), scheme0._defaultForeground);
VERIFY_ARE_EQUAL(ARGB(0, 5, 5, 5), scheme0._defaultBackground);
VERIFY_ARE_EQUAL(ARGB(0, 3, 3, 0), scheme0._selectionBackground);
VERIFY_ARE_EQUAL(ARGB(0, 3, 0, 0), scheme0._table[XTERM_RED_ATTR]);
VERIFY_ARE_EQUAL(ARGB(0, 0, 3, 0), scheme0._table[XTERM_GREEN_ATTR]);
VERIFY_ARE_EQUAL(ARGB(0, 0, 0, 2), scheme0._table[XTERM_BLUE_ATTR]);
Expand Down
15 changes: 13 additions & 2 deletions src/cascadia/LocalTests_TerminalApp/ProfileTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,8 @@ namespace TerminalAppLocalTests
"name": "profile0",
"guid": "{6239a42c-1111-49a3-80bd-e8fdd045185c}",
"foreground": "#000000",
"background": "#010101"
"background": "#010101",
"selectionBackground": "#010101"
})" };
const std::string profile1String{ R"({
"name": "profile1",
Expand All @@ -106,7 +107,8 @@ namespace TerminalAppLocalTests
const std::string profile2String{ R"({
"name": "profile2",
"guid": "{6239a42c-1111-49a3-80bd-e8fdd045185c}",
"foreground": "#030303"
"foreground": "#030303",
"selectionBackground": "#020202"
})" };

const auto profile0Json = VerifyParseSucceeded(profile0String);
Expand All @@ -120,6 +122,9 @@ namespace TerminalAppLocalTests
VERIFY_IS_TRUE(profile0._defaultBackground.has_value());
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 1), profile0._defaultBackground.value());

VERIFY_IS_TRUE(profile0._selectionBackground.has_value());
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 1), profile0._selectionBackground.value());

VERIFY_ARE_EQUAL(L"profile0", profile0._name);

VERIFY_IS_FALSE(profile0._startingDirectory.has_value());
Expand All @@ -134,6 +139,9 @@ namespace TerminalAppLocalTests
VERIFY_IS_TRUE(profile0._defaultBackground.has_value());
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 1), profile0._defaultBackground.value());

VERIFY_IS_TRUE(profile0._selectionBackground.has_value());
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 1), profile0._selectionBackground.value());

VERIFY_ARE_EQUAL(L"profile1", profile0._name);

VERIFY_IS_TRUE(profile0._startingDirectory.has_value());
Expand All @@ -149,6 +157,9 @@ namespace TerminalAppLocalTests
VERIFY_IS_TRUE(profile0._defaultBackground.has_value());
VERIFY_ARE_EQUAL(ARGB(0, 1, 1, 1), profile0._defaultBackground.value());

VERIFY_IS_TRUE(profile0._selectionBackground.has_value());
VERIFY_ARE_EQUAL(ARGB(0, 2, 2, 2), profile0._selectionBackground.value());

VERIFY_ARE_EQUAL(L"profile2", profile0._name);

VERIFY_IS_TRUE(profile0._startingDirectory.has_value());
Expand Down
22 changes: 19 additions & 3 deletions src/cascadia/TerminalApp/ColorScheme.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

#include "pch.h"
#include "ColorScheme.h"
#include "DefaultSettings.h"
#include "../../types/inc/Utils.hpp"
#include "Utils.h"
#include "JsonUtils.h"
Expand All @@ -16,6 +17,7 @@ static constexpr std::string_view NameKey{ "name" };
static constexpr std::string_view TableKey{ "colors" };
static constexpr std::string_view ForegroundKey{ "foreground" };
static constexpr std::string_view BackgroundKey{ "background" };
static constexpr std::string_view SelectionBackgroundKey{ "selectionBackground" };
static constexpr std::array<std::string_view, 16> TableColors = {
"black",
"red",
Expand All @@ -38,16 +40,18 @@ static constexpr std::array<std::string_view, 16> TableColors = {
ColorScheme::ColorScheme() :
_schemeName{ L"" },
_table{},
_defaultForeground{ RGB(242, 242, 242) },
_defaultBackground{ RGB(12, 12, 12) }
_defaultForeground{ DEFAULT_FOREGROUND_WITH_ALPHA },
_defaultBackground{ DEFAULT_BACKGROUND_WITH_ALPHA },
_selectionBackground{ DEFAULT_FOREGROUND }
{
}

ColorScheme::ColorScheme(std::wstring name, COLORREF defaultFg, COLORREF defaultBg) :
_schemeName{ name },
_table{},
_defaultForeground{ defaultFg },
_defaultBackground{ defaultBg }
_defaultBackground{ defaultBg },
_selectionBackground{ DEFAULT_FOREGROUND }
{
}

Expand All @@ -66,6 +70,7 @@ void ColorScheme::ApplyScheme(TerminalSettings terminalSettings) const
{
terminalSettings.DefaultForeground(_defaultForeground);
terminalSettings.DefaultBackground(_defaultBackground);
terminalSettings.SelectionBackground(_selectionBackground);

auto const tableCount = gsl::narrow_cast<int>(_table.size());
for (int i = 0; i < tableCount; i++)
Expand All @@ -86,6 +91,7 @@ Json::Value ColorScheme::ToJson() const
root[JsonKey(NameKey)] = winrt::to_string(_schemeName);
root[JsonKey(ForegroundKey)] = Utils::ColorToHexString(_defaultForeground);
root[JsonKey(BackgroundKey)] = Utils::ColorToHexString(_defaultBackground);
root[JsonKey(SelectionBackgroundKey)] = Utils::ColorToHexString(_selectionBackground);

int i = 0;
for (const auto& colorName : TableColors)
Expand Down Expand Up @@ -155,6 +161,11 @@ void ColorScheme::LayerJson(const Json::Value& json)
const auto color = Utils::ColorFromHexString(bgString.asString());
_defaultBackground = color;
}
if (auto sbString{ json[JsonKey(SelectionBackgroundKey)] })
{
const auto color = Utils::ColorFromHexString(sbString.asString());
_selectionBackground = color;
}

// Legacy Deserialization. Leave in place to allow forward compatibility
if (auto table{ json[JsonKey(TableKey)] })
Expand Down Expand Up @@ -204,6 +215,11 @@ COLORREF ColorScheme::GetBackground() const noexcept
return _defaultBackground;
}

COLORREF ColorScheme::GetSelectionBackground() const noexcept
{
return _selectionBackground;
}

// Method Description:
// - Parse the name from the JSON representation of a ColorScheme.
// Arguments:
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/ColorScheme.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class TerminalApp::ColorScheme
std::array<COLORREF, COLOR_TABLE_SIZE>& GetTable() noexcept;
COLORREF GetForeground() const noexcept;
COLORREF GetBackground() const noexcept;
COLORREF GetSelectionBackground() const noexcept;

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

Expand All @@ -57,6 +58,7 @@ class TerminalApp::ColorScheme
std::array<COLORREF, COLOR_TABLE_SIZE> _table;
COLORREF _defaultForeground;
COLORREF _defaultBackground;
COLORREF _selectionBackground;

friend class TerminalAppLocalTests::SettingsTests;
friend class TerminalAppLocalTests::ColorSchemeTests;
Expand Down
17 changes: 17 additions & 0 deletions src/cascadia/TerminalApp/Profile.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ static constexpr std::string_view HiddenKey{ "hidden" };

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 ColorTableKey{ "colorTable" };
static constexpr std::string_view TabTitleKey{ "tabTitle" };
static constexpr std::string_view HistorySizeKey{ "historySize" };
Expand Down Expand Up @@ -89,6 +90,7 @@ Profile::Profile(const std::optional<GUID>& guid) :

_defaultForeground{},
_defaultBackground{},
_selectionBackground{},
_colorTable{},
_tabTitle{},
_historySize{ DEFAULT_HISTORY_SIZE },
Expand Down Expand Up @@ -201,6 +203,10 @@ TerminalSettings Profile::CreateTerminalSettings(const std::unordered_map<std::w
{
terminalSettings.DefaultBackground(_defaultBackground.value());
}
if (_selectionBackground)
{
terminalSettings.SelectionBackground(_selectionBackground.value());
}

if (_scrollbarState)
{
Expand Down Expand Up @@ -258,6 +264,10 @@ Json::Value Profile::ToJson() const
{
root[JsonKey(BackgroundKey)] = Utils::ColorToHexString(_defaultBackground.value());
}
if (_selectionBackground)
{
root[JsonKey(SelectionBackgroundKey)] = Utils::ColorToHexString(_selectionBackground.value());
}
if (_schemeName)
{
const auto scheme = winrt::to_string(_schemeName.value());
Expand Down Expand Up @@ -579,6 +589,8 @@ void Profile::LayerJson(const Json::Value& json)

JsonUtils::GetOptionalColor(json, BackgroundKey, _defaultBackground);

JsonUtils::GetOptionalColor(json, SelectionBackgroundKey, _selectionBackground);

JsonUtils::GetOptionalString(json, ColorSchemeKey, _schemeName);
// TODO:GH#1069 deprecate old settings key
JsonUtils::GetOptionalString(json, ColorSchemeKeyOld, _schemeName);
Expand Down Expand Up @@ -733,6 +745,11 @@ void Profile::SetDefaultBackground(COLORREF defaultBackground) noexcept
_defaultBackground = defaultBackground;
}

void Profile::SetSelectionBackground(COLORREF selectionBackground) noexcept
{
_selectionBackground = selectionBackground;
}

void Profile::SetCloseOnExit(bool defaultClose) noexcept
{
_closeOnExit = defaultClose;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalApp/Profile.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ class TerminalApp::Profile final
void SetUseAcrylic(bool useAcrylic) noexcept;
void SetDefaultForeground(COLORREF defaultForeground) noexcept;
void SetDefaultBackground(COLORREF defaultBackground) noexcept;
void SetSelectionBackground(COLORREF selectionBackground) noexcept;
void SetCloseOnExit(bool defaultClose) noexcept;
void SetConnectionType(GUID connectionType) noexcept;

Expand Down Expand Up @@ -115,6 +116,7 @@ class TerminalApp::Profile final

std::optional<uint32_t> _defaultForeground;
std::optional<uint32_t> _defaultBackground;
std::optional<uint32_t> _selectionBackground;
std::array<uint32_t, COLOR_TABLE_SIZE> _colorTable;
std::optional<std::wstring> _tabTitle;
int32_t _historySize;
Expand Down
8 changes: 8 additions & 0 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
_root.Dispatcher().RunAsync(CoreDispatcherPriority::Normal, [this]() {
// Update our control settings
_ApplyUISettings();

// Update DxEngine's SelectionBackground
_renderEngine->SetSelectionBackground(_settings.SelectionBackground());

// Update the terminal core with its new Core settings
_terminal->UpdateSettings(_settings);

Expand Down Expand Up @@ -462,6 +466,10 @@ namespace winrt::Microsoft::Terminal::TerminalControl::implementation
// Resize our terminal connection to match that size, and initialize the terminal with that size.
const auto viewInPixels = Viewport::FromDimensions({ 0, 0 }, windowSize);
THROW_IF_FAILED(dxEngine->SetWindowSize({ viewInPixels.Width(), viewInPixels.Height() }));

// Update DxEngine's SelectionBackground
dxEngine->SetSelectionBackground(_settings.SelectionBackground());

const auto vp = dxEngine->GetViewportInCharacters(viewInPixels);
const auto width = vp.Width();
const auto height = vp.Height();
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettings/IControlSettings.idl
Original file line number Diff line number Diff line change
Expand Up @@ -40,5 +40,7 @@ namespace Microsoft.Terminal.Settings
Windows.UI.Xaml.Media.Stretch BackgroundImageStretchMode;
Windows.UI.Xaml.HorizontalAlignment BackgroundImageHorizontalAlignment;
Windows.UI.Xaml.VerticalAlignment BackgroundImageVerticalAlignment;

UInt32 SelectionBackground;
};
}
11 changes: 11 additions & 0 deletions src/cascadia/TerminalSettings/TerminalSettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
TerminalSettings::TerminalSettings() :
_defaultForeground{ DEFAULT_FOREGROUND_WITH_ALPHA },
_defaultBackground{ DEFAULT_BACKGROUND_WITH_ALPHA },
_selectionBackground{ DEFAULT_FOREGROUND },
_colorTable{},
_historySize{ DEFAULT_HISTORY_SIZE },
_initialRows{ 30 },
Expand Down Expand Up @@ -58,6 +59,16 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
_defaultBackground = value;
}

uint32_t TerminalSettings::SelectionBackground()
{
return _selectionBackground;
}

void TerminalSettings::SelectionBackground(uint32_t value)
{
_selectionBackground = value;
}

uint32_t TerminalSettings::GetColorTableEntry(int32_t index) const
{
return _colorTable[index];
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalSettings/terminalsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
void DefaultForeground(uint32_t value);
uint32_t DefaultBackground();
void DefaultBackground(uint32_t value);
uint32_t SelectionBackground();
void SelectionBackground(uint32_t value);
uint32_t GetColorTableEntry(int32_t index) const;
void SetColorTableEntry(int32_t index, uint32_t value);
int32_t HistorySize();
Expand Down Expand Up @@ -97,6 +99,7 @@ namespace winrt::Microsoft::Terminal::Settings::implementation
private:
uint32_t _defaultForeground;
uint32_t _defaultBackground;
uint32_t _selectionBackground;
std::array<uint32_t, COLOR_TABLE_SIZE> _colorTable;
int32_t _historySize;
int32_t _initialRows;
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/UnitTests_TerminalCore/MockTermSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ namespace TerminalCoreUnitTests
uint32_t CursorHeight() { return 42UL; }
winrt::hstring WordDelimiters() { return winrt::to_hstring(DEFAULT_WORD_DELIMITERS.c_str()); }
bool CopyOnSelect() { return _copyOnSelect; }
uint32_t SelectionBackground() { return COLOR_WHITE; }

// other implemented methods
uint32_t GetColorTableEntry(int32_t) const { return 123; }
Expand All @@ -49,6 +50,7 @@ namespace TerminalCoreUnitTests
void CursorHeight(uint32_t) {}
void WordDelimiters(winrt::hstring) {}
void CopyOnSelect(bool copyOnSelect) { _copyOnSelect = copyOnSelect; }
void SelectionBackground(uint32_t) {}

// other unimplemented methods
void SetColorTableEntry(int32_t /* index */, uint32_t /* value */) {}
Expand Down
Loading

0 comments on commit a404778

Please sign in to comment.