From 66fe6369a97a8efab2689dfabd20e8eb070eac01 Mon Sep 17 00:00:00 2001 From: sharpjd <59419827+sharpjd@users.noreply.github.com> Date: Tue, 28 May 2024 18:05:36 -0400 Subject: [PATCH] UX: don't close UI or grey out commit button if admin rights were denied --- GPUPrefSwitcher/OptionsFormUtils.cs | 7 ++++++- GPUPrefSwitcherGUI/MainForm.cs | 12 ++++++++---- GPUPrefSwitcherGUI/OptionsForm.cs | 11 ++++++++--- 3 files changed, 22 insertions(+), 8 deletions(-) diff --git a/GPUPrefSwitcher/OptionsFormUtils.cs b/GPUPrefSwitcher/OptionsFormUtils.cs index 70538f2..e31cb58 100644 --- a/GPUPrefSwitcher/OptionsFormUtils.cs +++ b/GPUPrefSwitcher/OptionsFormUtils.cs @@ -24,7 +24,7 @@ public static void ResetPreferencesXmlFile(bool showRestartConfirmation = true) } } - public static void AskRestartService() + public static bool AskRestartService() { const int ERROR_CANCELLED = 1223; //The operation was canceled by the user. @@ -41,10 +41,15 @@ public static void AskRestartService() catch (Win32Exception ex) { if (ex.NativeErrorCode == ERROR_CANCELLED) + { MessageBox.Show("Admin rights were denied, or error restarting service"); + return false; + } else throw; } + + return true; } public static void AskDeleteFolders() diff --git a/GPUPrefSwitcherGUI/MainForm.cs b/GPUPrefSwitcherGUI/MainForm.cs index e7978c2..4e4f68f 100644 --- a/GPUPrefSwitcherGUI/MainForm.cs +++ b/GPUPrefSwitcherGUI/MainForm.cs @@ -423,15 +423,19 @@ private void RevertChangesButton_Click(object sender, EventArgs e) private void CommitButton_Click(object sender, EventArgs e) { - OptionsFormUtils.AskRestartService(); - CommitButton.Enabled = false; + bool restartSuccess = OptionsFormUtils.AskRestartService(); - /* //not sure this does anything because there's no gauruntee the serrice has updated the file; maybe we need some sort of file update listener? + /* //not sure this does anything because there's no gauruntee the service has updated the file; maybe we need some sort of file update listener? switcherOptions.Reload(); UpdateGrid(); */ - Close(); + if (restartSuccess) + { + //CommitButton.Enabled = false; + Close(); + } + } private void OptionsButton_Click(object sender, EventArgs e) diff --git a/GPUPrefSwitcherGUI/OptionsForm.cs b/GPUPrefSwitcherGUI/OptionsForm.cs index bf84d13..cc9ae99 100644 --- a/GPUPrefSwitcherGUI/OptionsForm.cs +++ b/GPUPrefSwitcherGUI/OptionsForm.cs @@ -152,14 +152,19 @@ private void SpoofPowerStateComboBox_SelectedIndexChanged(object sender, EventAr private void CommitChangesButton_Click(object sender, EventArgs e) { - OptionsFormUtils.AskRestartService(); - CommitChangesButton.Enabled = false; + bool restartSuccess = OptionsFormUtils.AskRestartService(); + + if(restartSuccess) + { + //CommitChangesButton.Enabled = false; + parentMainForm.Close(); + } /* //not sure this does anything because there's no gauruntee the serrice has updated the file; maybe we need some sort of file update listener? switcherOptions.Reload(); UpdateFormComponents(switcherOptions.CurrentOptions); */ - parentMainForm.Close(); + } private void SaveButton_Click_1(object sender, EventArgs e)