96 lines
3.2 KiB
C#
96 lines
3.2 KiB
C#
using Lattice.UI.Docking.Abstractions;
|
||
|
||
namespace Lattice.UI.Docking.Services;
|
||
|
||
/// <summary>
|
||
/// Базовая реализация менеджера контекстных меню.
|
||
/// </summary>
|
||
public abstract class DockContextManagerBase : IDockContextManager
|
||
{
|
||
private readonly Dictionary<string, IDockCommand> _commands = new();
|
||
private IDockControl? _currentContextTarget;
|
||
|
||
/// <inheritdoc/>
|
||
public event EventHandler<ContextMenuShownEventArgs>? ContextMenuShown;
|
||
|
||
/// <inheritdoc/>
|
||
public event EventHandler? ContextMenuHidden;
|
||
|
||
/// <inheritdoc/>
|
||
public abstract void ShowContextMenu(IDockControl element, double x, double y);
|
||
|
||
/// <inheritdoc/>
|
||
public abstract void HideContextMenu();
|
||
|
||
/// <inheritdoc/>
|
||
public virtual void RegisterCommand(string commandId, IDockCommand command)
|
||
{
|
||
if (string.IsNullOrEmpty(commandId))
|
||
throw new ArgumentNullException(nameof(commandId));
|
||
|
||
_commands[commandId] = command ?? throw new ArgumentNullException(nameof(command));
|
||
}
|
||
|
||
/// <inheritdoc/>
|
||
public virtual void UnregisterCommand(string commandId)
|
||
{
|
||
_commands.Remove(commandId);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получает команду по идентификатору.
|
||
/// </summary>
|
||
public IDockCommand? GetCommand(string commandId)
|
||
{
|
||
_commands.TryGetValue(commandId, out var command);
|
||
return command;
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получает все доступные команды для указанного элемента.
|
||
/// </summary>
|
||
protected virtual IEnumerable<IDockCommand> GetCommandsForElement(IDockControl element)
|
||
{
|
||
// Фильтрация команд по типу элемента и его состоянию
|
||
return _commands.Values.Where(c => CanExecuteCommand(c, element));
|
||
}
|
||
|
||
/// <summary>
|
||
/// Проверяет, можно ли выполнить команду для указанного элемента.
|
||
/// </summary>
|
||
protected virtual bool CanExecuteCommand(IDockCommand command, IDockControl element)
|
||
{
|
||
return command.CanExecute(element);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Выполняет команду для указанного элемента.
|
||
/// </summary>
|
||
protected virtual void ExecuteCommand(IDockCommand command, IDockControl element)
|
||
{
|
||
command.Execute(element);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Вызывает событие показа контекстного меню.
|
||
/// </summary>
|
||
protected virtual void OnContextMenuShown(IDockControl target, double x, double y)
|
||
{
|
||
_currentContextTarget = target;
|
||
ContextMenuShown?.Invoke(this, new ContextMenuShownEventArgs(target, x, y));
|
||
}
|
||
|
||
/// <summary>
|
||
/// Вызывает событие скрытия контекстного меню.
|
||
/// </summary>
|
||
protected virtual void OnContextMenuHidden()
|
||
{
|
||
_currentContextTarget = null;
|
||
ContextMenuHidden?.Invoke(this, EventArgs.Empty);
|
||
}
|
||
|
||
/// <summary>
|
||
/// Получает текущий целевой элемент контекстного меню.
|
||
/// </summary>
|
||
protected IDockControl? CurrentContextTarget => _currentContextTarget;
|
||
} |