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