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

View file

@ -11,6 +11,7 @@ using namespace Platform;
using namespace Windows::ApplicationModel;
using namespace Windows::ApplicationModel::Core;
using namespace Windows::Foundation;
using namespace Windows::System::Profile;
using namespace Windows::UI;
using namespace Windows::UI::Core;
using namespace Windows::UI::ViewManagement;
@ -45,13 +46,13 @@ namespace CalculatorApp
void TitleBar::OnLoaded(_In_ Object ^ /*sender*/, _In_ RoutedEventArgs ^ /*e*/)
{
auto that(this);
// Register events
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 +=
ref new TypedEventHandler<CoreApplicationViewTitleBar ^, Object ^>([this](CoreApplicationViewTitleBar ^ cTitleBar, Object ^) {
this->LayoutRoot->Height = cTitleBar->Height;
this->SetTitleBarPadding();
ref new TypedEventHandler<CoreApplicationViewTitleBar ^, Object ^>([that](CoreApplicationViewTitleBar ^ cTitleBar, Object ^) {
that->SetTitleBarHeightAndPadding();
});
m_colorValuesChangedToken = m_uiSettings->ColorValuesChanged += ref new TypedEventHandler<UISettings ^, Object ^>(this, &TitleBar::ColorValuesChanged);
@ -60,11 +61,15 @@ namespace CalculatorApp
m_windowActivatedToken = Window::Current->Activated +=
ref new Windows::UI::Xaml::WindowActivatedEventHandler(this, &CalculatorApp::TitleBar::OnWindowActivated);
// Set properties
LayoutRoot->Height = m_coreTitleBar->Height;
SetTitleBarControlColors();
SetTitleBarHeightAndPadding();
SetTitleBarVisibility();
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*/)
@ -82,14 +87,20 @@ namespace CalculatorApp
m_windowActivatedToken.Value = 0;
}
void TitleBar::SetTitleBarVisibility()
void TitleBar::SetTitleBarVisibility(bool forceDisplay)
{
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 rightAddition = 0;
@ -105,11 +116,13 @@ namespace CalculatorApp
}
this->LayoutRoot->Padding = Thickness(leftAddition, 0, rightAddition, 0);
this->LayoutRoot->Height = m_coreTitleBar->Height;
}
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()
@ -161,9 +174,10 @@ namespace CalculatorApp
void TitleBar::OnHighContrastChanged(_In_ AccessibilitySettings ^ /*sender*/, _In_ Object ^ /*args*/)
{
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([this]() {
SetTitleBarControlColors();
SetTitleBarVisibility();
auto that(this);
Dispatcher->RunAsync(CoreDispatcherPriority::Normal, ref new DispatchedHandler([that]() {
that->SetTitleBarControlColors();
that->SetTitleBarVisibility(false);
}));
}

View file

@ -26,9 +26,8 @@ public
void OnLoaded(_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();
void SetTitleBarPadding();
void SetTitleBarVisibility(bool forceDisplay);
void SetTitleBarHeightAndPadding();
void SetTitleBarControlColors();
void ColorValuesChanged(_In_ Windows::UI::ViewManagement::UISettings ^ sender, _In_ Platform::Object ^ e);
void OnHighContrastChanged(Windows::UI::ViewManagement::AccessibilitySettings ^ sender, Platform::Object ^ args);