From: Jean-Philippe Bruyère Date: Tue, 14 Sep 2021 08:12:21 +0000 (+0000) Subject: abstract Command class, ActionCommand and ToggleCommand derived from it X-Git-Tag: v0.9.7-beta~10 X-Git-Url: https://git.osiis.dedyn.io/?a=commitdiff_plain;h=5dcfd372592d5d21193d1dc5a58c385b2e8a6a7c;p=jp%2Fcrow.git abstract Command class, ActionCommand and ToggleCommand derived from it --- diff --git a/Crow/src/Command/ActionCommand.cs b/Crow/src/Command/ActionCommand.cs new file mode 100644 index 00000000..02bec998 --- /dev/null +++ b/Crow/src/Command/ActionCommand.cs @@ -0,0 +1,59 @@ +// Copyright (c) 2013-2021 Jean-Philippe Bruyère +// +// This code is licensed under the MIT license (MIT) (http://opensource.org/licenses/MIT) + +using System; +using System.ComponentModel; +using System.Threading.Tasks; + +namespace Crow { + /// + /// helper class to bind in one step icon, caption, action, and validity tests to a controls + /// + public class ActionCommand : Command + { + + #region CTOR + public ActionCommand () {} + /// + /// Initializes a new instance of Command with the action passed as argument. + /// + /// action to excecute when command is triggered + public ActionCommand (Action _executeAction) { + execute = _executeAction; + } + /// + /// Initializes a new instance of Command with the action passed as argument. + /// + /// action to excecute when command is triggered + public ActionCommand (Action _executeAction) { + execute = _executeAction; + } + public ActionCommand (string caption, Action executeAction, string icon = null, bool _canExecute = true) + : base (caption, icon, _canExecute) + { + execute = executeAction; + } + public ActionCommand (string caption, Action executeAction, string icon = null, bool _canExecute = true) + : base (caption, icon, _canExecute) + { + execute = executeAction; + } + #endregion + + Delegate execute; + + /// + /// trigger the execution of the command + /// + public override void Execute (object sender = null) { + if (execute != null && CanExecute){ + Task task = (execute is Action a) ? + task = new Task(a) : + (execute is Action o) ? + task = new Task(o, sender) : throw new Exception("Invalid Delegate type in Crow.ActionCommand, expecting Action or Action"); + task.Start(); + } + } + } +} diff --git a/Crow/src/Command/Command.cs b/Crow/src/Command/Command.cs index 6b8851e5..f44fe5b3 100644 --- a/Crow/src/Command/Command.cs +++ b/Crow/src/Command/Command.cs @@ -8,9 +8,9 @@ using System.Threading.Tasks; namespace Crow { /// - /// helper class to bind in one step icon, caption, action, and validity tests to a controls + /// Base abstract class for commands with an execute method. /// - public class Command : CommandBase + public abstract class Command : CommandBase { #region CTOR @@ -18,42 +18,12 @@ namespace Crow { public Command (string caption, string icon = null, bool _canExecute = true) :base (caption, icon) {} - - /// - /// Initializes a new instance of Command with the action passed as argument. - /// - /// action to excecute when command is triggered - public Command (Action _executeAction) { - execute = _executeAction; - } - /// - /// Initializes a new instance of Command with the action passed as argument. - /// - /// action to excecute when command is triggered - public Command (Action _executeAction) { - execute = _executeAction; - } - public Command (string caption, Action executeAction, string icon = null, bool _canExecute = true) - :base (caption, icon) - { - execute = executeAction; - canExecute = _canExecute; - } - public Command (string caption, Action executeAction, string icon = null, bool _canExecute = true) - :base (caption, icon) - { - execute = executeAction; - canExecute = _canExecute; - } - #endregion - Delegate execute; - bool canExecute = true; /// - /// if true, action defined in this command may be executed, + /// if true, command may be executed, /// [DefaultValue(true)] public virtual bool CanExecute { @@ -69,15 +39,7 @@ namespace Crow { /// /// trigger the execution of the command /// - public virtual void Execute (object sender = null){ - if (execute != null && CanExecute){ - Task task = (execute is Action a) ? - task = new Task(a) : - (execute is Action o) ? - task = new Task(o, sender) : throw new Exception("Invalid Delegate type in Crow.Command, expecting Action or Action"); - task.Start(); - } - } + public abstract void Execute (object sender = null); internal override void raiseAllValuesChanged() { diff --git a/Crow/src/Command/CommandGroup.cs b/Crow/src/Command/CommandGroup.cs index afe13e6c..d323ae85 100644 --- a/Crow/src/Command/CommandGroup.cs +++ b/Crow/src/Command/CommandGroup.cs @@ -63,7 +63,7 @@ namespace Crow { } /// public void ToggleAllCommand (bool canExecute) { - foreach (Command c in Commands.OfType ()) + foreach (ActionCommand c in Commands.OfType ()) c.CanExecute = canExecute; } } diff --git a/Crow/src/Command/ToggleCommand.cs b/Crow/src/Command/ToggleCommand.cs index 42a44768..6182195c 100644 --- a/Crow/src/Command/ToggleCommand.cs +++ b/Crow/src/Command/ToggleCommand.cs @@ -15,18 +15,12 @@ namespace Crow { /// public class ToggleCommand : Command, IToggle, IDisposable { - object instance; - string memberName; - Action delSet; - Func delGet; - #region CTOR public ToggleCommand () {} public ToggleCommand (object instance, string memberName, string icon = null, bool _canExecute = true) - :this ("", instance, memberName, icon, _canExecute) { - } + : this ("", instance, memberName, icon, _canExecute) { } public ToggleCommand (string caption, object instance, string memberName, string icon = null, bool _canExecute = true) - :base (caption, icon, _canExecute) + : base (caption, icon, _canExecute) { this.instance = instance; this.memberName = memberName; @@ -65,7 +59,7 @@ namespace Crow { void instance_valueChanged (object sender, ValueChangeEventArgs e) { if (e.MemberName != memberName) return; - Console.WriteLine ($"ToggleCommand valueChanged triggered => {e.NewValue}"); + //Console.WriteLine ($"ToggleCommand valueChanged triggered => {e.NewValue}"); bool tog = (bool)e.NewValue; NotifyValueChanged ("IsToggled", tog); @@ -76,6 +70,12 @@ namespace Crow { } #endregion + object instance; + string memberName; + Action delSet; + Func delGet; + bool disposedValue; + /// /// trigger the execution of the command @@ -103,7 +103,7 @@ namespace Crow { return; delSet (value); NotifyValueChanged ("IsToggled", IsToggled); - Console.WriteLine ($"ToggleCommand.IsToggled => {value}"); + //Console.WriteLine ($"ToggleCommand.IsToggled => {value}"); if (IsToggled) ToggleOn.Raise (this, null); @@ -113,7 +113,6 @@ namespace Crow { } #endregion - bool disposedValue; protected virtual void Dispose(bool disposing) { if (!disposedValue) diff --git a/Crow/src/Widgets/DockWindow.cs b/Crow/src/Widgets/DockWindow.cs index 326bfce4..51e89885 100644 --- a/Crow/src/Widgets/DockWindow.cs +++ b/Crow/src/Widgets/DockWindow.cs @@ -48,11 +48,11 @@ namespace Crow [XmlIgnore] public bool IsDockedInTabView => LogicalParent is TabView; [XmlIgnore] public bool IsDockedInStack => Parent is DockStack; - public Command CMDFreezeDockState, CMDUnfreezeDockState; + public ActionCommand CMDFreezeDockState, CMDUnfreezeDockState; public CommandGroup DockCommands => new CommandGroup (CMDFreezeDockState, CMDUnfreezeDockState); void initCommands () { - CMDFreezeDockState = new Command ("Freeze Dock State", () => FreezeDockState = true, "#Crow.Icons.unpin.svg", !FreezeDockState); - CMDUnfreezeDockState = new Command ("Unfreeze Dock State", () => FreezeDockState = false, "#Crow.Icons.pin.svg", FreezeDockState); + CMDFreezeDockState = new ActionCommand ("Freeze Dock State", () => FreezeDockState = true, "#Crow.Icons.unpin.svg", !FreezeDockState); + CMDUnfreezeDockState = new ActionCommand ("Unfreeze Dock State", () => FreezeDockState = false, "#Crow.Icons.pin.svg", FreezeDockState); } /// diff --git a/Crow/src/Widgets/Window.cs b/Crow/src/Widgets/Window.cs index 636b1bb5..c1bf5b8b 100644 --- a/Crow/src/Widgets/Window.cs +++ b/Crow/src/Widgets/Window.cs @@ -143,7 +143,7 @@ namespace Crow Widget moveHandle, sizingHandle; - public Command CMDMinimize, CMDMaximize, CMDNormalize, CMDClose; + public ActionCommand CMDMinimize, CMDMaximize, CMDNormalize, CMDClose; CommandGroup commands; public CommandGroup Commands { @@ -168,10 +168,10 @@ namespace Crow void initCommands () { if (CMDMinimize != null) return; - CMDMinimize = new Command ("Minimize", () => CurrentState = Status.Minimized, "#Crow.Icons.minimize.svg", allowedStates.HasFlag (Status.Minimized)); - CMDMaximize = new Command ("Maximize", () => CurrentState = Status.Maximized, "#Crow.Icons.maximize.svg", allowedStates.HasFlag (Status.Maximized)); - CMDNormalize = new Command ("Normalize", () => CurrentState = Status.Normal, "#Crow.Icons.normalize.svg", false); - CMDClose = new Command ("Close", close, "#Crow.Icons.exit2.svg", true); + CMDMinimize = new ActionCommand ("Minimize", () => CurrentState = Status.Minimized, "#Crow.Icons.minimize.svg", allowedStates.HasFlag (Status.Minimized)); + CMDMaximize = new ActionCommand ("Maximize", () => CurrentState = Status.Maximized, "#Crow.Icons.maximize.svg", allowedStates.HasFlag (Status.Maximized)); + CMDNormalize = new ActionCommand ("Normalize", () => CurrentState = Status.Normal, "#Crow.Icons.normalize.svg", false); + CMDClose = new ActionCommand ("Close", close, "#Crow.Icons.exit2.svg", true); Commands = new CommandGroup(CMDMinimize, CMDNormalize, CMDMaximize, CMDClose); } diff --git a/Samples/common/src/Extensions.cs b/Samples/common/src/Extensions.cs index b4b0a1e5..7647dbc9 100644 --- a/Samples/common/src/Extensions.cs +++ b/Samples/common/src/Extensions.cs @@ -8,11 +8,11 @@ namespace Samples { public static CommandGroup GetCommands (this System.IO.DirectoryInfo di) => new CommandGroup( - new Command ("Set as root", ()=> {SampleBaseForEditor.CurrentProgramInstance.CurrentDir = di.FullName;}) - ); + new ActionCommand ("Set as root", ()=> {SampleBaseForEditor.CurrentProgramInstance.CurrentDir = di.FullName;}) + ); public static CommandGroup GetCommands (this System.IO.FileInfo fi) => new CommandGroup( - new Command ("Delete", (sender0) => { + new ActionCommand ("Delete", (sender0) => { MessageBox.ShowModal (SampleBaseForEditor.CurrentProgramInstance, MessageBox.Type.YesNo, $"Delete {fi.Name}?").Yes += (sender, e) => { System.IO.File.Delete(fi.FullName); Widget listContainer = ((sender0 as Widget).LogicalParent as Widget).DataSource as Widget; diff --git a/Samples/common/src/SampleBase.cs b/Samples/common/src/SampleBase.cs index 1c516bfa..0622c2cf 100644 --- a/Samples/common/src/SampleBase.cs +++ b/Samples/common/src/SampleBase.cs @@ -48,43 +48,43 @@ namespace Samples static void showMsgBox (object sender) { Widget w = sender as Widget; - Command cmd = w.DataSource as Command; + ActionCommand cmd = w.DataSource as ActionCommand; MessageBox.ShowModal(w.IFace, MessageBox.Type.Information, $"{cmd?.Caption} CLICKED"); } #region Test values for Binding public CommandGroup Commands, AllCommands; public CommandGroup EditCommands = new CommandGroup("Edit Commands", - new Command("Edit command 1", (sender) => showMsgBox (sender)), - new Command("Edit command 2 a bit longer", (sender) => showMsgBox (sender), null, false), - new Command("Edit command three", (sender) => showMsgBox (sender)), + new ActionCommand("Edit command 1", (sender) => showMsgBox (sender)), + new ActionCommand("Edit command 2 a bit longer", (sender) => showMsgBox (sender), null, false), + new ActionCommand("Edit command three", (sender) => showMsgBox (sender)), new CommandGroup("Subedit menu", - new Command("Subedit command 1", (sender) => showMsgBox (sender)), - new Command("Subedit command 2 a bit longer", (sender) => showMsgBox (sender), null, false), - new Command("Subedit command three", (sender) => showMsgBox (sender)) + new ActionCommand("Subedit command 1", (sender) => showMsgBox (sender)), + new ActionCommand("Subedit command 2 a bit longer", (sender) => showMsgBox (sender), null, false), + new ActionCommand("Subedit command three", (sender) => showMsgBox (sender)) ) ); public CommandGroup FileCommands = new CommandGroup("File Commands", - new Command("File command 1", (sender) => showMsgBox (sender), "#Icons.gavel.svg"), - new Command("File command 2 a bit longer", (sender) => showMsgBox (sender)), - new Command("File command three", (sender) => showMsgBox (sender)) + new ActionCommand("File command 1", (sender) => showMsgBox (sender), "#Icons.gavel.svg"), + new ActionCommand("File command 2 a bit longer", (sender) => showMsgBox (sender)), + new ActionCommand("File command three", (sender) => showMsgBox (sender)) ); - public Command SingleCommand => new Command("Single command 1", (sender) => showMsgBox (sender), "#Icons.gavel.svg"); + public ActionCommand SingleCommand => new ActionCommand("Single command 1", (sender) => showMsgBox (sender), "#Icons.gavel.svg"); public ToggleCommand CMDToggleBoolVal => new ToggleCommand ("Toggle", this, "BoolVal", "#Icons.gavel.svg", true); public ToggleCommand CMDToggleBoolValField => new ToggleCommand ("ToggleField", this, "boolVal", "#Icons.gavel.svg", true); void initCommands() { Commands = new CommandGroup("commands msg boxes", - new Command("Action 1", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 1 clicked")), - new Command("Action two", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 2 clicked"), null, false), - new Command("Action three", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 3 clicked")) + new ActionCommand("Action 1", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 1 clicked")), + new ActionCommand("Action two", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 2 clicked"), null, false), + new ActionCommand("Action three", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu 3 clicked")) ); AllCommands = new CommandGroup ("All Commands", FileCommands, EditCommands, new CommandGroup ("Combined commands", FileCommands, EditCommands), - new Command("Action A", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu A clicked")) + new ActionCommand("Action A", () => MessageBox.ShowModal(this, MessageBox.Type.Information, "context menu A clicked")) ); } public int intValue = 500; diff --git a/Samples/common/src/SampleBaseForEditor.cs b/Samples/common/src/SampleBaseForEditor.cs index 6c6b60b8..47b93103 100644 --- a/Samples/common/src/SampleBaseForEditor.cs +++ b/Samples/common/src/SampleBaseForEditor.cs @@ -129,20 +129,20 @@ namespace Samples public new bool IsDirty => source != origSource; - public Command CMDNew, CMDOpen, CMDSave, CMDSaveAs, CMDQuit, CMDShowLeftPane, + public ActionCommand CMDNew, CMDOpen, CMDSave, CMDSaveAs, CMDQuit, CMDShowLeftPane, CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDHelp, CMDAbout, CMDOptions; public CommandGroup EditorCommands => new CommandGroup (CMDUndo, CMDRedo, CMDCut, CMDCopy, CMDPaste, CMDSave, CMDSaveAs); protected virtual void initCommands () { - CMDNew = new Command ("New", new Action (onNewFile), "#Icons.blank-file.svg"); - CMDSave = new Command ("Save", new Action (onSave), "#Icons.save.svg", false); - CMDSaveAs = new Command ("Save As...", new Action (onSaveAs), "#Icons.save.svg"); - CMDQuit = new Command ("Quit", new Action (() => base.Quit ()), "#Icons.exit.svg"); - CMDUndo = new Command ("Undo", new Action (undo),"#Icons.undo.svg", false); - CMDRedo = new Command ("Redo", new Action (redo),"#Icons.redo.svg", false); - CMDCut = new Command ("Cut", new Action (cut), "#Icons.scissors.svg", false); - CMDCopy = new Command ("Copy", new Action (copy), "#Icons.copy-file.svg", false); - CMDPaste= new Command ("Paste", new Action (paste), "#Icons.paste-on-document.svg", false); + CMDNew = new ActionCommand ("New", new Action (onNewFile), "#Icons.blank-file.svg"); + CMDSave = new ActionCommand ("Save", new Action (onSave), "#Icons.save.svg", false); + CMDSaveAs = new ActionCommand ("Save As...", new Action (onSaveAs), "#Icons.save.svg"); + CMDQuit = new ActionCommand ("Quit", new Action (() => base.Quit ()), "#Icons.exit.svg"); + CMDUndo = new ActionCommand ("Undo", new Action (undo),"#Icons.undo.svg", false); + CMDRedo = new ActionCommand ("Redo", new Action (redo),"#Icons.redo.svg", false); + CMDCut = new ActionCommand ("Cut", new Action (cut), "#Icons.scissors.svg", false); + CMDCopy = new ActionCommand ("Copy", new Action (copy), "#Icons.copy-file.svg", false); + CMDPaste= new ActionCommand ("Paste", new Action (paste), "#Icons.paste-on-document.svg", false); }