feat: WIP set up interface.

This commit is contained in:
rabbitism
2024-02-04 12:32:08 +08:00
parent ed21ee8189
commit f3cd4fb74b
6 changed files with 122 additions and 71 deletions

View File

@@ -42,7 +42,7 @@ public class DialogControl: ContentControl
remove => RemoveHandler(LayerChangedEvent, value);
}
public static readonly RoutedEvent<ResultEventArgs> ClosedEvent = RoutedEvent.Register<DrawerControl, ResultEventArgs>(
public static readonly RoutedEvent<ResultEventArgs> ClosedEvent = RoutedEvent.Register<DrawerControlBase, ResultEventArgs>(
nameof(Closed), RoutingStrategies.Bubble);
public event EventHandler<ResultEventArgs> Closed

View File

@@ -168,6 +168,11 @@ public class OverlayDialogHost : Canvas
ResetZIndices();
}
internal void AddDrawer(DrawerControlBase control)
{
}
private void OnDialogControlClosing(object sender, object? e)
{
if (sender is DialogControl control)

View File

@@ -0,0 +1,6 @@
namespace Ursa.Controls;
public class DefaultDrawerControl: DrawerControlBase
{
}

View File

@@ -2,5 +2,12 @@
public static class Drawer
{
public static Task<TResult?> ShowDialogAsync<TView, TViewModel, TResult>(TViewModel viewModel)
{
var host = OverlayDialogManager.GetHost(null);
if (host is null) return Task.FromResult(default(TResult));
var dialog = new DefaultDrawerControl();
host.AddDrawer(dialog);
return dialog.ShowAsync<TResult>();
}
}

View File

@@ -1,69 +0,0 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Ursa.Common;
using Ursa.EventArgs;
namespace Ursa.Controls;
public class DrawerControl: ContentControl
{
public static readonly StyledProperty<Position> PositionProperty = AvaloniaProperty.Register<DrawerControl, Position>(
nameof(Position));
public Position Position
{
get => GetValue(PositionProperty);
set => SetValue(PositionProperty, value);
}
public static readonly StyledProperty<bool> IsOpenProperty = AvaloniaProperty.Register<DrawerControl, bool>(
nameof(IsOpen));
public bool IsOpen
{
get => GetValue(IsOpenProperty);
set => SetValue(IsOpenProperty, value);
}
public static readonly RoutedEvent<ResultEventArgs> CloseEvent = RoutedEvent.Register<DrawerControl, ResultEventArgs>(
"Close", RoutingStrategies.Bubble);
public event EventHandler<ResultEventArgs> Close
{
add => AddHandler(CloseEvent, value);
remove => RemoveHandler(CloseEvent, value);
}
public Task<T?> ShowAsync<T>(CancellationToken? token = default)
{
var tcs = new TaskCompletionSource<T?>();
token?.Register(() =>
{
Dispatcher.UIThread.Invoke(CloseDrawer);
});
void OnCloseHandler(object sender, ResultEventArgs args)
{
if (args is T result)
{
tcs.SetResult(result);
Close -= OnCloseHandler;
}
else
{
tcs.SetResult(default(T));
Close -= OnCloseHandler;
}
}
this.Close += OnCloseHandler;
return tcs.Task;
}
internal void CloseDrawer()
{
}
}

View File

@@ -0,0 +1,102 @@
using Avalonia;
using Avalonia.Controls;
using Avalonia.Interactivity;
using Avalonia.Threading;
using Ursa.Common;
using Ursa.EventArgs;
namespace Ursa.Controls;
public abstract class DrawerControlBase: ContentControl
{
internal bool CanClickOnMaskToClose { get; set; }
public static readonly StyledProperty<Position> PositionProperty = AvaloniaProperty.Register<DrawerControlBase, Position>(
nameof(Position));
public Position Position
{
get => GetValue(PositionProperty);
set => SetValue(PositionProperty, value);
}
public static readonly StyledProperty<bool> IsOpenProperty = AvaloniaProperty.Register<DrawerControlBase, bool>(
nameof(IsOpen));
public bool IsOpen
{
get => GetValue(IsOpenProperty);
set => SetValue(IsOpenProperty, value);
}
public static readonly StyledProperty<bool> IsCloseButtonVisibleProperty =
AvaloniaProperty.Register<DrawerControlBase, bool>(
nameof(IsCloseButtonVisible));
public bool IsCloseButtonVisible
{
get => GetValue(IsCloseButtonVisibleProperty);
set => SetValue(IsCloseButtonVisibleProperty, value);
}
public static readonly RoutedEvent<ResultEventArgs> ClosedEvent = RoutedEvent.Register<DrawerControlBase, ResultEventArgs>(
nameof(Closed), RoutingStrategies.Bubble);
public event EventHandler<ResultEventArgs> Closed
{
add => AddHandler(ClosedEvent, value);
remove => RemoveHandler(ClosedEvent, value);
}
static DrawerControlBase()
{
DataContextProperty.Changed.AddClassHandler<DrawerControlBase, object?>((o, e) => o.OnDataContextChange(e));
}
private void OnDataContextChange(AvaloniaPropertyChangedEventArgs<object?> args)
{
if(args.OldValue.Value is IDialogContext oldContext)
{
oldContext.RequestClose -= OnContextRequestClose;
}
if(args.NewValue.Value is IDialogContext newContext)
{
newContext.RequestClose += OnContextRequestClose;
}
}
private void OnContextRequestClose(object sender, object? e)
{
RaiseEvent(new ResultEventArgs(ClosedEvent, e));
}
public Task<T?> ShowAsync<T>(CancellationToken? token = default)
{
var tcs = new TaskCompletionSource<T?>();
token?.Register(() =>
{
Dispatcher.UIThread.Invoke(CloseDrawer);
});
void OnCloseHandler(object sender, ResultEventArgs args)
{
if (args.Result is T result)
{
tcs.SetResult(result);
}
else
{
tcs.SetResult(default(T));
}
RemoveHandler(ClosedEvent, OnCloseHandler);
}
AddHandler(ClosedEvent, OnCloseHandler);
return tcs.Task;
}
internal virtual void CloseDrawer()
{
RaiseEvent(new ResultEventArgs(ClosedEvent, null));
}
}