feat: implement closing related features.

This commit is contained in:
rabbitism
2024-01-22 17:27:11 +08:00
parent 9cff01c032
commit 5c62131a0a
6 changed files with 70 additions and 20 deletions

View File

@@ -1,8 +1,6 @@
using System;
using System.Threading.Tasks;
using System.Windows.Input;
using Avalonia.Controls;
using Avalonia.Controls.Shapes;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Ursa.Controls;
@@ -30,11 +28,11 @@ public class DialogDemoViewModel: ObservableObject
private async Task ShowGlobalOverlayDialog()
{
await DialogBox.ShowOverlayAsync<Banner, DateTime>(DateTime.Now, "GlobalHost");
await DialogBox.ShowOverlayAsync<ButtonGroupDemo, ButtonGroupDemoViewModel>(new ButtonGroupDemoViewModel(), "GlobalHost");
}
private async Task ShowLocalOverlayDialog()
{
await DialogBox.ShowOverlayAsync<Banner, DateTime>(DateTime.Now, "LocalHost");
await DialogBox.ShowOverlayAsync<ButtonGroupDemo, ButtonGroupDemoViewModel>(new ButtonGroupDemoViewModel(), "LocalHost");
}
}

View File

@@ -1,19 +1,35 @@
<ResourceDictionary xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here -->
<ResourceDictionary
xmlns="https://github.com/avaloniaui"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:u="https://irihi.tech/ursa">
<!-- Add Resources Here -->
<ControlTheme x:Key="{x:Type u:DialogControl}" TargetType="u:DialogControl">
<Setter Property="Template">
<ControlTemplate TargetType="u:DialogControl">
<Border HorizontalAlignment="Center" VerticalAlignment="Center" Theme="{DynamicResource CardBorder}" Width="100" Height="100" IsHitTestVisible="True">
<Border
Classes="Shadow"
HorizontalAlignment="Center"
VerticalAlignment="Center"
IsHitTestVisible="True"
Theme="{DynamicResource CardBorder}">
<Grid RowDefinitions="Auto, *, Auto">
<ContentPresenter Content="{TemplateBinding Content}"></ContentPresenter>
<StackPanel Grid.Row="0" HorizontalAlignment="Right">
<Button Name="{x:Static u:DialogControl.PART_CloseButton}">Close</Button>
</StackPanel>
<ContentPresenter
Grid.Row="0"
Grid.RowSpan="2"
Content="{TemplateBinding Content}" />
<StackPanel Grid.Row="2">
<Button>OK</Button>
<Button>Cancel</Button>
</StackPanel>
</Grid>
</Border>
</ControlTemplate>
</Setter>
</ControlTheme>
<ControlTheme x:Key="{x:Type u:DialogWindow}" TargetType="u:DialogWindow">
<Setter Property="Title" Value="{x:Null}" />
<Setter Property="Background" Value="{DynamicResource WindowDefaultBackground}" />
@@ -26,11 +42,15 @@
<Setter Property="WindowStartupLocation" Value="CenterOwner" />
<Setter Property="ExtendClientAreaTitleBarHeightHint" Value="1" />
<Setter Property="ExtendClientAreaToDecorationsHint" Value="True" />
<Setter Property="ExtendClientAreaChromeHints" Value="SystemChrome"/>
<Setter Property="ExtendClientAreaChromeHints" Value="SystemChrome" />
<Setter Property="SystemDecorations">
<OnPlatform >
<OnPlatform.Windows><SystemDecorations>Full</SystemDecorations></OnPlatform.Windows>
<OnPlatform.Default><SystemDecorations>BorderOnly</SystemDecorations></OnPlatform.Default>
<OnPlatform>
<OnPlatform.Windows>
<SystemDecorations>Full</SystemDecorations>
</OnPlatform.Windows>
<OnPlatform.Default>
<SystemDecorations>BorderOnly</SystemDecorations>
</OnPlatform.Default>
</OnPlatform>
</Setter>
<Setter Property="CanResize" Value="False" />
@@ -40,10 +60,10 @@
<Border Name="PART_TransparencyFallback" IsHitTestVisible="False" />
<Border Background="{TemplateBinding Background}" IsHitTestVisible="False" />
<Panel Margin="{TemplateBinding WindowDecorationMargin}" Background="Transparent" />
<ChromeOverlayLayer></ChromeOverlayLayer>
<ChromeOverlayLayer />
<Grid RowDefinitions="Auto, *, Auto">
<Button Name="{x:Static u:DialogWindow.PART_CloseButton}" VerticalAlignment="Top">Close</Button>
<ContentPresenter Content="{TemplateBinding Content}"></ContentPresenter>
<ContentPresenter Content="{TemplateBinding Content}" />
</Grid>
</Panel>
</ControlTemplate>

View File

@@ -58,7 +58,7 @@ public static class DialogBox
};
t.DataContext = vm;
var host = OverlayDialogManager.GetOverlayDialogHost(hostId);
host?.Children.Add(t);
host?.AddDialog(t);
return null;
}
}

View File

@@ -13,15 +13,36 @@ public class DialogControl: ContentControl
private Button? _closeButton;
public event EventHandler OnClose;
public event EventHandler<object?> OnClose;
protected override void OnApplyTemplate(TemplateAppliedEventArgs e)
{
base.OnApplyTemplate(e);
if (_closeButton != null)
{
_closeButton.Click -= Close;
}
_closeButton = e.NameScope.Find<Button>(PART_CloseButton);
if (_closeButton is not null)
{
_closeButton.Click += Close;
}
}
public void Show()
{
}
private void Close(object sender, object args)
{
if (this.DataContext is IDialogContext context)
{
OnClose?.Invoke(this, context.DefaultCloseResult);
}
else
{
OnClose?.Invoke(this, null);
}
}
}

View File

@@ -1,6 +1,6 @@
namespace Ursa.Controls;
public record OverlayDialogOptions
public record DialogOptions
{
public bool ShowCloseButton { get; set; } = true;
}

View File

@@ -84,6 +84,17 @@ public class OverlayDialogHost: Canvas
public void AddDialog(DialogControl control)
{
this.Children.Add(control);
control.OnClose += OnDialogClose;
}
private void OnDialogClose(object sender, object? e)
{
if (sender is DialogControl control)
{
this.Children.Remove(control);
control.OnClose -= OnDialogClose;
}
}
public void AddModalDialog(DialogControl control)