Доработан winui
This commit is contained in:
@@ -4,6 +4,9 @@ using Microsoft.UI.Xaml;
|
||||
using Microsoft.UI.Xaml.Controls;
|
||||
using System;
|
||||
using System.Collections.Concurrent;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Windows.Input;
|
||||
|
||||
namespace Lattice.UI.Docking.WinUI.Services;
|
||||
|
||||
@@ -21,9 +24,38 @@ public sealed class WinUIDockContextManager : DockContextManagerBase, IDisposabl
|
||||
/// </summary>
|
||||
public WinUIDockContextManager()
|
||||
{
|
||||
// Регистрируем стандартные команды
|
||||
RegisterDefaultCommands();
|
||||
}
|
||||
|
||||
private void RegisterDefaultCommands()
|
||||
{
|
||||
// Пример регистрации стандартных команд
|
||||
RegisterCommand("Close", new DockCommand("Close", "Close", "Close the selected content", () => "", () => true, OnCloseCommand));
|
||||
RegisterCommand("Float", new DockCommand("Float", "Float", "Float the window", () => "", () => true, OnFloatCommand));
|
||||
RegisterCommand("Dock", new DockCommand("Dock", "Dock", "Dock the window", () => "", () => true, OnDockCommand));
|
||||
}
|
||||
|
||||
private void OnCloseCommand()
|
||||
{
|
||||
if (_currentContextTarget is Lattice.UI.LatticeDockLeaf leafControl && leafControl.ActiveContent != null)
|
||||
{
|
||||
leafControl.CloseContent(leafControl.ActiveContent);
|
||||
}
|
||||
}
|
||||
|
||||
private void OnFloatCommand()
|
||||
{
|
||||
// TODO: Реализовать плавающее окно
|
||||
System.Diagnostics.Debug.WriteLine("Float command triggered");
|
||||
}
|
||||
|
||||
private void OnDockCommand()
|
||||
{
|
||||
// TODO: Реализовать закрепление окна
|
||||
System.Diagnostics.Debug.WriteLine("Dock command triggered");
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void ShowContextMenu(IDockControl element, double x, double y)
|
||||
{
|
||||
if (element is not FrameworkElement uiElement) return;
|
||||
@@ -39,13 +71,26 @@ public sealed class WinUIDockContextManager : DockContextManagerBase, IDisposabl
|
||||
var item = new MenuFlyoutItem
|
||||
{
|
||||
Text = command.Name,
|
||||
Command = new RelayCommand(() => ExecuteCommand(command, element))
|
||||
Tag = command,
|
||||
Command = new RelayCommand(() => ExecuteCommand(command, element),
|
||||
() => command.CanExecute(element))
|
||||
};
|
||||
|
||||
// Добавляем иконку, если есть
|
||||
// Устанавливаем иконку, если есть
|
||||
if (!string.IsNullOrEmpty(command.Icon))
|
||||
{
|
||||
// TODO: Добавить иконку команды
|
||||
var icon = new FontIcon
|
||||
{
|
||||
Glyph = command.Icon,
|
||||
FontSize = 12
|
||||
};
|
||||
item.Icon = icon;
|
||||
}
|
||||
|
||||
// Добавляем подсказку, если есть описание
|
||||
if (!string.IsNullOrEmpty(command.Description))
|
||||
{
|
||||
ToolTipService.SetToolTip(item, command.Description);
|
||||
}
|
||||
|
||||
flyout.Items.Add(item);
|
||||
@@ -68,7 +113,6 @@ public sealed class WinUIDockContextManager : DockContextManagerBase, IDisposabl
|
||||
OnContextMenuShown(element, x, y);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
public override void HideContextMenu()
|
||||
{
|
||||
if (_currentFlyout != null)
|
||||
@@ -84,30 +128,93 @@ public sealed class WinUIDockContextManager : DockContextManagerBase, IDisposabl
|
||||
}
|
||||
}
|
||||
|
||||
public override void RegisterCommand(string commandId, IDockCommand command)
|
||||
{
|
||||
if (string.IsNullOrEmpty(commandId))
|
||||
throw new ArgumentNullException(nameof(commandId));
|
||||
|
||||
_commands[commandId] = command ?? throw new ArgumentNullException(nameof(command));
|
||||
}
|
||||
|
||||
public override void UnregisterCommand(string commandId)
|
||||
{
|
||||
_commands.TryRemove(commandId, out _);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Класс-заглушка для реализации ICommand.
|
||||
/// Получает команду по идентификатору.
|
||||
/// </summary>
|
||||
private sealed class RelayCommand : System.Windows.Input.ICommand
|
||||
protected override IDockCommand? GetCommand(string commandId)
|
||||
{
|
||||
_commands.TryGetValue(commandId, out var command);
|
||||
return command;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Получает все доступные команды для указанного элемента.
|
||||
/// </summary>
|
||||
protected override IEnumerable<IDockCommand> GetCommandsForElement(IDockControl element)
|
||||
{
|
||||
return _commands.Values.Where(c => CanExecuteCommand(c, element));
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Класс для реализации ICommand.
|
||||
/// </summary>
|
||||
private sealed class RelayCommand : ICommand
|
||||
{
|
||||
private readonly Action _execute;
|
||||
private readonly Func<bool>? _canExecute;
|
||||
private readonly Func<bool> _canExecute;
|
||||
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
public RelayCommand(Action execute, Func<bool>? canExecute = null)
|
||||
public RelayCommand(Action execute, Func<bool> canExecute)
|
||||
{
|
||||
_execute = execute ?? throw new ArgumentNullException(nameof(execute));
|
||||
_canExecute = canExecute;
|
||||
}
|
||||
|
||||
public bool CanExecute(object? parameter) => _canExecute?.Invoke() ?? true;
|
||||
public bool CanExecute(object? parameter) => _canExecute();
|
||||
|
||||
public void Execute(object? parameter) => _execute();
|
||||
|
||||
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
/// <inheritdoc/>
|
||||
/// <summary>
|
||||
/// Базовая реализация команды докинга.
|
||||
/// </summary>
|
||||
private class DockCommand : IDockCommand
|
||||
{
|
||||
public string Id { get; }
|
||||
public string Name { get; }
|
||||
public string Description { get; }
|
||||
private readonly Func<string> _getIcon;
|
||||
private readonly Func<bool> _canExecute;
|
||||
private readonly Action _execute;
|
||||
|
||||
public DockCommand(string id, string name, string description, Func<string> getIcon, Func<bool> canExecute, Action execute)
|
||||
{
|
||||
Id = id;
|
||||
Name = name;
|
||||
Description = description;
|
||||
_getIcon = getIcon;
|
||||
_canExecute = canExecute;
|
||||
_execute = execute;
|
||||
}
|
||||
|
||||
public string Icon => _getIcon();
|
||||
public string Shortcut => "";
|
||||
|
||||
public bool CanExecute(object? parameter) => _canExecute();
|
||||
|
||||
public void Execute(object? parameter) => _execute();
|
||||
|
||||
public event EventHandler? CanExecuteChanged;
|
||||
|
||||
public void RaiseCanExecuteChanged() => CanExecuteChanged?.Invoke(this, EventArgs.Empty);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
HideContextMenu();
|
||||
|
||||
Reference in New Issue
Block a user