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

Option to default to show icon in tab, hide tabicon or make icon in tab monochrome. #15948

Merged
merged 4 commits into from
Sep 19, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/cascadia/LocalTests_SettingsModel/ThemeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,8 @@ namespace SettingsModelLocalTests
"tabRow":
{
"background": "#FFFF8800",
"unfocusedBackground": "#FF8844"
"unfocusedBackground": "#FF8844",
"iconStyle": "default"
},
"window":
{
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalApp/SettingsTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ namespace winrt::TerminalApp::implementation

// The TabViewItem Icon needs MUX while the IconSourceElement in the CommandPalette needs WUX...
Icon(winrt::hstring{ glyph });
TabViewItem().IconSource(IconPathConverter::IconSourceMUX(glyph));
TabViewItem().IconSource(IconPathConverter::IconSourceMUX(glyph, false));
}

winrt::Windows::UI::Xaml::Media::Brush SettingsTab::_BackgroundBrush()
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalApp/TabManagement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -176,7 +176,7 @@ namespace winrt::TerminalApp::implementation
{
if (!profile.Icon().empty())
{
newTabImpl->UpdateIcon(profile.Icon());
newTabImpl->UpdateIcon(profile.Icon(), _settings.GlobalSettings().CurrentTheme().Tab().IconStyle());
}
}

Expand Down Expand Up @@ -241,7 +241,7 @@ namespace winrt::TerminalApp::implementation
{
if (const auto profile = tab.GetFocusedProfile())
{
tab.UpdateIcon(profile.Icon());
tab.UpdateIcon(profile.Icon(), _settings.GlobalSettings().CurrentTheme().Tab().IconStyle());
}
}

Expand Down
25 changes: 17 additions & 8 deletions src/cascadia/TerminalApp/TerminalTab.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -278,17 +278,17 @@ namespace winrt::TerminalApp::implementation
// - iconPath: The new path string to use as the IconPath for our TabViewItem
// Return Value:
// - <none>
void TerminalTab::UpdateIcon(const winrt::hstring iconPath)
void TerminalTab::UpdateIcon(const winrt::hstring iconPath, const winrt::Microsoft::Terminal::Settings::Model::IconStyle iconStyle)
{
ASSERT_UI_THREAD();

// Don't reload our icon if it hasn't changed.
if (iconPath == _lastIconPath)
// Don't reload our icon and iconStyle hasn't changed.
if (iconPath == _lastIconPath && iconStyle == _lastIconStyle)
{
return;
}

_lastIconPath = iconPath;
_lastIconStyle = iconStyle;

// If the icon is currently hidden, just return here (but only after setting _lastIconPath to the new path
// for when we show the icon again)
Expand All @@ -297,9 +297,18 @@ namespace winrt::TerminalApp::implementation
return;
}

// The TabViewItem Icon needs MUX while the IconSourceElement in the CommandPalette needs WUX...
Icon(_lastIconPath);
TabViewItem().IconSource(IconPathConverter::IconSourceMUX(_lastIconPath));
if (iconStyle == IconStyle::Hidden)
{
// The TabViewItem Icon needs MUX while the IconSourceElement in the CommandPalette needs WUX...
Icon({});
TabViewItem().IconSource(IconSource{ nullptr });
}
else
{
Icon(_lastIconPath);
bool isMonochrome = iconStyle == IconStyle::Monochrome;
TabViewItem().IconSource(IconPathConverter::IconSourceMUX(_lastIconPath, isMonochrome));
}
}

// Method Description:
Expand All @@ -321,7 +330,7 @@ namespace winrt::TerminalApp::implementation
else
{
Icon(_lastIconPath);
TabViewItem().IconSource(IconPathConverter::IconSourceMUX(_lastIconPath));
TabViewItem().IconSource(IconPathConverter::IconSourceMUX(_lastIconPath, _lastIconStyle == IconStyle::Monochrome));
}
_iconHidden = hide;
}
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/TerminalTab.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ namespace winrt::TerminalApp::implementation
std::shared_ptr<Pane> newPane);

void ToggleSplitOrientation();
void UpdateIcon(const winrt::hstring iconPath);
void UpdateIcon(const winrt::hstring iconPath, const winrt::Microsoft::Terminal::Settings::Model::IconStyle iconStyle);
void HideIcon(const bool hide);

void ShowBellIndicator(const bool show);
Expand Down Expand Up @@ -110,6 +110,7 @@ namespace winrt::TerminalApp::implementation

Windows::UI::Xaml::Controls::MenuFlyoutItem _closePaneMenuItem;

winrt::Microsoft::Terminal::Settings::Model::IconStyle _lastIconStyle;
winrt::hstring _lastIconPath{};
std::optional<winrt::Windows::UI::Color> _runtimeTabColor{};
winrt::TerminalApp::TabHeaderControl _headerControl{};
Expand Down
24 changes: 14 additions & 10 deletions src/cascadia/TerminalSettingsModel/IconPathConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <Shlobj_core.h>
#include <wincodec.h>

#include <d2d1.h>
#include <d2d1effects_2.h>

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: do we need these headers?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are right, I missed removing them, it was from my idea to actually implement grayscale, but I will remove and sadly you guys have to re-approve.

namespace winrt
{
namespace MUX = Microsoft::UI::Xaml;
Expand Down Expand Up @@ -72,7 +75,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// Return Value:
// - An IconElement with its IconSource set, if possible.
template<typename TIconSource>
TIconSource _getColoredBitmapIcon(const winrt::hstring& path)
TIconSource _getColoredBitmapIcon(const winrt::hstring& path, bool monochrome)
{
// FontIcon uses glyphs in the private use area, whereas valid URIs only contain ASCII characters.
// To skip throwing on Uri construction, we can quickly check if the first character is ASCII.
Expand All @@ -85,7 +88,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// Make sure to set this to false, so we keep the RGB data of the
// image. Otherwise, the icon will be white for all the
// non-transparent pixels in the image.
iconSource.ShowAsMonochrome(false);
iconSource.ShowAsMonochrome(monochrome);
iconSource.UriSource(iconUri);
return iconSource;
}
Expand Down Expand Up @@ -121,14 +124,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
// Return Value:
// - An IconElement with its IconSource set, if possible.
template<typename TIconSource>
TIconSource _getIconSource(const winrt::hstring& iconPath)
TIconSource _getIconSource(const winrt::hstring& iconPath, bool monochrome)
{
TIconSource iconSource{ nullptr };

if (iconPath.size() != 0)
{
const auto expandedIconPath{ _expandIconPath(iconPath) };
iconSource = _getColoredBitmapIcon<TIconSource>(expandedIconPath);
iconSource = _getColoredBitmapIcon<TIconSource>(expandedIconPath, monochrome);

// If we fail to set the icon source using the "icon" as a path,
// let's try it as a symbol/emoji.
Expand Down Expand Up @@ -197,7 +200,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
const hstring& /* language */)
{
const auto& iconPath = winrt::unbox_value_or<winrt::hstring>(value, L"");
return _getIconSource<Controls::IconSource>(iconPath);
return _getIconSource<Controls::IconSource>(iconPath, false);
}

// unused for one-way bindings
Expand All @@ -211,12 +214,12 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation

Windows::UI::Xaml::Controls::IconSource _IconSourceWUX(hstring path)
{
return _getIconSource<Windows::UI::Xaml::Controls::IconSource>(path);
return _getIconSource<Windows::UI::Xaml::Controls::IconSource>(path, false);
}

Microsoft::UI::Xaml::Controls::IconSource _IconSourceMUX(hstring path)
Microsoft::UI::Xaml::Controls::IconSource _IconSourceMUX(hstring path, bool monochrome)
{
return _getIconSource<Microsoft::UI::Xaml::Controls::IconSource>(path);
return _getIconSource<Microsoft::UI::Xaml::Controls::IconSource>(path, monochrome);
}

SoftwareBitmap _convertToSoftwareBitmap(HICON hicon,
Expand Down Expand Up @@ -329,13 +332,14 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
return bitmapSource;
}

MUX::Controls::IconSource IconPathConverter::IconSourceMUX(const winrt::hstring& iconPath)
MUX::Controls::IconSource IconPathConverter::IconSourceMUX(const winrt::hstring& iconPath,
const bool monochrome)
{
std::wstring_view iconPathWithoutIndex;
const auto indexOpt = _getIconIndex(iconPath, iconPathWithoutIndex);
if (!indexOpt.has_value())
{
return _IconSourceMUX(iconPath);
return _IconSourceMUX(iconPath, monochrome);
}

const auto bitmapSource = _getImageIconSourceForBinary(iconPathWithoutIndex, indexOpt.value());
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/IconPathConverter.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
const hstring& language);

static Windows::UI::Xaml::Controls::IconElement IconWUX(const winrt::hstring& iconPath);
static Microsoft::UI::Xaml::Controls::IconSource IconSourceMUX(const winrt::hstring& iconPath);
static Microsoft::UI::Xaml::Controls::IconSource IconSourceMUX(const winrt::hstring& iconPath, const bool convertToGrayscale);
};
}

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/IconPathConverter.idl
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ namespace Microsoft.Terminal.Settings.Model
IconPathConverter();

static Windows.UI.Xaml.Controls.IconElement IconWUX(String path);
static Microsoft.UI.Xaml.Controls.IconSource IconSourceMUX(String path);
static Microsoft.UI.Xaml.Controls.IconSource IconSourceMUX(String path, Boolean convertToGrayscale);
};

}
7 changes: 4 additions & 3 deletions src/cascadia/TerminalSettingsModel/MTSMSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,8 @@ Author(s):
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, Background, "background", nullptr) \
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, UnfocusedBackground, "unfocusedBackground", nullptr)

#define MTSM_THEME_TAB_SETTINGS(X) \
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, Background, "background", nullptr) \
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, UnfocusedBackground, "unfocusedBackground", nullptr) \
#define MTSM_THEME_TAB_SETTINGS(X) \
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, Background, "background", nullptr) \
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, UnfocusedBackground, "unfocusedBackground", nullptr) \
X(winrt::Microsoft::Terminal::Settings::Model::IconStyle, IconStyle, "iconStyle", winrt::Microsoft::Terminal::Settings::Model::IconStyle::Default) \
X(winrt::Microsoft::Terminal::Settings::Model::TabCloseButtonVisibility, ShowCloseButton, "showCloseButton", winrt::Microsoft::Terminal::Settings::Model::TabCloseButtonVisibility::Always)
Original file line number Diff line number Diff line change
Expand Up @@ -652,6 +652,15 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::TabCloseButtonVi
};
};

JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::IconStyle)
{
JSON_MAPPINGS(3) = {
pair_type{ "default", ValueType::Default },
pair_type{ "hidden", ValueType::Hidden },
pair_type{ "monochrome", ValueType::Monochrome },
};
};

// Possible ScrollToMarkDirection values
JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Control::ScrollToMarkDirection)
{
Expand Down
8 changes: 8 additions & 0 deletions src/cascadia/TerminalSettingsModel/Theme.idl
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
namespace Microsoft.Terminal.Settings.Model
{

enum IconStyle
{
Default,
Hidden,
Monochrome
};

enum ThemeColorType
{
Accent,
Expand Down Expand Up @@ -63,6 +70,7 @@ namespace Microsoft.Terminal.Settings.Model
ThemeColor Background { get; };
ThemeColor UnfocusedBackground { get; };
TabCloseButtonVisibility ShowCloseButton { get; };
IconStyle IconStyle { get; };
}

[default_interface] runtimeclass Theme : Windows.Foundation.IStringable {
Expand Down
Loading