Skip to content

Commit 2dfa3a6

Browse files
Refactor various components for better null safety, cleanup, and performance:
1 parent fbc659d commit 2dfa3a6

3 files changed

Lines changed: 194 additions & 188 deletions

File tree

src/MacroDeck/ActionButton/ActionButton.cs

Lines changed: 35 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -53,20 +53,14 @@ protected virtual void Dispose(bool disposing)
5353

5454
HotkeyManager.RemoveHotkey(this);
5555
VariableManager.OnVariableChanged -= VariableChanged;
56-
if (Actions != null)
56+
foreach (var pluginAction in Actions)
5757
{
58-
foreach (var pluginAction in Actions)
59-
{
60-
pluginAction.OnActionButtonDelete();
61-
}
58+
pluginAction?.OnActionButtonDelete();
6259
}
63-
64-
if (EventListeners != null)
60+
61+
foreach (var pluginAction in EventListeners.SelectMany(eventListeners => eventListeners.Actions))
6562
{
66-
foreach (var pluginAction in EventListeners.SelectMany(eventListeners => eventListeners.Actions))
67-
{
68-
pluginAction.OnActionButtonDelete();
69-
}
63+
pluginAction.OnActionButtonDelete();
7064
}
7165

7266
_disposed = true;
@@ -90,18 +84,20 @@ private void VariableChanged(object sender, EventArgs e)
9084
return;
9185
}
9286

93-
var variable = sender as Variable;
94-
UpdateBindingState(variable);
87+
if (sender is Variable variable)
88+
{
89+
UpdateBindingState(variable);
90+
}
9591
}
9692

9793
private void UpdateBindingState(Variable variable)
9894
{
99-
if (variable == null || !variable.Name.Equals(StateBindingVariable))
95+
if (!variable.Name.Equals(StateBindingVariable))
10096
{
10197
return;
10298
}
10399

104-
bool.TryParse(variable.Value, out var newState);
100+
_ = bool.TryParse(variable.Value, out var newState);
105101
if (variable.Value.ToLower().Equals("on"))
106102
{
107103
newState = true;
@@ -110,90 +106,85 @@ private void UpdateBindingState(Variable variable)
110106
State = newState;
111107
}
112108

113-
public string Guid { get; set; } = System.Guid.NewGuid().ToString();
114-
115-
public event EventHandler StateChanged;
116-
public event EventHandler IconChanged;
117-
118-
private bool _state;
119-
private string _iconOff = string.Empty;
120-
private string _iconOn = string.Empty;
121-
private Color _backgroundColorOff = Color.FromArgb(65, 65, 65);
122-
private Color _backgroundColorOn = Color.FromArgb(65, 65, 65);
109+
public string Guid { get; set; } = System.Guid.CreateVersion7().ToString();
123110

111+
public event EventHandler? StateChanged;
112+
public event EventHandler? IconChanged;
124113

125114
public bool State
126115
{
127-
get => _state;
116+
get;
128117
set
129118
{
130-
if (_state == value)
119+
if (field == value)
131120
{
132121
return;
133122
}
134123

135-
_state = value;
124+
field = value;
136125
MacroDeckServer.UpdateState(this);
137126
StateChanged?.Invoke(this, EventArgs.Empty);
138127
}
139128
}
140129

141130
public string IconOff
142131
{
143-
get => _iconOff;
132+
get;
144133

145134
set
146135
{
147-
_iconOff = value;
136+
field = value;
148137
IconChanged?.Invoke(this, EventArgs.Empty);
149138
}
150-
}
139+
} = string.Empty;
151140

152141
public string IconOn
153142
{
154-
get => _iconOn;
143+
get;
155144
set
156145
{
157-
_iconOn = value;
146+
field = value;
158147
IconChanged?.Invoke(this, EventArgs.Empty);
159148
}
160-
}
149+
} = string.Empty;
161150

162151
public Color BackColorOff
163152
{
164-
get => _backgroundColorOff;
153+
get;
165154
set
166155
{
167-
if (_backgroundColorOff == value)
156+
if (field == value)
168157
{
169158
return;
170159
}
171160

172-
_backgroundColorOff = value;
161+
field = value;
173162
MacroDeckServer.UpdateState(this);
174163
StateChanged?.Invoke(this, EventArgs.Empty);
175164
}
176-
}
165+
} = Color.FromArgb(65, 65, 65);
177166

178167
public Color BackColorOn
179168
{
180-
get => _backgroundColorOn;
169+
get;
181170
set
182171
{
183-
if (_backgroundColorOn == value)
172+
if (field == value)
184173
{
185174
return;
186175
}
187176

188-
_backgroundColorOn = value;
177+
field = value;
189178
MacroDeckServer.UpdateState(this);
190179
StateChanged?.Invoke(this, EventArgs.Empty);
191180
}
192-
}
181+
} = Color.FromArgb(65, 65, 65);
193182

194-
public ButtonLabel LabelOff { get; set; } = new();
195-
public ButtonLabel LabelOn { get; set; } = new();
183+
public ButtonLabel? LabelOff { get; set; } = new();
184+
public ButtonLabel? LabelOn { get; set; } = new();
185+
// ReSharper disable once InconsistentNaming
196186
public int Position_X { get; set; } = -1;
187+
// ReSharper disable once InconsistentNaming
197188
public int Position_Y { get; set; } = -1;
198189
public string StateBindingVariable { get; set; } = string.Empty;
199190
public List<PluginAction?> Actions { get; set; } = new();

src/MacroDeck/Device/MacroDeckDevice.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public bool Available
2525

2626
public bool Blocked { get; set; } = false;
2727

28-
public string ProfileId { get; set; }
28+
public string? ProfileId { get; set; }
2929

3030
public DeviceConfiguration Configuration { get; set; } = new();
3131

0 commit comments

Comments
 (0)