From 9fc43470334d7ecff46369ef46670fccf916508b Mon Sep 17 00:00:00 2001 From: Anastasia Orishchenko Date: Mon, 27 Jul 2020 21:49:12 +0200 Subject: [PATCH] Adjust size and make title bar draggable --- windows/ReactTestApp/MainPage.cpp | 62 +++++++++++++++++++++--------- windows/ReactTestApp/MainPage.h | 7 ++++ windows/ReactTestApp/MainPage.xaml | 21 +++++----- 3 files changed, 60 insertions(+), 30 deletions(-) diff --git a/windows/ReactTestApp/MainPage.cpp b/windows/ReactTestApp/MainPage.cpp index 2905a4cea0..5e4b20cd76 100644 --- a/windows/ReactTestApp/MainPage.cpp +++ b/windows/ReactTestApp/MainPage.cpp @@ -3,18 +3,19 @@ #include "MainPage.h" #include +#include #include "ComponentViewModel.h" #include "MainPage.g.cpp" -#include "Manifest.h" + using winrt::Windows::ApplicationModel::Core::CoreApplication; using winrt::Windows::Foundation::IAsyncAction; +using winrt::Windows::UI::ViewManagement::ApplicationView; using winrt::Windows::UI::Xaml::RoutedEventArgs; +using winrt::Windows::UI::Xaml::Style; using winrt::Windows::UI::Xaml::Window; using winrt::Windows::UI::Xaml::Controls::Button; -using winrt::Windows::UI::Xaml::Input::TappedRoutedEventArgs; using winrt::Windows::UI::Xaml::Navigation::NavigationEventArgs; -using winrt::Windows::UI::Xaml::Style; namespace winrt::ReactTestApp::implementation { @@ -22,9 +23,7 @@ namespace winrt::ReactTestApp::implementation { InitializeComponent(); - auto coreTitleBar = CoreApplication::GetCurrentView().TitleBar(); - coreTitleBar.ExtendViewIntoTitleBar(true); - Window::Current().SetTitleBar(BackgroundElement()); + SetUpTitleBar(); auto menuItems = ReactMenu().Children(); std::optional<::ReactTestApp::Manifest> manifest = ::ReactTestApp::GetManifest(); @@ -38,19 +37,7 @@ namespace winrt::ReactTestApp::implementation auto &components = manifest.value().components; for (auto &&c : components) { - hstring componentDisplayName = to_hstring(c.displayName.value_or(c.appKey)); - hstring appKey = to_hstring(c.appKey); - ReactTestApp::ComponentViewModel newComponent = - winrt::make(appKey, componentDisplayName); - - Button newMenuItem; - auto style = this->Resources() - .Lookup(winrt::box_value(L"ReactMenuButton")) - .as<::Style>(); - newMenuItem.Style(style); - newMenuItem.Content(winrt::box_value(newComponent.DisplayName())); - newMenuItem.CommandParameter(newComponent); - newMenuItem.Click({this, &MainPage::SetReactComponentName}); + Button newMenuItem = GetComponentMenuButton(c); menuItems.Append(newMenuItem); } @@ -95,4 +82,41 @@ namespace winrt::ReactTestApp::implementation ReactButton().Flyout().ShowAt(ReactButton()); } + Button MainPage::GetComponentMenuButton(::ReactTestApp::Component &component) + { + hstring componentDisplayName = to_hstring(component.displayName.value_or(component.appKey)); + hstring appKey = to_hstring(component.appKey); + ReactTestApp::ComponentViewModel newComponent = + winrt::make(appKey, componentDisplayName); + + Button newMenuItem; + auto style = this->Resources().Lookup(winrt::box_value(L"ReactMenuButton")).as<::Style>(); + newMenuItem.Style(style); + newMenuItem.Content(winrt::box_value(newComponent.DisplayName())); + newMenuItem.CommandParameter(newComponent); + newMenuItem.Click({this, &MainPage::SetReactComponentName}); + return newMenuItem; + } + + void MainPage::SetUpTitleBar() + { + // Set close, minimize and maximize icons background to transparent + auto appView = ApplicationView::GetForCurrentView().TitleBar(); + appView.ButtonBackgroundColor(winrt::Windows::UI::Colors::Transparent()); + appView.BackgroundColor(winrt::Windows::UI::Colors::Transparent()); + + auto coreTitleBar = CoreApplication::GetCurrentView().TitleBar(); + coreTitleBar.LayoutMetricsChanged({this, &MainPage::CoreTitleBarLayoutMetricsChanged}); + coreTitleBar.ExtendViewIntoTitleBar(true); + Window::Current().SetTitleBar(BackgroundElement()); + } + + // Adjust height of custom title bar to match close, minimize and maximize icons + void MainPage::CoreTitleBarLayoutMetricsChanged( + winrt::Windows::ApplicationModel::Core::CoreApplicationViewTitleBar const &sender, + Windows::Foundation::IInspectable const &) + { + TitleBar().Height(sender.Height()); + } + } // namespace winrt::ReactTestApp::implementation diff --git a/windows/ReactTestApp/MainPage.h b/windows/ReactTestApp/MainPage.h index 41731c653a..8dc9dbc65e 100644 --- a/windows/ReactTestApp/MainPage.h +++ b/windows/ReactTestApp/MainPage.h @@ -1,6 +1,7 @@ #pragma once #include "MainPage.g.h" +#include "Manifest.h" #include "ReactInstance.h" namespace winrt::ReactTestApp::implementation @@ -21,6 +22,12 @@ namespace winrt::ReactTestApp::implementation void SetReactComponentName(Windows::Foundation::IInspectable const &, Windows::UI::Xaml::RoutedEventArgs); + Windows::UI::Xaml::Controls::Button + GetComponentMenuButton(::ReactTestApp::Component &component); + void CoreTitleBarLayoutMetricsChanged( + winrt::Windows::ApplicationModel::Core::CoreApplicationViewTitleBar const &sender, + Windows::Foundation::IInspectable const &); + void SetUpTitleBar(); }; } // namespace winrt::ReactTestApp::implementation diff --git a/windows/ReactTestApp/MainPage.xaml b/windows/ReactTestApp/MainPage.xaml index 954b82538f..f1ef281458 100644 --- a/windows/ReactTestApp/MainPage.xaml +++ b/windows/ReactTestApp/MainPage.xaml @@ -14,7 +14,7 @@ - + @@ -22,15 +22,15 @@ - - - + + - + + - - - + + + +