Set startup state in WindowsStore build

This commit is contained in:
Jaex 2017-05-02 18:30:13 +03:00
parent a2e98147f2
commit 0c79efe263
3 changed files with 70 additions and 12 deletions

View file

@ -258,6 +258,7 @@ public enum RegionCaptureType
public enum StartupTaskState public enum StartupTaskState
{ {
Error = -1,
Disabled = 0, Disabled = 0,
DisabledByUser = 1, DisabledByUser = 1,
Enabled = 2 Enabled = 2

View file

@ -113,14 +113,32 @@ private void UpdateControls()
cbTrayLeftClickAction.SelectedIndex = (int)Program.Settings.TrayLeftClickAction; cbTrayLeftClickAction.SelectedIndex = (int)Program.Settings.TrayLeftClickAction;
cbTrayMiddleClickAction.SelectedIndex = (int)Program.Settings.TrayMiddleClickAction; cbTrayMiddleClickAction.SelectedIndex = (int)Program.Settings.TrayMiddleClickAction;
#if STEAM #if STEAM || WindowsStore
cbCheckPreReleaseUpdates.Visible = false; cbCheckPreReleaseUpdates.Visible = false;
#else #else
cbCheckPreReleaseUpdates.Checked = Program.Settings.CheckPreReleaseUpdates; cbCheckPreReleaseUpdates.Checked = Program.Settings.CheckPreReleaseUpdates;
#endif #endif
// Integration // 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(); cbStartWithWindows.Checked = IntegrationHelpers.CheckStartupShortcut();
#endif
cbShellContextMenu.Checked = IntegrationHelpers.CheckShellContextMenuButton(); cbShellContextMenu.Checked = IntegrationHelpers.CheckShellContextMenuButton();
cbSendToMenu.Checked = IntegrationHelpers.CheckSendToMenuButton(); cbSendToMenu.Checked = IntegrationHelpers.CheckSendToMenuButton();
cbChromeExtensionSupport.Checked = IntegrationHelpers.CheckChromeExtensionSupport(); cbChromeExtensionSupport.Checked = IntegrationHelpers.CheckChromeExtensionSupport();
@ -360,7 +378,11 @@ private void cbStartWithWindows_CheckedChanged(object sender, EventArgs e)
{ {
if (ready) if (ready)
{ {
#if WindowsStore
IntegrationHelpers.SetStartupWindowsStore(cbStartWithWindows.Checked);
#else
IntegrationHelpers.CreateStartupShortcut(cbStartWithWindows.Checked); IntegrationHelpers.CreateStartupShortcut(cbStartWithWindows.Checked);
#endif
} }
} }

View file

@ -80,16 +80,51 @@ public static bool CheckStartupShortcut()
return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup, StartupTargetPath); return ShortcutHelpers.CheckShortcut(Environment.SpecialFolder.Startup, StartupTargetPath);
} }
public static bool CreateStartupShortcut(bool create) public static StartupTaskState CheckStartupWindowsStore()
{ {
#if WindowsStore string filepath = Helpers.GetAbsolutePath("ShareX_DesktopBridgeHelper.exe");
return CreateStartupShortcutWindowsStore(create);
#else if (!string.IsNullOrEmpty(filepath) && File.Exists(filepath))
return ShortcutHelpers.SetShortcut(create, Environment.SpecialFolder.Startup, StartupTargetPath, "-silent"); {
#endif 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"); string filepath = Helpers.GetAbsolutePath("ShareX_DesktopBridgeHelper.exe");
@ -99,7 +134,7 @@ private static bool CreateStartupShortcutWindowsStore(bool create)
{ {
string argument; string argument;
if (create) if (enable)
{ {
argument = "-StartupEnable"; argument = "-StartupEnable";
} }
@ -122,15 +157,16 @@ private static bool CreateStartupShortcutWindowsStore(bool create)
{ {
int code = process.ExitCode; int code = process.ExitCode;
DebugHelper.WriteLine($"CreateStartupWindowsStore: {code}");
if (code > -1) if (code > -1)
{ {
StartupTaskState state = (StartupTaskState)code; StartupTaskState state = (StartupTaskState)code;
if (create) if (enable)
{ {
if (state == StartupTaskState.Enabled) if (state == StartupTaskState.Enabled)
{ {
MessageBox.Show("Startup successfully enabled.", "ShareX");
return true; return true;
} }
else if (state == StartupTaskState.DisabledByUser) else if (state == StartupTaskState.DisabledByUser)
@ -141,7 +177,6 @@ private static bool CreateStartupShortcutWindowsStore(bool create)
} }
else else
{ {
MessageBox.Show("Startup successfully disabled.", "ShareX");
return true; return true;
} }
} }