From 5858150d7d94e3eb0c17410d66879c478d97871e Mon Sep 17 00:00:00 2001 From: Anurag Thakur Date: Thu, 10 Jun 2021 20:51:38 +0000 Subject: [PATCH 1/3] Dim out window controls on lost focus --- .github/actions/spelling/expect/expect.txt | 1 + .../TerminalApp/MinMaxCloseControl.cpp | 14 + .../TerminalApp/MinMaxCloseControl.xaml | 640 +++++++++--------- src/cascadia/TerminalApp/TitlebarControl.cpp | 12 + src/cascadia/TerminalApp/TitlebarControl.h | 4 + src/cascadia/TerminalApp/TitlebarControl.idl | 6 +- .../WindowsTerminal/NonClientIslandWindow.cpp | 28 + .../WindowsTerminal/NonClientIslandWindow.h | 2 + 8 files changed, 394 insertions(+), 313 deletions(-) diff --git a/.github/actions/spelling/expect/expect.txt b/.github/actions/spelling/expect/expect.txt index 722631b26ea..2e76b86916e 100644 --- a/.github/actions/spelling/expect/expect.txt +++ b/.github/actions/spelling/expect/expect.txt @@ -1464,6 +1464,7 @@ namestream nano natvis nbsp +NCACTIVATE NCCALCSIZE NCCREATE NCLBUTTONDOWN diff --git a/src/cascadia/TerminalApp/MinMaxCloseControl.cpp b/src/cascadia/TerminalApp/MinMaxCloseControl.cpp index 0072b2093ad..a499db02116 100644 --- a/src/cascadia/TerminalApp/MinMaxCloseControl.cpp +++ b/src/cascadia/TerminalApp/MinMaxCloseControl.cpp @@ -83,6 +83,20 @@ namespace winrt::TerminalApp::implementation MaximizeToolTip().Text(RS_(L"WindowRestoreDownButtonToolTip")); break; + + case WindowVisualState::WindowVisualStateUnfocused: + VisualStateManager::GoToState(MaximizeButton(), L"WindowStateUnfocused", false); + VisualStateManager::GoToState(MinimizeButton(), L"WindowStateUnfocused", false); + VisualStateManager::GoToState(CloseButton(), L"WindowStateUnfocused", false); + break; + + case WindowVisualState::WindowVisualStateFocused: + VisualStateManager::GoToState(MaximizeButton(), L"WindowStateFocused", false); + VisualStateManager::GoToState(MinimizeButton(), L"WindowStateFocused", false); + VisualStateManager::GoToState(CloseButton(), L"WindowStateFocused", false); + break; + + case WindowVisualState::WindowVisualStateNormal: case WindowVisualState::WindowVisualStateIconified: default: diff --git a/src/cascadia/TerminalApp/MinMaxCloseControl.xaml b/src/cascadia/TerminalApp/MinMaxCloseControl.xaml index fd3de4f4379..51e20575887 100644 --- a/src/cascadia/TerminalApp/MinMaxCloseControl.xaml +++ b/src/cascadia/TerminalApp/MinMaxCloseControl.xaml @@ -1,312 +1,328 @@ - - - - - - - - 1.0 - - - - - - - - Transparent - - - - - - #00e81123 - - - 1.0 - - - - - - - - Transparent - - - - - - #00e81123 - - - 3.0 - - - - - - - - - - - - - - - - - M 0 0 L 10 10 M 10 0 L 0 10 - M 0 0 L 10 10 M 10 0 L 0 10 - - - 36.0 - 32.0 - - - - - - - - - - + + + + + + + + 1.0 + + + + + + + + + Transparent + + + + + + #00e81123 + + + 1.0 + + + + + + + + + Transparent + + + + + + #00e81123 + + + 3.0 + + + + + + + + + + + + + + + + + + M 0 0 L 10 10 M 10 0 L 0 10 + M 0 0 L 10 10 M 10 0 L 0 10 + + + 36.0 + 32.0 + + + + + + + + + + diff --git a/src/cascadia/TerminalApp/TitlebarControl.cpp b/src/cascadia/TerminalApp/TitlebarControl.cpp index 05806726cfe..a7e42df1a94 100644 --- a/src/cascadia/TerminalApp/TitlebarControl.cpp +++ b/src/cascadia/TerminalApp/TitlebarControl.cpp @@ -66,6 +66,18 @@ namespace winrt::TerminalApp::implementation } } + void TitlebarControl::Focused(bool focused) + { + _focused = focused; + const auto state = focused ? winrt::TerminalApp::WindowVisualState::WindowVisualStateFocused : + winrt::TerminalApp::WindowVisualState::WindowVisualStateUnfocused; + MinMaxCloseControl().SetWindowVisualState(state); + } + + bool TitlebarControl::Focused() const + { + return _focused; + } void TitlebarControl::Maximize_Click(winrt::Windows::Foundation::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::RoutedEventArgs const& /*e*/) { _OnMaximizeOrRestore(HTMAXBUTTON); diff --git a/src/cascadia/TerminalApp/TitlebarControl.h b/src/cascadia/TerminalApp/TitlebarControl.h index a30f347d868..3e02c6d9cdb 100644 --- a/src/cascadia/TerminalApp/TitlebarControl.h +++ b/src/cascadia/TerminalApp/TitlebarControl.h @@ -17,6 +17,9 @@ namespace winrt::TerminalApp::implementation { TitlebarControl(uint64_t handle); + bool Focused() const; + void Focused(bool focused); + IInspectable Content(); void Content(IInspectable content); @@ -29,6 +32,7 @@ namespace winrt::TerminalApp::implementation void DragBar_DoubleTapped(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs const& e); private: + bool _focused; void _OnMaximizeOrRestore(byte flag); HWND _window{ nullptr }; // non-owning handle; should not be freed in the dtor. }; diff --git a/src/cascadia/TerminalApp/TitlebarControl.idl b/src/cascadia/TerminalApp/TitlebarControl.idl index 104f7a2f26f..dccbc5d0302 100644 --- a/src/cascadia/TerminalApp/TitlebarControl.idl +++ b/src/cascadia/TerminalApp/TitlebarControl.idl @@ -7,7 +7,9 @@ namespace TerminalApp { WindowVisualStateNormal = 0, WindowVisualStateMaximized, - WindowVisualStateIconified + WindowVisualStateIconified, + WindowVisualStateFocused, + WindowVisualStateUnfocused }; [default_interface] runtimeclass TitlebarControl : Windows.UI.Xaml.Controls.Grid @@ -15,6 +17,8 @@ namespace TerminalApp TitlebarControl(UInt64 parentWindowHandle); void SetWindowVisualState(WindowVisualState visualState); + Boolean Focused { get; set; }; + IInspectable Content; Windows.UI.Xaml.Controls.Border DragBar { get; }; } diff --git a/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp b/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp index eb4e6400ed4..8fcd98105b1 100644 --- a/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp +++ b/src/cascadia/WindowsTerminal/NonClientIslandWindow.cpp @@ -516,6 +516,32 @@ int NonClientIslandWindow::_GetResizeHandleHeight() const noexcept return 0; } +// Method Description: +// - Responds to the WM_NCACTIVATE message by changing the focus state of +// the window. +[[nodiscard]] LRESULT NonClientIslandWindow::_OnFocusChanged(const WPARAM wParam, const LPARAM lParam) noexcept +{ + const auto windowStyle = GetWindowStyle(_window.get()); + const auto isIconified = WI_IsFlagSet(windowStyle, WS_ICONIC); + + if (isIconified) + { + return DefWindowProc(_window.get(), WM_NCACTIVATE, wParam, lParam); + } + + if (_titlebar) + { + _isFocused = wParam; + try + { + _titlebar.Focused(_isFocused); + } + CATCH_LOG(); + } + + return TRUE; +} + // Method Description: // - Hit test the frame for resizing and moving. // Arguments: @@ -708,6 +734,8 @@ void NonClientIslandWindow::_UpdateFrameMargins() const noexcept return 0; case WM_NCCALCSIZE: return _OnNcCalcSize(wParam, lParam); + case WM_NCACTIVATE: + return _OnFocusChanged(wParam, lParam); case WM_NCHITTEST: return _OnNcHitTest({ GET_X_LPARAM(lParam), GET_Y_LPARAM(lParam) }); case WM_PAINT: diff --git a/src/cascadia/WindowsTerminal/NonClientIslandWindow.h b/src/cascadia/WindowsTerminal/NonClientIslandWindow.h index 10a39b4fcb1..b510611f106 100644 --- a/src/cascadia/WindowsTerminal/NonClientIslandWindow.h +++ b/src/cascadia/WindowsTerminal/NonClientIslandWindow.h @@ -61,6 +61,7 @@ class NonClientIslandWindow : public IslandWindow winrt::Windows::UI::Xaml::ElementTheme _theme; bool _isMaximized; + bool _isFocused; [[nodiscard]] static LRESULT __stdcall _StaticInputSinkWndProc(HWND const window, UINT const message, WPARAM const wparam, LPARAM const lparam) noexcept; [[nodiscard]] LRESULT _InputSinkMessageHandler(UINT const message, WPARAM const wparam, LPARAM const lparam) noexcept; @@ -73,6 +74,7 @@ class NonClientIslandWindow : public IslandWindow [[nodiscard]] LRESULT _OnNcCreate(WPARAM wParam, LPARAM lParam) noexcept override; [[nodiscard]] LRESULT _OnNcCalcSize(const WPARAM wParam, const LPARAM lParam) noexcept; + [[nodiscard]] LRESULT _OnFocusChanged(const WPARAM wParam, const LPARAM lParam) noexcept; [[nodiscard]] LRESULT _OnNcHitTest(POINT ptMouse) const noexcept; [[nodiscard]] LRESULT _OnPaint() noexcept; [[nodiscard]] LRESULT _OnSetCursor(WPARAM wParam, LPARAM lParam) const noexcept; From ca2f7e83a2f559e0888f0801674bd6dd9b492f44 Mon Sep 17 00:00:00 2001 From: Anurag Thakur Date: Fri, 11 Jun 2021 06:15:24 +0000 Subject: [PATCH 2/3] Fix diff --- .../TerminalApp/MinMaxCloseControl.cpp | 2 - .../TerminalApp/MinMaxCloseControl.xaml | 656 +++++++++--------- src/cascadia/TerminalApp/TitlebarControl.cpp | 7 +- 3 files changed, 332 insertions(+), 333 deletions(-) diff --git a/src/cascadia/TerminalApp/MinMaxCloseControl.cpp b/src/cascadia/TerminalApp/MinMaxCloseControl.cpp index a499db02116..75a264cdb32 100644 --- a/src/cascadia/TerminalApp/MinMaxCloseControl.cpp +++ b/src/cascadia/TerminalApp/MinMaxCloseControl.cpp @@ -83,7 +83,6 @@ namespace winrt::TerminalApp::implementation MaximizeToolTip().Text(RS_(L"WindowRestoreDownButtonToolTip")); break; - case WindowVisualState::WindowVisualStateUnfocused: VisualStateManager::GoToState(MaximizeButton(), L"WindowStateUnfocused", false); VisualStateManager::GoToState(MinimizeButton(), L"WindowStateUnfocused", false); @@ -96,7 +95,6 @@ namespace winrt::TerminalApp::implementation VisualStateManager::GoToState(CloseButton(), L"WindowStateFocused", false); break; - case WindowVisualState::WindowVisualStateNormal: case WindowVisualState::WindowVisualStateIconified: default: diff --git a/src/cascadia/TerminalApp/MinMaxCloseControl.xaml b/src/cascadia/TerminalApp/MinMaxCloseControl.xaml index 51e20575887..db71fff1946 100644 --- a/src/cascadia/TerminalApp/MinMaxCloseControl.xaml +++ b/src/cascadia/TerminalApp/MinMaxCloseControl.xaml @@ -1,328 +1,328 @@ - - - - - - - - 1.0 - - - - - - - - - Transparent - - - - - - #00e81123 - - - 1.0 - - - - - - - - - Transparent - - - - - - #00e81123 - - - 3.0 - - - - - - - - - - - - - - - - - - M 0 0 L 10 10 M 10 0 L 0 10 - M 0 0 L 10 10 M 10 0 L 0 10 - - - 36.0 - 32.0 - - - - - - - - - - + + + + + + + + 1.0 + + + + + + + + + Transparent + + + + + + #00e81123 + + + 1.0 + + + + + + + + + Transparent + + + + + + #00e81123 + + + 3.0 + + + + + + + + + + + + + + + + + + M 0 0 L 10 10 M 10 0 L 0 10 + M 0 0 L 10 10 M 10 0 L 0 10 + + + 36.0 + 32.0 + + + + + + + + + + diff --git a/src/cascadia/TerminalApp/TitlebarControl.cpp b/src/cascadia/TerminalApp/TitlebarControl.cpp index a7e42df1a94..3d61b283281 100644 --- a/src/cascadia/TerminalApp/TitlebarControl.cpp +++ b/src/cascadia/TerminalApp/TitlebarControl.cpp @@ -69,15 +69,16 @@ namespace winrt::TerminalApp::implementation void TitlebarControl::Focused(bool focused) { _focused = focused; - const auto state = focused ? winrt::TerminalApp::WindowVisualState::WindowVisualStateFocused : - winrt::TerminalApp::WindowVisualState::WindowVisualStateUnfocused; - MinMaxCloseControl().SetWindowVisualState(state); + const auto state = focused ? winrt::TerminalApp::WindowVisualState::WindowVisualStateFocused : + winrt::TerminalApp::WindowVisualState::WindowVisualStateUnfocused; + MinMaxCloseControl().SetWindowVisualState(state); } bool TitlebarControl::Focused() const { return _focused; } + void TitlebarControl::Maximize_Click(winrt::Windows::Foundation::IInspectable const& /*sender*/, winrt::Windows::UI::Xaml::RoutedEventArgs const& /*e*/) { _OnMaximizeOrRestore(HTMAXBUTTON); From 2ad963647a904d0f79fb5b97a2361bf126fcab9b Mon Sep 17 00:00:00 2001 From: Anurag Thakur Date: Fri, 11 Jun 2021 06:52:03 +0000 Subject: [PATCH 3/3] Get rekt format bot --- src/cascadia/TerminalApp/MinMaxCloseControl.xaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/cascadia/TerminalApp/MinMaxCloseControl.xaml b/src/cascadia/TerminalApp/MinMaxCloseControl.xaml index db71fff1946..4f7c020d456 100644 --- a/src/cascadia/TerminalApp/MinMaxCloseControl.xaml +++ b/src/cascadia/TerminalApp/MinMaxCloseControl.xaml @@ -27,7 +27,7 @@ + ResourceKey="SystemControlForegroundBaseLowBrush" /> + ResourceKey="SystemControlForegroundBaseLowBrush" /> + ResourceKey="SystemControlForegroundBaseLowBrush" />