From 56016c92f7fb86230aaacd0e5afd52cd54433af2 Mon Sep 17 00:00:00 2001 From: Davide Giacometti Date: Thu, 14 Jul 2022 13:32:54 +0200 Subject: [PATCH] Setting for hide window when it loses focus (#13478) ## Summary of the Pull Request Added setting for for hide window when it loses focus. Works on normal window and quake window. ## References ## PR Checklist * [x] Closes #10660 * [x] CLA signed. If not, go over [here](https://cla.opensource.microsoft.com/microsoft/Terminal) and sign the CLA * [ ] Tests added/passed * [ ] Documentation updated. If checked, please file a pull request on [our docs repo](https://github.com/MicrosoftDocs/terminal) and link it here: #xxx * [ ] Schema updated. * [ ] I've discussed this with core contributors already. If not checked, I'm ready to accept this work might be rejected in favor of a different grand plan. Issue number where discussion took place: #xxx ## Detailed Description of the Pull Request / Additional comments ## Validation Steps Performed - Enable **Settings > Appearance > Automatically hide window** - Terminal window should be minimized on taskbar when it loses focus - Quake window should be minimized on system tray when it loses focus - Enable also **Settings > Appearance > Hide Terminal in the notification area when it is minimized** - Terminal window should be minimized on system tray when it loses focus - Quake window should be minimized on system tray when it loses focus --- doc/cascadia/profiles.schema.json | 5 +++++ src/cascadia/TerminalApp/AppLogic.cpp | 11 +++++++++++ src/cascadia/TerminalApp/AppLogic.h | 1 + src/cascadia/TerminalApp/AppLogic.idl | 1 + .../GlobalAppearance.xaml | 6 ++++++ .../GlobalAppearanceViewModel.h | 1 + .../GlobalAppearanceViewModel.idl | 1 + .../Resources/en-US/Resources.resw | 11 +++++++++-- .../TerminalSettingsModel/GlobalAppSettings.idl | 1 + .../TerminalSettingsModel/MTSMSettings.h | 1 + src/cascadia/WindowsTerminal/AppHost.cpp | 2 ++ src/cascadia/WindowsTerminal/IslandWindow.cpp | 17 +++++++++++++++++ src/cascadia/WindowsTerminal/IslandWindow.h | 2 ++ 13 files changed, 58 insertions(+), 2 deletions(-) diff --git a/doc/cascadia/profiles.schema.json b/doc/cascadia/profiles.schema.json index 583c5f7d8b1..c72822b45cb 100644 --- a/doc/cascadia/profiles.schema.json +++ b/doc/cascadia/profiles.schema.json @@ -1968,6 +1968,11 @@ "useAnyExisting" ], "type": "string" + }, + "autoHideWindow": { + "default": false, + "description": "If enabled, Terminal window will be hidden as soon as it loses focus.", + "type": "boolean" } }, "required": [ diff --git a/src/cascadia/TerminalApp/AppLogic.cpp b/src/cascadia/TerminalApp/AppLogic.cpp index e85c3cff3fa..243455cc0a9 100644 --- a/src/cascadia/TerminalApp/AppLogic.cpp +++ b/src/cascadia/TerminalApp/AppLogic.cpp @@ -1500,6 +1500,17 @@ namespace winrt::TerminalApp::implementation return _root ? _root->AlwaysOnTop() : false; } + bool AppLogic::AutoHideWindow() + { + if (!_loadedInitialSettings) + { + // Load settings if we haven't already + LoadSettings(); + } + + return _settings.GlobalSettings().AutoHideWindow(); + } + Windows::Foundation::Collections::IMapView AppLogic::GlobalHotkeys() { return _settings.GlobalSettings().ActionMap().GlobalHotkeys(); diff --git a/src/cascadia/TerminalApp/AppLogic.h b/src/cascadia/TerminalApp/AppLogic.h index 7abf70f725a..6b715e0c7bb 100644 --- a/src/cascadia/TerminalApp/AppLogic.h +++ b/src/cascadia/TerminalApp/AppLogic.h @@ -79,6 +79,7 @@ namespace winrt::TerminalApp::implementation bool Fullscreen() const; void Maximized(bool newMaximized); bool AlwaysOnTop() const; + bool AutoHideWindow(); bool ShouldUsePersistedLayout(); bool ShouldImmediatelyHandoffToElevated(); diff --git a/src/cascadia/TerminalApp/AppLogic.idl b/src/cascadia/TerminalApp/AppLogic.idl index 7f2f0d15921..0f77af0c8cd 100644 --- a/src/cascadia/TerminalApp/AppLogic.idl +++ b/src/cascadia/TerminalApp/AppLogic.idl @@ -70,6 +70,7 @@ namespace TerminalApp Boolean Fullscreen { get; }; void Maximized(Boolean newMaximized); Boolean AlwaysOnTop { get; }; + Boolean AutoHideWindow { get; }; void IdentifyWindow(); String WindowName; diff --git a/src/cascadia/TerminalSettingsEditor/GlobalAppearance.xaml b/src/cascadia/TerminalSettingsEditor/GlobalAppearance.xaml index bf540a81465..53ea257ea68 100644 --- a/src/cascadia/TerminalSettingsEditor/GlobalAppearance.xaml +++ b/src/cascadia/TerminalSettingsEditor/GlobalAppearance.xaml @@ -120,6 +120,12 @@ + + + + + diff --git a/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.h b/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.h index 9256537430f..3826afcb1eb 100644 --- a/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.h +++ b/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.h @@ -42,6 +42,7 @@ namespace winrt::Microsoft::Terminal::Settings::Editor::implementation PERMANENT_OBSERVABLE_PROJECTED_SETTING(_GlobalSettings, UseAcrylicInTabRow); PERMANENT_OBSERVABLE_PROJECTED_SETTING(_GlobalSettings, ShowTitleInTitlebar); PERMANENT_OBSERVABLE_PROJECTED_SETTING(_GlobalSettings, AlwaysOnTop); + PERMANENT_OBSERVABLE_PROJECTED_SETTING(_GlobalSettings, AutoHideWindow); PERMANENT_OBSERVABLE_PROJECTED_SETTING(_GlobalSettings, AlwaysShowNotificationIcon); PERMANENT_OBSERVABLE_PROJECTED_SETTING(_GlobalSettings, MinimizeToNotificationArea); diff --git a/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.idl b/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.idl index 8d14d618be1..06053e7e528 100644 --- a/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.idl +++ b/src/cascadia/TerminalSettingsEditor/GlobalAppearanceViewModel.idl @@ -33,6 +33,7 @@ namespace Microsoft.Terminal.Settings.Editor PERMANENT_OBSERVABLE_PROJECTED_SETTING(Boolean, UseAcrylicInTabRow); PERMANENT_OBSERVABLE_PROJECTED_SETTING(Boolean, ShowTitleInTitlebar); PERMANENT_OBSERVABLE_PROJECTED_SETTING(Boolean, AlwaysOnTop); + PERMANENT_OBSERVABLE_PROJECTED_SETTING(Boolean, AutoHideWindow); PERMANENT_OBSERVABLE_PROJECTED_SETTING(Boolean, AlwaysShowNotificationIcon); PERMANENT_OBSERVABLE_PROJECTED_SETTING(Boolean, MinimizeToNotificationArea); } diff --git a/src/cascadia/TerminalSettingsEditor/Resources/en-US/Resources.resw b/src/cascadia/TerminalSettingsEditor/Resources/en-US/Resources.resw index 83f32f0fca0..fe4323391ed 100644 --- a/src/cascadia/TerminalSettingsEditor/Resources/en-US/Resources.resw +++ b/src/cascadia/TerminalSettingsEditor/Resources/en-US/Resources.resw @@ -317,7 +317,6 @@ What should be shown when the first terminal is created. - Open a tab with the default profile @@ -1450,4 +1449,12 @@ Bold font with bright colors An option to choose from for the "intense text format" setting. When selected, "intense" text will be rendered as both bold text and in a brighter color - + + Automatically hide window + Header for a control to toggle the "Automatically hide window" setting. If enabled, the window will be hidden as soon as it loses focus. + + + If enabled, the window will be hidden as soon as it loses focus. + A description for what the "Automatically hide window" setting does. + + \ No newline at end of file diff --git a/src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl b/src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl index 9e6564740f1..26ae0e10585 100644 --- a/src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl +++ b/src/cascadia/TerminalSettingsModel/GlobalAppSettings.idl @@ -82,6 +82,7 @@ namespace Microsoft.Terminal.Settings.Model INHERITABLE_SETTING(Boolean, DebugFeaturesEnabled); INHERITABLE_SETTING(Boolean, StartOnUserLogin); INHERITABLE_SETTING(Boolean, AlwaysOnTop); + INHERITABLE_SETTING(Boolean, AutoHideWindow); INHERITABLE_SETTING(TabSwitcherMode, TabSwitcherMode); INHERITABLE_SETTING(Boolean, DisableAnimations); INHERITABLE_SETTING(String, StartupActions); diff --git a/src/cascadia/TerminalSettingsModel/MTSMSettings.h b/src/cascadia/TerminalSettingsModel/MTSMSettings.h index c7abda9a29a..1503c223783 100644 --- a/src/cascadia/TerminalSettingsModel/MTSMSettings.h +++ b/src/cascadia/TerminalSettingsModel/MTSMSettings.h @@ -51,6 +51,7 @@ Author(s): X(bool, DebugFeaturesEnabled, "debugFeatures", debugFeaturesDefault) \ X(bool, StartOnUserLogin, "startOnUserLogin", false) \ X(bool, AlwaysOnTop, "alwaysOnTop", false) \ + X(bool, AutoHideWindow, "autoHideWindow", false) \ X(Model::TabSwitcherMode, TabSwitcherMode, "tabSwitcherMode", Model::TabSwitcherMode::InOrder) \ X(bool, DisableAnimations, "disableAnimations", false) \ X(hstring, StartupActions, "startupActions", L"") \ diff --git a/src/cascadia/WindowsTerminal/AppHost.cpp b/src/cascadia/WindowsTerminal/AppHost.cpp index 31e100e8baa..9a5c6887124 100644 --- a/src/cascadia/WindowsTerminal/AppHost.cpp +++ b/src/cascadia/WindowsTerminal/AppHost.cpp @@ -103,6 +103,7 @@ AppHost::AppHost() noexcept : _window->ShouldExitFullscreen({ &_logic, &winrt::TerminalApp::AppLogic::RequestExitFullscreen }); _window->SetAlwaysOnTop(_logic.GetInitialAlwaysOnTop()); + _window->SetAutoHideWindow(_logic.AutoHideWindow()); _window->MakeWindow(); @@ -1393,6 +1394,7 @@ void AppHost::_HandleSettingsChanged(const winrt::Windows::Foundation::IInspecta } _window->SetMinimizeToNotificationAreaBehavior(_logic.GetMinimizeToNotificationArea()); + _window->SetAutoHideWindow(_logic.AutoHideWindow()); _updateTheme(); } diff --git a/src/cascadia/WindowsTerminal/IslandWindow.cpp b/src/cascadia/WindowsTerminal/IslandWindow.cpp index 6bf1bc52b8e..7464aae2382 100644 --- a/src/cascadia/WindowsTerminal/IslandWindow.cpp +++ b/src/cascadia/WindowsTerminal/IslandWindow.cpp @@ -442,6 +442,18 @@ long IslandWindow::_calculateTotalSize(const bool isWidth, const long clientSize const bool activated = LOWORD(wparam) != 0; _WindowActivatedHandlers(activated); + if (_autoHideWindow && !activated) + { + if (_isQuakeWindow || _minimizeToNotificationArea) + { + HideWindow(); + } + else + { + ShowWindow(GetHandle(), SW_MINIMIZE); + } + } + break; } @@ -1626,6 +1638,11 @@ void IslandWindow::IsQuakeWindow(bool isQuakeWindow) noexcept } } +void IslandWindow::SetAutoHideWindow(bool autoHideWindow) noexcept +{ + _autoHideWindow = autoHideWindow; +} + // Method Description: // - Enter quake mode for the monitor this window is currently on. This involves // resizing it to the top half of the monitor. diff --git a/src/cascadia/WindowsTerminal/IslandWindow.h b/src/cascadia/WindowsTerminal/IslandWindow.h index 89bfcbc71fd..39e683da0b6 100644 --- a/src/cascadia/WindowsTerminal/IslandWindow.h +++ b/src/cascadia/WindowsTerminal/IslandWindow.h @@ -55,6 +55,7 @@ class IslandWindow : bool IsQuakeWindow() const noexcept; void IsQuakeWindow(bool isQuakeWindow) noexcept; + void SetAutoHideWindow(bool autoHideWindow) noexcept; void HideWindow(); @@ -135,6 +136,7 @@ class IslandWindow : void _moveToMonitor(const MONITORINFO activeMonitor); bool _isQuakeWindow{ false }; + bool _autoHideWindow{ false }; void _enterQuakeMode(); til::rect _getQuakeModeSize(HMONITOR hmon);