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

Add Mica Alt support (#17650) #17858

Draft
wants to merge 5 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions .github/actions/spelling/allow/microsoft.txt
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ symbolrequestprod
Sysinternals
sysnative
systemroot
TABBEDWINDOW
taskkill
tasklist
tdbuildteamid
Expand Down
4 changes: 2 additions & 2 deletions src/cascadia/TerminalSettingsEditor/MainPage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
const auto hr = DwmGetWindowAttribute(*_hostingHwnd, DWMWA_SYSTEMBACKDROP_TYPE, &attribute, sizeof(attribute));
if (SUCCEEDED(hr))
{
isMicaAvailable = attribute == DWMSBT_MAINWINDOW;
isMicaAvailable = (attribute == DWMSBT_MAINWINDOW) || (attribute == DWMSBT_TABBEDWINDOW);
}
}

Expand All @@ -716,7 +716,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation
// that our theme is different than the app's.
const bool actuallyUseMica = isMicaAvailable && (appTheme == requestedTheme);

const auto bgKey = (theme.Window() != nullptr && theme.Window().UseMica()) && actuallyUseMica ?
const auto bgKey = (theme.Window() != nullptr && theme.Window().MicaStyle() != MicaStyle::Default) && actuallyUseMica ?
L"SettingsPageMicaBackground" :
L"SettingsPageBackground";

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/TerminalSettingsModel/MTSMSettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ Author(s):
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, Frame, "frame", nullptr) \
X(winrt::Microsoft::Terminal::Settings::Model::ThemeColor, UnfocusedFrame, "unfocusedFrame", nullptr) \
X(bool, RainbowFrame, "experimental.rainbowFrame", false) \
X(bool, UseMica, "useMica", false)
X(winrt::Microsoft::Terminal::Settings::Model::MicaStyle, MicaStyle, "micaStyle", winrt::Microsoft::Terminal::Settings::Model::MicaStyle::Default)

#define MTSM_THEME_SETTINGS_SETTINGS(X) \
X(winrt::Windows::UI::Xaml::ElementTheme, RequestedTheme, "theme", winrt::Windows::UI::Xaml::ElementTheme::Default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -666,6 +666,15 @@ JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::IconStyle)
};
};

JSON_ENUM_MAPPER(::winrt::Microsoft::Terminal::Settings::Model::MicaStyle)
{
JSON_MAPPINGS(3) = {
pair_type{ "default", ValueType::Default },
pair_type{ "original", ValueType::Original },
pair_type{ "alt", ValueType::Alt },
};
};

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

enum MicaStyle
{
Default,
Original,
Alt
};

[default_interface] runtimeclass ThemePair
{
ThemePair();
Expand Down Expand Up @@ -60,7 +67,7 @@ namespace Microsoft.Terminal.Settings.Model

runtimeclass WindowTheme {
Windows.UI.Xaml.ElementTheme RequestedTheme { get; };
Boolean UseMica { get; };
MicaStyle MicaStyle { get; };
Boolean RainbowFrame { get; };
ThemeColor Frame { get; };
ThemeColor UnfocusedFrame { get; };
Expand Down
12 changes: 6 additions & 6 deletions src/cascadia/UnitTests_SettingsModel/ThemeTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ namespace SettingsModelUnitTests
"window":
{
"applicationTheme": "light",
"useMica": true
"micaStyle": "original"
}
})" };

Expand All @@ -68,7 +68,7 @@ namespace SettingsModelUnitTests

VERIFY_IS_NOT_NULL(theme->Window());
VERIFY_ARE_EQUAL(winrt::Windows::UI::Xaml::ElementTheme::Light, theme->Window().RequestedTheme());
VERIFY_ARE_EQUAL(true, theme->Window().UseMica());
VERIFY_ARE_NOT_EQUAL(winrt::Microsoft::Terminal::Settings::Model::MicaStyle::Default, theme->Window().MicaStyle());
}

void ThemeTests::ParseEmptyTheme()
Expand Down Expand Up @@ -151,7 +151,7 @@ namespace SettingsModelUnitTests
"window":
{
"applicationTheme": "light",
"useMica": true
"micaStyle": "original"
}
},
{
Expand All @@ -163,15 +163,15 @@ namespace SettingsModelUnitTests
"window":
{
"applicationTheme": "light",
"useMica": true
"micaStyle": "original"
}
},
{
"name": "backgroundOmittedEntirely",
"window":
{
"applicationTheme": "light",
"useMica": true
"micaStyle": "original"
}
}
]
Expand Down Expand Up @@ -227,7 +227,7 @@ namespace SettingsModelUnitTests
"window":
{
"applicationTheme": "light",
"useMica": true
"micaStyle": "original"
}
}
]
Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/WindowsTerminal/AppHost.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,7 +1130,7 @@ void AppHost::_updateTheme()
const auto colorOpacity = b ? color.A / 255.0 : 0.0;
const auto brushOpacity = _opacityFromBrush(b);
const auto opacity = std::min(colorOpacity, brushOpacity);
_window->UseMica(windowTheme ? windowTheme.UseMica() : false, opacity);
_window->SetMicaStyle(windowTheme ? windowTheme.MicaStyle() : MicaStyle::Default, opacity);

// This is a hack to make the window borders dark instead of light.
// It must be done before WM_NCPAINT so that the borders are rendered with
Expand Down
13 changes: 10 additions & 3 deletions src/cascadia/WindowsTerminal/IslandWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1940,16 +1940,23 @@ void IslandWindow::UseDarkTheme(const bool v)
std::ignore = DwmSetWindowAttribute(GetHandle(), DWMWA_USE_IMMERSIVE_DARK_MODE, &attribute, sizeof(attribute));
}

void IslandWindow::UseMica(const bool newValue, const double /*titlebarOpacity*/)
void IslandWindow::SetMicaStyle(const winrt::Microsoft::Terminal::Settings::Model::MicaStyle newValue, const double /*titlebarOpacity*/)
{
// This block of code enables Mica for our window. By all accounts, this
// version of the code will only work on Windows 11, SV2. There's a slightly
// different API surface for enabling Mica on Windows 11 22000.0.
//
// This API was only publicly supported as of Windows 11 SV2, 22621. Before
// that version, this API will just return an error and do nothing silently.

const int attribute = newValue ? DWMSBT_MAINWINDOW : DWMSBT_NONE;
int attribute = DWMSBT_NONE;
if (newValue == MicaStyle::Original)
{
attribute = DWMSBT_MAINWINDOW;
}
else if (newValue == MicaStyle::Alt)
{
attribute = DWMSBT_TABBEDWINDOW;
}
std::ignore = DwmSetWindowAttribute(GetHandle(), DWMWA_SYSTEMBACKDROP_TYPE, &attribute, sizeof(attribute));
}

Expand Down
2 changes: 1 addition & 1 deletion src/cascadia/WindowsTerminal/IslandWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class IslandWindow :
void RemoveFromSystemMenu(const winrt::hstring& itemLabel);

void UseDarkTheme(const bool v);
virtual void UseMica(const bool newValue, const double titlebarOpacity);
virtual void SetMicaStyle(const winrt::Microsoft::Terminal::Settings::Model::MicaStyle newValue, const double titlebarOpacity);

til::event<winrt::delegate<>> DragRegionClicked;
til::event<winrt::delegate<>> WindowCloseButtonClicked;
Expand Down
8 changes: 4 additions & 4 deletions src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -941,7 +941,7 @@ void NonClientIslandWindow::_UpdateFrameMargins() const noexcept
// use NCHITTEST to determine where to place the Snap Flyout. The drag
// rect will handle that.

margins.cyTopHeight = (_useMica || _titlebarOpacity < 1.0) ? 0 : -frame.top;
margins.cyTopHeight = (_micaStyle != winrt::Microsoft::Terminal::Settings::Model::MicaStyle::Default || _titlebarOpacity < 1.0) ? 0 : -frame.top;
}

// Extend the frame into the client area. microsoft/terminal#2735 - Just log
Expand Down Expand Up @@ -1174,15 +1174,15 @@ void NonClientIslandWindow::SetTitlebarBackground(winrt::Windows::UI::Xaml::Medi
_titlebar.Background(brush);
}

void NonClientIslandWindow::UseMica(const bool newValue, const double titlebarOpacity)
void NonClientIslandWindow::SetMicaStyle(const winrt::Microsoft::Terminal::Settings::Model::MicaStyle newValue, const double titlebarOpacity)
{
// Stash internally if we're using Mica. If we aren't, we don't want to
// totally blow away our titlebar with DwmExtendFrameIntoClientArea,
// especially on Windows 10
_useMica = newValue;
_micaStyle = newValue;
_titlebarOpacity = titlebarOpacity;

IslandWindow::UseMica(newValue, titlebarOpacity);
IslandWindow::SetMicaStyle(newValue, titlebarOpacity);

_UpdateFrameMargins();
}
5 changes: 3 additions & 2 deletions src/cascadia/WindowsTerminal/NonClientIslandWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ class NonClientIslandWindow : public IslandWindow

void SetTitlebarBackground(winrt::Windows::UI::Xaml::Media::Brush brush);

virtual void UseMica(const bool newValue, const double titlebarOpacity) override;
virtual void SetMicaStyle(const winrt::Microsoft::Terminal::Settings::Model::MicaStyle newValue, const double titlebarOpacity) override;

private:
std::optional<til::point> _oldIslandPos;
Expand All @@ -67,7 +67,8 @@ class NonClientIslandWindow : public IslandWindow

winrt::Windows::UI::Xaml::ElementTheme _theme;

bool _useMica{ false };
winrt::Microsoft::Terminal::Settings::Model::MicaStyle _micaStyle{ winrt::Microsoft::Terminal::Settings::Model::MicaStyle::Default };

double _titlebarOpacity{ 1.0 };

bool _isMaximized;
Expand Down
Loading