From eef90d9aaf240972569bb04039cf71bbc999e234 Mon Sep 17 00:00:00 2001 From: Aigio Liu Date: Thu, 17 Mar 2022 19:01:28 +0800 Subject: [PATCH] IsAdministrator Impl => ASF IsRunningAsRoot Add OnMainOpenTryShowNewVersionWindow --- .../Views/MainWindow.axaml.cs | 40 +++++++++++-------- .../AvaloniaApplicationUpdateServiceImpl.cs | 26 +++++++++++- .../LinuxPlatformServiceImpl.Administrator.cs | 28 ++++++------- .../MacPlatformServiceImpl.IsAdministrator.cs | 38 +++++++++--------- ...indowsPlatformServiceImpl.Administrator.cs | 38 +++++++++--------- .../Services/IApplicationUpdateService.cs | 10 +++++ .../IPlatformService.IsAdministrator.cs | 19 +++++++++ .../Services/IPlatformService.MacOS.cs | 1 - src/ST.Client/Services/IPlatformService.cs | 5 --- .../ApplicationUpdateServiceBaseImpl.cs | 12 ++++++ .../Implementation/NotificationServiceImpl.cs | 1 - src/ST.Client/UI/IApplication.Helper.cs | 1 + .../UI/Resx/AppResources.Designer.cs | 11 ++++- src/ST.Client/UI/Resx/AppResources.resx | 5 ++- 14 files changed, 157 insertions(+), 78 deletions(-) create mode 100644 src/ST.Client/Services/IPlatformService.IsAdministrator.cs diff --git a/src/ST.Client.Desktop.Avalonia.App/Views/MainWindow.axaml.cs b/src/ST.Client.Desktop.Avalonia.App/Views/MainWindow.axaml.cs index 37998babe..ac5c5b3b2 100644 --- a/src/ST.Client.Desktop.Avalonia.App/Views/MainWindow.axaml.cs +++ b/src/ST.Client.Desktop.Avalonia.App/Views/MainWindow.axaml.cs @@ -2,6 +2,7 @@ using Avalonia; using Avalonia.Controls; using Avalonia.Data; using Avalonia.Markup.Xaml; +using System.Application.Services; using System.Application.Settings; using System.Application.UI.ViewModels; using System.Application.UI.Views.Controls; @@ -42,29 +43,29 @@ namespace System.Application.UI.Views #endif } -// protected override void OnClosing(CancelEventArgs e) -// { -//#if !UI_DEMO -// if (StartupOptions.Value.HasNotifyIcon) -// { -// //IsHideWindow = true; -// //e.Cancel = true; -// //Hide(); + // protected override void OnClosing(CancelEventArgs e) + // { + //#if !UI_DEMO + // if (StartupOptions.Value.HasNotifyIcon) + // { + // //IsHideWindow = true; + // //e.Cancel = true; + // //Hide(); -// //if (ViewModel is not null) -// // foreach (var tab in ViewModel.TabItems) -// // tab.Deactivation(); -// } -//#endif -// base.OnClosing(e); -// } + // //if (ViewModel is not null) + // // foreach (var tab in ViewModel.TabItems) + // // tab.Deactivation(); + // } + //#endif + // base.OnClosing(e); + // } protected override void OnClosed(EventArgs e) { #if !UI_DEMO if (StartupOptions.Value.HasNotifyIcon) { - if (App.Current.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop) + if (App.Current!.ApplicationLifetime is Avalonia.Controls.ApplicationLifetimes.IClassicDesktopStyleApplicationLifetime desktop) { desktop.MainWindow = App.Instance.MainWindow = null; } @@ -77,6 +78,13 @@ namespace System.Application.UI.Views //base.OnClosed(e); } + protected override void OnOpened(EventArgs e) + { + base.OnOpened(e); + + IApplicationUpdateService.Instance.OnMainOpenTryShowNewVersionWindow(); + } + //protected override void FluentWindow_Opened(object? sender, EventArgs e) //{ // if (ViewModel is not null) diff --git a/src/ST.Client.Desktop.Avalonia/Application/Services/Implementation/AvaloniaApplicationUpdateServiceImpl.cs b/src/ST.Client.Desktop.Avalonia/Application/Services/Implementation/AvaloniaApplicationUpdateServiceImpl.cs index beda6545a..b48d2c03f 100644 --- a/src/ST.Client.Desktop.Avalonia/Application/Services/Implementation/AvaloniaApplicationUpdateServiceImpl.cs +++ b/src/ST.Client.Desktop.Avalonia/Application/Services/Implementation/AvaloniaApplicationUpdateServiceImpl.cs @@ -1,6 +1,8 @@ using Microsoft.Extensions.Options; using System.Application.Models; using System.Application.UI; +using System.Application.UI.Resx; +using System.Threading.Tasks; namespace System.Application.Services.Implementation { @@ -9,8 +11,10 @@ namespace System.Application.Services.Implementation { readonly IAvaloniaApplication app; readonly IWindowManager windowManager; + readonly INotificationService notificationService; public AvaloniaApplicationUpdateServiceImpl( IAvaloniaApplication app, + INotificationService notificationService, IWindowManager windowManager, IToast toast, ICloudServiceClient client, @@ -18,6 +22,21 @@ namespace System.Application.Services.Implementation { this.app = app; this.windowManager = windowManager; + this.notificationService = notificationService; + } + + protected override async Task ShowNewVersionWindowAsync() + { + await windowManager.Show(typeof(object), CustomWindow.NewVersion, isParent: false); + } + + public override async void OnMainOpenTryShowNewVersionWindow() + { + if (ShowNewVersionWindowOnMainOpen) + { + ShowNewVersionWindowOnMainOpen = false; + await ShowNewVersionWindowAsync(); + } } protected override async void OnExistNewVersion() @@ -25,7 +44,12 @@ namespace System.Application.Services.Implementation var hasActiveWindow = app.HasActiveWindow(); if (hasActiveWindow) { - await windowManager.Show(typeof(object), CustomWindow.NewVersion, isParent: false); + await ShowNewVersionWindowAsync(); + } + else + { + ShowNewVersionWindowOnMainOpen = true; + notificationService.Notify(AppResources.NewVersionUpdateNotifyText_.Format(NewVersionInfo?.Version), NotificationType.NewVersion); } } diff --git a/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxPlatformServiceImpl.Administrator.cs b/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxPlatformServiceImpl.Administrator.cs index b852cb2ae..dc14bbfdb 100644 --- a/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxPlatformServiceImpl.Administrator.cs +++ b/src/ST.Client.Desktop.Linux/Services/Implementation/LinuxPlatformServiceImpl.Administrator.cs @@ -1,17 +1,17 @@ -namespace System.Application.Services.Implementation -{ - partial class LinuxPlatformServiceImpl - { - static readonly Lazy mIsAdministrator = new(IsAdministrator_); +//namespace System.Application.Services.Implementation +//{ +// partial class LinuxPlatformServiceImpl +// { +// static readonly Lazy mIsAdministrator = new(IsAdministrator_); - static bool IsAdministrator_() => string.Equals(Environment.UserName, "root", StringComparison.Ordinal); +// static bool IsAdministrator_() => string.Equals(Environment.UserName, "root", StringComparison.Ordinal); - /// - /// 是否是管理员权限 - /// - /// - public static bool IsAdministrator => mIsAdministrator.Value; +// /// +// /// 是否是管理员权限 +// /// +// /// +// public static bool IsAdministrator => mIsAdministrator.Value; - bool IPlatformService.IsAdministrator => IsAdministrator; - } -} \ No newline at end of file +// bool IPlatformService.IsAdministrator => IsAdministrator; +// } +//} \ No newline at end of file diff --git a/src/ST.Client.Desktop.Mac/Services/Implementation/MacPlatformServiceImpl.IsAdministrator.cs b/src/ST.Client.Desktop.Mac/Services/Implementation/MacPlatformServiceImpl.IsAdministrator.cs index ff38d85a7..c7e75bd33 100644 --- a/src/ST.Client.Desktop.Mac/Services/Implementation/MacPlatformServiceImpl.IsAdministrator.cs +++ b/src/ST.Client.Desktop.Mac/Services/Implementation/MacPlatformServiceImpl.IsAdministrator.cs @@ -1,23 +1,23 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; +//using System; +//using System.Collections.Generic; +//using System.Linq; +//using System.Text; +//using System.Threading.Tasks; -namespace System.Application.Services.Implementation -{ - partial class MacPlatformServiceImpl - { - static readonly Lazy mIsAdministrator = new(IsAdministrator_); +//namespace System.Application.Services.Implementation +//{ +// partial class MacPlatformServiceImpl +// { +// static readonly Lazy mIsAdministrator = new(IsAdministrator_); - static bool IsAdministrator_() => string.Equals(Environment.UserName, "root", StringComparison.OrdinalIgnoreCase); +// static bool IsAdministrator_() => string.Equals(Environment.UserName, "root", StringComparison.OrdinalIgnoreCase); - /// - /// 是否是管理员权限 - /// - /// - public static bool IsAdministrator => mIsAdministrator.Value; +// /// +// /// 是否是管理员权限 +// /// +// /// +// public static bool IsAdministrator => mIsAdministrator.Value; - bool IPlatformService.IsAdministrator => IsAdministrator; - } -} +// bool IPlatformService.IsAdministrator => IsAdministrator; +// } +//} diff --git a/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsPlatformServiceImpl.Administrator.cs b/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsPlatformServiceImpl.Administrator.cs index c3f28b655..1c093bc07 100644 --- a/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsPlatformServiceImpl.Administrator.cs +++ b/src/ST.Client.Desktop.Windows/Services/Implementation/WindowsPlatformServiceImpl.Administrator.cs @@ -1,25 +1,25 @@ -using System.Security.Principal; +//using System.Security.Principal; -namespace System.Application.Services.Implementation -{ - partial class WindowsPlatformServiceImpl - { - #region Administrator +//namespace System.Application.Services.Implementation +//{ +// partial class WindowsPlatformServiceImpl +// { +// #region Administrator - static readonly Lazy mIsAdministrator = new(IsAdministrator_); +// static readonly Lazy mIsAdministrator = new(IsAdministrator_); - static bool IsAdministrator_() - => new WindowsPrincipal(WindowsIdentity.GetCurrent()) - .IsInRole(WindowsBuiltInRole.Administrator); +// static bool IsAdministrator_() +// => new WindowsPrincipal(WindowsIdentity.GetCurrent()) +// .IsInRole(WindowsBuiltInRole.Administrator); - /// - /// 是否是管理员权限 - /// - /// - public static bool IsAdministrator => mIsAdministrator.Value; +// /// +// /// 是否是管理员权限 +// /// +// /// +// public static bool IsAdministrator => mIsAdministrator.Value; - bool IPlatformService.IsAdministrator => IsAdministrator; +// bool IPlatformService.IsAdministrator => IsAdministrator; - #endregion - } -} \ No newline at end of file +// #endregion +// } +//} \ No newline at end of file diff --git a/src/ST.Client/Services/IApplicationUpdateService.cs b/src/ST.Client/Services/IApplicationUpdateService.cs index b694ab3b6..fddbeeefe 100644 --- a/src/ST.Client/Services/IApplicationUpdateService.cs +++ b/src/ST.Client/Services/IApplicationUpdateService.cs @@ -125,5 +125,15 @@ namespace System.Application.Services } } } + + /// + /// 是否需要在主窗口显示时显示新版本通知窗口 + /// + bool ShowNewVersionWindowOnMainOpen { get; } + + /// + /// 在主窗口显示时调用此函数检查是否需要显示新版本通知窗口 + /// + void OnMainOpenTryShowNewVersionWindow(); } } \ No newline at end of file diff --git a/src/ST.Client/Services/IPlatformService.IsAdministrator.cs b/src/ST.Client/Services/IPlatformService.IsAdministrator.cs new file mode 100644 index 000000000..0edc1bc68 --- /dev/null +++ b/src/ST.Client/Services/IPlatformService.IsAdministrator.cs @@ -0,0 +1,19 @@ +using System.Collections.Generic; +using System.Text; + +namespace System.Application.Services +{ + partial interface IPlatformService + { + /// + /// 当前程序是否以 Administrator/System(Windows) 或 Root(FreeBSD/Linux/MacOS/Android/iOS) 权限运行 + /// + bool IsAdministrator + { + get + { + return ArchiSteamFarm.Core.OS.IsRunningAsRoot(); + } + } + } +} diff --git a/src/ST.Client/Services/IPlatformService.MacOS.cs b/src/ST.Client/Services/IPlatformService.MacOS.cs index 8c319e8d3..f2bb8181f 100644 --- a/src/ST.Client/Services/IPlatformService.MacOS.cs +++ b/src/ST.Client/Services/IPlatformService.MacOS.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Text; using System.Runtime.Versioning; diff --git a/src/ST.Client/Services/IPlatformService.cs b/src/ST.Client/Services/IPlatformService.cs index 88d869c74..55b0744e6 100644 --- a/src/ST.Client/Services/IPlatformService.cs +++ b/src/ST.Client/Services/IPlatformService.cs @@ -315,11 +315,6 @@ namespace System.Application.Services /// void SetLightOrDarkThemeFollowingSystem(bool enable); - /// - /// 当前程序是否以 Administrator 或 Root 权限运行 - /// - bool IsAdministrator => false; - /// /// 获取当前默认字体 /// diff --git a/src/ST.Client/Services/Implementation/ApplicationUpdateServiceBaseImpl.cs b/src/ST.Client/Services/Implementation/ApplicationUpdateServiceBaseImpl.cs index 4f15e848e..abaafbedd 100644 --- a/src/ST.Client/Services/Implementation/ApplicationUpdateServiceBaseImpl.cs +++ b/src/ST.Client/Services/Implementation/ApplicationUpdateServiceBaseImpl.cs @@ -33,6 +33,18 @@ namespace System.Application.Services.Implementation public ICommand StartUpdateCommand { get; } + public bool ShowNewVersionWindowOnMainOpen { get; protected set; } + + protected virtual Task ShowNewVersionWindowAsync() + { + return Task.CompletedTask; + } + + public virtual void OnMainOpenTryShowNewVersionWindow() + { + + } + public ApplicationUpdateServiceBaseImpl( IApplication application, IToast toast, diff --git a/src/ST.Client/Services/Implementation/NotificationServiceImpl.cs b/src/ST.Client/Services/Implementation/NotificationServiceImpl.cs index 605d14ffa..84feb3a66 100644 --- a/src/ST.Client/Services/Implementation/NotificationServiceImpl.cs +++ b/src/ST.Client/Services/Implementation/NotificationServiceImpl.cs @@ -1,4 +1,3 @@ -using System; using System.Collections.Generic; using System.Properties; using System.Text; diff --git a/src/ST.Client/UI/IApplication.Helper.cs b/src/ST.Client/UI/IApplication.Helper.cs index 69a86bfeb..ec8e01f87 100644 --- a/src/ST.Client/UI/IApplication.Helper.cs +++ b/src/ST.Client/UI/IApplication.Helper.cs @@ -31,6 +31,7 @@ namespace System.Application.UI if (programName == null) throw new ArgumentNullException(nameof(programName)); ProgramName = programName; + } /// diff --git a/src/ST.Client/UI/Resx/AppResources.Designer.cs b/src/ST.Client/UI/Resx/AppResources.Designer.cs index 5a8428fd5..083ea34bf 100644 --- a/src/ST.Client/UI/Resx/AppResources.Designer.cs +++ b/src/ST.Client/UI/Resx/AppResources.Designer.cs @@ -1446,7 +1446,7 @@ namespace System.Application.UI.Resx { } /// - /// 查找类似 启动加速服务失败,遇到未知错误,具体原因请查看logs日志。 的本地化字符串。 + /// 查找类似 启动加速服务失败,遇到未知错误,具体原因请在 [设置] - [通用设置] - [存储空间] - [打开 Logs 文件夹] 查看当天日志。 的本地化字符串。 /// public static string CommunityFix_StartProxyFaild { get { @@ -3654,6 +3654,15 @@ namespace System.Application.UI.Resx { } } + /// + /// 查找类似 检测到新版本 v{0} 的本地化字符串。 + /// + public static string NewVersionUpdateNotifyText_ { + get { + return ResourceManager.GetString("NewVersionUpdateNotifyText_", resourceCulture); + } + } + /// /// 查找类似 Steam++ v{0} 版本更新 的本地化字符串。 /// diff --git a/src/ST.Client/UI/Resx/AppResources.resx b/src/ST.Client/UI/Resx/AppResources.resx index b7e91f36d..b64470e1f 100644 --- a/src/ST.Client/UI/Resx/AppResources.resx +++ b/src/ST.Client/UI/Resx/AppResources.resx @@ -448,7 +448,7 @@ AAAAA-BBBBB-CCCC3 一键加速 - 启动加速服务失败,遇到未知错误,具体原因请查看logs日志。 + 启动加速服务失败,遇到未知错误,具体原因请在 [设置] - [通用设置] - [存储空间] - [打开 Logs 文件夹] 查看当天日志。 加速服务启动失败,{0} 端口被占用。请关掉占用 {0} 端口的程序或者尝试使用系统代理模式。 @@ -2091,4 +2091,7 @@ AAAAA-BBBBB-CCCC3 控制台设置 + + 检测到新版本 v{0} + \ No newline at end of file