using System.Windows.Shell; // ReSharper disable once CheckNamespace namespace WPFluent.Controls; /// /// A custom WinUI Window with more convenience methods. /// public class FluentWindow : System.Windows.Window { /// /// Identifies the dependency property. /// public static readonly DependencyProperty ExtendsContentIntoTitleBarProperty = DependencyProperty.Register( nameof(ExtendsContentIntoTitleBar), typeof(bool), typeof(FluentWindow), new PropertyMetadata(false, OnExtendsContentIntoTitleBarChanged)); /// /// Identifies the dependency property. /// public static readonly DependencyProperty WindowBackdropTypeProperty = DependencyProperty.Register( nameof(WindowBackdropType), typeof(WindowBackdropType), typeof(FluentWindow), new PropertyMetadata(WindowBackdropType.None, OnWindowBackdropTypeChanged)); /// /// Identifies the dependency property. /// public static readonly DependencyProperty WindowCornerPreferenceProperty = DependencyProperty.Register( nameof(WindowCornerPreference), typeof(WindowCornerPreference), typeof(FluentWindow), new PropertyMetadata(WindowCornerPreference.Round, OnWindowCornerPreferenceChanged)); private WindowInteropHelper _interopHelper = null; /// /// Initializes static members of the class. Overrides default properties. /// /// /// Overrides default properties. /// static FluentWindow() { DefaultStyleKeyProperty.OverrideMetadata( typeof(FluentWindow), new FrameworkPropertyMetadata(typeof(FluentWindow))); } /// /// Initializes a new instance of the class. /// public FluentWindow() { SetResourceReference(StyleProperty, typeof(FluentWindow)); } /// /// Private property callback. /// private static void OnExtendsContentIntoTitleBarChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not FluentWindow window) { return; } if (e.OldValue == e.NewValue) { return; } window.OnExtendsContentIntoTitleBarChanged((bool)e.OldValue, (bool)e.NewValue); } /// /// Private property callback. /// private static void OnWindowBackdropTypeChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not FluentWindow window) { return; } if (e.OldValue == e.NewValue) { return; } window.OnBackdropTypeChanged((WindowBackdropType)e.OldValue, (WindowBackdropType)e.NewValue); } /// /// Private property callback. /// private static void OnWindowCornerPreferenceChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) { if (d is not FluentWindow window) { return; } if (e.OldValue == e.NewValue) { return; } window.OnCornerPreferenceChanged((WindowCornerPreference)e.OldValue, (WindowCornerPreference)e.NewValue); } /// /// This virtual method is called when is changed. /// protected virtual void OnBackdropTypeChanged(WindowBackdropType oldValue, WindowBackdropType newValue) { if (Appearance.ThemeManager.GetAppTheme() == Appearance.ThemeType.Unknow) { newValue = WindowBackdropType.None; } if (InteropHelper.Handle == IntPtr.Zero) { return; } } /// /// This virtual method is called when is changed. /// protected virtual void OnCornerPreferenceChanged(WindowCornerPreference oldValue, WindowCornerPreference newValue) { if (InteropHelper.Handle == IntPtr.Zero) { return; } } /// /// This virtual method is called when is changed. /// protected virtual void OnExtendsContentIntoTitleBarChanged(bool oldValue, bool newValue) { // AllowsTransparency = true; SetCurrentValue(WindowStyleProperty, WindowStyle.SingleBorderWindow); WindowChrome.SetWindowChrome( this, new WindowChrome { CaptionHeight = 0, CornerRadius = default, GlassFrameThickness = new Thickness(-1), ResizeBorderThickness = ResizeMode == ResizeMode.NoResize ? default : new Thickness(4), UseAeroCaptionButtons = false, }); } /// protected override void OnSourceInitialized(EventArgs e) { OnCornerPreferenceChanged(default, WindowCornerPreference); OnExtendsContentIntoTitleBarChanged(default, ExtendsContentIntoTitleBar); OnBackdropTypeChanged(default, WindowBackdropType); base.OnSourceInitialized(e); } /// /// Gets contains helper for accessing this window handle. /// protected WindowInteropHelper InteropHelper { get => _interopHelper ??= new WindowInteropHelper(this); } /// /// Gets or sets a value indicating whether the default title bar of the window should be hidden to create space for /// app content. /// public bool ExtendsContentIntoTitleBar { get => (bool)GetValue(ExtendsContentIntoTitleBarProperty); set => SetValue(ExtendsContentIntoTitleBarProperty, value); } /// /// Gets or sets a value determining preferred backdrop type for current . /// public WindowBackdropType WindowBackdropType { get => (WindowBackdropType)GetValue(WindowBackdropTypeProperty); set => SetValue(WindowBackdropTypeProperty, value); } /// /// Gets or sets a value determining corner preference for current . /// public WindowCornerPreference WindowCornerPreference { get => (WindowCornerPreference)GetValue(WindowCornerPreferenceProperty); set => SetValue(WindowCornerPreferenceProperty, value); } }