# HG changeset patch # User StephaneLenclud # Date 1472568637 -7200 # Node ID d44943088c676f21a911deeafa0bcf2909c77aab # Parent 74a66917910ad389cbc73f8151b6d9498b45a91a Adding user given name to EAR events and enabling User Events. diff -r 74a66917910a -r d44943088c67 Server/Actions/ActionCecUserControlPressed.cs --- a/Server/Actions/ActionCecUserControlPressed.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/Actions/ActionCecUserControlPressed.cs Tue Aug 30 16:50:37 2016 +0200 @@ -46,7 +46,7 @@ /// public override string Brief() { - string brief = Name + ": " + Code.ToString() + " to " + DeviceName; + string brief = AttributeName + ": " + Code.ToString() + " to " + DeviceName; if (Wait) { brief += " (wait)"; diff -r 74a66917910a -r d44943088c67 Server/Actions/ActionCecUserControlReleased.cs --- a/Server/Actions/ActionCecUserControlReleased.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/Actions/ActionCecUserControlReleased.cs Tue Aug 30 16:50:37 2016 +0200 @@ -38,7 +38,7 @@ /// public override string Brief() { - string brief = Name + " to " + DeviceName; + string brief = AttributeName + " to " + DeviceName; if (Wait) { brief += " (wait)"; diff -r 74a66917910a -r d44943088c67 Server/Actions/ActionDisplayMessage.cs --- a/Server/Actions/ActionDisplayMessage.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/Actions/ActionDisplayMessage.cs Tue Aug 30 16:50:37 2016 +0200 @@ -52,7 +52,7 @@ /// public override string Brief() { - string brief = Name + ": " + PrimaryText; + string brief = AttributeName + ": " + PrimaryText; if (!string.IsNullOrEmpty(SecondaryText)) { brief += " - " + SecondaryText; diff -r 74a66917910a -r d44943088c67 Server/ConsumerElectronicControl.cs --- a/Server/ConsumerElectronicControl.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/ConsumerElectronicControl.cs Tue Aug 30 16:50:37 2016 +0200 @@ -82,14 +82,14 @@ { MonitorPowerOn = true; //Trigger corresponding event thus executing associated actions - Properties.Settings.Default.EarManager.TriggerEvent(); + Properties.Settings.Default.EarManager.TriggerEvents(); } private void OnMonitorPowerOff() { MonitorPowerOn = false; //Trigger corresponding event thus executing associated actions - Properties.Settings.Default.EarManager.TriggerEvent(); + Properties.Settings.Default.EarManager.TriggerEvents(); } /// diff -r 74a66917910a -r d44943088c67 Server/Events/EventHid.cs --- a/Server/Events/EventHid.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/Events/EventHid.cs Tue Aug 30 16:50:37 2016 +0200 @@ -83,7 +83,7 @@ /// public override string Brief() { - string brief = Name + ": "; + string brief = AttributeName + ": "; if (!IsValid()) { diff -r 74a66917910a -r d44943088c67 Server/FormEditObject.Designer.cs --- a/Server/FormEditObject.Designer.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/FormEditObject.Designer.cs Tue Aug 30 16:50:37 2016 +0200 @@ -52,7 +52,9 @@ this.iComboBoxObjectType.Sorted = true; this.iComboBoxObjectType.TabIndex = 18; this.iComboBoxObjectType.SelectedIndexChanged += new System.EventHandler(this.comboBoxActionType_SelectedIndexChanged); + this.iComboBoxObjectType.Enter += new System.EventHandler(this.iComboBoxObjectType_Enter); this.iComboBoxObjectType.KeyPress += new System.Windows.Forms.KeyPressEventHandler(this.iComboBoxObjectType_KeyPress); + this.iComboBoxObjectType.Leave += new System.EventHandler(this.iComboBoxObjectType_Leave); // // labelActionType // @@ -63,12 +65,12 @@ this.labelActionType.TabIndex = 20; this.labelActionType.Text = "Type :"; // - // buttonOk + // iButtonOk // this.iButtonOk.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Left))); this.iButtonOk.DialogResult = System.Windows.Forms.DialogResult.OK; this.iButtonOk.Location = new System.Drawing.Point(12, 157); - this.iButtonOk.Name = "buttonOk"; + this.iButtonOk.Name = "iButtonOk"; this.iButtonOk.Size = new System.Drawing.Size(75, 23); this.iButtonOk.TabIndex = 21; this.iButtonOk.Text = "Ok"; @@ -104,11 +106,11 @@ this.iTableLayoutPanel.Size = new System.Drawing.Size(312, 46); this.iTableLayoutPanel.TabIndex = 23; // - // buttonTest + // iButtonTest // this.iButtonTest.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Bottom | System.Windows.Forms.AnchorStyles.Right))); this.iButtonTest.Location = new System.Drawing.Point(252, 157); - this.iButtonTest.Name = "buttonTest"; + this.iButtonTest.Name = "iButtonTest"; this.iButtonTest.Size = new System.Drawing.Size(75, 23); this.iButtonTest.TabIndex = 24; this.iButtonTest.Text = "Test"; @@ -160,7 +162,7 @@ this.MaximizeBox = false; this.MinimizeBox = false; this.Name = "FormEditObject"; - this.Text = "Edit action"; + this.Text = "Edit object"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FormEditObject_FormClosing); this.Load += new System.EventHandler(this.FormEditAction_Load); this.ResumeLayout(false); diff -r 74a66917910a -r d44943088c67 Server/FormEditObject.cs --- a/Server/FormEditObject.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/FormEditObject.cs Tue Aug 30 16:50:37 2016 +0200 @@ -107,11 +107,18 @@ private void comboBoxActionType_SelectedIndexChanged(object sender, EventArgs e) { //Instantiate an action corresponding to our type - Type actionType = ((ItemObjectType) iComboBoxObjectType.SelectedItem).Type; + Type objectType = ((ItemObjectType) iComboBoxObjectType.SelectedItem).Type; //Create another type of action only if needed - if (Object == null || Object.GetType() != actionType) + if (Object == null || Object.GetType() != objectType) { - Object = (T)Activator.CreateInstance(actionType); + if (Object != null) + { + // Make sure we exit edit mode and unhook from events + Object.CurrentState = SharpLib.Ear.Object.State.Rest; + Object.PropertyChanged -= PropertyChangedEventHandlerThreadSafe; + Object = null; + } + Object = (T)Activator.CreateInstance(objectType); } //Create input fields @@ -553,7 +560,7 @@ iLabelBrief.Text = Object.Brief(); // Update object description - iLabelDescription.Text = Object.Description; + iLabelDescription.Text = Object.AttributeDescription; } /// @@ -570,5 +577,25 @@ e.Handled = true; } } + + private void iComboBoxObjectType_Enter(object sender, EventArgs e) + { + //Only edit HID event when our type combo box has the focus + // TODO: That's an ugly workaround, fix that somehow. Maybe by only doing HID scan when a button property has the focus + if (Object is EventHid) + { + Object.CurrentState = SharpLib.Ear.Object.State.Edit; + } + } + + private void iComboBoxObjectType_Leave(object sender, EventArgs e) + { + //Only edit HID event when our type combo box has the focus + // TODO: That's an ugly workaround, fix that somehow. Maybe by only doing HID scan when a button property has the focus + if (Object is EventHid) + { + Object.CurrentState = SharpLib.Ear.Object.State.Rest; + } + } } } diff -r 74a66917910a -r d44943088c67 Server/FormMain.Hid.cs --- a/Server/FormMain.Hid.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/FormMain.Hid.cs Tue Aug 30 16:50:37 2016 +0200 @@ -119,7 +119,7 @@ //Trigger corresponding EAR event if any EventHid e = new EventHid(); e.Copy(aHidEvent); - Properties.Settings.Default.EarManager.TriggerEvent(e); + Properties.Settings.Default.EarManager.TriggerEvents(e); } } diff -r 74a66917910a -r d44943088c67 Server/FormMain.cs --- a/Server/FormMain.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/Server/FormMain.cs Tue Aug 30 16:50:37 2016 +0200 @@ -233,7 +233,7 @@ ResetHarmonyAsync(); //Setup Events - PopulateEventsTreeView(); + PopulateTreeViewEvents(); //Setup notification icon SetupTrayIcon(); @@ -312,7 +312,7 @@ /// /// Populate tree view with events and actions /// - private void PopulateEventsTreeView() + private void PopulateTreeViewEvents() { //Disable action buttons buttonActionAdd.Enabled = false; @@ -328,7 +328,20 @@ foreach (Ear.Event e in Properties.Settings.Default.EarManager.Events) { //Create our event node - TreeNode eventNode = iTreeViewEvents.Nodes.Add(e.Brief()); + //Work out the name of our node + string eventNodeName = ""; + if (!string.IsNullOrEmpty(e.Name)) + { + //That event has a proper name, use it then + eventNodeName = $"{e.Name} - {e.Brief()}"; + } + else + { + //Unnamed events just use brief + eventNodeName = e.Brief(); + } + + TreeNode eventNode = iTreeViewEvents.Nodes.Add(eventNodeName); eventNode.Tag = e; //For easy access to our event if (!e.Enabled) { @@ -337,7 +350,7 @@ } //Add event description as child node - eventNode.Nodes.Add(e.Description).ForeColor = eventNode.ForeColor; + eventNode.Nodes.Add(e.AttributeDescription).ForeColor = eventNode.ForeColor; //Create child node for actions root TreeNode actionsNodes = eventNode.Nodes.Add("Actions"); actionsNodes.ForeColor = eventNode.ForeColor; @@ -2717,7 +2730,7 @@ { selectedEvent.Actions.Add(ea.Object); Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } } @@ -2747,7 +2760,7 @@ selectedEvent.Actions[actionIndex]=ea.Object; //Save and rebuild our event tree view Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } } @@ -2768,7 +2781,7 @@ Properties.Settings.Default.EarManager.RemoveAction(action); Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } /// @@ -2811,7 +2824,7 @@ //Save and populate our tree again Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } @@ -2840,7 +2853,7 @@ //Save and populate our tree again Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } @@ -2915,7 +2928,7 @@ { Properties.Settings.Default.EarManager.Events.Add(ea.Object); Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); SelectEvent(ea.Object); } } @@ -2936,7 +2949,7 @@ Properties.Settings.Default.EarManager.Events.Remove(currentEvent); Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } /// @@ -2966,7 +2979,7 @@ Properties.Settings.Default.EarManager.Events[index] = ea.Object; //Save and rebuild our event tree view Properties.Settings.Default.Save(); - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } } @@ -3043,7 +3056,7 @@ Program.HarmonyConfig = await Program.HarmonyClient.GetConfigAsync(); PopulateTreeViewHarmony(Program.HarmonyConfig); //Make sure harmony command actions are showing device name instead of device id - PopulateEventsTreeView(); + PopulateTreeViewEvents(); } /// diff -r 74a66917910a -r d44943088c67 SharpLibEar/ActionLaunchApp.cs --- a/SharpLibEar/ActionLaunchApp.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/SharpLibEar/ActionLaunchApp.cs Tue Aug 30 16:50:37 2016 +0200 @@ -46,7 +46,7 @@ public override string Brief() { - return Name + ": " + Path.GetFileName(File.FullPath); + return AttributeName + ": " + Path.GetFileName(File.FullPath); } public override bool IsValid() diff -r 74a66917910a -r d44943088c67 SharpLibEar/ActionOpticalDriveEject.cs --- a/SharpLibEar/ActionOpticalDriveEject.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/SharpLibEar/ActionOpticalDriveEject.cs Tue Aug 30 16:50:37 2016 +0200 @@ -38,7 +38,7 @@ public override string Brief() { - return Name + " " + Drive.CurrentItem ; + return AttributeName + " " + Drive.CurrentItem ; } public override bool IsValid() diff -r 74a66917910a -r d44943088c67 SharpLibEar/ActionSleep.cs --- a/SharpLibEar/ActionSleep.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/SharpLibEar/ActionSleep.cs Tue Aug 30 16:50:37 2016 +0200 @@ -39,7 +39,7 @@ public override string Brief() { - return Name + " for " + TimeoutInMilliseconds + " ms"; + return AttributeName + " for " + TimeoutInMilliseconds + " ms"; } diff -r 74a66917910a -r d44943088c67 SharpLibEar/Event.cs --- a/SharpLibEar/Event.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/SharpLibEar/Event.cs Tue Aug 30 16:50:37 2016 +0200 @@ -10,7 +10,8 @@ namespace SharpLib.Ear { [DataContract] - public abstract class Event : Object + [AttributeObject(Id = "Event", Name = "User Event", Description = "An event that can be triggered by users.")] + public class Event : Object { [DataMember] [AttributeObjectProperty @@ -23,6 +24,16 @@ public bool Enabled { get; set; } = true; [DataMember] + [AttributeObjectProperty + ( + Id = "Event.Name", + Name = "Name", + Description = "Given event name. Can be used to trigger it." + ) + ] + public string Name { get; set; } = ""; + + [DataMember] public List Actions = new List(); @@ -42,16 +53,16 @@ /// /// Allows testing from generic edit dialog. /// - public void Test() + public async void Test() { Trace.WriteLine("Event test"); - Trigger(); + await Trigger(); } public async Task Trigger() { - Trace.WriteLine("Event triggered: " + Name); + Trace.WriteLine("Event triggered: " + AttributeName); foreach (Action action in Actions) { await action.Execute(); diff -r 74a66917910a -r d44943088c67 SharpLibEar/Manager.cs --- a/SharpLibEar/Manager.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/SharpLibEar/Manager.cs Tue Aug 30 16:50:37 2016 +0200 @@ -48,12 +48,12 @@ /// Trigger the given event. /// /// - public void TriggerEvent() where T: class + public async void TriggerEvents() where T: class { //Only trigger enabled events matching the desired type foreach (Event e in Events.Where(e => e.GetType() == typeof(T) && e.Enabled)) { - e.Trigger(); + await e.Trigger(); } } @@ -61,15 +61,36 @@ /// Trigger the given event. /// /// - public void TriggerEvent(T aEvent) where T : class + public async void TriggerEvents(T aEvent) where T : class { //Only trigger events matching the desired type foreach (Event e in Events.Where(e => e.Equals(aEvent) && e.Enabled)) { - e.Trigger(); + await e.Trigger(); } } + /// + /// + /// + /// + /// + public async void TriggerEventsByName(string aName) + { + if (string.IsNullOrEmpty(aName)) + { + //Just don't do that that would be silly + return; + } + //Only trigger events matching the desired type + foreach (Event e in Events.Where(e => e.Name.Equals(aName))) + { + await e.Trigger(); + } + } + + + /// /// Remove the specified action from the event it belongs too. diff -r 74a66917910a -r d44943088c67 SharpLibEar/Object.cs --- a/SharpLibEar/Object.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/SharpLibEar/Object.cs Tue Aug 30 16:50:37 2016 +0200 @@ -73,7 +73,7 @@ /// /// Static object name. /// - public string Name + public string AttributeName { //Get the name of this object attribute get { return Utils.Reflection.GetAttribute(GetType()).Name; } @@ -83,7 +83,7 @@ /// /// Static object description. /// - public string Description + public string AttributeDescription { //Get the description of this object attribute get { return Utils.Reflection.GetAttribute(GetType()).Description; } @@ -96,7 +96,7 @@ /// public virtual string Brief() { - return Name; + return AttributeName; } /// diff -r 74a66917910a -r d44943088c67 SharpLibUtils/Reflection.cs --- a/SharpLibUtils/Reflection.cs Tue Aug 30 11:04:40 2016 +0200 +++ b/SharpLibUtils/Reflection.cs Tue Aug 30 16:50:37 2016 +0200 @@ -12,6 +12,7 @@ { /// /// Get a list of all the concrete types derived from the given type in all loaded assembly. + /// That includes the given type itself if it's intanciable. /// /// /// @@ -21,8 +22,8 @@ foreach (Assembly asm in AppDomain.CurrentDomain.GetAssemblies()) { - foreach (Type type in asm.GetTypes() - .Where(myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf(typeof(T)))) + foreach (Type type in asm.GetTypes() + .Where(myType => myType.IsClass && !myType.IsAbstract && (myType.IsSubclassOf(typeof(T)) || myType == typeof(T)))) { objects.Add(type); }