--- /dev/null
+// 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();
+ }
+ }
+ }
+}
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
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 {
/// <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()
{
}
///
public void ToggleAllCommand (bool canExecute) {
- foreach (Command c in Commands.OfType<Command> ())
+ foreach (ActionCommand c in Commands.OfType<ActionCommand> ())
c.CanExecute = canExecute;
}
}
/// </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;
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);
}
#endregion
+ object instance;
+ string memberName;
+ Action<bool> delSet;
+ Func<bool> delGet;
+ bool disposedValue;
+
/// <summary>
/// trigger the execution of the command
return;
delSet (value);
NotifyValueChanged ("IsToggled", IsToggled);
- Console.WriteLine ($"ToggleCommand.IsToggled => {value}");
+ //Console.WriteLine ($"ToggleCommand.IsToggled => {value}");
if (IsToggled)
ToggleOn.Raise (this, null);
}
#endregion
- bool disposedValue;
protected virtual void Dispose(bool disposing)
{
if (!disposedValue)
[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>
Widget moveHandle, sizingHandle;
- public Command CMDMinimize, CMDMaximize, CMDNormalize, CMDClose;
+ public ActionCommand CMDMinimize, CMDMaximize, CMDNormalize, CMDClose;
CommandGroup commands;
public CommandGroup Commands {
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);
}
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;
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;
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);
}