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

Allow Opacity to be set differently in both focused and unfocused terminals #15974

Merged
merged 13 commits into from
Oct 13, 2023
Merged
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
7 changes: 7 additions & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,13 @@
"useAcrylic":{
"description": "When set to true, the window will have an acrylic material background when unfocused. When set to false, the window will have a plain, untextured background when unfocused.",
"type": "boolean"
},
"opacity": {
"default": 100,
"description": "Sets the opacity of the window for the profile when unfocused. Accepts values from 0-100.",
"maximum": 100,
"minimum": 0,
"type": "number"
}
},
"type": "object"
Expand Down
38 changes: 29 additions & 9 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -680,7 +680,14 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_setOpacity(Opacity() + adjustment);
}

void ControlCore::_setOpacity(const double opacity)
// Method Description:
// - Updates the opacity of the terminal
// Arguments:
// - opacity: The new opacity to set.
// - focused (default == true): Whether the window is focused or unfocused.
// Return Value:
// - <none>
void ControlCore::_setOpacity(const double opacity, bool focused)
{
const auto newOpacity = std::clamp(opacity,
0.0,
Expand All @@ -694,6 +701,10 @@ namespace winrt::Microsoft::Terminal::Control::implementation
// Update our runtime opacity value
_runtimeOpacity = newOpacity;

//Stores the focused runtime opacity separately from unfocused opacity
//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();

Expand Down Expand Up @@ -824,6 +835,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_cellWidth = CSSLengthPercentage::FromString(_settings->CellWidth().c_str());
_cellHeight = CSSLengthPercentage::FromString(_settings->CellHeight().c_str());
_runtimeOpacity = std::nullopt;
_runtimeFocusedOpacity = std::nullopt;

// Manually turn off acrylic if they turn off transparency.
_runtimeUseAcrylic = _settings->Opacity() < 1.0 && _settings->UseAcrylic();
Expand Down Expand Up @@ -874,21 +886,29 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_renderEngine->SetRetroTerminalEffect(newAppearance->RetroTerminalEffect());
_renderEngine->SetPixelShaderPath(newAppearance->PixelShaderPath());

// Incase EnableUnfocusedAcrylic is disabled and Focused Acrylic is set to true,
// the terminal should ignore the unfocused opacity from settings.
// The Focused Opacity from settings should be ignored if overridden at runtime.
bool useFocusedRuntimeOpacity = focused || (!_settings->EnableUnfocusedAcrylic() && UseAcrylic());
double newOpacity = useFocusedRuntimeOpacity ?
FocusedOpacity() :
newAppearance->Opacity();
_setOpacity(newOpacity, focused);

// 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();
}

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

auto eventArgs = winrt::make_self<TransparencyChangedEventArgs>(Opacity());
// Update the renderer as well. It might need to fall back from
// cleartype -> grayscale if the BG is transparent / acrylic.
_renderEngine->EnableTransparentBackground(_isBackgroundTransparent());
_renderer->NotifyPaintFrame();

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

_renderer->TriggerRedrawAll(true, true);
}
Expand Down
3 changes: 2 additions & 1 deletion src/cascadia/TerminalControl/ControlCore.h
Original file line number Diff line number Diff line change
Expand Up @@ -246,6 +246,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
bool ShouldShowSelectOutput();

RUNTIME_SETTING(double, Opacity, _settings->Opacity());
RUNTIME_SETTING(double, FocusedOpacity, FocusedAppearance().Opacity());
RUNTIME_SETTING(bool, UseAcrylic, _settings->UseAcrylic());

// -------------------------------- WinRT Events ---------------------------------
Expand Down Expand Up @@ -386,7 +387,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
void _updateAntiAliasingMode();
void _connectionOutputHandler(const hstring& hstr);
void _updateHoveredCell(const std::optional<til::point> terminalPosition);
void _setOpacity(const double opacity);
void _setOpacity(const double opacity, const bool focused = true);

bool _isBackgroundTransparent();
void _focusChanged(bool focused);
Expand Down