using Avalonia.Controls; using Avalonia.Input; using Mesen.Config; using Mesen.Localization; using Mesen.Utilities; using Mesen.ViewModels; using ReactiveUI; using ReactiveUI.Fody.Helpers; using System; using System.Collections.Generic; using System.Reactive; namespace Mesen.Debugger.Utilities { public class ContextMenuAction : ViewModelBase { public ActionType ActionType; public string? CustomText { get; set; } public string Name { get { if(ActionType == ActionType.Custom) { return CustomText ?? ""; } string label = ResourceHelper.GetEnumText(ActionType); if(HintText != null) { label += " (" + HintText() + ")"; } return label; } } public Image? Icon { get { IconFileAttribute? attr = ActionType.GetAttribute(); if(!string.IsNullOrEmpty(attr?.Icon)) { return ImageUtilities.FromAsset(attr.Icon); } else if(IsSelected?.Invoke() == true) { return ImageUtilities.FromAsset("Assets/MenuItemChecked.png"); } return null; } } List? _subActions; public List? SubActions { get => _subActions; set { _subActions = value; if(_subActions != null) { IsEnabled = () => { foreach(object subAction in _subActions) { if(subAction is ContextMenuAction act) { if(act.IsEnabled == null || act.IsEnabled()) { return true; } } } return false; }; } } } public Func? HintText { get; set; } public Func? IsEnabled { get; set; } public Func? IsSelected { get; set; } public Func? Shortcut { get; set; } public string ShortcutText => Shortcut?.Invoke().ToString() ?? ""; [Reactive] public bool Enabled { get; set; } private ReactiveCommand? _clickCommand; public ReactiveCommand? ClickCommand { get { Enabled = IsEnabled?.Invoke() ?? true; return _clickCommand; } } private Action _onClick = () => { }; public Action OnClick { get => _onClick; set { _onClick = () => { if(IsEnabled == null || IsEnabled()) { value(); } }; _clickCommand = ReactiveCommand.Create(_onClick, this.WhenAnyValue(x => x.Enabled)); } } public void Update() { Enabled = IsEnabled?.Invoke() ?? true; } } public enum ActionType { Custom, [IconFile("Copy")] Copy, [IconFile("Paste")] Paste, [IconFile("SelectAll")] SelectAll, [IconFile("EditLabel")] EditLabel, [IconFile("Add")] AddWatch, [IconFile("BreakpointEnableDisable")] EditBreakpoint, MarkSelectionAs, [IconFile("Accept")] MarkAsCode, [IconFile("CheatCode")] MarkAsData, [IconFile("Help")] MarkAsUnidentified, [IconFile("Add")] Add, [IconFile("Edit")] Edit, [IconFile("Close")] Delete, [IconFile("Breakpoint")] AddBreakpoint, [IconFile("MoveUp")] MoveUp, [IconFile("MoveDown")] MoveDown, WatchDecimalDisplay, WatchHexDisplay, WatchBinaryDisplay, RowDisplayFormat, RowFormatBinary, RowFormatHex8Bits, RowFormatHex16Bits, RowFormatHex24Bits, RowFormatSigned8Bits, RowFormatSigned16Bits, RowFormatSigned24Bits, RowFormatUnsigned, [IconFile("Close")] ClearFormat, [IconFile("Import")] Import, [IconFile("Export")] Export, ShowConsoleStatus, ShowBreakpointList, ShowWatchList, ShowLabelList, ShowCallStack, ShowSettingsPanel, ShowMemoryMappings, [IconFile("Settings")] Preferences, [IconFile("MediaPlay")] Continue, [IconFile("MediaPause")] Break, [IconFile("StepInto")] StepInto, [IconFile("StepOver")] StepOver, [IconFile("StepOut")] StepOut, [IconFile("StepBack")] StepBack, [IconFile("RunPpuCycle")] RunPpuCycle, [IconFile("RunPpuScanline")] RunPpuScanline, [IconFile("RunPpuFrame")] RunPpuFrame, BreakIn, BreakOn, [IconFile("Refresh")] Reset, [IconFile("PowerCycle")] PowerCycle, [IconFile("Script")] NewScript, [IconFile("MediaPlay")] RunScript, [IconFile("MediaStop")] StopScript, [IconFile("Folder")] Open, [IconFile("SaveFloppy")] Save, SaveAs, [IconFile("Exit")] Exit, [IconFile("Help")] HelpApiReference, RecentScripts, [IconFile("Refresh")] Refresh, EnableAutoRefresh, RefreshOnBreakPause, ZoomIn, ZoomOut, } }