Skip to content

Commit

Permalink
Add adjustOpacity action (#12092)
Browse files Browse the repository at this point in the history
Adds an action which can be used to change the opacity at runtime. This is a follow up to some of the other work I had been doing around opacity and settings previewing at the end of the year.

> edit: pseudo-spec
> 
> * `adjustOpacity`:
>   * `"opacity"`: (**required**) an integer number to set the opacity to.
>   * `"relative"`: (defaults to `true`)
>     * If false, set the opacity to the given value, which should be between [0, 100].
>     * If true, then use `opacity` as a relative adjustment to the current opacity. So the implementation would get the current opacity for this pane, then add this action's opacity to that value. Should be between [-100, 100]
> 
> This would allow both setting exactly 25% opacity with an action, and binding an action that increases/decreases the opacity one {amount}

It's preview-able too, which is neat. 

![adjust-opacity-preview-001](https://user-images.githubusercontent.com/18356694/140785805-5eecf6ec-0933-40e4-bcba-41b88d25b6a8.gif)



* [x] Closes #11205
* [x] I work here
* [x] Docs updated: MicrosoftDocs/terminal#477
  • Loading branch information
zadjii-msft authored Jan 12, 2022
1 parent 2f68814 commit c3fd26a
Show file tree
Hide file tree
Showing 18 changed files with 224 additions and 16 deletions.
32 changes: 32 additions & 0 deletions doc/cascadia/profiles.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@
"toggleShaderEffects",
"wt",
"quit",
"adjustOpacity",
"restoreLastClosed",
"unbound"
],
Expand Down Expand Up @@ -1385,6 +1386,34 @@
}
]
},
"AdjustOpacityAction": {
"description": "Changes the opacity of the active Terminal window. If `relative` is specified, then this action will increase/decrease relative to the current opacity.",
"allOf": [
{
"$ref": "#/$defs/ShortcutAction"
},
{
"properties": {
"action": {
"type": "string",
"const": "adjustOpacity"
},
"opacity": {
"type": "integer",
"minimum": -100,
"maximum": 100,
"default": 0,
"description": "How opaque the terminal should become or how much the opacity should be changed by, depending on the value of `relative`"
},
"relative": {
"type": "boolean",
"default": true,
"description": "If true, then adjust the current opacity by the given `opacity` parameter, additively. If false, set the opacity to exactly that value."
}
}
}
]
},
"Keybinding": {
"additionalProperties": false,
"properties": {
Expand Down Expand Up @@ -1493,6 +1522,9 @@
{
"$ref": "#/$defs/QuakeModeAction"
},
{
"$ref": "#/$defs/AdjustOpacityAction"
},
{
"type": "null"
}
Expand Down
6 changes: 3 additions & 3 deletions src/cascadia/LocalTests_TerminalApp/TabTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,7 @@ namespace TerminalAppLocalTests
Log::Comment(L"Emulate committing the SetColorScheme action");

SetColorSchemeArgs args{ L"Vintage" };
page->_EndPreviewColorScheme();
page->_EndPreview();
page->_HandleSetColorScheme(nullptr, ActionEventArgs{ args });
});

Expand Down Expand Up @@ -1395,7 +1395,7 @@ namespace TerminalAppLocalTests

TestOnUIThread([&page]() {
Log::Comment(L"Emulate dismissing the SetColorScheme action");
page->_EndPreviewColorScheme();
page->_EndPreview();
});

TestOnUIThread([&page]() {
Expand Down Expand Up @@ -1469,7 +1469,7 @@ namespace TerminalAppLocalTests
Log::Comment(L"Emulate committing the SetColorScheme action");

SetColorSchemeArgs args{ L"One Half Light" };
page->_EndPreviewColorScheme();
page->_EndPreview();
page->_HandleSetColorScheme(nullptr, ActionEventArgs{ args });
});

Expand Down
36 changes: 31 additions & 5 deletions src/cascadia/TerminalApp/ActionPreviewHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -49,23 +49,23 @@ namespace winrt::TerminalApp::implementation
switch (_lastPreviewedCommand.ActionAndArgs().Action())
{
case ShortcutAction::SetColorScheme:
case ShortcutAction::AdjustOpacity:
{
_EndPreviewColorScheme();
_RunRestorePreviews();
break;
}
}
_lastPreviewedCommand = nullptr;
}

// Method Description:
// - Revert any changes from the preview on a SetColorScheme action. This
// will remove the preview TerminalSettings we inserted into the control's
// TerminalSettings graph, and update the control.
// - Revert any changes from the preview action. This will run the restore
// function that the preview added to _restorePreviewFuncs
// Arguments:
// - <none>
// Return Value:
// - <none>
void TerminalPage::_EndPreviewColorScheme()
void TerminalPage::_RunRestorePreviews()
{
// Apply the reverts in reverse order - If we had multiple previews
// stacked on top of each other, then this will ensure the first one in
Expand Down Expand Up @@ -111,6 +111,27 @@ namespace winrt::TerminalApp::implementation
}
}

void TerminalPage::_PreviewAdjustOpacity(const Settings::Model::AdjustOpacityArgs& args)
{
// Clear the saved preview funcs because we don't need to add a restore each time
// the preview changes, we only need to be able to restore the last one.
_restorePreviewFuncs.clear();

_ApplyToActiveControls([&](const auto& control) {
// Stash a copy of the original opacity.
auto originalOpacity{ control.BackgroundOpacity() };

// Apply the new opacity
control.AdjustOpacity(args.Opacity(), args.Relative());

_restorePreviewFuncs.emplace_back([=]() {
// On dismiss:
// Don't adjust relatively, just set outright.
control.AdjustOpacity(::base::saturated_cast<int>(originalOpacity * 100), false);
});
});
}

// Method Description:
// - Handler for the CommandPalette::PreviewAction event. The Command
// Palette will raise this even when an action is selected, but _not_
Expand Down Expand Up @@ -142,6 +163,11 @@ namespace winrt::TerminalApp::implementation
_PreviewColorScheme(args.ActionAndArgs().Args().try_as<SetColorSchemeArgs>());
break;
}
case ShortcutAction::AdjustOpacity:
{
_PreviewAdjustOpacity(args.ActionAndArgs().Args().try_as<AdjustOpacityArgs>());
break;
}
}

// GH#9818 Other ideas for actions that could be preview-able:
Expand Down
15 changes: 15 additions & 0 deletions src/cascadia/TerminalApp/AppActionHandlers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -958,4 +958,19 @@ namespace winrt::TerminalApp::implementation
}
}
}

void TerminalPage::_HandleAdjustOpacity(const IInspectable& /*sender*/,
const ActionEventArgs& args)
{
if (args)
{
if (const auto& realArgs = args.ActionArgs().try_as<AdjustOpacityArgs>())
{
const auto res = _ApplyToActiveControls([&](auto& control) {
control.AdjustOpacity(realArgs.Opacity(), realArgs.Relative());
});
args.Handled(res);
}
}
}
}
3 changes: 2 additions & 1 deletion src/cascadia/TerminalApp/TerminalPage.h
Original file line number Diff line number Diff line change
Expand Up @@ -398,8 +398,9 @@ namespace winrt::TerminalApp::implementation

void _PreviewActionHandler(const IInspectable& sender, const Microsoft::Terminal::Settings::Model::Command& args);
void _EndPreview();
void _EndPreviewColorScheme();
void _RunRestorePreviews();
void _PreviewColorScheme(const Microsoft::Terminal::Settings::Model::SetColorSchemeArgs& args);
void _PreviewAdjustOpacity(const Microsoft::Terminal::Settings::Model::AdjustOpacityArgs& args);
winrt::Microsoft::Terminal::Settings::Model::Command _lastPreviewedCommand{ nullptr };
std::vector<std::function<void()>> _restorePreviewFuncs{};

Expand Down
31 changes: 27 additions & 4 deletions src/cascadia/TerminalControl/ControlCore.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -445,11 +445,20 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return;
}

auto newOpacity = std::clamp(Opacity() + adjustment,
0.0,
1.0);
_setOpacity(Opacity() + adjustment);
}

void ControlCore::_setOpacity(const double opacity)
{
const auto newOpacity = std::clamp(opacity,
0.0,
1.0);

if (newOpacity == Opacity())
{
return;
}

auto lock = _terminal->LockForWriting();
// Update our runtime opacity value
Opacity(newOpacity);

Expand Down Expand Up @@ -1701,6 +1710,7 @@ namespace winrt::Microsoft::Terminal::Control::implementation
_settings->FocusedAppearance()->SetColorTableEntry(15, scheme.BrightWhite);

_terminal->ApplyScheme(scheme);

_renderEngine->SetSelectionBackground(til::color{ _settings->SelectionBackground() });

_renderer->TriggerRedrawAll();
Expand All @@ -1712,6 +1722,19 @@ namespace winrt::Microsoft::Terminal::Control::implementation
return _settings->HasUnfocusedAppearance();
}

void ControlCore::AdjustOpacity(const int32_t& opacity, const bool& relative)
{
const double opacityAdjust = static_cast<double>(opacity) / 100.0;
if (relative)
{
AdjustOpacity(opacityAdjust);
}
else
{
_setOpacity(opacityAdjust);
}
}

bool ControlCore::_isBackgroundTransparent()
{
// If we're:
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 @@ -166,6 +166,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation

static bool IsVintageOpacityAvailable() noexcept;

void AdjustOpacity(const int32_t& opacity, const bool& relative);

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

Expand Down Expand Up @@ -269,6 +271,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);

bool _isBackgroundTransparent();

Expand Down
2 changes: 2 additions & 0 deletions src/cascadia/TerminalControl/ControlCore.idl
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,8 @@ namespace Microsoft.Terminal.Control

String ReadEntireBuffer();

void AdjustOpacity(Int32 Opacity, Boolean relative);

event FontSizeChangedEventArgs FontSizeChanged;

event Windows.Foundation.TypedEventHandler<Object, CopyToClipboardEventArgs> CopyToClipboard;
Expand Down
15 changes: 14 additions & 1 deletion src/cascadia/TerminalControl/TermControl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2001,7 +2001,6 @@ namespace winrt::Microsoft::Terminal::Control::implementation
const winrt::Windows::Foundation::Size minSize{ 1, 1 };
const double scaleFactor = DisplayInformation::GetForCurrentView().RawPixelsPerViewPixel();
const auto dpi = ::base::saturated_cast<uint32_t>(USER_DEFAULT_SCREEN_DPI * scaleFactor);

return GetProposedDimensions(_core.Settings(), dpi, minSize);
}
}
Expand Down Expand Up @@ -2708,4 +2707,18 @@ namespace winrt::Microsoft::Terminal::Control::implementation
{
_core.ColorScheme(scheme);
}

void TermControl::AdjustOpacity(const int32_t& opacity, const bool& relative)
{
_core.AdjustOpacity(opacity, relative);
}

// - 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
// set by the settings should call this instead.
double TermControl::BackgroundOpacity() const
{
return _core.Opacity();
}
}
5 changes: 5 additions & 0 deletions src/cascadia/TerminalControl/TermControl.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
#include "SearchBoxControl.h"

#include "ControlInteractivity.h"
#include "ControlSettings.h"

namespace Microsoft::Console::VirtualTerminal
{
Expand Down Expand Up @@ -56,6 +57,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
int BufferHeight() const;

bool BracketedPasteEnabled() const noexcept;

double BackgroundOpacity() const;
#pragma endregion

void ScrollViewport(int viewTop);
Expand Down Expand Up @@ -107,6 +110,8 @@ namespace winrt::Microsoft::Terminal::Control::implementation
winrt::Microsoft::Terminal::Core::Scheme ColorScheme() const noexcept;
void ColorScheme(const winrt::Microsoft::Terminal::Core::Scheme& scheme) const noexcept;

void AdjustOpacity(const int32_t& opacity, const bool& relative);

// -------------------------------- WinRT Events ---------------------------------
// clang-format off
WINRT_CALLBACK(FontSizeChanged, Control::FontSizeChangedEventArgs);
Expand Down
9 changes: 9 additions & 0 deletions src/cascadia/TerminalControl/TermControl.idl
Original file line number Diff line number Diff line change
Expand Up @@ -72,5 +72,14 @@ namespace Microsoft.Terminal.Control
void ToggleReadOnly();

String ReadEntireBuffer();

void AdjustOpacity(Int32 Opacity, Boolean relative);

// 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 set by the settings should call this instead.
Double BackgroundOpacity { get; };

}
}
2 changes: 2 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionAndArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ static constexpr std::string_view ExportBufferKey{ "exportBuffer" };
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 RestoreLastClosedKey{ "restoreLastClosed" };

static constexpr std::string_view ActionKey{ "action" };
Expand Down Expand Up @@ -383,6 +384,7 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{ ShortcutAction::ClearBuffer, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::MultipleActions, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::Quit, RS_(L"QuitCommandKey") },
{ ShortcutAction::AdjustOpacity, L"" }, // Intentionally omitted, must be generated by GenerateName
{ ShortcutAction::RestoreLastClosed, RS_(L"RestoreLastClosedCommandKey") },
};
}();
Expand Down
32 changes: 32 additions & 0 deletions src/cascadia/TerminalSettingsModel/ActionArgs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
#include "ExportBufferArgs.g.cpp"
#include "ClearBufferArgs.g.cpp"
#include "MultipleActionsArgs.g.cpp"
#include "AdjustOpacityArgs.g.cpp"

#include <LibraryResources.h>
#include <WtExeUtils.h>
Expand Down Expand Up @@ -769,4 +770,35 @@ namespace winrt::Microsoft::Terminal::Settings::Model::implementation
{
return L"";
}

winrt::hstring AdjustOpacityArgs::GenerateName() const
{
if (Relative())
{
if (Opacity() >= 0)
{
// "Increase background opacity by {Opacity}%"
return winrt::hstring{
fmt::format(std::wstring_view(RS_(L"IncreaseOpacityCommandKey")),
Opacity())
};
}
else
{
// "Decrease background opacity by {Opacity}%"
return winrt::hstring{
fmt::format(std::wstring_view(RS_(L"DecreaseOpacityCommandKey")),
Opacity())
};
}
}
else
{
// "Set background opacity to {Opacity}%"
return winrt::hstring{
fmt::format(std::wstring_view(RS_(L"AdjustOpacityCommandKey")),
Opacity())
};
}
}
}
Loading

0 comments on commit c3fd26a

Please sign in to comment.