More work

This commit is contained in:
TheXamlGuy
2024-02-04 14:08:38 +00:00
parent be3fe89387
commit 731cf3cdf3
34 changed files with 381 additions and 177 deletions
@@ -0,0 +1,56 @@
using Microsoft.UI.Xaml;
namespace Hyperbar.Controls.Windows;
public class DesktopApplicationBar :
DependencyObject
{
public static readonly DependencyProperty ContentProperty =
DependencyProperty.Register(nameof(Content),
typeof(object), typeof(DesktopApplicationBar),
new PropertyMetadata(null));
public static readonly DependencyProperty PlacementProperty =
DependencyProperty.Register(nameof(Placement),
typeof(DesktopApplicationBarPlacemenet), typeof(DesktopApplicationBar),
new PropertyMetadata(DesktopApplicationBarPlacemenet.Left, OnPlacementPropertyChanged));
private readonly DesktopApplicationBarHost host;
private readonly DesktopApplicationBarPresenter presenter;
public DesktopApplicationBar()
{
presenter = new DesktopApplicationBarPresenter
{
Parent = this
};
host = new DesktopApplicationBarHost(presenter);
host.Activate();
}
public object Content
{
get => GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}
public DesktopApplicationBarPlacemenet Placement
{
get => (DesktopApplicationBarPlacemenet)GetValue(PlacementProperty);
set => SetValue(PlacementProperty, value);
}
private static void OnPlacementPropertyChanged(DependencyObject dependencyObject,
DependencyPropertyChangedEventArgs args)
{
if (dependencyObject is DesktopApplicationBar sender)
{
sender.OnPlacementPropertyChanged();
}
}
private void OnPlacementPropertyChanged() => UpdatePlacement();
private void UpdatePlacement() => host.UpdatePlacement(Placement);
}
@@ -0,0 +1,80 @@
using Microsoft.UI.Xaml;
using Windows.Foundation;
using Microsoft.UI.Xaml.Media;
using Hyperbar.Interop.Windows;
using Hyperbar.UI.Windows;
namespace Hyperbar.Controls.Windows;
internal class DesktopApplicationBarHost : Window
{
private readonly DesktopApplicationBarPresenter presenter;
private readonly WindowSnapping windowSnapping;
private DesktopApplicationBarPlacemenet placement;
public DesktopApplicationBarHost(DesktopApplicationBarPresenter presenter)
{
this.SetOpacity(0);
this.SetStyle(WindowStyle.SysMenu | WindowStyle.Visible);
this.SetStyle(ExtendedWindowStyle.NoActivate);
this.MoveAndResize(0, 0, 0, 0);
this.SetTopMost(true);
this.SetIsAvailableInSwitchers(false);
SystemBackdrop = new MicaBackdrop();
windowSnapping = WindowSnapping.Create(this.GetHandle());
this.presenter = presenter;
presenter.Loaded += OnLoaded;
Content = presenter;
Closed += OnClosed;
}
internal void UpdatePlacement(DesktopApplicationBarPlacemenet placement)
{
this.placement = placement;
UpdatePlacement();
}
internal void UpdatePlacement()
{
presenter.Measure(new Size(double.PositiveInfinity, double.PositiveInfinity));
double size = Math.Min(presenter.DesiredSize.Height, presenter.DesiredSize.Width);
switch (placement)
{
case DesktopApplicationBarPlacemenet.Left:
windowSnapping.Snap(WindowSnappingPlacement.Left, (int)size);
break;
case DesktopApplicationBarPlacemenet.Top:
windowSnapping.Snap(WindowSnappingPlacement.Top, (int)size);
break;
case DesktopApplicationBarPlacemenet.Right:
windowSnapping.Snap(WindowSnappingPlacement.Right, (int)size);
break;
case DesktopApplicationBarPlacemenet.Bottom:
windowSnapping.Snap(WindowSnappingPlacement.Bottom, (int)size);
break;
default:
break;
}
presenter.UpdatePlacementState(placement);
}
private void OnClosed(object sender, WindowEventArgs args)
{
windowSnapping.Dispose();
}
private void OnLoaded(object sender,
RoutedEventArgs args)
{
UpdatePlacement();
this.SetOpacity(255);
}
}
@@ -0,0 +1,9 @@
namespace Hyperbar.Controls.Windows;
public enum DesktopApplicationBarPlacemenet
{
Left,
Top,
Right,
Bottom
}
@@ -0,0 +1,40 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Data;
namespace Hyperbar.Controls.Windows;
public class DesktopApplicationBarPresenter :
ContentControl
{
public static readonly DependencyProperty TemplateSettingsProperty =
DependencyProperty.Register(nameof(TemplateSettings),
typeof(DesktopApplicationBarPresenterTemplateSettings), typeof(DesktopApplicationBarPresenter),
new PropertyMetadata(null));
internal new DesktopApplicationBar? Parent;
public DesktopApplicationBarPresenter()
{
DefaultStyleKey = typeof(DesktopApplicationBarPresenter);
TemplateSettings = new DesktopApplicationBarPresenterTemplateSettings();
}
protected override void OnApplyTemplate()
{
SetBinding(ContentProperty, new Binding
{
Source = Parent,
Mode = BindingMode.TwoWay,
Path = new PropertyPath(nameof(Parent.Content)),
});
}
public DesktopApplicationBarPresenterTemplateSettings TemplateSettings
{
get => (DesktopApplicationBarPresenterTemplateSettings)GetValue(TemplateSettingsProperty);
set => SetValue(TemplateSettingsProperty, value);
}
internal void UpdatePlacementState(DesktopApplicationBarPlacemenet placement) => VisualStateManager.GoToState(this, $"{placement}Placement", true);
}
@@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8" ?>
<ResourceDictionary
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="using:Hyperbar.Controls.Windows">
<ResourceDictionary.ThemeDictionaries>
<ResourceDictionary x:Key="Default">
<StaticResource x:Key="DesktopApplicationBarPresenterBackground" ResourceKey="AcrylicInAppFillColorDefaultBrush" />
<StaticResource x:Key="DesktopApplicationBarPresenterForeground" ResourceKey="TextFillColorPrimaryBrush" />
<StaticResource x:Key="DesktopApplicationBarPresenterBorderBrush" ResourceKey="ControlStrokeColorDefaultBrush" />
</ResourceDictionary>
<ResourceDictionary x:Key="Light">
<StaticResource x:Key="DesktopApplicationBarPresenterBackground" ResourceKey="AcrylicInAppFillColorDefaultBrush" />
<StaticResource x:Key="DesktopApplicationBarPresenterForeground" ResourceKey="TextFillColorPrimaryBrush" />
<StaticResource x:Key="DesktopApplicationBarPresenterBorderBrush" ResourceKey="ControlStrokeColorDefaultBrush" />
</ResourceDictionary>
</ResourceDictionary.ThemeDictionaries>
<Thickness x:Key="DesktopApplicationBarPresenterBorderThemeThickness">0</Thickness>
<Style TargetType="controls:DesktopApplicationBarPresenter">
<Setter Property="Foreground" Value="{ThemeResource DesktopApplicationBarPresenterForeground}" />
<Setter Property="BorderBrush" Value="{ThemeResource DesktopApplicationBarPresenterBorderBrush}" />
<Setter Property="BorderThickness" Value="{ThemeResource DesktopApplicationBarPresenterBorderThemeThickness}" />
<Setter Property="HorizontalContentAlignment" Value="Stretch" />
<Setter Property="VerticalContentAlignment" Value="Stretch" />
<Setter Property="HorizontalAlignment" Value="Stretch" />
<Setter Property="VerticalAlignment" Value="Stretch" />
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="controls:DesktopApplicationBarPresenter">
<Border
MinHeight="48"
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
Background="{TemplateBinding Background}"
BackgroundSizing="OuterBorderEdge"
BorderBrush="{TemplateBinding BorderBrush}"
BorderThickness="{TemplateBinding BorderThickness}"
CornerRadius="{TemplateBinding CornerRadius}"
FlowDirection="{TemplateBinding FlowDirection}">
<ContentControl
HorizontalAlignment="{TemplateBinding HorizontalAlignment}"
VerticalAlignment="{TemplateBinding VerticalAlignment}"
HorizontalContentAlignment="{TemplateBinding HorizontalContentAlignment}"
VerticalContentAlignment="{TemplateBinding VerticalContentAlignment}"
Content="{TemplateBinding Content}"
ContentTemplate="{TemplateBinding ContentTemplate}"
ContentTransitions="{TemplateBinding ContentTransitions}" />
</Border>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</ResourceDictionary>
@@ -0,0 +1,50 @@
using Microsoft.UI.Xaml;
namespace Hyperbar.Controls.Windows;
public class DesktopApplicationBarPresenterTemplateSettings : DependencyObject
{
public static readonly DependencyProperty HeightProperty =
DependencyProperty.Register(nameof(Height),
typeof(double), typeof(DesktopApplicationBarPresenterTemplateSettings),
new PropertyMetadata(0d));
public static readonly DependencyProperty NegativeHeightProperty =
DependencyProperty.Register(nameof(NegativeHeight),
typeof(double), typeof(DesktopApplicationBarPresenterTemplateSettings),
new PropertyMetadata(0d));
public static readonly DependencyProperty NegativeWidthProperty =
DependencyProperty.Register(nameof(NegativeWidth),
typeof(double), typeof(DesktopApplicationBarPresenterTemplateSettings),
new PropertyMetadata(0d));
public static readonly DependencyProperty WidthProperty =
DependencyProperty.Register(nameof(Width),
typeof(double), typeof(DesktopApplicationBarPresenterTemplateSettings),
new PropertyMetadata(0d));
public double Height
{
get => (double)GetValue(HeightProperty);
set => SetValue(HeightProperty, value);
}
public double NegativeHeight
{
get => (double)GetValue(NegativeHeightProperty);
set => SetValue(NegativeHeightProperty, value);
}
public double NegativeWidth
{
get => (double)GetValue(NegativeWidthProperty);
set => SetValue(NegativeWidthProperty, value);
}
public double Width
{
get => (double)GetValue(WidthProperty);
set => SetValue(WidthProperty, value);
}
}