diff --git a/WinUIGallery/App.xaml b/WinUIGallery/App.xaml index e0e2d8e3d..1d2895e51 100644 --- a/WinUIGallery/App.xaml +++ b/WinUIGallery/App.xaml @@ -25,6 +25,7 @@ + diff --git a/WinUIGallery/App.xaml.cs b/WinUIGallery/App.xaml.cs index a39a23bd9..bbf88c6dd 100644 --- a/WinUIGallery/App.xaml.cs +++ b/WinUIGallery/App.xaml.cs @@ -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; @@ -32,47 +27,11 @@ namespace WinUIGallery; /// 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 windowsAppRuntimeVersion = - from module in Process.GetCurrentProcess().Modules.OfType() - 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}"; - } - } - } - - /// - /// Get the initial window created for this app. - /// - public static Window StartupWindow - { - get => startupWindow; - } - /// /// 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(). @@ -83,22 +42,6 @@ public App() UnhandledException += HandleExceptions; } - /// - /// Converts a string into a enum. - /// - /// The output enum type. - /// The input text. - /// The parsed enum. - /// Thrown when the TEnum type is not a enum. - public static TEnum GetEnum(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); - } - /// /// 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. @@ -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) @@ -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); @@ -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(); - } - - /// - /// Gets the frame of the StartupWindow. - /// - /// The frame of the StartupWindow. - /// Thrown if the window doesn't have a frame with the name "rootFrame". - 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; - } - - /// - /// Invoked when Navigation to a certain page fails - /// - /// The Frame which failed navigation - /// Details about the navigation failure - void OnNavigationFailed(object sender, NavigationFailedEventArgs e) - { - throw new Exception("Failed to load Page " + e.SourcePageType.FullName); + MainWindow.Activate(); } /// diff --git a/WinUIGallery/Helpers/EnumHelper.cs b/WinUIGallery/Helpers/EnumHelper.cs new file mode 100644 index 000000000..d12d8b4cb --- /dev/null +++ b/WinUIGallery/Helpers/EnumHelper.cs @@ -0,0 +1,22 @@ +using System; +using System.Reflection; + +namespace WinUIGallery.Helpers; +internal static class EnumHelper +{ + /// + /// Converts a string into a enum. + /// + /// The output enum type. + /// The input text. + /// The parsed enum. + /// Thrown when the TEnum type is not a enum. + public static TEnum GetEnum(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); + } +} diff --git a/WinUIGallery/Helpers/NavigationHelper.cs b/WinUIGallery/Helpers/NavigationHelper.cs index e321decdf..5ea74c0d7 100644 --- a/WinUIGallery/Helpers/NavigationHelper.cs +++ b/WinUIGallery/Helpers/NavigationHelper.cs @@ -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; @@ -296,11 +291,6 @@ private bool TryGoForward() } return navigated; } - - private void UpdateBackButton() - { - this.CurrentNavView.IsBackEnabled = this.Frame.CanGoBack ? true : false; - } } /// diff --git a/WinUIGallery/Helpers/NavigationOrientationHelper.cs b/WinUIGallery/Helpers/NavigationOrientationHelper.cs index e2f834a02..b7760cbcf 100644 --- a/WinUIGallery/Helpers/NavigationOrientationHelper.cs +++ b/WinUIGallery/Helpers/NavigationOrientationHelper.cs @@ -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; @@ -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); } } - } diff --git a/WinUIGallery/Helpers/ProtocolActivationClipboardHelper.cs b/WinUIGallery/Helpers/ProtocolActivationClipboardHelper.cs index 586ed1e1e..6011da495 100644 --- a/WinUIGallery/Helpers/ProtocolActivationClipboardHelper.cs +++ b/WinUIGallery/Helpers/ProtocolActivationClipboardHelper.cs @@ -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) diff --git a/WinUIGallery/Helpers/ThemeHelper.cs b/WinUIGallery/Helpers/ThemeHelper.cs index a80d91265..d160ce80b 100644 --- a/WinUIGallery/Helpers/ThemeHelper.cs +++ b/WinUIGallery/Helpers/ThemeHelper.cs @@ -29,7 +29,7 @@ public static ElementTheme ActualTheme } } - return WinUIGallery.App.GetEnum(App.Current.RequestedTheme.ToString()); + return EnumHelper.GetEnum(App.Current.RequestedTheme.ToString()); } } @@ -75,7 +75,7 @@ public static void Initialize() if (savedTheme != null) { - RootTheme = WinUIGallery.App.GetEnum(savedTheme); + RootTheme = EnumHelper.GetEnum(savedTheme); } } } diff --git a/WinUIGallery/Helpers/TitleBarHelper.cs b/WinUIGallery/Helpers/TitleBarHelper.cs index c031867a7..5b26e12cc 100644 --- a/WinUIGallery/Helpers/TitleBarHelper.cs +++ b/WinUIGallery/Helpers/TitleBarHelper.cs @@ -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) { @@ -20,7 +20,7 @@ public static Windows.UI.Color ApplySystemThemeToCaptionButtons(Window window) { color = Colors.Black; } - SetCaptionButtonColors(window,color); + SetCaptionButtonColors(window, color); return color; } diff --git a/WinUIGallery/Helpers/VersionHelper.cs b/WinUIGallery/Helpers/VersionHelper.cs new file mode 100644 index 000000000..23e7bc3cc --- /dev/null +++ b/WinUIGallery/Helpers/VersionHelper.cs @@ -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 windowsAppRuntimeVersion = + from module in Process.GetCurrentProcess().Modules.OfType() + 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}"; + } + } + } +} diff --git a/WinUIGallery/Pages/NavigationRootPage.xaml b/WinUIGallery/MainWindow.xaml similarity index 73% rename from WinUIGallery/Pages/NavigationRootPage.xaml rename to WinUIGallery/MainWindow.xaml index b12daf111..b191617d7 100644 --- a/WinUIGallery/Pages/NavigationRootPage.xaml +++ b/WinUIGallery/MainWindow.xaml @@ -1,63 +1,56 @@ - - + - - - - - - + xmlns:d="http://schemas.microsoft.com/expression/blend/2008" + xmlns:local="using:WinUIGallery" + xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" + xmlns:pages="using:WinUIGallery.Pages" + Title="MainWindow" + mc:Ignorable="d"> + + + + - - - - - - - + + + + + - - - - - + x:FieldModifier="public" + KeyboardAcceleratorPlacementMode="Hidden" + PlaceholderText="Search samples.." + QueryIcon="Find" + QuerySubmitted="OnControlsSearchBoxQuerySubmitted" + TextChanged="OnControlsSearchBoxTextChanged"> + + + + + + + - - - - - - - - - - - - - - - - - - Foreground Color - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + - - + + + + + + + + -<!--UIElement set as titlebar--> -<Border x:Name="AppTitleBar" Grid.Column="1" VerticalAlignment="Top"> - <TextBlock x:Name="AppTitle" Text="{StaticResource AppTitleName}" VerticalAlignment="Top" Margin="0,8,0,0" /> -</Border> - +<TitleBar + Title="$(Title)" + Subtitle=""$(Subtitle)" + IsBackButtonVisible="$(BackButtonVisibility)" + IsPaneToggleButtonVisible="$(PaneToggleVisibility)"> + <TitleBar.IconSource> + <ImageIconSource ImageSource="/Assets/Tiles/GalleryIcon.ico" /> + </TitleBar.IconSource> + <TitleBar.CenterContent> + <AutoSuggestBox + Width="360" + VerticalAlignment="Center" + PlaceholderText="Search.." + QueryIcon="Find" /> + </TitleBar.CenterContent> + <TitleBar.RightContent> + <PersonPicture + Width="30" + Height="30" + Initials="JD" /> + </TitleBar.RightContent> +</TitleBar> + + + + + + + - + + - - - WinUI custom titlebar supports setting a custom height by configuring the PreferredHeightOption property of the titlebar. This feature is useful when you need to adjust the titlebar height to match your app's design or accommodate additional content. - - - - This option will be invalid when the style of titlebar is reset to system titlebar! - - - - - Tall - Standard - Collapsed - - + + + - - - - - diff --git a/WinUIGallery/Samples/ControlPages/TitleBarPage.xaml.cs b/WinUIGallery/Samples/ControlPages/TitleBarPage.xaml.cs index 52492b70e..5a576b881 100644 --- a/WinUIGallery/Samples/ControlPages/TitleBarPage.xaml.cs +++ b/WinUIGallery/Samples/ControlPages/TitleBarPage.xaml.cs @@ -7,238 +7,22 @@ // PURPOSE, MERCHANTABILITY, OR NON-INFRINGEMENT. // //********************************************************* -using WinUIGallery.Helpers; -using Microsoft.UI; using Microsoft.UI.Xaml; using Microsoft.UI.Xaml.Controls; -using Microsoft.UI.Xaml.Media; -using Microsoft.UI.Xaml.Shapes; -using System.Threading.Tasks; -using Microsoft.UI.Windowing; -using Microsoft.UI.Xaml.Navigation; -using Microsoft.UI.Input; -using Windows.Foundation; -using System; +using WinUIGallery.Samples.SamplePages; namespace WinUIGallery.ControlPages; public sealed partial class TitleBarPage : Page { - private Windows.UI.Color currentBgColor = Colors.Transparent; - private Windows.UI.Color currentFgColor = ThemeHelper.ActualTheme == ElementTheme.Dark ? Colors.White : Colors.Black; - private bool sizeChangedEventHandlerAdded = false; - public TitleBarPage() { this.InitializeComponent(); - Loaded += (object sender, RoutedEventArgs e) => - { - (sender as TitleBarPage).UpdateTitleBarColor(); - UpdateButtonText(); - }; - } - - protected override void OnNavigatedFrom(NavigationEventArgs e) - { - ResetTitlebarSettings(); - } - - private void SetTitleBar(bool forceCustomTitlebar = false) - { - var window = WindowHelper.GetWindowForElement(this as UIElement); - var titleBarElement = UIHelper.FindElementByName(this as UIElement, "AppTitleBar"); - if (forceCustomTitlebar || !window.ExtendsContentIntoTitleBar) - { - titleBarElement.Visibility = Visibility.Visible; - window.ExtendsContentIntoTitleBar = true; - window.SetTitleBar(titleBarElement); - TitleBarHelper.SetCaptionButtonBackgroundColors(window, Colors.Transparent); - } - else - { - titleBarElement.Visibility = Visibility.Collapsed; - window.ExtendsContentIntoTitleBar = false; - TitleBarHelper.SetCaptionButtonBackgroundColors(window, null); - } - UpdateButtonText(); - UpdateTitleBarColor(); - } - - private void ResetTitlebarSettings() - { - var window = WindowHelper.GetWindowForElement(this as UIElement); - SetTitleBar(forceCustomTitlebar: true); - ClearClickThruRegions(); - var txtBoxNonClientArea = UIHelper.FindElementByName(this as UIElement, "AppTitleBarTextBox") as FrameworkElement; - txtBoxNonClientArea.Visibility = Visibility.Collapsed; - addInteractiveElements.Content = "Add interactive control to titlebar"; - } - - private void SetClickThruRegions(Windows.Graphics.RectInt32[] rects) - { - var window = WindowHelper.GetWindowForElement(this as UIElement); - var nonClientInputSrc = InputNonClientPointerSource.GetForWindowId(window.AppWindow.Id); - nonClientInputSrc.SetRegionRects(NonClientRegionKind.Passthrough, rects); - } - - private void ClearClickThruRegions() - { - var window = WindowHelper.GetWindowForElement(this as UIElement); - var noninputsrc = InputNonClientPointerSource.GetForWindowId(window.AppWindow.Id); - noninputsrc.ClearRegionRects(NonClientRegionKind.Passthrough); - } - - public void UpdateButtonText() - { - var window = WindowHelper.GetWindowForElement(this as UIElement); - - if (window.ExtendsContentIntoTitleBar) - { - customTitleBar.Content = "Reset to System TitleBar"; - defaultTitleBar.Content = "Reset to System TitleBar"; - } - else - { - customTitleBar.Content = "Set Custom TitleBar"; - defaultTitleBar.Content = "Set Default Custom TitleBar"; - } - } - private void BgGridView_ItemClick(object sender, ItemClickEventArgs e) + private void CreateTitleBarWindowClick(object sender, RoutedEventArgs e) { - var rect = (Rectangle)e.ClickedItem; - var color = ((SolidColorBrush)rect.Fill).Color; - BackgroundColorElement.Background = new SolidColorBrush(color); - - currentBgColor = color; - UpdateTitleBarColor(); - - // Delay required to circumvent GridView bug: https://github.com/microsoft/microsoft-ui-xaml/issues/6350 - Task.Delay(10).ContinueWith(_ => myBgColorButton.Flyout.Hide(), TaskScheduler.FromCurrentSynchronizationContext()); + TitleBarWindow titleBarWindow = new TitleBarWindow(); + titleBarWindow.Activate(); } - - private void FgGridView_ItemClick(object sender, ItemClickEventArgs e) - { - var rect = (Rectangle)e.ClickedItem; - var color = ((SolidColorBrush)rect.Fill).Color; - - ForegroundColorElement.Background = new SolidColorBrush(color); - - currentFgColor = color; - UpdateTitleBarColor(); - - // Delay required to circumvent GridView bug: https://github.com/microsoft/microsoft-ui-xaml/issues/6350 - Task.Delay(10).ContinueWith(_ => myFgColorButton.Flyout.Hide(), TaskScheduler.FromCurrentSynchronizationContext()); - } - - - public void UpdateTitleBarColor() - { - var window = WindowHelper.GetWindowForElement(this); - var titleBarElement = UIHelper.FindElementByName(this, "AppTitleBar"); - var titleBarAppNameElement = UIHelper.FindElementByName(this, "AppTitle"); - - (titleBarElement as Border).Background = new SolidColorBrush(currentBgColor); // Changing titlebar uielement's color. - - if (currentFgColor != Colors.Transparent) - { - (titleBarAppNameElement as TextBlock).Foreground = new SolidColorBrush(currentFgColor); - } - else - { - (titleBarAppNameElement as TextBlock).Foreground = Application.Current.Resources["TextFillColorPrimaryBrush"] as SolidColorBrush; - } - - TitleBarHelper.SetCaptionButtonColors(window, currentFgColor); - - if (currentBgColor == Colors.Transparent) - { - // If the current background is null, we want to revert to the default titlebar which is achieved using null as color. - TitleBarHelper.SetBackgroundColor(window, null); - } - else - { - TitleBarHelper.SetBackgroundColor(window, currentBgColor); - } - - TitleBarHelper.SetForegroundColor(window, currentFgColor); - } - - private void customTitleBar_Click(object sender, RoutedEventArgs e) - { - SetTitleBar(); - // announce visual change to automation - UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId"); - } - private void defaultTitleBar_Click(object sender, RoutedEventArgs e) - { - SetTitleBar(); - - // announce visual change to automation - UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId"); - } - - private void setTxtBoxAsPasthrough(FrameworkElement txtBoxNonClientArea) - { - GeneralTransform transformTxtBox = txtBoxNonClientArea.TransformToVisual(null); - Rect bounds = transformTxtBox.TransformBounds(new Rect(0, 0, txtBoxNonClientArea.ActualWidth, txtBoxNonClientArea.ActualHeight)); - - var scale = WindowHelper.GetRasterizationScaleForElement(this); - - var transparentRect = new Windows.Graphics.RectInt32( - _X: (int)Math.Round(bounds.X * scale), - _Y: (int)Math.Round(bounds.Y * scale), - _Width: (int)Math.Round(bounds.Width * scale), - _Height: (int)Math.Round(bounds.Height * scale) - ); - var rectArr = new Windows.Graphics.RectInt32[] { transparentRect }; - SetClickThruRegions(rectArr); - } - - private void TitleBarHeightComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e) - { - var selectedHeight = titlebarHeight.SelectedItem.ToString(); - var window = WindowHelper.GetWindowForElement(this); - - if (selectedHeight != null && window != null && window.ExtendsContentIntoTitleBar) - { - window.AppWindow.TitleBar.PreferredHeightOption = App.GetEnum(selectedHeight); - } - } - - private void AddInteractiveElements_Click(object sender, RoutedEventArgs e) - { - var txtBoxNonClientArea = UIHelper.FindElementByName(sender as UIElement, "AppTitleBarTextBox") as FrameworkElement; - - if (txtBoxNonClientArea.Visibility == Visibility.Visible) - { - ResetTitlebarSettings(); - } - else - { - addInteractiveElements.Content = "Remove interactive control from titlebar"; - txtBoxNonClientArea.Visibility = Visibility.Visible; - if (sizeChangedEventHandlerAdded) - { - setTxtBoxAsPasthrough(txtBoxNonClientArea); - } - else - { - sizeChangedEventHandlerAdded = true; - // run this code when textbox has been made visible and its actual width and height has been calculated - txtBoxNonClientArea.SizeChanged += (object sender, SizeChangedEventArgs e) => - { - if (txtBoxNonClientArea.Visibility != Visibility.Collapsed) - { - setTxtBoxAsPasthrough(txtBoxNonClientArea); - } - }; - } - - // announce visual change to automation - UIHelper.AnnounceActionForAccessibility(sender as UIElement, "TitleBar size and width changed", "TitleBarChangedNotificationActivityId"); - } - } - } diff --git a/WinUIGallery/Samples/Data/ControlInfoData.json b/WinUIGallery/Samples/Data/ControlInfoData.json index 3bcc0209d..8090c96d7 100644 --- a/WinUIGallery/Samples/Data/ControlInfoData.json +++ b/WinUIGallery/Samples/Data/ControlInfoData.json @@ -3622,20 +3622,20 @@ { "UniqueId": "TitleBar", "Title": "TitleBar", - "ApiNamespace": "Microsoft.UI.Xaml", + "ApiNamespace": "Microsoft.UI.Xaml.Controls", "Subtitle": "An example showing a custom UIElement used as the titlebar for the app's window.", "ImagePath": "ms-appx:///Assets/ControlImages/TitleBar.png", - "Description": "This sample shows how to use a custom titlebar for the app's window. There are 2 ways of doing it: using default titlebar and setting an UIElement as a custom titlebar.", + "Description": "The TitleBar control provides a simple way to create a modern titlebar UX with interactive content.", "Content": "

Look at the TitleBarPage.xaml file in Visual Studio to see the full code for this page.

", "IsUpdated": true, "Docs": [ { - "Title": "TitleBar - API", - "Uri": "https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.window.extendscontentintotitlebar" + "Title": "Title bar customization", + "Uri": "https://learn.microsoft.com/windows/apps/develop/title-bar" }, { - "Title": "Guidelines", - "Uri": "https://learn.microsoft.com/windows/windows-app-sdk/api/winrt/microsoft.ui.xaml.window" + "Title": "Titl bar - design guidelines", + "Uri": "https://learn.microsoft.com/windows/apps/design/basics/titlebar-design" } ], "RelatedControls": [ diff --git a/WinUIGallery/Samples/SamplePages/ModalWindow.xaml.cs b/WinUIGallery/Samples/SamplePages/ModalWindow.xaml.cs index cc594c813..469b2ce20 100644 --- a/WinUIGallery/Samples/SamplePages/ModalWindow.xaml.cs +++ b/WinUIGallery/Samples/SamplePages/ModalWindow.xaml.cs @@ -21,7 +21,7 @@ public ModalWindow() // Set this modal window's owner (the main application window). // The main window can be retrieved from App.xaml.cs if it's set as a static property. - SetOwnership(appWindow, App.StartupWindow); + SetOwnership(appWindow, App.MainWindow); // Make the window modal (blocks interaction with the owner window until closed). presenter.IsModal = true; @@ -74,7 +74,7 @@ private void SetOwnership(AppWindow ownedAppWindow, Window ownerWindow) private void ModalWindow_Closed(object sender, WindowEventArgs args) { // Reactivate the main application window when the modal window closes. - App.StartupWindow.Activate(); + App.MainWindow.Activate(); } private void OKButton_Click(object sender, RoutedEventArgs e) diff --git a/WinUIGallery/Samples/SamplePages/TitleBarWindow.xaml b/WinUIGallery/Samples/SamplePages/TitleBarWindow.xaml new file mode 100644 index 000000000..9422d4335 --- /dev/null +++ b/WinUIGallery/Samples/SamplePages/TitleBarWindow.xaml @@ -0,0 +1,72 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/WinUIGallery/Samples/SamplePages/TitleBarWindow.xaml.cs b/WinUIGallery/Samples/SamplePages/TitleBarWindow.xaml.cs new file mode 100644 index 000000000..b89bebd06 --- /dev/null +++ b/WinUIGallery/Samples/SamplePages/TitleBarWindow.xaml.cs @@ -0,0 +1,41 @@ +using System; +using System.Linq; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; + +namespace WinUIGallery.Samples.SamplePages; + +public sealed partial class TitleBarWindow : Window +{ + public TitleBarWindow() + { + this.InitializeComponent(); + this.ExtendsContentIntoTitleBar = true; // Extend the content into the title bar and hide the default title bar + this.AppWindow.TitleBar.PreferredHeightOption = Microsoft.UI.Windowing.TitleBarHeightOption.Tall; + this.SetTitleBar(titleBar); // Set the custom title bar + navView.SelectedItem = navView.MenuItems.OfType().First(); + } + + private void TitleBar_PaneToggleRequested(TitleBar sender, object args) + { + navView.IsPaneOpen = !navView.IsPaneOpen; + } + + private void TitleBar_BackRequested(TitleBar sender, object args) + { + navFrame.GoBack(); + } + + private void navView_SelectionChanged(NavigationView sender, NavigationViewSelectionChangedEventArgs args) + { + var selectedItem = (NavigationViewItem)args.SelectedItem; + if (selectedItem != null) + { + string selectedItemTag = ((string)selectedItem.Tag); + sender.Header = "Sample Page " + selectedItemTag.Substring(selectedItemTag.Length - 1); + string pageName = "WinUIGallery.SamplePages." + selectedItemTag; + Type pageType = Type.GetType(pageName); + navFrame.Navigate(pageType); + } + } +} diff --git a/WinUIGallery/Styles/TitleBar.xaml b/WinUIGallery/Styles/TitleBar.xaml new file mode 100644 index 000000000..5a0754d2a --- /dev/null +++ b/WinUIGallery/Styles/TitleBar.xaml @@ -0,0 +1,285 @@ + + + + + + + + + + + diff --git a/WinUIGallery/WinUIGallery.csproj b/WinUIGallery/WinUIGallery.csproj index b0eec4242..27150a930 100644 --- a/WinUIGallery/WinUIGallery.csproj +++ b/WinUIGallery/WinUIGallery.csproj @@ -297,6 +297,7 @@ + @@ -306,11 +307,13 @@ + + @@ -487,6 +490,18 @@ + + + MSBuild:Compile + + + + + + MSBuild:Compile + + + MSBuild:Compile @@ -537,6 +552,12 @@ MSBuild:Compile + + + + MSBuild:Compile + + MSBuild:Compile diff --git a/standalone.props b/standalone.props index 8738d66f4..cfb0df268 100644 --- a/standalone.props +++ b/standalone.props @@ -1,24 +1,24 @@ - - - 10.0.22621.42 - 1.6.250108002 - 6.2.11 - 1.0.4 - 2.0.13 - 8.0.230907 - 2.0.3 - - obj\$(MSBuildProjectName)\ - bin\$(MSBuildProjectName)\ - + 10.0.22621.42 + 1.7.250208002-preview1 + 6.2.11 + 1.3.2 + 2.0.15 + 8.1.240916 + 2.2.0 + + obj\$(MSBuildProjectName)\ + bin\$(MSBuildProjectName)\ + - obj\**;bin\**;$(DefaultItemExcludes) - - obj\generated - + obj\**;bin\**;$(DefaultItemExcludes) + + obj\generated + \ No newline at end of file