Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[DRAFT] MainWindow.xaml with new TitleBar + TitleBar samples #1797

Draft
wants to merge 21 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from 14 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions WinUIGallery/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
<ResourceDictionary Source="/Styles/Grid.xaml" />
<ResourceDictionary Source="/Styles/GridViewItem.xaml" />
<ResourceDictionary Source="/Styles/TextBlock.xaml" />
<ResourceDictionary Source="/Styles/TitleBar.xaml" />
<ResourceDictionary Source="/Samples/ControlPages/Fundamentals/Controls/CounterControl.xaml" />
<ResourceDictionary Source="/Samples/ControlPages/Fundamentals/Controls/ValidatedPasswordBox.xaml" />
</ResourceDictionary.MergedDictionaries>
Expand Down
125 changes: 11 additions & 114 deletions WinUIGallery/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,20 +10,15 @@
using System;
using System.Diagnostics;
using System.Linq;
using System.Reflection;
using WinUIGallery.Models;
using WinUIGallery.Helpers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.Windows.AppLifecycle;
using Windows.ApplicationModel.Activation;
using WASDK = Microsoft.WindowsAppSDK;
using Microsoft.Windows.AppNotifications;
using Microsoft.Windows.AppNotifications.Builder;
using System.Collections.Generic;
using static WinUIGallery.Helpers.Win32;
using Windows.ApplicationModel.Activation;
using WinUIGallery.Helpers;
using WinUIGallery.Pages;
using static WinUIGallery.Helpers.Win32;

namespace WinUIGallery;

Expand All @@ -32,47 +27,11 @@ namespace WinUIGallery;
/// </summary>
sealed partial class App : Application
{
private static Window startupWindow;
private static Win32WindowHelper win32WindowHelper;
internal static MainWindow MainWindow { get; private set; } = null!;

private static int registeredKeyPressedHook = 0;
private HookProc keyEventHook;


public static string WinAppSdkDetails
{
// TODO: restore patch number and version tag when WinAppSDK supports them both
get => string.Format("Windows App SDK {0}.{1}",
WASDK.Release.Major, WASDK.Release.Minor);
}

public static string WinAppSdkRuntimeDetails
{
get
{
try
{
// Retrieve Windows App Runtime version info dynamically
IEnumerable<FileVersionInfo> windowsAppRuntimeVersion =
from module in Process.GetCurrentProcess().Modules.OfType<ProcessModule>()
where module.FileName.EndsWith("Microsoft.WindowsAppRuntime.Insights.Resource.dll")
select FileVersionInfo.GetVersionInfo(module.FileName);
return WinAppSdkDetails + ", Windows App Runtime " + windowsAppRuntimeVersion.First().FileVersion;
}
catch
{
return WinAppSdkDetails + $", Windows App Runtime {WASDK.Runtime.Version.Major}.{WASDK.Runtime.Version.Minor}";
}
}
}

/// <summary>
/// Get the initial window created for this app.
/// </summary>
public static Window StartupWindow
{
get => startupWindow;
}

/// <summary>
/// Initializes the singleton Application object. This is the first line of authored code
/// executed, and as such is the logical equivalent of main() or WinMain().
Expand All @@ -83,22 +42,6 @@ public App()
UnhandledException += HandleExceptions;
}

/// <summary>
/// Converts a string into a enum.
/// </summary>
/// <typeparam name="TEnum">The output enum type.</typeparam>
/// <param name="text">The input text.</param>
/// <returns>The parsed enum.</returns>
/// <exception cref="InvalidOperationException">Thrown when the TEnum type is not a enum.</exception>
public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
{
if (!typeof(TEnum).GetTypeInfo().IsEnum)
{
throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum.");
}
return (TEnum)Enum.Parse(typeof(TEnum), text);
}

/// <summary>
/// Invoked when the application is launched normally by the end user. Other entry points
/// will be used such as when the application is launched to open a specific file.
Expand All @@ -108,11 +51,8 @@ protected override void OnLaunched(Microsoft.UI.Xaml.LaunchActivatedEventArgs ar
{
IdleSynchronizer.Init();

startupWindow = WindowHelper.CreateWindow();
startupWindow.ExtendsContentIntoTitleBar = true;

win32WindowHelper = new Win32WindowHelper(startupWindow);
win32WindowHelper.SetWindowMinMaxSize(new Win32WindowHelper.POINT() { x = 500, y = 500 });
MainWindow = new MainWindow();
WindowHelper.TrackWindow(MainWindow);

#if DEBUG
if (Debugger.IsAttached)
Expand Down Expand Up @@ -153,9 +93,7 @@ private async void EnsureWindow()
{
await ControlInfoDataSource.Instance.GetGroupsAsync();
await IconsDataSource.Instance.LoadIcons();

Frame rootFrame = GetRootFrame();

MainWindow.AddNavigationMenuItems();
ThemeHelper.Initialize();

var targetPageType = typeof(HomePage);
Expand Down Expand Up @@ -186,57 +124,16 @@ private async void EnsureWindow()
}
}

var rootPage = StartupWindow.Content as NavigationRootPage;
rootPage.Navigate(targetPageType, targetPageArguments);
MainWindow.Navigate(targetPageType, targetPageArguments);

if (targetPageType == typeof(HomePage))
{
var navItem = (NavigationViewItem)rootPage.NavigationView.MenuItems[0];
var navItem = (NavigationViewItem)MainWindow.NavigationView.MenuItems[0];
navItem.IsSelected = true;
}

// Activate the startup window.
StartupWindow.Activate();
}

/// <summary>
/// Gets the frame of the StartupWindow.
/// </summary>
/// <returns>The frame of the StartupWindow.</returns>
/// <exception cref="Exception">Thrown if the window doesn't have a frame with the name "rootFrame".</exception>
public Frame GetRootFrame()
{
Frame rootFrame;
if (StartupWindow.Content is NavigationRootPage rootPage)
{
rootFrame = (Frame)rootPage.FindName("rootFrame");
}
else
{
rootPage = new NavigationRootPage();
rootFrame = (Frame)rootPage.FindName("rootFrame");
if (rootFrame == null)
{
throw new Exception("Root frame not found");
}
SuspensionManager.RegisterFrame(rootFrame, "AppFrame");
rootFrame.Language = Windows.Globalization.ApplicationLanguages.Languages[0];
rootFrame.NavigationFailed += OnNavigationFailed;

StartupWindow.Content = rootPage;
}

return rootFrame;
}

/// <summary>
/// Invoked when Navigation to a certain page fails
/// </summary>
/// <param name="sender">The Frame which failed navigation</param>
/// <param name="e">Details about the navigation failure</param>
void OnNavigationFailed(object sender, NavigationFailedEventArgs e)
{
throw new Exception("Failed to load Page " + e.SourcePageType.FullName);
MainWindow.Activate();
}

/// <summary>
Expand Down
22 changes: 22 additions & 0 deletions WinUIGallery/Helpers/EnumHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
using System;
using System.Reflection;

namespace WinUIGallery.Helpers;
internal static class EnumHelper
{
/// <summary>
/// Converts a string into a enum.
/// </summary>
/// <typeparam name="TEnum">The output enum type.</typeparam>
/// <param name="text">The input text.</param>
/// <returns>The parsed enum.</returns>
/// <exception cref="InvalidOperationException">Thrown when the TEnum type is not a enum.</exception>
public static TEnum GetEnum<TEnum>(string text) where TEnum : struct
{
if (!typeof(TEnum).GetTypeInfo().IsEnum)
{
throw new InvalidOperationException("Generic parameter 'TEnum' must be an enum.");
}
return (TEnum)Enum.Parse(typeof(TEnum), text);
}
}
10 changes: 0 additions & 10 deletions WinUIGallery/Helpers/NavigationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -187,11 +187,6 @@ public RootFrameNavigationHelper(Frame rootFrame, NavigationView currentNavView)
}

this.Frame = rootFrame;
this.Frame.Navigated += (s, e) =>
{
// Update the Back button whenever a navigation occurs.
UpdateBackButton();
};
this.CurrentNavView = currentNavView;

CurrentNavView.BackRequested += NavView_BackRequested;
Expand Down Expand Up @@ -296,11 +291,6 @@ private bool TryGoForward()
}
return navigated;
}

private void UpdateBackButton()
{
this.CurrentNavView.IsBackEnabled = this.Frame.CanGoBack ? true : false;
}
}

/// <summary>
Expand Down
11 changes: 4 additions & 7 deletions WinUIGallery/Helpers/NavigationOrientationHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ public static bool IsLeftMode()
}
}

public static void IsLeftModeForElement(bool isLeftMode, UIElement element)
public static void IsLeftModeForElement(bool isLeftMode)
{
UpdateNavigationViewForElement(isLeftMode, element);
UpdateNavigationViewForElement(isLeftMode);
if (NativeHelper.IsAppPackaged)
{
ApplicationData.Current.LocalSettings.Values[IsLeftModeKey] = isLeftMode;
Expand All @@ -42,19 +42,16 @@ public static void IsLeftModeForElement(bool isLeftMode, UIElement element)
}
}

public static void UpdateNavigationViewForElement(bool isLeftMode, UIElement element)
public static void UpdateNavigationViewForElement(bool isLeftMode)
{
NavigationView _navView = NavigationRootPage.GetForElement(element).NavigationView;
NavigationView _navView = App.MainWindow.NavigationView;
if (isLeftMode)
{
_navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Auto;
Grid.SetRow(_navView, 0);
}
else
{
_navView.PaneDisplayMode = NavigationViewPaneDisplayMode.Top;
Grid.SetRow(_navView, 1);
}
}

}
17 changes: 13 additions & 4 deletions WinUIGallery/Helpers/ProtocolActivationClipboardHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,23 @@ public static bool ShowCopyLinkTeachingTip

public static void Copy(ControlInfoDataItem item)
{
var uri = new Uri($"winui3gallery://item/{item.UniqueId}", UriKind.Absolute);
ProtocolActivationClipboardHelper.Copy(uri, $"{Package.Current.DisplayName} - {item.Title} Sample");
var uri = new Uri($"{GetAppName()}://item/{item.UniqueId}", UriKind.Absolute);
Copy(uri, $"{Package.Current.DisplayName} - {item.Title} Sample");
}

public static void Copy(ControlInfoDataGroup group)
{
var uri = new Uri($"winui3gallery://category/{group.UniqueId}", UriKind.Absolute);
ProtocolActivationClipboardHelper.Copy(uri, $"{Package.Current.DisplayName} - {group.Title} Samples");
var uri = new Uri($"{GetAppName()}://category/{group.UniqueId}", UriKind.Absolute);
Copy(uri, $"{Package.Current.DisplayName} - {group.Title} Samples");
}

private static string GetAppName()
{
#if DEBUG
return "winui3gallerydev";
#else
return "winui3gallery";
#endif
}

private static void Copy(Uri uri, string displayName)
Expand Down
4 changes: 2 additions & 2 deletions WinUIGallery/Helpers/ThemeHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ public static ElementTheme ActualTheme
}
}

return WinUIGallery.App.GetEnum<ElementTheme>(App.Current.RequestedTheme.ToString());
return EnumHelper.GetEnum<ElementTheme>(App.Current.RequestedTheme.ToString());
}
}

Expand Down Expand Up @@ -75,7 +75,7 @@ public static void Initialize()

if (savedTheme != null)
{
RootTheme = WinUIGallery.App.GetEnum<ElementTheme>(savedTheme);
RootTheme = EnumHelper.GetEnum<ElementTheme>(savedTheme);
}
}
}
Expand Down
6 changes: 3 additions & 3 deletions WinUIGallery/Helpers/TitleBarHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ namespace WinUIGallery.Helpers;

internal class TitleBarHelper
{
// workaround as Appwindow titlebar doesn't update caption button colors correctly when changed while app is running
// workaround as AppWindow TitleBar doesn't update caption button colors correctly when changed while app is running
// https://task.ms/44172495
public static Windows.UI.Color ApplySystemThemeToCaptionButtons(Window window)
{
var frame = (Application.Current as WinUIGallery.App).GetRootFrame() as FrameworkElement;
var frame = App.MainWindow.GetRootFrame() as FrameworkElement;
Windows.UI.Color color;
if (frame.ActualTheme == ElementTheme.Dark)
{
Expand All @@ -20,7 +20,7 @@ public static Windows.UI.Color ApplySystemThemeToCaptionButtons(Window window)
{
color = Colors.Black;
}
SetCaptionButtonColors(window,color);
SetCaptionButtonColors(window, color);
return color;
}

Expand Down
35 changes: 35 additions & 0 deletions WinUIGallery/Helpers/VersionHelper.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using WASDK = Microsoft.WindowsAppSDK;

namespace WinUIGallery.Helpers;
internal static class VersionHelper
{
public static string WinAppSdkDetails
{
// TODO: restore patch number and version tag when WinAppSDK supports them both
get => string.Format("Windows App SDK {0}.{1}",
WASDK.Release.Major, WASDK.Release.Minor);
}

public static string WinAppSdkRuntimeDetails
{
get
{
try
{
// Retrieve Windows App Runtime version info dynamically
IEnumerable<FileVersionInfo> windowsAppRuntimeVersion =
from module in Process.GetCurrentProcess().Modules.OfType<ProcessModule>()
where module.FileName.EndsWith("Microsoft.WindowsAppRuntime.Insights.Resource.dll")
select FileVersionInfo.GetVersionInfo(module.FileName);
return WinAppSdkDetails + ", Windows App Runtime " + windowsAppRuntimeVersion.First().FileVersion;
}
catch
{
return WinAppSdkDetails + $", Windows App Runtime {WASDK.Runtime.Version.Major}.{WASDK.Runtime.Version.Minor}";
}
}
}
}
Loading