From b428fc97e97e455cf5deb73a98c9c496b1369d3e Mon Sep 17 00:00:00 2001
From: Lavius Motileng <58791731+laviusmotileng-ms@users.noreply.github.com>
Date: Sun, 3 May 2020 03:17:06 -0700
Subject: [PATCH] fix: Start as Admin Error: Unable to open app when running
elevated (#2411)
* fixed run-elevated error and powerpreview settings not working
* removed EXTENDED_STARTUPINFO_PRESENT flag
* removed test string
* Update Microsoft.PowerToys.Settings.UnitTest.csproj
* Rename PowerLauncher.cs to PowerLauncherViewModelTest.cs
---
.../GeneralSettings.cs | 8 ++
.../GeneralSettingsCustomAction.cs | 25 ++++++
.../PowerPreviewProperties.cs | 13 ++-
.../SndModuleSettings.cs | 1 -
.../SndPowerPreviewSettings.cs | 10 ++-
...rosoft.PowerToys.Settings.UI.Runner.csproj | 1 -
.../Strings/en-us/Resources.resw | 12 +++
.../ViewModels/GeneralViewModel.cs | 29 ++++++-
.../ViewModels/PowerPreviewViewModel.cs | 12 +--
.../Views/GeneralPage.xaml | 19 ++--
.../Views/PowerPreviewPage.xaml | 4 +-
...crosoft.PowerToys.Settings.UnitTest.csproj | 3 +-
.../ViewModelTests/ImageResizer.cs | 5 ++
.../ViewModelTests/PowerPreview.cs | 86 +++++++++++++++++++
.../ViewModelTests/ShortcutGuide.cs | 13 ++-
src/runner/settings_window.cpp | 3 +-
src/settings/main.cpp | 1 -
17 files changed, 208 insertions(+), 37 deletions(-)
create mode 100644 src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettingsCustomAction.cs
create mode 100644 src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs
index d8041639d9..8250d947e1 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettings.cs
@@ -41,19 +41,27 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
[JsonPropertyName("powertoys_version")]
public string PowertoysVersion { get; set; }
+ [JsonPropertyName("action_name")]
+ public string CustomActionName { get; set; }
+
[JsonPropertyName("enabled")]
public EnabledModules Enabled { get; set; }
+ [JsonPropertyName("download_updates_automatically")]
+ public bool AutoDownloadUpdates { get; set; }
+
public GeneralSettings()
{
this.Packaged = false;
this.Startup = false;
this.IsAdmin = false;
this.IsElevated = false;
+ this.AutoDownloadUpdates = false;
this.Theme = "system";
this.SystemTheme = "light";
this.PowertoysVersion = "v0.15.3";
this.Enabled = new EnabledModules();
+ this.CustomActionName = string.Empty;
}
// converts the current to a json string.
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettingsCustomAction.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettingsCustomAction.cs
new file mode 100644
index 0000000000..18d28daa01
--- /dev/null
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/GeneralSettingsCustomAction.cs
@@ -0,0 +1,25 @@
+using System.Text.Json;
+using System.Text.Json.Serialization;
+
+namespace Microsoft.PowerToys.Settings.UI.Lib
+{
+ public class GeneralSettingsCustomAction
+ {
+ [JsonPropertyName("action")]
+ public OutGoingGeneralSettings GeneralSettingsAction { get; set; }
+
+ public GeneralSettingsCustomAction()
+ {
+ }
+
+ public GeneralSettingsCustomAction(OutGoingGeneralSettings action)
+ {
+ GeneralSettingsAction = action;
+ }
+
+ public override string ToString()
+ {
+ return JsonSerializer.Serialize(this);
+ }
+ }
+}
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs
index 34e3797f48..ff9438075c 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/PowerPreviewProperties.cs
@@ -2,9 +2,6 @@
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
-using System;
-using System.Collections.Generic;
-using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
@@ -12,14 +9,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
{
public class PowerPreviewProperties
{
- public BoolProperty IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL { get; set; }
+ [JsonPropertyName("svg-previewer-toggle-setting")]
+ public BoolProperty EnableSvg { get; set; }
- public BoolProperty PREVPANE_MD_BOOL_TOGGLE_CONTROLL_ID { get; set; }
+ [JsonPropertyName("md-previewer-toggle-setting")]
+ public BoolProperty EnableMd { get; set; }
public PowerPreviewProperties()
{
- IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL = new BoolProperty();
- PREVPANE_MD_BOOL_TOGGLE_CONTROLL_ID = new BoolProperty();
+ EnableSvg = new BoolProperty();
+ EnableMd = new BoolProperty();
}
public override string ToString()
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndModuleSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndModuleSettings.cs
index ef067090ba..aed13e7d0d 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndModuleSettings.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndModuleSettings.cs
@@ -15,7 +15,6 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
public SndModuleSettings()
{
-
}
public SndModuleSettings(T settings)
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndPowerPreviewSettings.cs b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndPowerPreviewSettings.cs
index dcd4661c4b..c0decebfe2 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndPowerPreviewSettings.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Lib/SndPowerPreviewSettings.cs
@@ -12,12 +12,16 @@ namespace Microsoft.PowerToys.Settings.UI.Lib
{
public class SndPowerPreviewSettings
{
- [JsonPropertyName("File Explorer Preview")]
- public PowerPreviewSettings File_Explorer_Preview { get; set; }
+ [JsonPropertyName("File Explorer")]
+ public PowerPreviewSettings FileExplorerPreviewSettings { get; set; }
+
+ public SndPowerPreviewSettings()
+ {
+ }
public SndPowerPreviewSettings(PowerPreviewSettings settings)
{
- File_Explorer_Preview = settings;
+ FileExplorerPreviewSettings = settings;
}
public string ToJsonString()
diff --git a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj
index 5eea92d8c5..619915a358 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj
+++ b/src/core/Microsoft.PowerToys.Settings.UI.Runner/Microsoft.PowerToys.Settings.UI.Runner.csproj
@@ -57,7 +57,6 @@
-
diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw b/src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw
index 19229d6c69..2f6e29e84c 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw
+++ b/src/core/Microsoft.PowerToys.Settings.UI/Strings/en-us/Resources.resw
@@ -524,4 +524,16 @@
On
+
+ Always Run as Admin
+
+
+ Learn about Admin mode
+
+
+ Download updates automatically
+
+
+ Currently running as administrator
+
\ No newline at end of file
diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs
index 5ab16a997b..5b62a3ef43 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/GeneralViewModel.cs
@@ -56,6 +56,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
_startup = GeneralSettingsConfigs.Startup;
+ _autoDownloadUpdates = GeneralSettingsConfigs.AutoDownloadUpdates;
}
private bool _packaged = false;
@@ -65,6 +66,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
private bool _isDarkThemeRadioButtonChecked = false;
private bool _isLightThemeRadioButtonChecked = false;
private bool _isSystemThemeRadioButtonChecked = false;
+ private bool _autoDownloadUpdates = false;
// Gets or sets a value indicating whether packaged.
public bool Packaged
@@ -97,6 +99,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (_startup != value)
{
_startup = value;
+ GeneralSettingsConfigs.Startup = value;
RaisePropertyChanged();
}
}
@@ -138,6 +141,24 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
}
}
+ public bool AutoDownloadUpdates
+ {
+ get
+ {
+ return _autoDownloadUpdates;
+ }
+
+ set
+ {
+ if (_autoDownloadUpdates != value)
+ {
+ _autoDownloadUpdates = value;
+ GeneralSettingsConfigs.AutoDownloadUpdates = value;
+ RaisePropertyChanged();
+ }
+ }
+ }
+
public bool IsDarkThemeRadioButtonChecked
{
get
@@ -211,15 +232,17 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
await Launcher.LaunchUriAsync(new Uri("https://github.com/microsoft/PowerToys/releases"));
}
- private void Restart_Elevated()
+ public void Restart_Elevated()
{
GeneralSettings settings = SettingsUtils.GetSettings(string.Empty);
- settings.RunElevated = true;
+ settings.CustomActionName = "restart_elevation";
+
OutGoingGeneralSettings outsettings = new OutGoingGeneralSettings(settings);
+ GeneralSettingsCustomAction customaction = new GeneralSettingsCustomAction(outsettings);
if (ShellPage.DefaultSndMSGCallback != null)
{
- ShellPage.DefaultSndMSGCallback(outsettings.ToString());
+ ShellPage.DefaultSndMSGCallback(customaction.ToString());
}
}
}
diff --git a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs
index 9e88b0dbed..fbd6fb5133 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UI/ViewModels/PowerPreviewViewModel.cs
@@ -27,14 +27,14 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
SettingsUtils.SaveSettings(Settings.ToJsonString(), ModuleName);
}
- this._svgRenderIsEnabled = Settings.properties.IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL.Value;
- this._mdRenderIsEnabled = Settings.properties.PREVPANE_MD_BOOL_TOGGLE_CONTROLL_ID.Value;
+ this._svgRenderIsEnabled = Settings.properties.EnableSvg.Value;
+ this._mdRenderIsEnabled = Settings.properties.EnableMd.Value;
}
private bool _svgRenderIsEnabled = false;
private bool _mdRenderIsEnabled = false;
- public bool SVGRenderIsEnebled
+ public bool SVGRenderIsEnabled
{
get
{
@@ -46,13 +46,13 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (value != _svgRenderIsEnabled)
{
_svgRenderIsEnabled = value;
- Settings.properties.IDS_PREVPANE_SVG_BOOL_TOGGLE_CONTROLL.Value = value;
+ Settings.properties.EnableSvg.Value = value;
RaisePropertyChanged();
}
}
}
- public bool MDRenderIsEnebled
+ public bool MDRenderIsEnabled
{
get
{
@@ -64,7 +64,7 @@ namespace Microsoft.PowerToys.Settings.UI.ViewModels
if (value != _mdRenderIsEnabled)
{
_mdRenderIsEnabled = value;
- Settings.properties.PREVPANE_MD_BOOL_TOGGLE_CONTROLL_ID.Value = value;
+ Settings.properties.EnableMd.Value = value;
RaisePropertyChanged();
}
}
diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml
index 9ca3684c49..ab43e216f7 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml
+++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/GeneralPage.xaml
@@ -52,20 +52,24 @@
+ IsOn="{Binding Mode=TwoWay, Path=Startup, Source={StaticResource eventViewModel}}"/>
+
+
+ IsChecked="{ Binding Mode=TwoWay, Path=IsDarkThemeRadioButtonChecked, Source={StaticResource eventViewModel}}"/>
+ IsChecked="{ Binding Mode=TwoWay, Path=IsLightThemeRadioButtonChecked, Source={StaticResource eventViewModel}}"/>
+ IsChecked="{ Binding Mode=TwoWay, Path=IsSystemThemeRadioButtonChecked, Source={StaticResource eventViewModel}}"/>
-
+ NavigateUri="https://github.com/microsoft/PowerToys/blob/master/NOTICE.md"/>
+
+
diff --git a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml
index 791d43611e..d314e36378 100644
--- a/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml
+++ b/src/core/Microsoft.PowerToys.Settings.UI/Views/PowerPreviewPage.xaml
@@ -46,11 +46,11 @@
+ IsOn="{Binding Mode=TwoWay, Path=SVGRenderIsEnabled}" />
+ IsOn="{Binding Mode=TwoWay, Path=MDRenderIsEnabled}" />
UnitTestApp.xaml
+
@@ -183,4 +184,4 @@
-->
-
\ No newline at end of file
+
diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs
index aca4e550f7..31143ff9af 100644
--- a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ImageResizer.cs
@@ -38,6 +38,11 @@ namespace ViewModelTests
{
DeleteFolder(generalSettings_file_name);
}
+
+ if (SettingsUtils.SettingsFolderExists(Module))
+ {
+ DeleteFolder(Module);
+ }
}
public void DeleteFolder(string powertoy)
diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs
new file mode 100644
index 0000000000..bd1d8e0854
--- /dev/null
+++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/PowerPreview.cs
@@ -0,0 +1,86 @@
+using Microsoft.PowerToys.Settings.UI.Lib;
+using Microsoft.PowerToys.Settings.UI.ViewModels;
+using Microsoft.PowerToys.Settings.UI.Views;
+using Microsoft.VisualStudio.TestTools.UnitTesting;
+using System;
+using System.Collections.Generic;
+using System.IO;
+using System.Linq;
+using System.Text;
+using System.Text.Json;
+using System.Threading.Tasks;
+
+namespace ViewModelTests
+{
+ [TestClass]
+ public class PowerPreview
+ {
+ public const string Module = "File Explorer";
+
+ [TestInitialize]
+ public void Setup()
+ {
+ // initialize creation of test settings file.
+ GeneralSettings generalSettings = new GeneralSettings();
+ PowerPreviewSettings powerpreview = new PowerPreviewSettings();
+
+ SettingsUtils.SaveSettings(generalSettings.ToJsonString());
+ SettingsUtils.SaveSettings(powerpreview.ToJsonString(), powerpreview.name);
+ }
+
+ [TestCleanup]
+ public void CleanUp()
+ {
+ // delete folder created.
+ string generalSettings_file_name = string.Empty;
+ if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
+ {
+ DeleteFolder(generalSettings_file_name);
+ }
+
+ if (SettingsUtils.SettingsFolderExists(Module))
+ {
+ DeleteFolder(Module);
+ }
+ }
+
+ public void DeleteFolder(string powertoy)
+ {
+ Directory.Delete(Path.Combine(SettingsUtils.LocalApplicationDataFolder(), $"Microsoft\\PowerToys\\{powertoy}"), true);
+ }
+
+ [TestMethod]
+ public void SVGRenderIsEnabled_ShouldPrevHandler_WhenSuccessful()
+ {
+ // arrange
+ PowerPreviewViewModel viewModel = new PowerPreviewViewModel();
+
+ // Assert
+ ShellPage.DefaultSndMSGCallback = msg =>
+ {
+ SndModuleSettings snd = JsonSerializer.Deserialize>(msg);
+ Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.properties.EnableSvg.Value);
+ };
+
+ // act
+ viewModel.SVGRenderIsEnabled = true;
+ }
+
+ [TestMethod]
+ public void MDRenderIsEnabled_ShouldPrevHandler_WhenSuccessful()
+ {
+ // arrange
+ PowerPreviewViewModel viewModel = new PowerPreviewViewModel();
+
+ // Assert
+ ShellPage.DefaultSndMSGCallback = msg =>
+ {
+ SndModuleSettings snd = JsonSerializer.Deserialize>(msg);
+ Assert.IsTrue(snd.powertoys.FileExplorerPreviewSettings.properties.EnableMd.Value);
+ };
+
+ // act
+ viewModel.MDRenderIsEnabled = true;
+ }
+ }
+}
diff --git a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ShortcutGuide.cs b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ShortcutGuide.cs
index 5b8a786b5f..e502f2bab0 100644
--- a/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ShortcutGuide.cs
+++ b/src/core/Microsoft.PowerToys.Settings.UnitTest/ViewModelTests/ShortcutGuide.cs
@@ -15,6 +15,8 @@ namespace ViewModelTests
[TestClass]
public class ShortcutGuide
{
+ private const string ModuleName = "Shortcut Guide";
+
[TestInitialize]
public void Setup()
{
@@ -32,10 +34,15 @@ namespace ViewModelTests
public void CleanUp()
{
// delete folder created.
- string file_name = "\\test";
- if (SettingsUtils.SettingsFolderExists(file_name))
+ string generalSettings_file_name = string.Empty;
+ if (SettingsUtils.SettingsFolderExists(generalSettings_file_name))
{
- DeleteFolder(file_name);
+ DeleteFolder(generalSettings_file_name);
+ }
+
+ if (SettingsUtils.SettingsFolderExists(ModuleName))
+ {
+ DeleteFolder(ModuleName);
}
}
diff --git a/src/runner/settings_window.cpp b/src/runner/settings_window.cpp
index 8bb28ff0c0..e2e1f44d39 100644
--- a/src/runner/settings_window.cpp
+++ b/src/runner/settings_window.cpp
@@ -197,7 +197,7 @@ BOOL run_settings_non_elevated(LPCWSTR executable_path, LPWSTR executable_args,
nullptr,
nullptr,
FALSE,
- EXTENDED_STARTUPINFO_PRESENT,
+ 0,
nullptr,
nullptr,
&siex.StartupInfo,
@@ -262,7 +262,6 @@ void run_settings_window()
executable_args.append(settings_theme);
BOOL process_created = false;
-
if (is_process_elevated())
{
process_created = run_settings_non_elevated(executable_path.c_str(), executable_args.data(), &process_info);
diff --git a/src/settings/main.cpp b/src/settings/main.cpp
index 88977ae2aa..e15737924b 100644
--- a/src/settings/main.cpp
+++ b/src/settings/main.cpp
@@ -129,7 +129,6 @@ void send_message_to_powertoys_runner(const std::wstring& msg)
{
if (g_message_pipe != nullptr)
{
- MessageBox(g_main_wnd, msg.c_str(), L"From Webview", MB_OK);
g_message_pipe->send(msg);
}
else