From 0c79efe263eb1fff03f84f2698d9d7ed4fe05103 Mon Sep 17 00:00:00 2001 From: Jaex Date: Tue, 2 May 2017 18:30:13 +0300 Subject: [PATCH] Set startup state in WindowsStore build --- ShareX/Enums.cs | 1 + ShareX/Forms/ApplicationSettingsForm.cs | 24 ++++++++++- ShareX/IntegrationHelpers.cs | 57 ++++++++++++++++++++----- 3 files changed, 70 insertions(+), 12 deletions(-) diff --git a/ShareX/Enums.cs b/ShareX/Enums.cs index 50aa32c7f..83da32542 100644 --- a/ShareX/Enums.cs +++ b/ShareX/Enums.cs @@ -258,6 +258,7 @@ public enum RegionCaptureType public enum StartupTaskState { + Error = -1, Disabled = 0, DisabledByUser = 1, Enabled = 2 diff --git a/ShareX/Forms/ApplicationSettingsForm.cs b/ShareX/Forms/ApplicationSettingsForm.cs index 20da25ebb..56fa6d1d3 100644 --- a/ShareX/Forms/ApplicationSettingsForm.cs +++ b/ShareX/Forms/ApplicationSettingsForm.cs @@ -113,14 +113,32 @@ private void UpdateControls() cbTrayLeftClickAction.SelectedIndex = (int)Program.Settings.TrayLeftClickAction; cbTrayMiddleClickAction.SelectedIndex = (int)Program.Settings.TrayMiddleClickAction; -#if STEAM +#if STEAM || WindowsStore cbCheckPreReleaseUpdates.Visible = false; #else cbCheckPreReleaseUpdates.Checked = Program.Settings.CheckPreReleaseUpdates; #endif // Integration +#if WindowsStore + cbStartWithWindows.Enabled = false; + StartupTaskState state = StartupTaskState.Error; + + TaskEx.Run(() => + { + state = IntegrationHelpers.CheckStartupWindowsStore(); + }, + () => + { + if (!IsDisposed && state != StartupTaskState.Error && state != StartupTaskState.DisabledByUser) + { + cbStartWithWindows.Enabled = true; + cbStartWithWindows.Checked = state == StartupTaskState.Enabled; + } + }); +#else cbStartWithWindows.Checked = IntegrationHelpers.CheckStartupShortcut(); +#endif cbShellContextMenu.Checked = IntegrationHelpers.CheckShellContextMenuButton(); cbSendToMenu.Checked = IntegrationHelpers.CheckSendToMenuButton(); cbChromeExtensionSupport.Checked = IntegrationHelpers.CheckChromeExtensionSupport(); @@ -360,7 +378,11 @@ private void cbStartWithWindows_CheckedChanged(object sender, EventArgs e) { if (ready) { +#if WindowsStore + IntegrationHelpers.SetStartupWindowsStore(cbStartWithWindows.Checked); +#else IntegrationHelpers.CreateStartupShortcut(cbStartWithWindows.Checked); +#endif } } diff --git a/ShareX/IntegrationHelpers.cs b/ShareX/IntegrationHelpers.cs index e3c0be562..c91e9b98d 100644 --- a/ShareX/IntegrationHelpers.cs +++ b/ShareX/IntegrationHelpers.cs @@ -80,16 +80,51 @@ public static bool CheckStartupShortcut() return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup, StartupTargetPath); } - public static bool CreateStartupShortcut(bool create) + public static StartupTaskState CheckStartupWindowsStore() { -#if WindowsStore - return CreateStartupShortcutWindowsStore(create); -#else - return ShortcutHelpers.SetShortcut(create, Environment.SpecialFolder.Startup, StartupTargetPath, "-silent"); -#endif + string filepath = Helpers.GetAbsolutePath("ShareX_DesktopBridgeHelper.exe"); + + if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath)) + { + try + { + ProcessStartInfo startInfo = new ProcessStartInfo() + { + FileName = filepath, + Arguments = "-StartupState", + UseShellExecute = false, + CreateNoWindow = true + }; + + Process process = Process.Start(startInfo); + + if (process.WaitForExit(5000)) + { + int code = process.ExitCode; + + DebugHelper.WriteLine($"CheckStartupWindowsStore: {code}"); + + if (code > -1) + { + return (StartupTaskState)code; + } + } + } + catch (Exception e) + { + MessageBox.Show("Startup state check failed:\r\n" + e.ToString(), "ShareX"); + } + } + + return StartupTaskState.Error; } - private static bool CreateStartupShortcutWindowsStore(bool create) + public static bool CreateStartupShortcut(bool create) + { + return ShortcutHelpers.SetShortcut(create, Environment.SpecialFolder.Startup, StartupTargetPath, "-silent"); + } + + public static bool SetStartupWindowsStore(bool enable) { string filepath = Helpers.GetAbsolutePath("ShareX_DesktopBridgeHelper.exe"); @@ -99,7 +134,7 @@ private static bool CreateStartupShortcutWindowsStore(bool create) { string argument; - if (create) + if (enable) { argument = "-StartupEnable"; } @@ -122,15 +157,16 @@ private static bool CreateStartupShortcutWindowsStore(bool create) { int code = process.ExitCode; + DebugHelper.WriteLine($"CreateStartupWindowsStore: {code}"); + if (code > -1) { StartupTaskState state = (StartupTaskState)code; - if (create) + if (enable) { if (state == StartupTaskState.Enabled) { - MessageBox.Show("Startup successfully enabled.", "ShareX"); return true; } else if (state == StartupTaskState.DisabledByUser) @@ -141,7 +177,6 @@ private static bool CreateStartupShortcutWindowsStore(bool create) } else { - MessageBox.Show("Startup successfully disabled.", "ShareX"); return true; } }