Prevents the user interface from shifting upwards at launch

This commit is contained in:
Rudy Huyn 2019-08-07 02:02:55 -07:00
commit 87702512e1
3 changed files with 32 additions and 21 deletions

View file

@ -12,8 +12,7 @@
Height="32" Height="32"
HorizontalAlignment="Stretch"> HorizontalAlignment="Stretch">
<Grid x:Name="BackgroundElement" <Grid x:Name="BackgroundElement"
Background="Transparent" Background="Transparent">
Height="32">
<VisualStateManager.VisualStateGroups> <VisualStateManager.VisualStateGroups>
<VisualStateGroup x:Name="WindowFocusStates"> <VisualStateGroup x:Name="WindowFocusStates">
<VisualState x:Name="WindowFocused"/> <VisualState x:Name="WindowFocused"/>
@ -31,7 +30,6 @@
VerticalAlignment="Center" VerticalAlignment="Center"
Foreground="{ThemeResource TitleBarForegroundBaseHighBrush}" Foreground="{ThemeResource TitleBarForegroundBaseHighBrush}"
FontSize="12" FontSize="12"
TextAlignment="Left"
TextTrimming="CharacterEllipsis" TextTrimming="CharacterEllipsis"
Visibility="{x:Bind ApplicationViewModel.IsAlwaysOnTop, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}"/> Visibility="{x:Bind ApplicationViewModel.IsAlwaysOnTop, Converter={StaticResource BooleanToVisibilityNegationConverter}, Mode=OneWay}"/>
</Grid> </Grid>

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->LayoutRoot->Height = cTitleBar->Height; 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,11 +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
LayoutRoot->Height = m_coreTitleBar->Height;
SetTitleBarControlColors(); SetTitleBarControlColors();
SetTitleBarHeightAndPadding();
SetTitleBarVisibility(); // As of Windows 10 1903: when an app runs on a PC (without Tablet mode activated)
SetTitleBarPadding(); // 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*/)
@ -82,14 +87,20 @@ namespace CalculatorApp
m_windowActivatedToken.Value = 0; m_windowActivatedToken.Value = 0;
} }
void TitleBar::SetTitleBarVisibility(bool forceDisplay)
void TitleBar::SetTitleBarVisibility()
{ {
this->LayoutRoot->Visibility = m_coreTitleBar->IsVisible || ApplicationViewModel->IsAlwaysOnTop ? ::Visibility::Visible : ::Visibility::Collapsed; this->LayoutRoot->Visibility =
forceDisplay || m_coreTitleBar->IsVisible || ApplicationViewModel->IsAlwaysOnTop ? ::Visibility::Visible : ::Visibility::Collapsed;
} }
void TitleBar::SetTitleBarPadding() void TitleBar::SetTitleBarHeightAndPadding()
{ {
if (m_coreTitleBar->Height == 0)
{
// The titlebar isn't init
return;
}
double leftAddition = 0; double leftAddition = 0;
double rightAddition = 0; double rightAddition = 0;
@ -105,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()
@ -161,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);
})); }));
} }

View file

@ -26,9 +26,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 SetTitleBarText(Platform::String ^ text); void SetTitleBarVisibility(bool forceDisplay);
void SetTitleBarVisibility(); 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);