diff options
| author | Jacob Palecki <[email protected]> | 2021-01-07 23:11:24 -0800 |
|---|---|---|
| committer | Jacob Palecki <[email protected]> | 2021-01-07 23:11:24 -0800 |
| commit | 8273ecf0c22876f0f4e9dfa9eb29b9d491614faa (patch) | |
| tree | 089cb8e024722781670433502e1452a59456cc6a | |
| parent | rough GUI integration of "Device Hardware ID" (diff) | |
| download | rawaccel-8273ecf0c22876f0f4e9dfa9eb29b9d491614faa.tar.xz rawaccel-8273ecf0c22876f0f4e9dfa9eb29b9d491614faa.zip | |
Refactor
| -rw-r--r-- | grapher/Models/AccelGUI.cs | 9 | ||||
| -rw-r--r-- | grapher/Models/AccelGUIFactory.cs | 11 | ||||
| -rw-r--r-- | grapher/Models/Devices/DeviceIDItem.cs | 73 | ||||
| -rw-r--r-- | grapher/Models/Devices/DeviceIDManager.cs | 90 | ||||
| -rw-r--r-- | grapher/Models/Devices/DeviceList.cs | 33 | ||||
| -rw-r--r-- | grapher/Models/Serialized/SettingsManager.cs | 73 |
6 files changed, 185 insertions, 104 deletions
diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs index 5c25813..9f97eed 100644 --- a/grapher/Models/AccelGUI.cs +++ b/grapher/Models/AccelGUI.cs @@ -1,4 +1,5 @@ using grapher.Models.Calculations; +using grapher.Models.Devices; using grapher.Models.Mouse; using grapher.Models.Options; using grapher.Models.Serialized; @@ -23,7 +24,8 @@ namespace grapher Button writeButton, ButtonBase toggleButton, MouseWatcher mouseWatcher, - ToolStripMenuItem scaleMenuItem) + ToolStripMenuItem scaleMenuItem, + DeviceIDManager deviceIDManager) { AccelForm = accelForm; AccelCalculator = accelCalculator; @@ -36,6 +38,7 @@ namespace grapher DefaultButtonFont = WriteButton.Font; SmallButtonFont = new Font(WriteButton.Font.Name, WriteButton.Font.Size * Constants.SmallButtonSizeFactor); MouseWatcher = mouseWatcher; + DeviceIDManager = deviceIDManager; ScaleMenuItem.Click += new System.EventHandler(OnScaleMenuItemClick); WriteButton.Click += new System.EventHandler(OnWriteButtonClick); @@ -95,6 +98,8 @@ namespace grapher public ToolStripMenuItem ScaleMenuItem { get; } + public DeviceIDManager DeviceIDManager { get; } + private Timer ChartRefresh { get; } private Font SmallButtonFont { get; } @@ -142,7 +147,7 @@ namespace grapher args = newArgs, minimumTime = driverSettings.minimumTime, directionalMultipliers = driverSettings.directionalMultipliers, - deviceHardwareID = driverSettings.deviceHardwareID + deviceHardwareID = DeviceIDManager.HWID, }; ButtonDelay(WriteButton); diff --git a/grapher/Models/AccelGUIFactory.cs b/grapher/Models/AccelGUIFactory.cs index 0e3646d..0669bf5 100644 --- a/grapher/Models/AccelGUIFactory.cs +++ b/grapher/Models/AccelGUIFactory.cs @@ -1,4 +1,5 @@ using grapher.Models.Calculations; +using grapher.Models.Devices; using grapher.Models.Mouse; using grapher.Models.Options; using grapher.Models.Serialized; @@ -325,17 +326,20 @@ namespace grapher.Models lockXYLabel, accelCharts); + var deviceIdManager = new DeviceIDManager(useSpecificDeviceMenuItem); + var settings = new SettingsManager( activeAccel, accelCalculator.DPI, accelCalculator.PollRate, autoWriteMenuItem, - useSpecificDeviceMenuItem, showLastMouseMoveMenuItem, - showVelocityGainToolStripMenuItem); + showVelocityGainToolStripMenuItem, + deviceIdManager); var mouseWatcher = new MouseWatcher(form, mouseLabel, accelCharts, settings); + return new AccelGUI( form, accelCalculator, @@ -345,7 +349,8 @@ namespace grapher.Models writeButton, toggleButton, mouseWatcher, - scaleMenuItem); + scaleMenuItem, + deviceIdManager); } #endregion Methods diff --git a/grapher/Models/Devices/DeviceIDItem.cs b/grapher/Models/Devices/DeviceIDItem.cs new file mode 100644 index 0000000..2bdff81 --- /dev/null +++ b/grapher/Models/Devices/DeviceIDItem.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Devices +{ + public class DeviceIDItem + { + public DeviceIDItem(string name, string hwid, DeviceIDManager manager) + { + Name = name; + HWID = hwid; + Manager = manager; + DeviceIDMenuItem = new ToolStripMenuItem(); + DeviceIDMenuItem.Checked = false; + DeviceIDMenuItem.Text = MenuItemText(); + DeviceIDMenuItem.Click += OnClicked; + manager.DeviceIDsMenuItem.DropDownItems.Add(DeviceIDMenuItem); + } + + private ToolStripMenuItem DeviceIDMenuItem { get; } + + public string Name { get; } + + public string HWID { get; } + + private DeviceIDManager Manager { get; } + + public void SetActivated() + { + DeviceIDMenuItem.Checked = true; + } + + public void SetDeactivated() + { + DeviceIDMenuItem.Checked = false; + } + + private string MenuItemText() => string.IsNullOrWhiteSpace(HWID) ? $"{Name}" : $"{Name}: {HWID}"; + + private string DisconnectedText() => $"Disconnected: {HWID}"; + + public void SetDisconnected() + { + DeviceIDMenuItem.ForeColor = Color.DarkGray; + DeviceIDMenuItem.Text = DisconnectedText(); + } + + public void OnClicked(object sender, EventArgs e) + { + Manager.SetActive(this); + } + + public override bool Equals(object obj) + { + return obj is DeviceIDItem item && + Name == item.Name && + HWID == item.HWID; + } + + public override int GetHashCode() + { + int hashCode = -1692744877; + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name); + hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(HWID); + return hashCode; + } + } +} diff --git a/grapher/Models/Devices/DeviceIDManager.cs b/grapher/Models/Devices/DeviceIDManager.cs new file mode 100644 index 0000000..00e7f9d --- /dev/null +++ b/grapher/Models/Devices/DeviceIDManager.cs @@ -0,0 +1,90 @@ +using System; +using System.Collections.Generic; +using System.Drawing; +using System.Linq; +using System.Management; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace grapher.Models.Devices +{ + public class DeviceIDManager + { + public DeviceIDManager(ToolStripMenuItem deviceIDs) + { + DeviceIDsMenuItem = deviceIDs; + } + + public ToolStripMenuItem DeviceIDsMenuItem { get; } + + public string HWID { get => SelectedDeviceID.HWID; } + + public DeviceIDItem SelectedDeviceID { get; private set; } + + public Dictionary<string, DeviceIDItem> DeviceIDs { get; private set; } + + public static IEnumerable<(string, string)> GetDeviceHardwareIDs(string PNPClass = "Mouse") + { + ManagementObjectSearcher searcher = new ManagementObjectSearcher(new SelectQuery("Win32_PnPEntity")); + + foreach (ManagementObject obj in searcher.Get()) + { + if (obj["PNPClass"] != null && obj["PNPClass"].ToString().Equals(PNPClass) && obj["HardwareID"] != null) + { + string[] hwidArray = (string[])(obj["HardwareID"]); + if (hwidArray.Length > 0) + { + string hwid = hwidArray[0].ToString(); + string name = obj["Name"].ToString(); + yield return (name, hwid); + } + } + } + } + + public void SetActive(DeviceIDItem deviceIDItem) + { + if (SelectedDeviceID != null) + { + SelectedDeviceID.SetDeactivated(); + } + + SelectedDeviceID = deviceIDItem; + SelectedDeviceID.SetActivated(); + } + + public void OnStartup(string hwid) + { + var nonEmptyHwid = !string.IsNullOrWhiteSpace(hwid); + + DeviceIDsMenuItem.Checked = nonEmptyHwid; + DeviceIDsMenuItem.DropDownItems.Clear(); + var anyDevice = new DeviceIDItem("Any", string.Empty, this); + if (!nonEmptyHwid) + { + SetActive(anyDevice); + } + + bool found = false; + + foreach (var device in GetDeviceHardwareIDs()) + { + var deviceItem = new DeviceIDItem(device.Item1, device.Item2, this); + if (deviceItem.HWID.Equals(hwid)) + { + found = true; + deviceItem.SetActivated(); + SelectedDeviceID = deviceItem; + } + } + + if (nonEmptyHwid && !found) + { + var deviceItem = new DeviceIDItem(string.Empty, hwid, this); + deviceItem.SetDisconnected(); + } + } + + } +} diff --git a/grapher/Models/Devices/DeviceList.cs b/grapher/Models/Devices/DeviceList.cs deleted file mode 100644 index 9f2b81d..0000000 --- a/grapher/Models/Devices/DeviceList.cs +++ /dev/null @@ -1,33 +0,0 @@ -using System; -using System.Management; -using System.Collections.Generic; - -namespace grapher.Models.Devices -{ - class DeviceList - { - public static List<Tuple<string, string>> GetDeviceHardwareIDs(string PNPClass = "Mouse") - { - var results = new List<Tuple<string, string>>(); - - ManagementObjectSearcher searcher = new ManagementObjectSearcher(new SelectQuery("Win32_PnPEntity")); - - foreach (ManagementObject obj in searcher.Get()) - { - if (obj["PNPClass"] != null && obj["PNPClass"].ToString() == PNPClass && obj["HardwareID"] != null) - { - String[] hwidArray = (String[])(obj["HardwareID"]); - if (hwidArray.Length > 0) - { - String hwid = hwidArray[0].ToString(); - String name = obj["Name"].ToString(); - results.Add(Tuple.Create(name, hwid)); - } - } - } - - return results; - } - - } -} diff --git a/grapher/Models/Serialized/SettingsManager.cs b/grapher/Models/Serialized/SettingsManager.cs index 3adbc8b..4dbf7bf 100644 --- a/grapher/Models/Serialized/SettingsManager.cs +++ b/grapher/Models/Serialized/SettingsManager.cs @@ -4,6 +4,7 @@ using System.Windows.Forms; using System.Threading; using System.Text; using System.Drawing; +using grapher.Models.Devices; namespace grapher.Models.Serialized { @@ -16,17 +17,17 @@ namespace grapher.Models.Serialized Field dpiField, Field pollRateField, ToolStripMenuItem autoWrite, - ToolStripMenuItem useSpecificDevice, ToolStripMenuItem showLastMouseMove, - ToolStripMenuItem showVelocityAndGain) + ToolStripMenuItem showVelocityAndGain, + DeviceIDManager deviceIDManager) { ActiveAccel = activeAccel; DpiField = dpiField; PollRateField = pollRateField; AutoWriteMenuItem = autoWrite; - UseSpecificDeviceMenuItem = useSpecificDevice; ShowLastMouseMoveMenuItem = showLastMouseMove; ShowVelocityAndGainMoveMenuItem = showVelocityAndGain; + DeviceIDManager = deviceIDManager; } #endregion Constructors @@ -43,12 +44,12 @@ namespace grapher.Models.Serialized private ToolStripMenuItem AutoWriteMenuItem { get; set; } - private ToolStripMenuItem UseSpecificDeviceMenuItem { get; set; } - private ToolStripMenuItem ShowLastMouseMoveMenuItem { get; set; } private ToolStripMenuItem ShowVelocityAndGainMoveMenuItem { get; set; } + private DeviceIDManager DeviceIDManager { get; } + #endregion Properties #region Methods @@ -66,66 +67,6 @@ namespace grapher.Models.Serialized return errors; } - private void SpecificDeviceClickHnadler(Object o, EventArgs a, string hwid) - { - var item = (ToolStripMenuItem)o; - foreach (ToolStripMenuItem i in UseSpecificDeviceMenuItem.DropDownItems) - { - i.Checked = false; - } - item.Checked = true; - if (hwid == null || hwid == "") - { - UseSpecificDeviceMenuItem.Checked = false; - } else - { - UseSpecificDeviceMenuItem.Checked = true; - } - RawAccelSettings.AccelerationSettings.deviceHardwareID = hwid; - - TryUpdateActiveSettings(RawAccelSettings.AccelerationSettings); - - } - private void UpdateUseSpecificDeviceMenu() - { - var hwid = RawAccelSettings.AccelerationSettings.deviceHardwareID; - if (hwid == null) { hwid = ""; } - - UseSpecificDeviceMenuItem.Checked = hwid.Length > 0; - UseSpecificDeviceMenuItem.DropDownItems.Clear(); - - var any_device = new ToolStripMenuItem(); - any_device.Text = ""; - any_device.Checked = hwid.Length == 0; - any_device.Click += new EventHandler(delegate(Object o, EventArgs a) { SpecificDeviceClickHnadler(o, a, ""); }); - UseSpecificDeviceMenuItem.DropDownItems.Add(any_device); - - var hwid_not_found = true; - - foreach (Tuple<string,string> device in Models.Devices.DeviceList.GetDeviceHardwareIDs()) - { - if (hwid == device.Item2) - { - hwid_not_found = false; - } - var dev = new ToolStripMenuItem(); - dev.Text = device.Item1; - dev.Checked = device.Item2 == RawAccelSettings.AccelerationSettings.deviceHardwareID; - dev.Click += new EventHandler(delegate (Object o, EventArgs a) { SpecificDeviceClickHnadler(o, a, device.Item2); }); - UseSpecificDeviceMenuItem.DropDownItems.Add(dev); - } - - if (hwid.Length > 0 && hwid_not_found) - { - var current_hwid = new ToolStripMenuItem(); - current_hwid.Text = "Disconnected (" + hwid + ")"; - current_hwid.ForeColor = Color.DarkGray; - current_hwid.Checked = true; - current_hwid.Click += new EventHandler(delegate (Object o, EventArgs a) { SpecificDeviceClickHnadler(o, a, hwid); }); - UseSpecificDeviceMenuItem.DropDownItems.Add(current_hwid); - } - } - public void UpdateFieldsFromGUISettings() { DpiField.SetToEntered(RawAccelSettings.GUISettings.DPI); @@ -133,6 +74,7 @@ namespace grapher.Models.Serialized ShowLastMouseMoveMenuItem.Checked = RawAccelSettings.GUISettings.ShowLastMouseMove; ShowVelocityAndGainMoveMenuItem.Checked = RawAccelSettings.GUISettings.ShowVelocityAndGain; AutoWriteMenuItem.Checked = RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup; + DeviceIDManager.OnStartup(RawAccelSettings.AccelerationSettings.deviceHardwareID); } public SettingsErrors TryUpdateAccel(DriverSettings settings) @@ -175,7 +117,6 @@ namespace grapher.Models.Serialized { RawAccelSettings = RawAccelSettings.Load(() => MakeGUISettingsFromFields()); UpdateFieldsFromGUISettings(); - UpdateUseSpecificDeviceMenu(); if (RawAccelSettings.GUISettings.AutoWriteToDriverOnStartup) { TryUpdateAccel(RawAccelSettings.AccelerationSettings); |