[General]Display in titlebar/tray if running as admin (#28425)

* display in titlebar if running as admin

* show run as admin in tray tooltip
This commit is contained in:
Davide Giacometti
2023-09-12 12:11:26 +02:00
committed by GitHub
parent 63975600f1
commit ebd051ff61
17 changed files with 75 additions and 24 deletions

View File

@@ -6,7 +6,6 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:views="using:PowerToys.FileLocksmithUI.Views"
xmlns:winuiex="using:WinUIEx"
x:Uid="AppTitle"
Width="680"
MinWidth="480"
MinHeight="320"
@@ -39,7 +38,6 @@
Height="16"/>
<TextBlock
x:Name="AppTitleTextBlock"
x:Uid="AppTitleText"
Grid.Column="2"
VerticalAlignment="Center"
Style="{StaticResource CaptionTextBlockStyle}" />

View File

@@ -5,8 +5,8 @@
using System;
using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using PowerToys.FileLocksmithUI.Helpers;
using WinUIEx;
namespace FileLocksmithUI
@@ -21,6 +21,11 @@ namespace FileLocksmithUI
SetTitleBar(AppTitleBar);
Activated += MainWindow_Activated;
AppWindow.SetIcon("Assets/FileLocksmith/Icon.ico");
var loader = ResourceLoaderInstance.ResourceLoader;
var title = isElevated ? loader.GetString("AppAdminTitle") : loader.GetString("AppTitle");
Title = title;
AppTitleTextBlock.Text = title;
}
private void MainWindow_Activated(object sender, WindowActivatedEventArgs args)

View File

@@ -0,0 +1,18 @@
// Copyright (c) Microsoft Corporation
// The Microsoft Corporation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using Microsoft.Windows.ApplicationModel.Resources;
namespace PowerToys.FileLocksmithUI.Helpers
{
internal static class ResourceLoaderInstance
{
internal static ResourceLoader ResourceLoader { get; private set; }
static ResourceLoaderInstance()
{
ResourceLoader = new Microsoft.Windows.ApplicationModel.Resources.ResourceLoader("PowerToys.FileLocksmithUI.pri");
}
}
}

View File

@@ -117,11 +117,9 @@
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<data name="AppTitle.Title" xml:space="preserve">
<value>File Locksmith</value>
</data>
<data name="AppTitleText.Text" xml:space="preserve">
<data name="AppTitle" xml:space="preserve">
<value>File Locksmith</value>
<comment>Title of the window when running as user.</comment>
</data>
<data name="EmptyListDescription.Text" xml:space="preserve">
<value>No results</value>
@@ -162,4 +160,8 @@
<data name="User.Header" xml:space="preserve">
<value>User</value>
</data>
<data name="AppAdminTitle" xml:space="preserve">
<value>Administrator: File Locksmith</value>
<comment>Title of the window when running as administrator.</comment>
</data>
</root>

View File

@@ -37,8 +37,7 @@
x:Name="AppTitleTextBlock"
Grid.Column="2"
Style="{StaticResource CaptionTextBlockStyle}"
VerticalAlignment="Center"
Text="Hosts File Editor" />
VerticalAlignment="Center" />
</Grid>
<views:MainPage Grid.Row="1" />

View File

@@ -2,7 +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 Hosts.Helpers;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Media;
@@ -19,7 +18,11 @@ namespace Hosts
ExtendsContentIntoTitleBar = true;
SetTitleBar(titleBar);
AppWindow.SetIcon("Assets/Hosts/Hosts.ico");
Title = ResourceLoaderInstance.ResourceLoader.GetString("WindowTitle");
var loader = ResourceLoaderInstance.ResourceLoader;
var title = App.GetService<IElevationHelper>().IsElevated ? loader.GetString("WindowAdminTitle") : loader.GetString("WindowTitle");
Title = title;
AppTitleTextBlock.Text = title;
BringToForeground();

View File

@@ -307,8 +307,12 @@
<data name="WarningDialog_Title" xml:space="preserve">
<value>Warning</value>
</data>
<data name="WindowAdminTitle" xml:space="preserve">
<value>Administrator: Hosts File Editor</value>
<comment>Title of the window when running as administrator. "Hosts File Editor" is the name of the utility. "Hosts" refers to the system hosts file, do not loc</comment>
</data>
<data name="WindowTitle" xml:space="preserve">
<value>Hosts File Editor</value>
<comment>"Hosts File Editor" is the name of the utility. "Hosts" refers to the system hosts file, do not loc</comment>
<comment>Title of the window when running as user. "Hosts File Editor" is the name of the utility. "Hosts" refers to the system hosts file, do not loc</comment>
</data>
</root>

View File

@@ -126,4 +126,7 @@
<value>A new PowerToys version has been installed. Please restart the computer when possible, to fully reload File Explorer extensions.</value>
<comment>File Explorer refers to the Windows File Explorer application.</comment>
</data>
<data name="TRAY_ICON_ADMIN_TOOLTIP" xml:space="preserve">
<value>Administrator</value>
</data>
</root>

View File

@@ -100,7 +100,7 @@ int runner(bool isProcessElevated, bool openSettings, std::string settingsWindow
//init_global_error_handlers();
#endif
Trace::RegisterProvider();
start_tray_icon();
start_tray_icon(isProcessElevated);
CentralizedKeyboardHook::Start();
int result = -1;

View File

@@ -118,6 +118,9 @@
<ItemGroup>
<Manifest Include="PowerToys.exe.manifest" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources.resx" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<Import Project="..\..\deps\spdlog.props" />
<ImportGroup Label="ExtensionTargets">

View File

@@ -112,4 +112,9 @@
<ItemGroup>
<ResourceCompile Include="Generated Files/runner.rc" />
</ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Resources.resx">
<Filter>Resource Files</Filter>
</EmbeddedResource>
</ItemGroup>
</Project>

View File

@@ -268,7 +268,7 @@ LRESULT __stdcall tray_icon_window_proc(HWND window, UINT message, WPARAM wparam
return DefWindowProc(window, message, wparam, lparam);
}
void start_tray_icon()
void start_tray_icon(bool isProcessElevated)
{
auto h_instance = reinterpret_cast<HINSTANCE>(&__ImageBase);
auto icon = LoadIcon(h_instance, MAKEINTRESOURCE(APPICON));
@@ -304,8 +304,16 @@ void start_tray_icon()
tray_icon_data.hWnd = hwnd;
tray_icon_data.uID = id_tray_icon;
tray_icon_data.uCallbackMessage = wm_icon_notify;
std::wstring about_msg_pt_version = L"PowerToys " + get_product_version();
wcscpy_s(tray_icon_data.szTip, sizeof(tray_icon_data.szTip) / sizeof(WCHAR), about_msg_pt_version.c_str());
std::wstringstream pt_version_tooltip_stream;
if (isProcessElevated)
{
pt_version_tooltip_stream << GET_RESOURCE_STRING(IDS_TRAY_ICON_ADMIN_TOOLTIP) << L": ";
}
pt_version_tooltip_stream << L"PowerToys " << get_product_version() << '\0';
std::wstring pt_version_tooltip = pt_version_tooltip_stream.str();
wcscpy_s(tray_icon_data.szTip, sizeof(tray_icon_data.szTip) / sizeof(WCHAR), pt_version_tooltip.c_str());
tray_icon_data.uFlags = NIF_ICON | NIF_TIP | NIF_MESSAGE;
ChangeWindowMessageFilterEx(hwnd, WM_COMMAND, MSGFLT_ALLOW, nullptr);

View File

@@ -3,7 +3,7 @@
#include <string>
// Start the Tray Icon
void start_tray_icon();
void start_tray_icon(bool isProcessElevated);
// Stop the Tray Icon
void stop_tray_icon();
// Open the Settings Window

View File

@@ -50,8 +50,8 @@ namespace Microsoft.PowerToys.Settings.UI
NativeMethods.SetWindowPlacement(hWnd, ref placement);
var loader = Helpers.ResourceLoaderInstance.ResourceLoader;
Title = loader.GetString("SettingsWindow_Title");
var loader = ResourceLoaderInstance.ResourceLoader;
Title = App.IsElevated ? loader.GetString("SettingsWindow_AdminTitle") : loader.GetString("SettingsWindow_Title");
// send IPC Message
ShellPage.SetDefaultSndMessageCallback(msg =>

View File

@@ -49,7 +49,6 @@
Source="/Assets/Settings/icon.ico" />
<TextBlock
x:Name="AppTitleBarText"
x:Uid="SettingsWindow_TitleTxt"
Margin="12,0,0,0"
VerticalAlignment="Center"
Style="{StaticResource CaptionTextBlockStyle}"

View File

@@ -373,6 +373,8 @@ namespace Microsoft.PowerToys.Settings.UI.Views
// https://docs.microsoft.com/windows/apps/develop/title-bar?tabs=winui3#full-customization
u.ExtendsContentIntoTitleBar = true;
u.SetTitleBar(AppTitleBar);
var loader = ResourceLoaderInstance.ResourceLoader;
AppTitleBarText.Text = App.IsElevated ? loader.GetString("SettingsWindow_AdminTitle") : loader.GetString("SettingsWindow_Title");
#if DEBUG
DebugMessage.Visibility = Visibility.Visible;
#endif

View File

@@ -2152,6 +2152,7 @@ From there, simply click on one of the supported files in the File Explorer and
</data>
<data name="SettingsWindow_Title" xml:space="preserve">
<value>PowerToys Settings</value>
<comment>Title of the settings window when running as user</comment>
</data>
<data name="Awake.ModuleTitle" xml:space="preserve">
<value>Awake</value>
@@ -3612,9 +3613,6 @@ Activate by holding the key for the character you want to add an accent to, then
<value>Maximum width (px)</value>
<comment>px = pixels</comment>
</data>
<data name="SettingsWindow_TitleTxt.Text" xml:space="preserve">
<value>PowerToys Settings</value>
</data>
<data name="Oobe_Peek.Description" xml:space="preserve">
<value>A lightning fast file preview feature for Windows.</value>
</data>
@@ -3674,4 +3672,8 @@ Activate by holding the key for the character you want to add an accent to, then
<data name="MouseUtils_FindMyMouse_ActivationShortcut.Header" xml:space="preserve">
<value>Activation shortcut</value>
</data>
<data name="SettingsWindow_AdminTitle" xml:space="preserve">
<value>Administrator: PowerToys Settings</value>
<comment>Title of the settings window when running as administrator</comment>
</data>
</root>