Skip to content

Commit

Permalink
Maui Misc
Browse files Browse the repository at this point in the history
  • Loading branch information
AigioL committed Jun 7, 2022
1 parent 12393aa commit 50cd52d
Show file tree
Hide file tree
Showing 19 changed files with 186 additions and 32 deletions.
5 changes: 3 additions & 2 deletions src/Common.ClientLib/Application/Essentials/DeviceInfo2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,10 @@ public static DeviceIdiom Idiom()
var i = IDeviceInfoPlatformService.Interface;
if (i != null && i.IsUWP)
{
return OSNames.Value.UWP;
if (i.IsWinUI) return OSNames.Value.WinUI;
if (i.IsUWP) return OSNames.Value.UWP;
}
else if (DesktopBridge.IsRunningAsUwp)
if (DesktopBridge.IsRunningAsUwp)
{
return OSNames.Value.WindowsDesktopBridge;
}
Expand Down
10 changes: 9 additions & 1 deletion src/Common.ClientLib/Application/Essentials/MainThread2.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,15 @@ public static void BeginInvokeOnMainThread(Action action, ThreadingDispatcherPri
}
else
{
IMainThreadPlatformService.Instance.PlatformBeginInvokeOnMainThread(action, priority);
try
{
IMainThreadPlatformService.Instance.PlatformBeginInvokeOnMainThread(action, priority);
}
catch (InvalidOperationException)
{
// https://github.com/dotnet/maui/blob/48840b8dd4f63e298ac63af7f9696f7e0581589c/src/Essentials/src/MainThread/MainThread.uwp.cs#L16-L20
action();
}
}
}

Expand Down
5 changes: 5 additions & 0 deletions src/Common.CoreLib/Application/OSName.cs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,11 @@ public enum Value : byte
/// https://github.com/chromeos
/// </summary>
ChromeOS,

/// <summary>
/// Windows UI 库 (WinUI) 3
/// </summary>
WinUI,
}

public static string ToDisplayName(this Value value) => value == default ? string.Empty :
Expand Down
5 changes: 5 additions & 0 deletions src/Common.CoreLib/Platform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,11 @@ public enum Platform
/// </summary>
[Obsolete]
UWP = 64,

/// <summary>
/// Windows UI 库 (WinUI) 3
/// </summary>
WinUI = 128,
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion src/Common.Essentials.Xamarin/EssentialsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -135,15 +135,17 @@ public static XEEmailAttachment Convert(this IEmailAttachment value)
nameof(DevicePlatform.Android) => Platform.Android,
#pragma warning disable IDE0079 // 请删除不必要的忽略
#pragma warning disable CS0618 // 类型或成员已过时
#pragma warning disable CS0612 // 类型或成员已过时
nameof(DevicePlatform.UWP) => Platform.UWP,
#pragma warning restore CS0612 // 类型或成员已过时
#pragma warning restore CS0618 // 类型或成员已过时
#pragma warning restore IDE0079 // 请删除不必要的忽略
nameof(DevicePlatform.iOS) or
nameof(DevicePlatform.tvOS) or
nameof(DevicePlatform.watchOS) => Platform.Apple,
nameof(DevicePlatform.Tizen) => Platform.Linux,
#if MAUI
nameof(DevicePlatform.WinUI) => Platform.UWP,
nameof(DevicePlatform.WinUI) => Platform.WinUI,
nameof(DevicePlatform.MacCatalyst) => Platform.Apple,
#endif
_ => Platform.Unknown,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,5 +20,12 @@ public class DeviceInfoPlatformServiceImpl : IDeviceInfoPlatformService
#pragma warning restore CS0618 // 类型或成员已过时
#pragma warning restore IDE0079 // 请删除不必要的忽略

public virtual bool IsWinUI =>
#if MAUI
DeviceInfo.Platform == DevicePlatform.WinUI;
#else
false;
#endif

public virtual DeviceIdiom Idiom => DeviceInfo.Idiom.Convert();
}
2 changes: 2 additions & 0 deletions src/Common.Essentials/Services/IDeviceInfoPlatformService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ public interface IDeviceInfoPlatformService

bool IsUWP { get; }

bool IsWinUI { get; }

DeviceIdiom Idiom { get; }

static EPlatform Platform
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
using Avalonia.Controls;
using Avalonia.Controls.ApplicationLifetimes;
using System.Application.Mvvm;
using System.Application.Services;
using System.Application.UI.ViewModels;
using System.Linq;
using System.Threading.Tasks;
using AvaloniaApplication = Avalonia.Application;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
using Microsoft.Extensions.DependencyInjection.Extensions;
using System.Application.Services;
#if __ANDROID__
#if MAUI
using WindowManagerImpl_ = System.Application.Services.Implementation.MauiWindowManagerImpl;
#elif __ANDROID__
using WindowManagerImpl_ = System.Application.Services.Implementation.AndroidWindowManagerImpl;
#elif AVALONIA
using WindowManagerImpl_ = System.Application.Services.Implementation.AvaloniaWindowManagerImpl;
Expand Down
1 change: 1 addition & 0 deletions src/ST.Client.Maui/ST.Client.Maui.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
<Compile Include="..\ST.Client.XamarinForms\UI\Views\Base\BaseContentPage.cs">
<Link>UI\Views\Base\BaseContentPage.cs</Link>
</Compile>
<Compile Include="..\ST.Client.Desktop.Avalonia\ServiceCollectionExtensions.AddWindowManager.cs" />
</ItemGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// https://docs.microsoft.com/zh-cn/dotnet/maui/fundamentals/windows

using System.Application.UI;
using System.Application.UI.ViewModels;

namespace System.Application.Services.Implementation
{
/// <inheritdoc cref="IWindowManager"/>
internal sealed class MauiWindowManagerImpl : IWindowManagerImpl
{
readonly IMauiApplication app;

public MauiWindowManagerImpl(IMauiApplication app)
{
this.app = app;
}

Type? IWindowManagerImpl.WindowType => typeof(Page);

public Task Show<TWindowViewModel>(CustomWindow customWindow, TWindowViewModel? viewModel = null, string title = "", ResizeMode resizeMode = ResizeMode.NoResize, bool isDialog = false, bool isParent = true) where TWindowViewModel : WindowViewModel, new()
{
throw new NotImplementedException();
}

public Task Show(Type typeWindowViewModel, CustomWindow customWindow, WindowViewModel? viewModel = null, string title = "", ResizeMode resizeMode = ResizeMode.NoResize, bool isDialog = false, bool isParent = true)
{
throw new NotImplementedException();
}

public Task Show(CustomWindow customWindow, WindowViewModel? viewModel = null, string title = "", ResizeMode resizeMode = ResizeMode.NoResize, bool isDialog = false, bool isParent = true)
{
throw new NotImplementedException();
}

public Task<bool> ShowDialog<TWindowViewModel>(CustomWindow customWindow, TWindowViewModel? viewModel = null, string title = "", ResizeMode resizeMode = ResizeMode.NoResize, bool isDialog = true, bool isParent = true) where TWindowViewModel : WindowViewModel, new()
{
throw new NotImplementedException();
}

public Task ShowDialog(Type typeWindowViewModel, CustomWindow customWindow, WindowViewModel? viewModel = null, string title = "", ResizeMode resizeMode = ResizeMode.NoResize, bool isDialog = true)
{
throw new NotImplementedException();
}

public Task ShowDialog(CustomWindow customWindow, WindowViewModel? viewModel = null, string title = "", ResizeMode resizeMode = ResizeMode.NoResize, bool isDialog = true)
{
throw new NotImplementedException();
}
}
}
59 changes: 58 additions & 1 deletion src/ST.Client.Maui/UI/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
using System.Application.Mvvm;
using System.Application.Services;
using System.Application.Settings;
using System.Application.UI.Styles;
using System.Application.UI.ViewModels;
using System.Reactive.Disposables;
using System.Windows.Input;
using EAppTheme = System.Application.Models.AppTheme;
Expand All @@ -8,7 +11,7 @@

namespace System.Application.UI;

public partial class App : MauiApplication, IDisposableHolder, IApplication
public partial class App : MauiApplication, IDisposableHolder, IApplication, IMauiApplication
{
public static App Instance => Current is App app ? app : throw new ArgumentNullException(nameof(app));

Expand All @@ -25,9 +28,63 @@ public App(IApplication.IDesktopProgramHost host)
ProgramHost = host;

InitializeComponent();

AppTheme = PlatformAppTheme;
RequestedThemeChanged += (_, e) => AppTheme = e.RequestedTheme;

MainPage = new AppShell();
Initialize();
}

public Window? MainWindow => Windows.Count >= 1 ? Windows[0] : null;

MauiApplication IMauiApplication.Current => this;

void Initialize()
{
const bool isTrace =
#if StartWatchTrace
true;
#else
false;
#endif
ProgramHost.OnCreateAppExecuted(handlerViewModelManager: vmService =>
{
switch (vmService.MainWindow)
{
case AchievementWindowViewModel:
ProgramHost.IsMinimize = false;
//MainWindow = new AchievementWindow();
break;
default:
#region 主窗口启动时加载的资源
#if !UI_DEMO
compositeDisposable.Add(SettingsHost.Save);
compositeDisposable.Add(ProxyService.Current.Dispose);
compositeDisposable.Add(SteamConnectService.Current.Dispose);
compositeDisposable.Add(ASFService.Current.StopASF);
#pragma warning disable CA1416 // 验证平台兼容性
if (GeneralSettings.IsStartupAppMinimized.Value)
ProgramHost.IsMinimize = true;
#pragma warning restore CA1416 // 验证平台兼容性
#endif
#endregion
//MainWindow = new MainWindow();
break;
}
MainPage!.BindingContext = vmService.MainWindow;
}, isTrace: isTrace);
}

public Window GetActiveWindow()
{
var activeWindow = Windows.FirstOrDefault(x => x.IsActivated());
if (activeWindow != null)
{
return activeWindow;
}
return MainWindow!;
}

#region IDisposable members
Expand Down
2 changes: 2 additions & 0 deletions src/ST.Client.Maui/UI/AppShell.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using System.Application.Services;

namespace System.Application.UI;

public partial class AppShell : Shell
Expand Down
15 changes: 15 additions & 0 deletions src/ST.Client.Maui/UI/IMauiApplication.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using MauiApplication = Microsoft.Maui.Controls.Application;

namespace System.Application.UI
{
public interface IMauiApplication : IApplication
{
static new IMauiApplication Instance => DI.Get<IMauiApplication>();

Window? MainWindow { get; }

MauiApplication Current { get; }

Window GetActiveWindow();
}
}
4 changes: 2 additions & 2 deletions src/ST.Client/Services/Mvvm/SteamConnectService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -493,7 +493,7 @@ public async void RefreshSteamUsers()
IReadOnlyDictionary<long, string?>? accountRemarks = SteamAccountSettings.AccountRemarks.Value;

List<(string title, string applicationPath, string iconResourcePath, string arguments, string description, string customCategory)>? jumplistData = OperatingSystem2.IsWindows() ? new() : null;
foreach (var user in SteamConnectService.Current.SteamUsers.Items)
foreach (var user in SteamUsers.Items)
{
if (accountRemarks?.TryGetValue(user.SteamId64, out var remark) == true &&
!string.IsNullOrEmpty(remark))
Expand Down Expand Up @@ -526,7 +526,7 @@ public async void RefreshSteamUsers()
});
}

SteamConnectService.Current.SteamUsers.Refresh();
SteamUsers.Refresh();
#endregion

#region 通过webapi加载头像图片用户信息
Expand Down
17 changes: 9 additions & 8 deletions src/ST.Client/Settings/SteamSettings.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Application.Models;
using System.Application.UI;
using System.Runtime.Versioning;

namespace System.Application.Settings
Expand All @@ -10,7 +11,7 @@ public sealed class SteamSettings : SettingsHost2<SteamSettings>
{
static SteamSettings()
{
if (!OperatingSystem2.Application.UseAvalonia()) return;
if (!IApplication.IsDesktopPlatform) return;
IsRunSteamMinimized.ValueChanged += IsRunSteamMinimized_ValueChanged;
IsRunSteamNoCheckUpdate.ValueChanged += IsRunSteamNoCheckUpdate_ValueChanged;
}
Expand All @@ -31,7 +32,7 @@ static void IsRunSteamMinimized_ValueChanged(object? sender, ValueChangedEventAr
SteamStratParameter.Value = SteamStratParameter.Value.Replace("-silent", "").Trim();
}

static readonly SerializableProperty<string?>? _SteamStratParameter = OperatingSystem2.Application.UseAvalonia() ?
static readonly SerializableProperty<string?>? _SteamStratParameter = IApplication.IsDesktopPlatform ?
GetProperty<string?>(defaultValue: null, autoSave: true) : null;

/// <summary>
Expand All @@ -42,7 +43,7 @@ static void IsRunSteamMinimized_ValueChanged(object? sender, ValueChangedEventAr
[SupportedOSPlatform("Linux")]
public static SerializableProperty<string?> SteamStratParameter => _SteamStratParameter ?? throw new PlatformNotSupportedException();

//static readonly SerializableProperty<string>? _SteamSkin = OperatingSystem2.Application.UseAvalonia() ? GetProperty(defaultValue: string.Empty, autoSave: true) : null;
//static readonly SerializableProperty<string>? _SteamSkin = IApplication.IsDesktopPlatform ? GetProperty(defaultValue: string.Empty, autoSave: true) : null;
///// <summary>
///// Steam皮肤
///// </summary>
Expand All @@ -51,7 +52,7 @@ static void IsRunSteamMinimized_ValueChanged(object? sender, ValueChangedEventAr
//[SupportedOSPlatform("Linux")]
//public static SerializableProperty<string> SteamSkin => _SteamSkin ?? throw new PlatformNotSupportedException();

static readonly SerializableProperty<bool>? _IsAutoRunSteam = OperatingSystem2.Application.UseAvalonia() ? GetProperty(defaultValue: false, autoSave: true) : null;
static readonly SerializableProperty<bool>? _IsAutoRunSteam = IApplication.IsDesktopPlatform ? GetProperty(defaultValue: false, autoSave: true) : null;

/// <summary>
/// 自动运行Steam
Expand All @@ -61,7 +62,7 @@ static void IsRunSteamMinimized_ValueChanged(object? sender, ValueChangedEventAr
[SupportedOSPlatform("Linux")]
public static SerializableProperty<bool> IsAutoRunSteam => _IsAutoRunSteam ?? throw new PlatformNotSupportedException();

static readonly SerializableProperty<bool>? _IsRunSteamMinimized = OperatingSystem2.Application.UseAvalonia() ? GetProperty(defaultValue: false, autoSave: true) : null;
static readonly SerializableProperty<bool>? _IsRunSteamMinimized = IApplication.IsDesktopPlatform ? GetProperty(defaultValue: false, autoSave: true) : null;

/// <summary>
/// Steam启动时最小化到托盘
Expand All @@ -71,7 +72,7 @@ static void IsRunSteamMinimized_ValueChanged(object? sender, ValueChangedEventAr
[SupportedOSPlatform("Linux")]
public static SerializableProperty<bool> IsRunSteamMinimized => _IsRunSteamMinimized ?? throw new PlatformNotSupportedException();

static readonly SerializableProperty<bool>? _IsRunSteamNoCheckUpdate = OperatingSystem2.Application.UseAvalonia() ? GetProperty(defaultValue: false, autoSave: true) : null;
static readonly SerializableProperty<bool>? _IsRunSteamNoCheckUpdate = IApplication.IsDesktopPlatform ? GetProperty(defaultValue: false, autoSave: true) : null;

/// <summary>
/// Steam启动时不检查更新
Expand All @@ -81,7 +82,7 @@ static void IsRunSteamMinimized_ValueChanged(object? sender, ValueChangedEventAr
[SupportedOSPlatform("Linux")]
public static SerializableProperty<bool> IsRunSteamNoCheckUpdate => _IsRunSteamNoCheckUpdate ?? throw new PlatformNotSupportedException();

static readonly SerializableProperty<bool>? _IsEnableSteamLaunchNotification = OperatingSystem2.Application.UseAvalonia() ? GetProperty(defaultValue: true, autoSave: true) : null;
static readonly SerializableProperty<bool>? _IsEnableSteamLaunchNotification = IApplication.IsDesktopPlatform ? GetProperty(defaultValue: true, autoSave: true) : null;

/// <summary>
/// 检测到Steam登录时弹出消息通知
Expand All @@ -97,6 +98,6 @@ static void IsRunSteamMinimized_ValueChanged(object? sender, ValueChangedEventAr
[SupportedOSPlatform("Windows7.0")]
[SupportedOSPlatform("macOS")]
[SupportedOSPlatform("Linux")]
public static SerializableProperty<SystemEndMode>? DownloadCompleteSystemEndMode = OperatingSystem2.Application.UseAvalonia() ? GetProperty(defaultValue: SystemEndMode.Sleep, autoSave: true) : null;
public static SerializableProperty<SystemEndMode>? DownloadCompleteSystemEndMode = IApplication.IsDesktopPlatform ? GetProperty(defaultValue: SystemEndMode.Sleep, autoSave: true) : null;
}
}
6 changes: 5 additions & 1 deletion src/ST.Client/UI/IApplication.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Application.Settings;
using System.Application.UI.Resx;
using System.Collections.Generic;
using System.Runtime.Versioning;
using System.Text;
using System.Threading.Tasks;

Expand All @@ -16,7 +17,10 @@ public partial interface IApplication
/// <summary>
/// IsWindows or IsMacOS or (IsLinux and !IsAndroid)
/// </summary>
public static readonly bool IsDesktopPlatform = OperatingSystem2.IsWindows() || OperatingSystem2.IsMacOS() || (OperatingSystem2.IsLinux() && !OperatingSystem2.IsAndroid());
[SupportedOSPlatformGuard("Windows7.0")]
[SupportedOSPlatformGuard("macOS")]
[SupportedOSPlatformGuard("Linux")]
static readonly bool IsDesktopPlatform = OperatingSystem2.IsWindows() || OperatingSystem2.IsMacOS() || (OperatingSystem2.IsLinux() && !OperatingSystem2.IsAndroid());

static IApplication Instance => DI.Get<IApplication>();

Expand Down
1 change: 0 additions & 1 deletion src/ST.Client/UI/ViewModels/General/WindowViewModel.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using ReactiveUI;
using System;
using System.Application.Models;
using System.Application.Services;
using System.Application.Settings;
Expand Down
Loading

0 comments on commit 50cd52d

Please sign in to comment.