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

Show a dialog to the user when app execution alias is disabled and want to run PI as admin #3172

Merged
merged 9 commits into from
Jun 10, 2024
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 common/Extensions/WindowExExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ await window.ShowMessageDialogAsync(dialog =>
/// </summary>
/// <param name="window">Target window.</param>
/// <param name="action">Action performed on the created dialog.</param>
private static async Task ShowMessageDialogAsync(this Window window, Action<ContentDialog> action)
public static async Task ShowMessageDialogAsync(this Window window, Action<ContentDialog> action)
{
var dialog = new ContentDialog()
{
Expand Down
15 changes: 13 additions & 2 deletions tools/PI/DevHome.PI/Helpers/CommonHelper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,15 @@
// Licensed under the MIT License.

using System;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using DevHome.Common.Extensions;
using DevHome.Common.Services;
using Microsoft.UI.Xaml;
using Serilog;
using Windows.ApplicationModel;
using Windows.Win32.Foundation;

namespace DevHome.PI.Helpers;

Expand Down Expand Up @@ -54,9 +56,18 @@ internal static void RunAsAdmin(int pid, string pageName)
var primaryWindow = Application.Current.GetService<PrimaryWindow>();
primaryWindow.Close();
}
catch (Exception ex)
catch (Win32Exception ex)
{
_log.Error(ex, "UAC to run PI as admin was denied");
_log.Error(ex, "Could not run PI as admin");
if (ex.NativeErrorCode == (int)WIN32_ERROR.ERROR_CANT_ACCESS_FILE)
{
var barWindow = Application.Current.GetService<PrimaryWindow>().DBarWindow;
barWindow?.ShowDialogToEnableAppExecutionAlias();
}
else if (ex.NativeErrorCode == (int)WIN32_ERROR.ERROR_CANCELLED)
{
_log.Error(ex, "UAC to run PI as admin was denied");
}
}
}
}
14 changes: 13 additions & 1 deletion tools/PI/DevHome.PI/Strings/en-us/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -933,6 +933,18 @@
<value>X64 process running on X64 machine</value>
<comment>Message text when the process is x64 CPU architecture running on an X64 CPU machine.</comment>
</data>
<data name="AliasDisabledDialogButtonText" xml:space="preserve">
<value>Go to Advanced app settings</value>
<comment>Content Dialog button text which on click opens the Advanced App settings page in Windows Settings app.</comment>
</data>
<data name="AliasDisabledDialogContent" xml:space="preserve">
<value>Go to Windows Settings &gt; Apps &gt; Advanced app settings &gt; App execution alias &gt; Project Ironsides &gt; Enable the toggle and try to launch PI as admin again.</value>
<comment>Locked="{Ironsides}" Content Dialog content suggesting users steps to enable app execution alias from Windows Settings app</comment>
</data>
<data name="AliasDisabledDialogTitle" xml:space="preserve">
<value>Enable App Execution Alias for Project Ironsides and try again</value>
<comment>Locked="{Ironsides}" Content Dialog title to enable app execution alias for Project Ironsides</comment>
</data>
<data name="DetachAppButton.Content" xml:space="preserve">
<value>Detach from target app</value>
<comment>Button in the expanded view to detach from the target app</comment>
Expand All @@ -945,4 +957,4 @@
<value>Detach</value>
<comment>Menu Item that allows the user to detach from the current process</comment>
</data>
</root>
</root>
7 changes: 7 additions & 0 deletions tools/PI/DevHome.PI/ViewModels/BarWindowViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media.Imaging;
using Windows.Graphics;
using Windows.System;
using Windows.Win32.Foundation;
using Windows.Win32.Graphics.Gdi;

Expand Down Expand Up @@ -345,4 +346,10 @@ private void InvokeTool(ExternalTool tool, int? pid, HWND hWnd)
Windows.Win32.UI.WindowsAndMessaging.MESSAGEBOX_STYLE.MB_ICONERROR);
}
}

[RelayCommand]
public void LaunchAdvancedAppsPageInWindowsSettings()
{
_ = Launcher.LaunchUriAsync(new("ms-settings:advanced-apps"));
zadesai marked this conversation as resolved.
Show resolved Hide resolved
}
}
20 changes: 20 additions & 0 deletions tools/PI/DevHome.PI/Views/BarWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Collections.Generic;
using System.ComponentModel;
using DevHome.Common.Extensions;
using DevHome.PI.Helpers;
using DevHome.PI.Models;
using DevHome.PI.Properties;
using DevHome.PI.ViewModels;
Expand All @@ -18,6 +19,10 @@ namespace DevHome.PI.Views;

public partial class BarWindow
{
private readonly string _aliasDisabledDialogTitle = CommonHelper.GetLocalizedString("AliasDisabledDialogTitle");
private readonly string _aliasDisabledDialogContent = CommonHelper.GetLocalizedString("AliasDisabledDialogContent");
private readonly string _aliasDisabledDialogButtonText = CommonHelper.GetLocalizedString("AliasDisabledDialogButtonText");

private readonly Settings _settings = Settings.Default;
private readonly BarWindowHorizontal _horizontalWindow;
private readonly BarWindowVertical _verticalWindow;
Expand Down Expand Up @@ -117,6 +122,21 @@ private void ViewModel_PropertyChanged(object? sender, PropertyChangedEventArgs
}
}

public void ShowDialogToEnableAppExecutionAlias()
{
_ = _horizontalWindow.ShowMessageDialogAsync(dialog =>
{
dialog.Title = _aliasDisabledDialogTitle;
dialog.Content = new TextBlock()
{
Text = _aliasDisabledDialogContent,
TextWrapping = TextWrapping.WrapWholeWords,
};
dialog.PrimaryButtonText = _aliasDisabledDialogButtonText;
dialog.PrimaryButtonCommand = _viewModel.LaunchAdvancedAppsPageInWindowsSettingsCommand;
});
}

public void RotateBar()
{
if (_horizontalWindow.Visible)
Expand Down
Loading