Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix tray #20

Merged
merged 2 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion StageManager/AutoStart.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,17 @@
var key = Registry.CurrentUser.OpenSubKey(REG_KEY, writable: true);

if (startup)
key.SetValue(appName, GetAppPath());

Check warning on line 16 in StageManager/AutoStart.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.
else
key.DeleteValue(appName, throwOnMissingValue: false);

Check warning on line 18 in StageManager/AutoStart.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Dereference of a possibly null reference.
}

public static bool IsStartup(string appName) => IsStartup(Registry.CurrentUser.OpenSubKey(REG_KEY), appName);

Check warning on line 21 in StageManager/AutoStart.cs

View workflow job for this annotation

GitHub Actions / build (Release)

Possible null reference argument for parameter 'key' in 'bool AutoStart.IsStartup(RegistryKey key, string appName)'.

public static bool IsStartup(RegistryKey key, string appName) => GetValueAsString(key, appName).Equals(GetAppPath(), StringComparison.OrdinalIgnoreCase);

private static string GetValueAsString(RegistryKey key, string appName) => key.GetValue(appName)?.ToString() ?? "";

private static string GetAppPath() => $"\"{Assembly.GetEntryAssembly().Location}\"";
private static string GetAppPath() => $@"""{Environment.ProcessPath}""";
}
}
4 changes: 3 additions & 1 deletion StageManager/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,9 @@
IconSource="StageManager.ico"
MenuActivation="LeftOrRightClick">
<tb:TaskbarIcon.ContextMenu>
<ContextMenu>
<ContextMenu
Opened="ContextMenu_Opened"
Closed="ContextMenu_Closed">
<MenuItem
Header="Start with Windows"
IsCheckable="True"
Expand Down
49 changes: 38 additions & 11 deletions StageManager/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -57,21 +57,12 @@ protected override void OnInitialized(EventArgs e)
_thisHandle = new System.Windows.Interop.WindowInteropHelper(this).Handle;
_lastWidth = Width;

_hook = new TaskPoolGlobalHook();

_hook.MousePressed += OnMousePressed;
_hook.MouseReleased += OnMouseReleased;
_hook.MouseMoved += _hook_MouseMoved;

Task.Run(_hook.Run);
StartHook();
}

protected override void OnClosed(EventArgs e)
{
_hook.MousePressed -= OnMousePressed;
_hook.MouseReleased -= OnMouseReleased;
_hook.MouseMoved -= _hook_MouseMoved;
_hook.Dispose();
StopHook();

trayIcon.Dispose();

Expand Down Expand Up @@ -318,6 +309,32 @@ private void ApplyWindowMode()
BeginAnimation(LeftProperty, animation);
}

private void StartHook()
{
_hook = new TaskPoolGlobalHook();

_hook.MousePressed += OnMousePressed;
_hook.MouseReleased += OnMouseReleased;
_hook.MouseMoved += _hook_MouseMoved;

Task.Run(_hook.Run);
}

private void StopHook()
{
_hook.MousePressed -= OnMousePressed;
_hook.MouseReleased -= OnMouseReleased;
_hook.MouseMoved -= _hook_MouseMoved;

try
{
_hook.Dispose();
}
catch (HookException)
{
}
}

private void _hook_MouseMoved(object? sender, MouseHookEventArgs e)
{
_mouse.X = e.Data.X;
Expand Down Expand Up @@ -378,6 +395,16 @@ private void MenuItem_Quit_Click(object sender, RoutedEventArgs e)
{
Close();
}

private void ContextMenu_Closed(object sender, RoutedEventArgs e)
{
StartHook();
}

private void ContextMenu_Opened(object sender, RoutedEventArgs e)
{
StopHook();
}
}

public enum WindowMode
Expand Down
Loading