Prevents the user interface from shifting upwards at launch (#633)

Co-authored-by: Matt Cooley <macool@microsoft.com>
Co-authored-by: Pepe Rivera <joseartrivera@gmail.com>
This commit is contained in:
Rudy Huyn 2020-12-16 14:46:37 -08:00 committed by GitHub
parent c4793785c3
commit 3172f7fea2
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 33 additions and 25 deletions

View File

@ -27,7 +27,6 @@
</VisualStateGroup> </VisualStateGroup>
</VisualStateManager.VisualStateGroups> </VisualStateManager.VisualStateGroups>
<Grid x:Name="BackgroundElement" <Grid x:Name="BackgroundElement"
Height="32"
Background="Transparent"> Background="Transparent">
<TextBlock x:Name="AppName" <TextBlock x:Name="AppName"
x:Uid="AppName" x:Uid="AppName"
@ -36,7 +35,6 @@
VerticalAlignment="Center" VerticalAlignment="Center"
Foreground="{ThemeResource TitleBarForegroundBaseHighBrush}" Foreground="{ThemeResource TitleBarForegroundBaseHighBrush}"
FontSize="12" FontSize="12"
TextAlignment="Left"
TextTrimming="CharacterEllipsis"/> TextTrimming="CharacterEllipsis"/>
</Grid> </Grid>
<Button x:Name="ExitAlwaysOnTopButton" <Button x:Name="ExitAlwaysOnTopButton"

View File

@ -11,6 +11,7 @@ using namespace Platform;
using namespace Windows::ApplicationModel; using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core; using namespace Windows::ApplicationModel::Core;
using namespace Windows::Foundation; using namespace Windows::Foundation;
using namespace Windows::System::Profile;
using namespace Windows::UI; using namespace Windows::UI;
using namespace Windows::UI::Core; using namespace Windows::UI::Core;
using namespace Windows::UI::ViewManagement; using namespace Windows::UI::ViewManagement;
@ -45,13 +46,13 @@ namespace CalculatorApp
void TitleBar::OnLoaded(_In_ Object ^ /*sender*/, _In_ RoutedEventArgs ^ /*e*/) void TitleBar::OnLoaded(_In_ Object ^ /*sender*/, _In_ RoutedEventArgs ^ /*e*/)
{ {
auto that(this);
// Register events // Register events
m_visibilityChangedToken = m_coreTitleBar->IsVisibleChanged += ref new TypedEventHandler<CoreApplicationViewTitleBar ^, Object ^>( m_visibilityChangedToken = m_coreTitleBar->IsVisibleChanged += ref new TypedEventHandler<CoreApplicationViewTitleBar ^, Object ^>(
[this](CoreApplicationViewTitleBar ^ cTitleBar, Object ^) { this->SetTitleBarVisibility(); }); [that](CoreApplicationViewTitleBar ^ cTitleBar, Object ^) { that->SetTitleBarVisibility(false); });
m_layoutChangedToken = m_coreTitleBar->LayoutMetricsChanged += m_layoutChangedToken = m_coreTitleBar->LayoutMetricsChanged +=
ref new TypedEventHandler<CoreApplicationViewTitleBar ^, Object ^>([this](CoreApplicationViewTitleBar ^ cTitleBar, Object ^) { ref new TypedEventHandler<CoreApplicationViewTitleBar ^, Object ^>([that](CoreApplicationViewTitleBar ^ cTitleBar, Object ^) {
this->SetTitleBarHeight(); that->SetTitleBarHeightAndPadding();
this->SetTitleBarPadding();
}); });
m_colorValuesChangedToken = m_uiSettings->ColorValuesChanged += ref new TypedEventHandler<UISettings ^, Object ^>(this, &TitleBar::ColorValuesChanged); m_colorValuesChangedToken = m_uiSettings->ColorValuesChanged += ref new TypedEventHandler<UISettings ^, Object ^>(this, &TitleBar::ColorValuesChanged);
@ -60,10 +61,15 @@ namespace CalculatorApp
m_windowActivatedToken = Window::Current->Activated += m_windowActivatedToken = Window::Current->Activated +=
ref new Windows::UI::Xaml::WindowActivatedEventHandler(this, &CalculatorApp::TitleBar::OnWindowActivated); ref new Windows::UI::Xaml::WindowActivatedEventHandler(this, &CalculatorApp::TitleBar::OnWindowActivated);
// Set properties // Set properties
this->SetTitleBarHeight(); SetTitleBarControlColors();
this->SetTitleBarControlColors(); SetTitleBarHeightAndPadding();
this->SetTitleBarVisibility();
this->SetTitleBarPadding(); // As of Windows 10 1903: when an app runs on a PC (without Tablet mode activated)
// properties of CoreApplicationViewTitleBar aren't initialized during the first seconds after launch.
auto forceDisplay = AnalyticsInfo::VersionInfo->DeviceFamily == L"Windows.Desktop"
&& UIViewSettings::GetForCurrentView()->UserInteractionMode == UserInteractionMode::Mouse;
SetTitleBarVisibility(forceDisplay);
} }
void TitleBar::OnUnloaded(_In_ Object ^ /*sender*/, _In_ RoutedEventArgs ^ /*e*/) void TitleBar::OnUnloaded(_In_ Object ^ /*sender*/, _In_ RoutedEventArgs ^ /*e*/)
@ -81,18 +87,20 @@ namespace CalculatorApp
m_windowActivatedToken.Value = 0; m_windowActivatedToken.Value = 0;
} }
void TitleBar::SetTitleBarVisibility() void TitleBar::SetTitleBarVisibility(bool forceDisplay)
{ {
this->LayoutRoot->Visibility = m_coreTitleBar->IsVisible || IsAlwaysOnTopMode ? ::Visibility::Visible : ::Visibility::Collapsed; this->LayoutRoot->Visibility =
forceDisplay || m_coreTitleBar->IsVisible || IsAlwaysOnTopMode ? ::Visibility::Visible : ::Visibility::Collapsed;
} }
void TitleBar::SetTitleBarHeight() void TitleBar::SetTitleBarHeightAndPadding()
{ {
this->LayoutRoot->Height = m_coreTitleBar->Height; if (m_coreTitleBar->Height == 0)
} {
// The titlebar isn't init
return;
}
void TitleBar::SetTitleBarPadding()
{
double leftAddition = 0; double leftAddition = 0;
double rightAddition = 0; double rightAddition = 0;
@ -108,11 +116,13 @@ namespace CalculatorApp
} }
this->LayoutRoot->Padding = Thickness(leftAddition, 0, rightAddition, 0); this->LayoutRoot->Padding = Thickness(leftAddition, 0, rightAddition, 0);
this->LayoutRoot->Height = m_coreTitleBar->Height;
} }
void TitleBar::ColorValuesChanged(_In_ UISettings ^ /*sender*/, _In_ Object ^ /*e*/) void TitleBar::ColorValuesChanged(_In_ UISettings ^ /*sender*/, _In_ Object ^ /*e*/)
{ {
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]() { SetTitleBarControlColors(); })); auto that(this);
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([that]() { that->SetTitleBarControlColors(); }));
} }
void TitleBar::SetTitleBarControlColors() void TitleBar::SetTitleBarControlColors()
@ -164,9 +174,10 @@ namespace CalculatorApp
void TitleBar::OnHighContrastChanged(_In_ AccessibilitySettings ^ /*sender*/, _In_ Object ^ /*args*/) void TitleBar::OnHighContrastChanged(_In_ AccessibilitySettings ^ /*sender*/, _In_ Object ^ /*args*/)
{ {
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]() { auto that(this);
SetTitleBarControlColors(); Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([that]() {
SetTitleBarVisibility(); that->SetTitleBarControlColors();
that->SetTitleBarVisibility(false);
})); }));
} }
@ -178,7 +189,7 @@ namespace CalculatorApp
void TitleBar::OnIsAlwaysOnTopModePropertyChanged(bool /*oldValue*/, bool newValue) void TitleBar::OnIsAlwaysOnTopModePropertyChanged(bool /*oldValue*/, bool newValue)
{ {
SetTitleBarVisibility(); SetTitleBarVisibility(false);
VisualStateManager::GoToState(this, newValue ? "AOTMiniState" : "AOTNormalState", false); VisualStateManager::GoToState(this, newValue ? "AOTMiniState" : "AOTNormalState", false);
} }

View File

@ -27,9 +27,8 @@ public
void OnLoaded(_In_ Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e); void OnLoaded(_In_ Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void OnUnloaded(_In_ Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e); void OnUnloaded(_In_ Object ^ sender, Windows::UI::Xaml::RoutedEventArgs ^ e);
void SetTitleBarVisibility(); void SetTitleBarVisibility(bool forceDisplay);
void SetTitleBarHeight(); void SetTitleBarHeightAndPadding();
void SetTitleBarPadding();
void SetTitleBarControlColors(); void SetTitleBarControlColors();
void ColorValuesChanged(_In_ Windows::UI::ViewManagement::UISettings ^ sender, _In_ Platform::Object ^ e); void ColorValuesChanged(_In_ Windows::UI::ViewManagement::UISettings ^ sender, _In_ Platform::Object ^ e);
void OnHighContrastChanged(Windows::UI::ViewManagement::AccessibilitySettings ^ sender, Platform::Object ^ args); void OnHighContrastChanged(Windows::UI::ViewManagement::AccessibilitySettings ^ sender, Platform::Object ^ args);