Move the actual MMC handling to the TitlebarControl

This commit is contained in:
Mike Griese
2019-07-11 15:15:27 -05:00
parent ac1f20bf33
commit 19058520c0
6 changed files with 91 additions and 85 deletions

View File

@@ -17,64 +17,26 @@ namespace winrt::TerminalApp::implementation
winrt::Windows::UI::Xaml::Application::LoadComponent(*this, resourceLocator, winrt::Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation::Nested);
}
uint64_t MinMaxCloseControl::ParentWindowHandle() const
void MinMaxCloseControl::Maximize()
{
return reinterpret_cast<uint64_t>(_window);
winrt::Windows::UI::Xaml::VisualStateManager::GoToState(MaximizeButton(), L"WindowStateMaximized", false);
}
void MinMaxCloseControl::ParentWindowHandle(uint64_t handle)
void MinMaxCloseControl::RestoreDown()
{
_window = reinterpret_cast<HWND>(handle);
}
void MinMaxCloseControl::_OnMaximize(byte flag)
{
if (_window)
{
POINT point1 = {};
::GetCursorPos(&point1);
const LPARAM lParam = MAKELPARAM(point1.x, point1.y);
WINDOWPLACEMENT placement = { sizeof(placement) };
::GetWindowPlacement(_window, &placement);
if (placement.showCmd == SW_SHOWNORMAL)
{
winrt::Windows::UI::Xaml::VisualStateManager::GoToState(this->Maximize(), L"WindowStateMaximized", false);
::PostMessage(_window, WM_SYSCOMMAND, SC_MAXIMIZE | flag, lParam);
}
else if (placement.showCmd == SW_SHOWMAXIMIZED)
{
winrt::Windows::UI::Xaml::VisualStateManager::GoToState(this->Maximize(), L"WindowStateNormal", false);
::PostMessage(_window, WM_SYSCOMMAND, SC_RESTORE | flag, lParam);
}
}
}
void MinMaxCloseControl::Maximize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
_OnMaximize(HTMAXBUTTON);
}
void MinMaxCloseControl::DoubleClickDragBar()
{
_OnMaximize(HTCAPTION);
}
void MinMaxCloseControl::DragBar_DoubleTapped(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs const& e)
{
_OnMaximize(HTCAPTION);
winrt::Windows::UI::Xaml::VisualStateManager::GoToState(MaximizeButton(), L"WindowStateNormal", false);
}
// These need to be defined here to make the compiler happy. They do nothing
// on their own, it's up to the control that's embedding us to set a
// callback when these buttons are pressed.
void MinMaxCloseControl::Minimize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
if (_window)
{
::PostMessage(_window, WM_SYSCOMMAND, SC_MINIMIZE | HTMINBUTTON, 0);
}
}
void MinMaxCloseControl::Maximize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
}
void MinMaxCloseControl::Close_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
::PostQuitMessage(0);
}
}

View File

@@ -15,19 +15,12 @@ namespace winrt::TerminalApp::implementation
{
MinMaxCloseControl();
void Maximize();
void RestoreDown();
void Minimize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void Maximize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void Close_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void DragBar_DoubleTapped(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs const& e);
uint64_t ParentWindowHandle() const;
void ParentWindowHandle(uint64_t handle);
void DoubleClickDragBar();
private:
void _OnMaximize(byte flag);
HWND _window{ nullptr }; // non-owning handle; should not be freed in the dtor.
};
}

View File

@@ -4,12 +4,11 @@
{
MinMaxCloseControl();
// Windows.UI.Xaml.Controls.Grid Content { get; };
// Windows.UI.Xaml.Controls.Border DragBar { get; };
void Maximize();
void RestoreDown();
UInt64 ParentWindowHandle;
// TODO: probably just do this in TitlebarControl
void DoubleClickDragBar();
Windows.UI.Xaml.Controls.Button MinimizeButton { get; };
Windows.UI.Xaml.Controls.Button MaximizeButton { get; };
Windows.UI.Xaml.Controls.Button CloseButton { get; };
}
}

View File

@@ -129,9 +129,7 @@
</ResourceDictionary>
</StackPanel.Resources>
<!-- <Grid x:Name="Content"></Grid> -->
<!-- <Border Height="36.0" MinWidth="45.0" x:Name="DragBar" Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" DoubleTapped="DragBar_DoubleTapped"/> -->
<Button Height="36.0" MinWidth="45.0" Width="45.0" x:Name="Minimize" Style="{StaticResource CaptionButton}" Click="Minimize_Click"
<Button Height="36.0" MinWidth="45.0" Width="45.0" x:Name="MinimizeButton" Style="{StaticResource CaptionButton}" Click="Minimize_Click"
AutomationProperties.Name="Minimize">
<Button.Resources>
<ResourceDictionary>
@@ -139,7 +137,7 @@
</ResourceDictionary>
</Button.Resources>
</Button>
<Button Height="36.0" MinWidth="45.0" Width="45.0" x:Name="Maximize" Style="{StaticResource CaptionButton}" Click="Maximize_Click"
<Button Height="36.0" MinWidth="45.0" Width="45.0" x:Name="MaximizeButton" Style="{StaticResource CaptionButton}" Click="Maximize_Click"
AutomationProperties.Name="Maximize">
<Button.Resources>
<ResourceDictionary>
@@ -148,7 +146,7 @@
</ResourceDictionary>
</Button.Resources>
</Button>
<Button Height="36.0" MinWidth="45.0" Width="45.0" x:Name="Close" Style="{StaticResource CaptionButton}" Click="Close_Click"
<Button Height="36.0" MinWidth="45.0" Width="45.0" x:Name="CloseButton" Style="{StaticResource CaptionButton}" Click="Close_Click"
AutomationProperties.Name="Close">
<Button.Resources>
<ResourceDictionary>

View File

@@ -15,16 +15,11 @@ namespace winrt::TerminalApp::implementation
{
const winrt::Windows::Foundation::Uri resourceLocator{ L"ms-appx:///TitlebarControl.xaml" };
winrt::Windows::UI::Xaml::Application::LoadComponent(*this, resourceLocator, winrt::Windows::UI::Xaml::Controls::Primitives::ComponentResourceLocation::Nested);
}
uint64_t TitlebarControl::ParentWindowHandle()
{
return MinMaxCloseControl().ParentWindowHandle();
}
void TitlebarControl::ParentWindowHandle(uint64_t handle)
{
MinMaxCloseControl().ParentWindowHandle(handle);
// Register our event handlers on the MMC buttons.
MinMaxCloseControl().MinimizeButton().Click({ this, &TitlebarControl::Minimize_Click });
MinMaxCloseControl().MaximizeButton().Click({ this, &TitlebarControl::Maximize_Click });
MinMaxCloseControl().CloseButton().Click({ this, &TitlebarControl::Close_Click });
}
Windows::UI::Xaml::UIElement TitlebarControl::Content()
@@ -38,11 +33,6 @@ namespace winrt::TerminalApp::implementation
ContentRoot().Children().Append(content);
}
void TitlebarControl::DragBar_DoubleTapped(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs const& e)
{
MinMaxCloseControl().DoubleClickDragBar();
}
void TitlebarControl::Root_SizeChanged(const IInspectable& sender, Windows::UI::Xaml::SizeChangedEventArgs const& e)
{
const auto windowWidth = ActualWidth();
@@ -51,4 +41,60 @@ namespace winrt::TerminalApp::implementation
const auto maxWidth = windowWidth - minMaxCloseWidth - dragBarMinWidth;
ContentRoot().MaxWidth(maxWidth);
}
uint64_t TitlebarControl::ParentWindowHandle() const
{
return reinterpret_cast<uint64_t>(_window);
}
void TitlebarControl::ParentWindowHandle(uint64_t handle)
{
_window = reinterpret_cast<HWND>(handle);
}
void TitlebarControl::_OnMaximize(byte flag)
{
if (_window)
{
POINT point1 = {};
::GetCursorPos(&point1);
const LPARAM lParam = MAKELPARAM(point1.x, point1.y);
WINDOWPLACEMENT placement = { sizeof(placement) };
::GetWindowPlacement(_window, &placement);
if (placement.showCmd == SW_SHOWNORMAL)
{
MinMaxCloseControl().Maximize();
::PostMessage(_window, WM_SYSCOMMAND, SC_MAXIMIZE | flag, lParam);
}
else if (placement.showCmd == SW_SHOWMAXIMIZED)
{
MinMaxCloseControl().RestoreDown();
::PostMessage(_window, WM_SYSCOMMAND, SC_RESTORE | flag, lParam);
}
}
}
void TitlebarControl::Maximize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
_OnMaximize(HTMAXBUTTON);
}
void TitlebarControl::DragBar_DoubleTapped(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs const& e)
{
_OnMaximize(HTCAPTION);
}
void TitlebarControl::Minimize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
if (_window)
{
::PostMessage(_window, WM_SYSCOMMAND, SC_MINIMIZE | HTMINBUTTON, 0);
}
}
void TitlebarControl::Close_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e)
{
::PostQuitMessage(0);
}
}

View File

@@ -15,14 +15,22 @@ namespace winrt::TerminalApp::implementation
{
TitlebarControl();
uint64_t ParentWindowHandle();
uint64_t ParentWindowHandle() const;
void ParentWindowHandle(uint64_t handle);
Windows::UI::Xaml::UIElement Content();
void Content(Windows::UI::Xaml::UIElement content);
void DragBar_DoubleTapped(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs const& e);
void Root_SizeChanged(const IInspectable& sender, Windows::UI::Xaml::SizeChangedEventArgs const& e);
void Minimize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void Maximize_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void Close_Click(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::RoutedEventArgs const& e);
void DragBar_DoubleTapped(winrt::Windows::Foundation::IInspectable const& sender, winrt::Windows::UI::Xaml::Input::DoubleTappedRoutedEventArgs const& e);
private:
void _OnMaximize(byte flag);
HWND _window{ nullptr }; // non-owning handle; should not be freed in the dtor.
};
}