]> O.S.I.I.S - jp/crow.git/commitdiff
abstract Command class, ActionCommand and ToggleCommand derived from it
authorJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 14 Sep 2021 08:12:21 +0000 (08:12 +0000)
committerJean-Philippe Bruyère <jp_bruyere@hotmail.com>
Tue, 14 Sep 2021 08:12:21 +0000 (08:12 +0000)
Crow/src/Command/ActionCommand.cs [new file with mode: 0644]
Crow/src/Command/Command.cs
Crow/src/Command/CommandGroup.cs
Crow/src/Command/ToggleCommand.cs
Crow/src/Widgets/DockWindow.cs
Crow/src/Widgets/Window.cs
Samples/common/src/Extensions.cs
Samples/common/src/SampleBase.cs
Samples/common/src/SampleBaseForEditor.cs

diff --git a/Crow/src/Command/ActionCommand.cs b/Crow/src/Command/ActionCommand.cs
new file mode 100644 (file)
index 0000000..02bec99
--- /dev/null
@@ -0,0 +1,59 @@
+// Copyright (c) 2013-2021  Jean-Philippe Bruyère <jp_bruyere@hotmail.com>
+//
+// 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 {
+       /// <summary>
+       /// helper class to bind in one step icon, caption, action, and validity tests to a controls
+       /// </summary>
+       public class ActionCommand : Command
+       {
+
+               #region CTOR
+               public ActionCommand () {}
+               /// <summary>
+               /// Initializes a new instance of Command with the action passed as argument.
+               /// </summary>
+               /// <param name="_executeAction">action to excecute when command is triggered</param>
+               public ActionCommand (Action _executeAction) {
+                       execute = _executeAction;
+               }
+               /// <summary>
+               /// Initializes a new instance of Command with the action<object> passed as argument.
+               /// </summary>
+               /// <param name="_executeAction">action to excecute when command is triggered</param>
+               public ActionCommand (Action<object> _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<object> executeAction, string icon = null, bool _canExecute = true)
+                       : base (caption, icon, _canExecute)
+               {
+                       execute = executeAction;
+               }
+               #endregion
+
+               Delegate execute;
+
+               /// <summary>
+               /// trigger the execution of the command
+               /// </summary>
+               public override void Execute (object sender = null) {
+                       if (execute != null && CanExecute){
+                               Task task =     (execute is Action a) ?
+                                       task = new Task(a) :
+                               (execute is Action<object> o) ?
+                                       task = new Task(o, sender) : throw new Exception("Invalid Delegate type in Crow.ActionCommand, expecting Action or Action<object>");
+                               task.Start();
+                       }
+               }
+       }
+}
index 6b8851e5c607b98912ae65564030cd83d39ee4c7..f44fe5b35803fc3b634b234fdd2e9a0713063ded 100644 (file)
@@ -8,9 +8,9 @@ using System.Threading.Tasks;
 
 namespace Crow {
        /// <summary>
-       /// 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.
        /// </summary>
-       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)
                {}
-
-               /// <summary>
-               /// Initializes a new instance of Command with the action passed as argument.
-               /// </summary>
-               /// <param name="_executeAction">action to excecute when command is triggered</param>
-               public Command (Action _executeAction) {
-                       execute = _executeAction;
-               }
-               /// <summary>
-               /// Initializes a new instance of Command with the action<object> passed as argument.
-               /// </summary>
-               /// <param name="_executeAction">action to excecute when command is triggered</param>
-               public Command (Action<object> _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<object> executeAction, string icon = null, bool _canExecute = true)
-                       :base (caption, icon)
-               {
-                       execute = executeAction;
-                       canExecute = _canExecute;
-               }
-
                #endregion
 
-               Delegate execute;
-
                bool canExecute = true;
 
                /// <summary>
-               /// if true, action defined in this command may be executed,
+               /// if true, command may be executed,
                /// </summary>
                [DefaultValue(true)]
                public virtual bool CanExecute {
@@ -69,15 +39,7 @@ namespace Crow {
                /// <summary>
                /// trigger the execution of the command
                /// </summary>
-               public virtual void Execute (object sender = null){
-                       if (execute != null && CanExecute){
-                               Task task =     (execute is Action a) ?
-                                       task = new Task(a) :
-                               (execute is Action<object> o) ?
-                                       task = new Task(o, sender) : throw new Exception("Invalid Delegate type in Crow.Command, expecting Action or Action<object>");
-                               task.Start();
-                       }
-               }
+               public abstract void Execute (object sender = null);
 
                internal override void raiseAllValuesChanged()
                {
index afe13e6c5cb23c9b952cce2d284b37365aad60b2..d323ae85897d7e9dc29a04a1c15e900bb0cd939a 100644 (file)
@@ -63,7 +63,7 @@ namespace Crow {
                }
                ///
                public void ToggleAllCommand (bool canExecute) {
-                       foreach  (Command c in Commands.OfType<Command> ())
+                       foreach  (ActionCommand c in Commands.OfType<ActionCommand> ())
                                c.CanExecute = canExecute;
                }
        }
index 42a447685183531f9e0cfe619fed619feadf3a81..6182195ca31da94c8353add48496d11e77e2cb8a 100644 (file)
@@ -15,18 +15,12 @@ namespace Crow {
        /// </summary>
        public class ToggleCommand : Command, IToggle, IDisposable
        {
-               object instance;
-               string memberName;
-               Action<bool> delSet;
-               Func<bool> 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<bool> delSet;
+               Func<bool> delGet;
+               bool disposedValue;
+
 
                /// <summary>
                /// 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)
index 326bfce48ecf0c644b61da9b4864a2115fe8c168..51e898855d4f4e263216291ed5241c5ef038e93c 100644 (file)
@@ -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);
                }
 
                /// <summary>
index 636b1bb55c62f7115c31d4b27d59786db1916b64..c1bf5b8b01306137b9bd81468d12097c2c85530a 100644 (file)
@@ -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);
                }
 
index b4b0a1e585221342b112fa6c5f5dc49eca1ac790..7647dbc970f57e8d8b69eeaa91c31aeaf7955323 100644 (file)
@@ -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;
index 1c516bfa63f94d061c4e5bee02f2aeb01ac10d04..0622c2cff0060d8d39353195bec047294f34f8d7 100644 (file)
@@ -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;
index 6c6b60b869f04e52c3bfc9890bd4b307ebe35a01..47b931030387c6823e6352e82e6f26fe21ad82e5 100644 (file)
@@ -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);
                }