-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #88 from CommunityToolkit/niels9001/titlebar-impro…
…vements Gallery design improvements
- Loading branch information
Showing
15 changed files
with
991 additions
and
282 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
44 changes: 44 additions & 0 deletions
44
CommunityToolkit.App.Shared/Controls/TitleBar/NativeMethods.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
#if WINAPPSDK | ||
using System.Runtime.InteropServices; | ||
using WinRT.Interop; | ||
using Microsoft.UI; | ||
using Microsoft.UI.Windowing; | ||
|
||
namespace CommunityToolkit.App.Shared.Controls; | ||
|
||
public partial class TitleBar : Control | ||
{ | ||
[DllImport("Shcore.dll", SetLastError = true)] | ||
internal static extern int GetDpiForMonitor(IntPtr hmonitor, Monitor_DPI_Type dpiType, out uint dpiX, out uint dpiY); | ||
|
||
internal enum Monitor_DPI_Type : int | ||
{ | ||
MDT_Effective_DPI = 0, | ||
MDT_Angular_DPI = 1, | ||
MDT_Raw_DPI = 2, | ||
MDT_Default = MDT_Effective_DPI | ||
} | ||
|
||
private double GetScaleAdjustment() | ||
{ | ||
IntPtr hWnd = WindowNative.GetWindowHandle(this.Window); | ||
WindowId wndId = Win32Interop.GetWindowIdFromWindow(hWnd); | ||
DisplayArea displayArea = DisplayArea.GetFromWindowId(wndId, DisplayAreaFallback.Primary); | ||
IntPtr hMonitor = Win32Interop.GetMonitorFromDisplayId(displayArea.DisplayId); | ||
|
||
// Get DPI. | ||
int result = GetDpiForMonitor(hMonitor, Monitor_DPI_Type.MDT_Default, out uint dpiX, out uint _); | ||
if (result != 0) | ||
{ | ||
throw new Exception("Could not get DPI for monitor."); | ||
} | ||
|
||
uint scaleFactorPercent = (uint)(((long)dpiX * 100 + (96 >> 1)) / 96); | ||
return scaleFactorPercent / 100.0; | ||
} | ||
} | ||
#endif |
225 changes: 225 additions & 0 deletions
225
CommunityToolkit.App.Shared/Controls/TitleBar/TitleBar.Properties.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,225 @@ | ||
// Licensed to the .NET Foundation under one or more agreements. | ||
// The .NET Foundation licenses this file to you under the MIT license. | ||
// See the LICENSE file in the project root for more information. | ||
|
||
namespace CommunityToolkit.App.Shared.Controls; | ||
|
||
public partial class TitleBar : Control | ||
{ | ||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="Icon"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty IconProperty = DependencyProperty.Register(nameof(Icon), typeof(IconElement), typeof(TitleBar), new PropertyMetadata(null, IconChanged)); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="Title"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty TitleProperty = DependencyProperty.Register(nameof(Title), typeof(string), typeof(TitleBar), new PropertyMetadata(default(string))); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="Subtitle"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty SubtitleProperty = DependencyProperty.Register(nameof(Subtitle), typeof(string), typeof(TitleBar), new PropertyMetadata(default(string))); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="Content"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty ContentProperty = DependencyProperty.Register(nameof(Content), typeof(object), typeof(TitleBar), new PropertyMetadata(null, ContentChanged)); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="Footer"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty FooterProperty = DependencyProperty.Register(nameof(Footer), typeof(object), typeof(TitleBar), new PropertyMetadata(null, FooterChanged)); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsBackButtonVisible"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty IsBackButtonVisibleProperty = DependencyProperty.Register(nameof(IsBackButtonVisible), typeof(bool), typeof(TitleBar), new PropertyMetadata(false, IsBackButtonVisibleChanged)); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="IsPaneButtonVisible"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty IsPaneButtonVisibleProperty = DependencyProperty.Register(nameof(IsPaneButtonVisible), typeof(bool), typeof(TitleBar), new PropertyMetadata(false, IsPaneButtonVisibleChanged)); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="Display"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty DisplayModeProperty = DependencyProperty.Register(nameof(DisplayMode), typeof(DisplayMode), typeof(TitleBar), new PropertyMetadata(DisplayMode.Standard, DisplayModeChanged)); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="CompactStateBreakpoint | ||
/// "/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty CompactStateBreakpointProperty = DependencyProperty.Register(nameof(CompactStateBreakpoint), typeof(int), typeof(TitleBar), new PropertyMetadata(850)); | ||
|
||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="AutoConfigureCustomTitleBar"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty AutoConfigureCustomTitleBarProperty = DependencyProperty.Register(nameof(AutoConfigureCustomTitleBar), typeof(bool), typeof(TitleBar), new PropertyMetadata(true, AutoConfigureCustomTitleBarChanged)); | ||
|
||
#if WINAPPSDK | ||
/// <summary> | ||
/// The backing <see cref="DependencyProperty"/> for the <see cref="Window"/> property. | ||
/// </summary> | ||
public static readonly DependencyProperty WindowProperty = DependencyProperty.Register(nameof(Window), typeof(Window), typeof(TitleBar), new PropertyMetadata(null)); | ||
#endif | ||
|
||
/// <summary> | ||
/// The event that gets fired when the back button is clicked | ||
/// </summary> | ||
public event EventHandler<RoutedEventArgs>? BackButtonClick; | ||
|
||
/// <summary> | ||
/// The event that gets fired when the pane toggle button is clicked | ||
/// </summary> | ||
public event EventHandler<RoutedEventArgs>? PaneButtonClick; | ||
|
||
/// <summary> | ||
/// Gets or sets the Icon | ||
/// </summary> | ||
public IconElement Icon | ||
{ | ||
get => (IconElement)GetValue(IconProperty); | ||
set => SetValue(IconProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the Title | ||
/// </summary> | ||
public string Title | ||
{ | ||
get => (string)GetValue(TitleProperty); | ||
set => SetValue(TitleProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the Subtitle | ||
/// </summary> | ||
public string Subtitle | ||
{ | ||
get => (string)GetValue(SubtitleProperty); | ||
set => SetValue(SubtitleProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the content shown at the center of the TitleBar. When setting this, using DisplayMode=Tall is recommended. | ||
/// </summary> | ||
public object Content | ||
{ | ||
get => (object)GetValue(ContentProperty); | ||
set => SetValue(ContentProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the content shown at the right of the TitleBar, next to the caption buttons. When setting this, using DisplayMode=Tall is recommended. | ||
/// </summary> | ||
public object Footer | ||
{ | ||
get => (object)GetValue(FooterProperty); | ||
set => SetValue(FooterProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets DisplayMode. Compact is default (32px), Tall is recommended when setting the Content or Footer. | ||
/// </summary> | ||
public DisplayMode DisplayMode | ||
{ | ||
get => (DisplayMode)GetValue(DisplayModeProperty); | ||
set => SetValue(DisplayModeProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the visibility of the back button. | ||
/// </summary> | ||
public bool IsBackButtonVisible | ||
{ | ||
get => (bool)GetValue(IsBackButtonVisibleProperty); | ||
set => SetValue(IsBackButtonVisibleProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the visibility of the pane toggle button. | ||
/// </summary> | ||
public bool IsPaneButtonVisible | ||
{ | ||
get => (bool)GetValue(IsPaneButtonVisibleProperty); | ||
set => SetValue(IsPaneButtonVisibleProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets the breakpoint of when the compact state is triggered. | ||
/// </summary> | ||
public int CompactStateBreakpoint | ||
{ | ||
get => (int)GetValue(CompactStateBreakpointProperty); | ||
set => SetValue(CompactStateBreakpointProperty, value); | ||
} | ||
|
||
/// <summary> | ||
/// Gets or sets if the TitleBar should auto configure ExtendContentIntoTitleBar and CaptionButtion background colors. | ||
/// </summary> | ||
public bool AutoConfigureCustomTitleBar | ||
{ | ||
get => (bool)GetValue(AutoConfigureCustomTitleBarProperty); | ||
set => SetValue(AutoConfigureCustomTitleBarProperty, value); | ||
} | ||
|
||
#if WINAPPSDK | ||
/// <summary> | ||
/// Gets or sets the window the TitleBar should configure (WASDK only). | ||
/// </summary> | ||
public Window Window | ||
{ | ||
get => (Window)GetValue(WindowProperty); | ||
set => SetValue(WindowProperty, value); | ||
} | ||
#endif | ||
|
||
private static void IsBackButtonVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
((TitleBar)d).Update(); | ||
} | ||
|
||
private static void IsPaneButtonVisibleChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
((TitleBar)d).Update(); | ||
} | ||
|
||
private static void DisplayModeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
((TitleBar)d).Update(); | ||
} | ||
|
||
private static void ContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
((TitleBar)d).Update(); | ||
} | ||
|
||
private static void FooterChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
((TitleBar)d).Update(); | ||
} | ||
|
||
private static void IconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
((TitleBar)d).Update(); | ||
} | ||
|
||
private static void AutoConfigureCustomTitleBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) | ||
{ | ||
if (((TitleBar)d).AutoConfigureCustomTitleBar) | ||
{ | ||
((TitleBar)d).Configure(); | ||
} | ||
else | ||
{ | ||
((TitleBar)d).Reset(); | ||
} | ||
} | ||
} | ||
|
||
public enum DisplayMode | ||
{ | ||
Standard, | ||
Tall | ||
} |
Oops, something went wrong.