Skip to content

Commit

Permalink
Migrate to Invoke() (#231)
Browse files Browse the repository at this point in the history
* Change all BeginInvoke to Invoke

* for loops should be inside dispatcher, not outside
  • Loading branch information
Valkirie authored and CasperH2O committed Apr 8, 2024
1 parent dfdd03a commit 2aeca9c
Showing 31 changed files with 166 additions and 160 deletions.
6 changes: 1 addition & 5 deletions HandheldCompanion/Controllers/DS4Controller.cs
Original file line number Diff line number Diff line change
@@ -107,11 +107,7 @@ public override void Unplug()

public override void SetLightColor(byte R, byte G, byte B)
{
// UI thread
Application.Current.Dispatcher.Invoke(() =>
{
JslSetLightColour(UserIndex, CommonUtils.rgb_to_int(R, G, B));
});
JslSetLightColour(UserIndex, CommonUtils.rgb_to_int(R, G, B));
}

public override string GetGlyph(ButtonFlags button)
6 changes: 1 addition & 5 deletions HandheldCompanion/Controllers/DualSenseController.cs
Original file line number Diff line number Diff line change
@@ -107,11 +107,7 @@ public override void Unplug()

public override void SetLightColor(byte R, byte G, byte B)
{
// UI thread
Application.Current.Dispatcher.Invoke(() =>
{
JslSetLightColour(UserIndex, CommonUtils.rgb_to_int(R, G, B));
});
JslSetLightColour(UserIndex, CommonUtils.rgb_to_int(R, G, B));
}

public override string GetGlyph(ButtonFlags button)
8 changes: 4 additions & 4 deletions HandheldCompanion/Controllers/IController.xaml.cs
Original file line number Diff line number Diff line change
@@ -172,7 +172,7 @@ protected byte UserIndex
private void SetVirtualControllerVisualIndex(int value)
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
foreach (FrameworkElement frameworkElement in UserIndexPanel.Children)
{
@@ -335,7 +335,7 @@ protected void UpdateUI()
return;

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// ui_button_hook.Content = IsPlugged ? Properties.Resources.Controller_Disconnect : Properties.Resources.Controller_Connect;
ui_button_hide.Content = IsHidden() ? Properties.Resources.Controller_Unhide : Properties.Resources.Controller_Hide;
@@ -458,7 +458,7 @@ public virtual void Plug()
InjectedButtons.Clear();

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
ui_button_hook.IsEnabled = false;
});
@@ -468,7 +468,7 @@ public virtual void Plug()
public virtual void Unplug()
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
ui_button_hook.IsEnabled = true;
});
Original file line number Diff line number Diff line change
@@ -42,7 +42,7 @@ private void CheckSettings()
int EmbeddedIntegerScalingSupport = RegistryUtils.GetInt(@"SYSTEM\ControlSet001\Control\Class\{4d36e968-e325-11ce-bfc1-08002be10318}\0000", "DalEmbeddedIntegerScalingSupport");

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
this.Visibility = EmbeddedIntegerScalingSupport != 1 ? Visibility.Visible : Visibility.Collapsed;
});
Original file line number Diff line number Diff line change
@@ -59,7 +59,7 @@ private void CheckSettings()
VulnerableDriverBlocklistEnable = RegistryUtils.GetBoolean(@"SYSTEM\CurrentControlSet\Control\CI\Config", "VulnerableDriverBlocklistEnable");

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
this.Visibility = HypervisorEnforcedCodeIntegrityEnabled || VulnerableDriverBlocklistEnable ? Visibility.Visible : Visibility.Collapsed;
});
4 changes: 2 additions & 2 deletions HandheldCompanion/Controls/Hints/Hint_LegionGoLegionSpace.cs
Original file line number Diff line number Diff line change
@@ -83,7 +83,7 @@ private void ServiceTimer_Elapsed(object? sender, ElapsedEventArgs e)
}

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
this.Visibility = anyRunning ? Visibility.Visible : Visibility.Collapsed;
});
@@ -121,7 +121,7 @@ protected override void HintActionButton_Click(object sender, RoutedEventArgs e)

public override void Stop()
{
serviceTimer.Stop();
serviceTimer?.Stop();
base.Stop();
}
}
4 changes: 2 additions & 2 deletions HandheldCompanion/Controls/Hints/Hint_MSIClawCenter.cs
Original file line number Diff line number Diff line change
@@ -104,7 +104,7 @@ private void WatcherTimer_Elapsed(object? sender, ElapsedEventArgs e)
bool hasProcesses = HasProcesses();

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
this.Visibility = hasTask || hasProcesses ? Visibility.Visible : Visibility.Collapsed;
});
@@ -118,7 +118,7 @@ protected override void HintActionButton_Click(object sender, RoutedEventArgs e)

public override void Stop()
{
watcherTimer.Stop();
watcherTimer?.Stop();
base.Stop();
}
}
4 changes: 2 additions & 2 deletions HandheldCompanion/Controls/Hints/Hint_RogAllyServiceCheck.cs
Original file line number Diff line number Diff line change
@@ -77,7 +77,7 @@ private void ServiceTimer_Elapsed(object? sender, ElapsedEventArgs e)
}

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
this.Visibility = anyRunning ? Visibility.Visible : Visibility.Collapsed;
});
@@ -102,7 +102,7 @@ protected override void HintActionButton_Click(object sender, RoutedEventArgs e)

public override void Stop()
{
serviceTimer.Stop();
serviceTimer?.Stop();
base.Stop();
}
}
Original file line number Diff line number Diff line change
@@ -28,7 +28,7 @@ private void Steam_Updated(PlatformStatus status)
bool DesktopProfileApplied = PlatformManager.Steam.HasDesktopProfileApplied();

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
switch (status)
{
2 changes: 1 addition & 1 deletion HandheldCompanion/Controls/Hints/Hint_SteamXboxDrivers.cs
Original file line number Diff line number Diff line change
@@ -34,7 +34,7 @@ private void CheckDrivers()
bool HasXboxDriversInstalled = PlatformManager.Steam.HasXboxDriversInstalled();

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
this.Visibility = HasXboxDriversInstalled ? Visibility.Visible : Visibility.Collapsed;
});
14 changes: 7 additions & 7 deletions HandheldCompanion/Managers/Hotkeys/Hotkey.cs
Original file line number Diff line number Diff line change
@@ -185,7 +185,7 @@ public void Draw()
private void DrawGlyph()
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// update glyphs
mainControl.HotkeyIcon.FontFamily = quickControl.QuickIcon.FontFamily = inputsHotkey.fontFamily;
@@ -197,7 +197,7 @@ private void DrawGlyph()
private void DrawName()
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
mainControl.HotkeyDesc.Text = inputsHotkey.GetDescription();
mainControl.HotkeyName.Text = quickControl.QuickName.Text = mainControl.HotkeyCustomName.Text = Name;
@@ -284,7 +284,7 @@ private bool HasOutput()
public void DrawInput()
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// mainButton content
SimpleStackPanel inputContent = new()
@@ -381,7 +381,7 @@ public void DrawInput()
private void DrawOutput()
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// update button content
switch (HasOutput())
@@ -406,7 +406,7 @@ private void DrawOutput()
private void DrawPin()
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// update pin button
switch (IsPinned)
@@ -426,7 +426,7 @@ private void DrawPin()
private void DrawErase()
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// update delete button status
mainControl.HotkeyErase.IsEnabled = HasInput() || HasOutput();
@@ -441,7 +441,7 @@ public void Highlight()
public void SetToggle(bool toggle)
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
switch (toggle)
{
43 changes: 24 additions & 19 deletions HandheldCompanion/Managers/HotkeysManager.cs
Original file line number Diff line number Diff line change
@@ -78,28 +78,31 @@ private static void ControllerManager_ControllerPlugged(IController Controller,
{
// when the target emulated controller is Dualshock
// only enable HIDmode switch hotkey when controller is plugged (last stage of HIDmode change in this case)
var targetHIDmode = (HIDmode)SettingsManager.GetInt("HIDmode", true);
HIDmode targetHIDmode = (HIDmode)SettingsManager.GetInt("HIDmode", true);
if (targetHIDmode == HIDmode.DualShock4Controller)
{
var hotkeys = Hotkeys.Values.Where(item => item.inputsHotkey.Listener.Equals("shortcutChangeHIDMode")).ToList();
foreach (Hotkey? hotkey in hotkeys)
List<Hotkey> hotkeys = Hotkeys.Values.Where(item => item.inputsHotkey.Listener.Equals("shortcutChangeHIDMode")).ToList();
Application.Current.Dispatcher.Invoke(() =>
{
Application.Current.Dispatcher.BeginInvoke(() => { hotkey.IsEnabled = !hasProfileHID; });
}
foreach (Hotkey? hotkey in hotkeys)
hotkey.IsEnabled = !hasProfileHID;
});
}
}

private static void ControllerManager_ControllerUnplugged(IController Controller, bool IsPowerCycling, bool WasTarget)
{
// when the target emulated controller is Xbox Controller
// only enable HIDmode switch hotkey when controller is unplugged (last stage of HIDmode change in this case)
var targetHIDmode = (HIDmode)SettingsManager.GetInt("HIDmode", true);

HIDmode targetHIDmode = (HIDmode)SettingsManager.GetInt("HIDmode", true);
if (targetHIDmode == HIDmode.Xbox360Controller)
{
List<Hotkey> hotkeys = Hotkeys.Values.Where(item => item.inputsHotkey.Listener.Equals("shortcutChangeHIDMode")).ToList();
foreach (Hotkey? hotkey in hotkeys)
Application.Current.Dispatcher.Invoke(() => { hotkey.IsEnabled = !hasProfileHID; });
Application.Current.Dispatcher.Invoke(() =>
{
foreach (Hotkey? hotkey in hotkeys)
hotkey.IsEnabled = !hasProfileHID;
});
}
}

@@ -128,8 +131,11 @@ private static void ProfileManager_Applied(Profile profile, UpdateSource source)

// enable/disable hotkey based on profile HIDmode
List<Hotkey> hotkeys = Hotkeys.Values.Where(item => item.inputsHotkey.Listener.Equals("shortcutChangeHIDMode")).ToList();
foreach (Hotkey? hotkey in hotkeys)
Application.Current.Dispatcher.Invoke(() => { hotkey.IsEnabled = !hasProfileHID; });
Application.Current.Dispatcher.Invoke(() =>
{
foreach (Hotkey? hotkey in hotkeys)
hotkey.IsEnabled = !hasProfileHID;
});

// change glyph at startup only
if (!IsInitialized)
@@ -306,17 +312,16 @@ private static int CountPinned()
private static void TriggerUpdated(string listener, InputsChord inputs, ListenerType type)
{
IEnumerable<Hotkey> hotkeys = Hotkeys.Values.Where(item => item.inputsHotkey.Listener.Equals(listener)).ToList();
foreach (Hotkey hotkey in hotkeys)

Application.Current.Dispatcher.Invoke(() =>
{
// UI thread
Application.Current.Dispatcher.Invoke(() =>
foreach (Hotkey hotkey in hotkeys)
{
// stop recording and update UI
hotkey.StopListening(inputs, type);
});

SerializeHotkey(hotkey, true);
}
SerializeHotkey(hotkey, true);
}
});
}

private static Hotkey ProcessHotkey(string fileName)
@@ -356,7 +361,7 @@ public static void TriggerRaised(string listener, InputsChord input, InputsHotke
List<Hotkey> hotkeys = Hotkeys.Values.Where(item => item.inputsChord.State.Equals(input.State) && (item.inputsHotkey.OnKeyDown == IsKeyDown || item.inputsHotkey.OnKeyUp == IsKeyUp)).ToList();

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
foreach (Hotkey hotkey in hotkeys)
hotkey.Highlight();
18 changes: 12 additions & 6 deletions HandheldCompanion/Managers/InputsManager.cs
Original file line number Diff line number Diff line change
@@ -14,6 +14,7 @@
using static HandheldCompanion.Managers.InputsHotkey;
using ButtonState = HandheldCompanion.Inputs.ButtonState;
using Timer = System.Timers.Timer;
using Application = System.Windows.Application;

namespace HandheldCompanion.Managers;

@@ -167,13 +168,18 @@ private static bool CheckForSequence(bool IsKeyDown, bool IsKeyUp)
InputsChordType inputType = currentChord.InputsType;
if ((inputType == InputsChordType.Click && IsKeyUp) || (inputType == InputsChordType.Long && IsKeyDown))
{
var hidHotkeys = HotkeysManager.Hotkeys.Values.Where(item => item.inputsHotkey.Listener.Equals("shortcutChangeHIDMode"));
foreach (var hidHotkey in hidHotkeys)
List<Hotkey> hidHotkeys = HotkeysManager.Hotkeys.Values.Where(item => item.inputsHotkey.Listener.Equals("shortcutChangeHIDMode")).ToList();

Application.Current.Dispatcher.Invoke(() =>
{
if (!hidHotkey.IsEnabled)
return false;
System.Windows.Application.Current.Dispatcher.Invoke(() => { hidHotkey.IsEnabled = false; });
}
foreach (Hotkey hidHotkey in hidHotkeys)
{
if (!hidHotkey.IsEnabled)
continue;

hidHotkey.IsEnabled = false;
}
});
}
}

2 changes: 1 addition & 1 deletion HandheldCompanion/Managers/LayoutManager.cs
Original file line number Diff line number Diff line change
@@ -114,7 +114,7 @@ public static void Stop()
private static void LayoutWatcher_Template(object sender, FileSystemEventArgs e)
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
ProcessLayoutTemplate(e.FullPath);
});
4 changes: 2 additions & 2 deletions HandheldCompanion/Misc/PowerProfile.cs
Original file line number Diff line number Diff line change
@@ -26,7 +26,7 @@ public string Name
_Name = value;

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
foreach (UIElement uIElement in uIElements.Values)
uIElement.textBlock1.Text = value;
@@ -46,7 +46,7 @@ public string Description
_Description = value;

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
foreach (UIElement uIElement in uIElements.Values)
uIElement.textBlock2.Text = value;
4 changes: 2 additions & 2 deletions HandheldCompanion/UI/UIGamepad.cs
Original file line number Diff line number Diff line change
@@ -173,7 +173,7 @@ private void _gamepadFrame_ContentRendered(object? sender, EventArgs e)
private void _gamepadFrame_PageRendered(object? sender, System.Timers.ElapsedEventArgs e)
{
// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// specific-cases
switch (_gamepadPage.Tag)
@@ -400,7 +400,7 @@ private void InputsUpdated(ControllerState controllerState)
}

// UI thread (async)
Application.Current.Dispatcher.BeginInvoke(() =>
Application.Current.Dispatcher.Invoke(() =>
{
// get current focused element
Control focusedElement = FocusedElement(_currentWindow);
Loading

0 comments on commit 2aeca9c

Please sign in to comment.