summaryrefslogtreecommitdiff
path: root/grapher/Models
diff options
context:
space:
mode:
authora1xd <[email protected]>2021-09-15 06:41:57 -0400
committera1xd <[email protected]>2021-09-23 22:34:51 -0400
commit7c0305ae2c99c191baf61eb920025826057d5753 (patch)
tree6a394d4ac8d69436f7bd8f1255f1d4fd3439280c /grapher/Models
parentfix potential leaks (diff)
downloadrawaccel-7c0305ae2c99c191baf61eb920025826057d5753.tar.xz
rawaccel-7c0305ae2c99c191baf61eb920025826057d5753.zip
add device menu
Diffstat (limited to 'grapher/Models')
-rw-r--r--grapher/Models/AccelGUI.cs23
-rw-r--r--grapher/Models/AccelGUIFactory.cs6
-rw-r--r--grapher/Models/Devices/DeviceDialogItem.cs26
-rw-r--r--grapher/Models/Devices/DeviceIDItem.cs73
-rw-r--r--grapher/Models/Devices/DeviceIDManager.cs68
-rw-r--r--grapher/Models/Serialized/SettingsManager.cs67
6 files changed, 107 insertions, 156 deletions
diff --git a/grapher/Models/AccelGUI.cs b/grapher/Models/AccelGUI.cs
index 1836b65..51e31a6 100644
--- a/grapher/Models/AccelGUI.cs
+++ b/grapher/Models/AccelGUI.cs
@@ -4,6 +4,7 @@ using grapher.Models.Mouse;
using grapher.Models.Options;
using grapher.Models.Serialized;
using System;
+using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;
using System.Windows.Forms.DataVisualization.Charting;
@@ -24,7 +25,8 @@ namespace grapher
Button writeButton,
ButtonBase resetButton,
MouseWatcher mouseWatcher,
- ToolStripMenuItem scaleMenuItem)
+ ToolStripMenuItem scaleMenuItem,
+ ToolStripMenuItem deviceMenuItem)
{
AccelForm = accelForm;
AccelCalculator = accelCalculator;
@@ -33,11 +35,13 @@ namespace grapher
WriteButton = writeButton;
ResetButton = (CheckBox)resetButton;
ScaleMenuItem = scaleMenuItem;
+ DeviceMenuItem = deviceMenuItem;
Settings = settings;
DefaultButtonFont = WriteButton.Font;
SmallButtonFont = new Font(WriteButton.Font.Name, WriteButton.Font.Size * Constants.SmallButtonSizeFactor);
MouseWatcher = mouseWatcher;
+ DeviceMenuItem.Click += DeviceMenuItemClick;
ScaleMenuItem.Click += new System.EventHandler(OnScaleMenuItemClick);
WriteButton.Click += new System.EventHandler(OnWriteButtonClick);
ResetButton.Click += new System.EventHandler(ResetDriverEventHandler);
@@ -83,6 +87,8 @@ namespace grapher
public ToolStripMenuItem ScaleMenuItem { get; }
+ public ToolStripMenuItem DeviceMenuItem { get; }
+
private Timer ChartRefresh { get; }
private Font SmallButtonFont { get; }
@@ -146,6 +152,7 @@ namespace grapher
else
{
RefreshActive();
+ Settings.SetActiveHandles();
return;
}
}
@@ -240,10 +247,12 @@ namespace grapher
{
ButtonTimer.Stop();
SetButtonDefaults();
+ DeviceMenuItem.Enabled = true;
}
private void StartButtonTimer()
{
+ DeviceMenuItem.Enabled = false;
ButtonTimer.Interval = ButtonTimerInterval;
ButtonTimer.Start();
}
@@ -268,6 +277,18 @@ namespace grapher
MouseWatcher.UpdateLastMove();
}
+ private void DeviceMenuItemClick(object sender, EventArgs e)
+ {
+ using (var devMenu = new DeviceMenuForm(Settings))
+ {
+ if (devMenu.ShowDialog() == DialogResult.OK)
+ {
+ Settings.Submit(devMenu.defaultConfig, devMenu.Items);
+ UpdateActiveSettingsFromFields();
+ }
+ }
+ }
+
#endregion Methods
}
diff --git a/grapher/Models/AccelGUIFactory.cs b/grapher/Models/AccelGUIFactory.cs
index 4d0a483..91a649a 100644
--- a/grapher/Models/AccelGUIFactory.cs
+++ b/grapher/Models/AccelGUIFactory.cs
@@ -639,8 +639,7 @@ namespace grapher.Models
autoWriteMenuItem,
showLastMouseMoveMenuItem,
showVelocityGainToolStripMenuItem,
- streamingModeToolStripMenuItem,
- deviceMenuItem);
+ streamingModeToolStripMenuItem);
var mouseWatcher = new MouseWatcher(form, mouseLabel, accelCharts, settings);
@@ -653,7 +652,8 @@ namespace grapher.Models
writeButton,
toggleButton,
mouseWatcher,
- scaleMenuItem);
+ scaleMenuItem,
+ deviceMenuItem);
}
#endregion Methods
diff --git a/grapher/Models/Devices/DeviceDialogItem.cs b/grapher/Models/Devices/DeviceDialogItem.cs
new file mode 100644
index 0000000..9ca5528
--- /dev/null
+++ b/grapher/Models/Devices/DeviceDialogItem.cs
@@ -0,0 +1,26 @@
+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 DeviceDialogItem
+ {
+ public MultiHandleDevice device;
+ public DeviceSettings oldSettings;
+ public DeviceConfig newConfig;
+ public string newProfile;
+ public bool overrideDefaultConfig;
+
+ public override string ToString()
+ {
+ return string.IsNullOrWhiteSpace(device.name) ?
+ device.id :
+ device.name;
+ }
+ }
+}
diff --git a/grapher/Models/Devices/DeviceIDItem.cs b/grapher/Models/Devices/DeviceIDItem.cs
deleted file mode 100644
index 8f1587b..0000000
--- a/grapher/Models/Devices/DeviceIDItem.cs
+++ /dev/null
@@ -1,73 +0,0 @@
-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 id, DeviceIDManager manager)
- {
- Name = name;
- ID = id;
- 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 ID { get; }
-
- private DeviceIDManager Manager { get; }
-
- public void SetActivated()
- {
- DeviceIDMenuItem.Checked = true;
- }
-
- public void SetDeactivated()
- {
- DeviceIDMenuItem.Checked = false;
- }
-
- private string MenuItemText() => string.IsNullOrEmpty(ID) ? $"{Name}" : ID.Replace("&", "&&");
-
- private string DisconnectedText() => $"Disconnected: {ID}";
-
- 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 &&
- ID == item.ID;
- }
-
- public override int GetHashCode()
- {
- int hashCode = -1692744877;
- hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(Name);
- hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(ID);
- return hashCode;
- }
- }
-}
diff --git a/grapher/Models/Devices/DeviceIDManager.cs b/grapher/Models/Devices/DeviceIDManager.cs
deleted file mode 100644
index e0ee686..0000000
--- a/grapher/Models/Devices/DeviceIDManager.cs
+++ /dev/null
@@ -1,68 +0,0 @@
-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;
- DeviceIDsMenuItem.Checked = false;
- }
-
- public ToolStripMenuItem DeviceIDsMenuItem { get; }
-
- public string ID { get => SelectedDeviceID.ID; }
-
- public DeviceIDItem SelectedDeviceID { get; private set; }
-
- public Dictionary<string, DeviceIDItem> DeviceIDs { get; private set; }
-
- public void SetActive(DeviceIDItem deviceIDItem)
- {
- if (SelectedDeviceID != null)
- {
- SelectedDeviceID.SetDeactivated();
- }
-
- SelectedDeviceID = deviceIDItem;
- SelectedDeviceID.SetActivated();
- }
-
- public void Update(string devID)
- {
- DeviceIDsMenuItem.DropDownItems.Clear();
-
- bool found = string.IsNullOrEmpty(devID);
-
- var anyDevice = new DeviceIDItem("Any", string.Empty, this);
-
- if (found) SetActive(anyDevice);
-
-/* foreach (string id in RawInputInterop.GetDeviceIDs())
- {
- var deviceItem = new DeviceIDItem(string.Empty, id, this);
- if (!found && deviceItem.ID.Equals(devID))
- {
- SetActive(deviceItem);
- found = true;
- }
- }*/
-
- if (!found)
- {
- var deviceItem = new DeviceIDItem(string.Empty, devID, this);
- deviceItem.SetDisconnected();
- SetActive(deviceItem);
- }
- }
-
- }
-}
diff --git a/grapher/Models/Serialized/SettingsManager.cs b/grapher/Models/Serialized/SettingsManager.cs
index 3789c05..8c8bf15 100644
--- a/grapher/Models/Serialized/SettingsManager.cs
+++ b/grapher/Models/Serialized/SettingsManager.cs
@@ -21,8 +21,7 @@ namespace grapher.Models.Serialized
ToolStripMenuItem autoWrite,
ToolStripMenuItem showLastMouseMove,
ToolStripMenuItem showVelocityAndGain,
- ToolStripMenuItem streamingMode,
- ToolStripMenuItem deviceMenuItem)
+ ToolStripMenuItem streamingMode)
{
DpiField = dpiField;
PollRateField = pollRateField;
@@ -30,7 +29,6 @@ namespace grapher.Models.Serialized
ShowLastMouseMoveMenuItem = showLastMouseMove;
ShowVelocityAndGainMoveMenuItem = showVelocityAndGain;
StreamingModeMenuItem = streamingMode;
- deviceMenuItem.Click += (s, e) => new DeviceMenuForm(this).ShowDialog();
SystemDevices = new List<MultiHandleDevice>();
ActiveHandles = new List<IntPtr>();
@@ -47,14 +45,17 @@ namespace grapher.Models.Serialized
UpdateFieldsFromGUISettings();
}
- UserConfig = InitActiveAndGetUserConfig();
+ UserConfigField = InitActiveAndGetUserConfig();
}
#endregion Constructors
#region Fields
+ private EventHandler DeviceChangeField;
+
private DriverConfig ActiveConfigField;
+ private DriverConfig UserConfigField;
#endregion Fields
@@ -62,6 +63,12 @@ namespace grapher.Models.Serialized
public GUISettings GuiSettings { get; private set; }
+ public event EventHandler DeviceChange
+ {
+ add => DeviceChangeField += value;
+ remove => DeviceChangeField -= value;
+ }
+
public DriverConfig ActiveConfig
{
get => ActiveConfigField;
@@ -87,11 +94,16 @@ namespace grapher.Models.Serialized
get => ActiveConfig.accels[0];
}
- public DriverConfig UserConfig { get; private set; }
+ public DriverConfig UserConfig
+ {
+ get => UserConfigField;
+ private set => UserConfigField = value;
+ }
public Profile UserProfile
{
- get => UserConfig.profiles[0];
+ get => UserConfigField.profiles[0];
+ private set => UserConfigField.SetProfileAt(0, value);
}
public HashSet<string> ActiveProfileNamesSet { get; private set; }
@@ -133,12 +145,12 @@ namespace grapher.Models.Serialized
public bool TryActivate(Profile settings, out string errors)
{
- var old = ActiveProfile;
- ActiveProfile = settings;
- bool success = TryActivate(ActiveConfig, out errors);
+ var old = UserProfile;
+ UserProfile = settings;
+ bool success = TryActivate(UserConfig, out errors);
if (!success)
{
- ActiveProfile = old;
+ UserProfile = old;
}
return success;
}
@@ -183,7 +195,7 @@ namespace grapher.Models.Serialized
};
}
- private void SetActiveHandles()
+ public void SetActiveHandles()
{
ActiveHandles.Clear();
@@ -217,6 +229,37 @@ namespace grapher.Models.Serialized
}
}
+ public void Submit(DeviceConfig newDefaultConfig, DeviceDialogItem[] items)
+ {
+ UserConfig.defaultDeviceConfig = newDefaultConfig;
+ foreach (var item in items)
+ {
+ if (item.overrideDefaultConfig)
+ {
+ if (item.oldSettings is null)
+ {
+ UserConfig.devices.Add(
+ new DeviceSettings
+ {
+ name = item.device.name,
+ profile = item.newProfile,
+ id = item.device.id,
+ config = item.newConfig
+ });
+ }
+ else
+ {
+ item.oldSettings.config = item.newConfig;
+ item.oldSettings.profile = item.newProfile;
+ }
+ }
+ else if (!(item.oldSettings is null))
+ {
+ UserConfig.devices.Remove(item.oldSettings);
+ }
+ }
+ }
+
public void OnProfileSelectionChange()
{
SetActiveHandles();
@@ -226,6 +269,8 @@ namespace grapher.Models.Serialized
{
SystemDevices = MultiHandleDevice.GetList();
SetActiveHandles();
+
+ DeviceChangeField?.Invoke(this, EventArgs.Empty);
}
private DriverConfig InitActiveAndGetUserConfig()