feat: add TryGetXXManager to WindowXXManager.
This commit is contained in:
@@ -1,8 +1,11 @@
|
||||
using System;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.VisualTree;
|
||||
using Ursa.Controls;
|
||||
using WindowNotificationManager = Ursa.Controls.WindowNotificationManager;
|
||||
|
||||
namespace Ursa.Demo.Dialogs;
|
||||
|
||||
@@ -25,6 +28,13 @@ public partial class CustomDemoDialog : UserControl
|
||||
_viewModel.NotificationManager = new WindowNotificationManager(visualLayerManager) { MaxItems = 3 };
|
||||
_viewModel.ToastManager = new WindowToastManager(visualLayerManager) { MaxItems = 3 };
|
||||
}
|
||||
|
||||
WindowNotificationManager.TryGetNotificationManager(visualLayerManager, out var manager);
|
||||
if (manager is not null && _viewModel is not null)
|
||||
{
|
||||
Console.WriteLine(ReferenceEquals(_viewModel.NotificationManager, manager));
|
||||
manager.Position = NotificationPosition.TopCenter;
|
||||
}
|
||||
}
|
||||
|
||||
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.LogicalTree;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Demo.ViewModels;
|
||||
|
||||
@@ -21,12 +20,11 @@ public partial class NotificationDemo : UserControl
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
var topLevel = TopLevel.GetTopLevel(this);
|
||||
_viewModel.NotificationManager = new WindowNotificationManager(topLevel) { MaxItems = 3 };
|
||||
}
|
||||
|
||||
protected override void OnDetachedFromLogicalTree(LogicalTreeAttachmentEventArgs e)
|
||||
WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager);
|
||||
if (manager is not null)
|
||||
{
|
||||
base.OnDetachedFromLogicalTree(e);
|
||||
_viewModel.NotificationManager?.Uninstall();
|
||||
_viewModel.NotificationManager = manager;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,16 +1,19 @@
|
||||
using System;
|
||||
using System.Collections.ObjectModel;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Styling;
|
||||
using CommunityToolkit.Mvvm.ComponentModel;
|
||||
using CommunityToolkit.Mvvm.Messaging;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Themes.Semi;
|
||||
using Notification = Ursa.Controls.Notification;
|
||||
using WindowNotificationManager = Ursa.Controls.WindowNotificationManager;
|
||||
|
||||
namespace Ursa.Demo.ViewModels;
|
||||
|
||||
public partial class MainViewViewModel : ViewModelBase
|
||||
{
|
||||
public WindowNotificationManager? NotificationManager { get; set; }
|
||||
public MenuViewModel Menus { get; set; } = new MenuViewModel();
|
||||
|
||||
private object? _content;
|
||||
@@ -107,6 +110,10 @@ public partial class MainViewViewModel : ViewModelBase
|
||||
if (app is not null)
|
||||
{
|
||||
app.RequestedThemeVariant = newValue.Theme;
|
||||
NotificationManager?.Show(
|
||||
new Notification("Theme changed", $"Theme changed to {newValue.Name}"),
|
||||
type: NotificationType.Success,
|
||||
classes: ["Light"]);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,28 @@
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Ursa.Controls;
|
||||
using Ursa.Demo.ViewModels;
|
||||
|
||||
namespace Ursa.Demo.Views;
|
||||
|
||||
public partial class MainView : UserControl
|
||||
{
|
||||
private MainViewViewModel? _viewModel;
|
||||
|
||||
public MainView()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override void OnAttachedToVisualTree(VisualTreeAttachmentEventArgs e)
|
||||
{
|
||||
base.OnAttachedToVisualTree(e);
|
||||
_viewModel = DataContext as MainViewViewModel;
|
||||
var topLevel = TopLevel.GetTopLevel(this);
|
||||
WindowNotificationManager.TryGetNotificationManager(topLevel, out var manager);
|
||||
if (manager is not null && _viewModel is not null)
|
||||
{
|
||||
_viewModel.NotificationManager = manager;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -5,9 +5,12 @@ namespace Ursa.Demo.Views;
|
||||
|
||||
public partial class MainWindow : UrsaWindow
|
||||
{
|
||||
public WindowNotificationManager? NotificationManager { get; set; }
|
||||
|
||||
public MainWindow()
|
||||
{
|
||||
InitializeComponent();
|
||||
NotificationManager = new WindowNotificationManager(this) { MaxItems = 3 };
|
||||
}
|
||||
|
||||
protected override async Task<bool> CanClose()
|
||||
|
||||
@@ -5,6 +5,7 @@ using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Layout;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -69,6 +70,12 @@ public class WindowNotificationManager : WindowMessageManager, INotificationMana
|
||||
VerticalAlignmentProperty.OverrideDefaultValue<WindowNotificationManager>(VerticalAlignment.Stretch);
|
||||
}
|
||||
|
||||
public static bool TryGetNotificationManager(Visual? visual, out WindowNotificationManager? manager)
|
||||
{
|
||||
manager = visual?.FindDescendantOfType<WindowNotificationManager>();
|
||||
return manager is not null;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Show(INotification content)
|
||||
{
|
||||
|
||||
@@ -84,6 +84,12 @@ public abstract class WindowMessageManager : TemplatedControl
|
||||
}
|
||||
}
|
||||
|
||||
public static bool TryGetMessageManager(Visual? visual, out WindowMessageManager? manager)
|
||||
{
|
||||
manager = visual?.FindDescendantOfType<WindowMessageManager>();
|
||||
return manager is not null;
|
||||
}
|
||||
|
||||
protected void TopLevelOnTemplateApplied(object? sender, TemplateAppliedEventArgs e)
|
||||
{
|
||||
if (Parent is AdornerLayer adornerLayer)
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia;
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Controls.Notifications;
|
||||
using Avalonia.Controls.Primitives;
|
||||
using Avalonia.Threading;
|
||||
using Avalonia.VisualTree;
|
||||
|
||||
namespace Ursa.Controls;
|
||||
|
||||
@@ -33,6 +35,12 @@ public class WindowToastManager : WindowMessageManager, IToastManager
|
||||
{
|
||||
}
|
||||
|
||||
public static bool TryGetToastManager(Visual? visual, out WindowToastManager? manager)
|
||||
{
|
||||
manager = visual?.FindDescendantOfType<WindowToastManager>();
|
||||
return manager is not null;
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public void Show(IToast content)
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user