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 a key binding Toggle Acrylic #15717

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
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
9 changes: 9 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1225,6 +1225,15 @@ namespace winrt::TerminalApp::implementation
}
}

void TerminalPage::_HandleToggleAcrylic(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
const auto res = _ApplyToActiveControls([&](auto& control) {
control.ToggleAcrylic();
});
args.Handled(res);
}

void TerminalPage::_HandleSelectAll(const IInspectable& sender,
const ActionEventArgs& args)
{
Expand Down
33 changes: 27 additions & 6 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -687,7 +687,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// - focused (default == true): Whether the window is focused or unfocused.
// Return Value:
// - <none>
void ControlCore::_setOpacity(const double opacity, bool focused)
void ControlCore::_setOpacity(const double opacity, const bool focused)
{
const auto newOpacity = std::clamp(opacity,
0.0,
Expand All @@ -705,8 +705,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
//to transition smoothly between the two.
_runtimeFocusedOpacity = focused ? newOpacity : _runtimeFocusedOpacity;

// Manually turn off acrylic if they turn off transparency.
_runtimeUseAcrylic = newOpacity < 1.0 && _settings->UseAcrylic();
//Forces focused acrylic when Command Palette is open upon changing opacity
_runtimeUseAcrylic = FocusedAcrylic();

// Update the renderer as well. It might need to fall back from
// cleartype -> grayscale if the BG is transparent / acrylic.
Expand All @@ -721,6 +721,26 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_TransparencyChangedHandlers(*this, *eventArgs);
}

void ControlCore::ToggleAcrylic(const bool focused)
{
UseAcrylic(!UseAcrylic());

//Stores the focused runtime acrylic separately from unfocused acrylic
//to transition smoothly between the two.
_runtimeFocusedAcrylic = focused ? UseAcrylic() : _runtimeFocusedAcrylic;

// Update the renderer as well. It might need to fall back from
// cleartype -> grayscale if the BG is transparent / acrylic.
if (_renderEngine)
{
_renderEngine->EnableTransparentBackground(_isBackgroundTransparent());
_renderer->NotifyPaintFrame();
}

auto eventArgs = winrt::make_self<TransparencyChangedEventArgs>(0.0);
_TransparencyChangedHandlers(*this, *eventArgs);
}

void ControlCore::ToggleShaderEffects()
{
const auto path = _settings->PixelShaderPath();
Expand Down Expand Up @@ -838,7 +858,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_runtimeFocusedOpacity = std::nullopt;

// Manually turn off acrylic if they turn off transparency.
_runtimeUseAcrylic = _settings->Opacity() < 1.0 && _settings->UseAcrylic();
_runtimeUseAcrylic = _settings->UseAcrylic();

const auto sizeChanged = _setFontSizeUnderLock(_settings->FontSize());

Expand Down Expand Up @@ -898,8 +918,9 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// No need to update Acrylic if UnfocusedAcrylic is disabled
if (_settings->EnableUnfocusedAcrylic())
{
// Manually turn off acrylic if they turn off transparency.
_runtimeUseAcrylic = Opacity() < 1.0 && newAppearance->UseAcrylic();
// Focused Acrylic from settings should be ignored if overridden at runtime
bool newAcrylic = focused ? FocusedAcrylic() : newAppearance->UseAcrylic();
UseAcrylic(newAcrylic);
}

// Update the renderer as well. It might need to fall back from
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation

void AdjustOpacity(const double opacity, const bool relative);

void ToggleAcrylic(const bool focused);

void WindowVisibilityChanged(const bool showOrHide);

uint64_t OwningHwnd();
Expand All @@ -248,6 +250,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
RUNTIME_SETTING(double, Opacity, _settings->Opacity());
RUNTIME_SETTING(double, FocusedOpacity, FocusedAppearance().Opacity());
RUNTIME_SETTING(bool, UseAcrylic, _settings->UseAcrylic());
RUNTIME_SETTING(bool, FocusedAcrylic, FocusedAppearance().UseAcrylic());

// -------------------------------- WinRT Events ---------------------------------
// clang-format off
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@ namespace Microsoft.Terminal.Control
CommandHistoryContext CommandHistory();

void AdjustOpacity(Double Opacity, Boolean relative);
void ToggleAcrylic(Boolean focused);
void WindowVisibilityChanged(Boolean showOrHide);

void ColorSelection(SelectionColor fg, SelectionColor bg, Microsoft.Terminal.Core.MatchMode matchMode);
Expand Down
11 changes: 8 additions & 3 deletions src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// GH#11743: Make sure to use the Core's current UseAcrylic value, not
// the one from the settings. The Core's runtime UseAcrylic may have
// changed from what was in the original settings.
if (_core.UseAcrylic() && !transparentBg)
if (_core.UseAcrylic() && !transparentBg && _core.Opacity() < 1.0)
{
// See if we've already got an acrylic background brush
// to avoid the flicker when setting up a new one
Expand Down Expand Up @@ -903,7 +903,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// acrylic is enabled at runtime (GH#2531)
if (auto acrylic = RootGrid().Background().try_as<Media::AcrylicBrush>())
{
if (!useAcrylic)
if (!useAcrylic || _core.Opacity() == 1.0)
{
_InitializeBackgroundBrush();
return;
Expand All @@ -913,7 +913,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
}
else if (auto solidColor = RootGrid().Background().try_as<Media::SolidColorBrush>())
{
if (useAcrylic)
if (useAcrylic && _core.Opacity() < 1.0)
{
_InitializeBackgroundBrush();
return;
Expand Down Expand Up @@ -3483,6 +3483,11 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_core.AdjustOpacity(opacity, relative);
}

void TermControl::ToggleAcrylic()
{
_core.ToggleAcrylic(_focused);
}

// - You'd think this should just be "Opacity", but UIElement already
// defines an "Opacity", which we're actually not setting at all. We're
// not overriding or changing _that_ value. Callers that want the opacity
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,6 +149,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation

void AdjustOpacity(const double opacity, const bool relative);

void ToggleAcrylic();

bool RawWriteKeyEvent(const WORD vkey, const WORD scanCode, const winrt::Microsoft::Terminal::Core::ControlKeyStates modifiers, const bool keyDown);
bool RawWriteChar(const wchar_t character, const WORD scanCode, const winrt::Microsoft::Terminal::Core::ControlKeyStates modifiers);
void RawWriteString(const winrt::hstring& text);
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ namespace Microsoft.Terminal.Control

void AdjustOpacity(Double Opacity, Boolean relative);

void ToggleAcrylic();

// You'd think this should just be "Opacity", but UIElement already
// defines an "Opacity", which we're actually not setting at all. We're
// not overriding or changing _that_ value. Callers that want the
Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ static constexpr std::string_view ClearBufferKey{ "clearBuffer" };
static constexpr std::string_view MultipleActionsKey{ "multipleActions" };
static constexpr std::string_view QuitKey{ "quit" };
static constexpr std::string_view AdjustOpacityKey{ "adjustOpacity" };
static constexpr std::string_view ToggleAcrylicKey{ "toggleAcrylic" };
static constexpr std::string_view RestoreLastClosedKey{ "restoreLastClosed" };
static constexpr std::string_view SelectAllKey{ "selectAll" };
static constexpr std::string_view SelectCommandKey{ "selectCommand" };
Expand Down Expand Up @@ -419,6 +420,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::MultipleActions, MustGenerate },
{ ShortcutAction::Quit, RS_(L"QuitCommandKey") },
{ ShortcutAction::AdjustOpacity, MustGenerate },
{ ShortcutAction::ToggleAcrylic, RS_(L"ToggleAcrylicCommandKey") },
{ ShortcutAction::RestoreLastClosed, RS_(L"RestoreLastClosedCommandKey") },
{ ShortcutAction::SelectCommand, MustGenerate },
{ ShortcutAction::SelectOutput, MustGenerate },
Expand Down
1 change: 1 addition & 0 deletions src/cascadia/TerminalSettingsModel/AllShortcutActions.h
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@
ON_ALL_ACTIONS(MultipleActions) \
ON_ALL_ACTIONS(Quit) \
ON_ALL_ACTIONS(AdjustOpacity) \
ON_ALL_ACTIONS(ToggleAcrylic) \
ON_ALL_ACTIONS(RestoreLastClosed) \
ON_ALL_ACTIONS(SelectAll) \
ON_ALL_ACTIONS(SelectCommand) \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -580,6 +580,10 @@
<value>Set background opacity to {0}%</value>
<comment>A command to change how transparent the background of the window is</comment>
</data>
<data name="ToggleAcrylicCommandKey" xml:space="preserve">
<value>Toggle acrylic</value>
<comment>A command to toggle whether the "acrylic" window effect is enabled or not. "Acrylic" is a Microsoft-specific term: https://docs.microsoft.com/en-us/windows/apps/design/style/acrylic</comment>
</data>
<data name="RestoreLastClosedCommandKey" xml:space="preserve">
<value>Restore the last closed pane or tab</value>
</data>
Expand Down
26 changes: 15 additions & 11 deletions src/cascadia/TerminalSettingsModel/defaults.json
Original file line number Diff line number Diff line change
Expand Up @@ -489,6 +489,21 @@
{ "command": "resetFontSize", "keys": "ctrl+0" },
{ "command": "resetFontSize", "keys": "ctrl+numpad_0" },

// Transparency Effects
{
"name": { "key": "SetOpacityParentCommandName" },
"commands": [
{ "command": { "action": "adjustOpacity", "opacity": 0, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 25, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 50, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 75, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 100, "relative": false } }
]


},
{ "command": "toggleAcrylic"},

// Other commands
{
// Select color scheme...
Expand Down Expand Up @@ -541,16 +556,5 @@
}
]
},
{
// Set opacity...
"name": { "key": "SetOpacityParentCommandName" },
"commands": [
{ "command": { "action": "adjustOpacity", "opacity": 0, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 25, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 50, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 75, "relative": false } },
{ "command": { "action": "adjustOpacity", "opacity": 100, "relative": false } }
]
}
]
}
Loading