From b60bbede0f76afd957a634512bbd64cd70af4261 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E5=B9=BF=E5=BB=BA?= Date: Thu, 28 Sep 2023 12:06:24 +0800 Subject: [PATCH] add launch app page --- src/ElectronBot.Braincase/App.xaml.cs | 3 + src/ElectronBot.Braincase/Constants.cs | 2 + .../Controls/LaunchAppPage.xaml | 112 ++++++++++++++++ .../Controls/LaunchAppPage.xaml.cs | 29 ++++ .../ElectronBot.Braincase.csproj | 10 +- .../ViewModels/GestureAppConfigViewModel.cs | 57 +++++++- .../ViewModels/LaunchAppViewModel.cs | 126 ++++++++++++++++++ .../Views/GestureAppConfigPage.xaml | 89 +++++++++++-- 8 files changed, 415 insertions(+), 13 deletions(-) create mode 100644 src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml create mode 100644 src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml.cs create mode 100644 src/ElectronBot.Braincase/ViewModels/LaunchAppViewModel.cs diff --git a/src/ElectronBot.Braincase/App.xaml.cs b/src/ElectronBot.Braincase/App.xaml.cs index 2e84bf8..b6aa383 100644 --- a/src/ElectronBot.Braincase/App.xaml.cs +++ b/src/ElectronBot.Braincase/App.xaml.cs @@ -157,6 +157,9 @@ public partial class App : Application services.AddTransient(); services.AddTransient(); + services.AddTransient(); + services.AddTransient(); + services.AddTransient(); services.AddTransient(); diff --git a/src/ElectronBot.Braincase/Constants.cs b/src/ElectronBot.Braincase/Constants.cs index 34f2624..d2fe48b 100644 --- a/src/ElectronBot.Braincase/Constants.cs +++ b/src/ElectronBot.Braincase/Constants.cs @@ -18,6 +18,8 @@ public class Constants public const string RandomContentListKey = "RandomContentListKey"; + public const string LaunchAppListKey = "LaunchAppListKey"; + public const string EmojisFolder = "EmojisAction"; public const string EmojisTempFileFolder = "EmojisFileTemp"; diff --git a/src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml b/src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml new file mode 100644 index 0000000..47e5e14 --- /dev/null +++ b/src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml @@ -0,0 +1,112 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml.cs b/src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml.cs new file mode 100644 index 0000000..efe8246 --- /dev/null +++ b/src/ElectronBot.Braincase/Controls/LaunchAppPage.xaml.cs @@ -0,0 +1,29 @@ +// Copyright (c) Microsoft Corporation and Contributors. +// Licensed under the MIT License. + +using ElectronBot.Braincase; +using ElectronBot.Braincase.ViewModels; +using Microsoft.UI.Xaml.Controls; + +// To learn more about WinUI, the WinUI project structure, +// and more about our project templates, see: http://aka.ms/winui-project-info. + +namespace Controls +{ + /// + /// An empty page that can be used on its own or navigated to within a Frame. + /// + public sealed partial class LaunchAppPage : Page + { + public LaunchAppViewModel ViewModel + { + get; + } + + public LaunchAppPage() + { + ViewModel = App.GetService(); + this.InitializeComponent(); + } + } +} diff --git a/src/ElectronBot.Braincase/ElectronBot.Braincase.csproj b/src/ElectronBot.Braincase/ElectronBot.Braincase.csproj index 8993950..f37ba98 100644 --- a/src/ElectronBot.Braincase/ElectronBot.Braincase.csproj +++ b/src/ElectronBot.Braincase/ElectronBot.Braincase.csproj @@ -1,7 +1,7 @@  WinExe - net7.0-windows10.0.19041.0 + net7.0-windows10.0.22621.0 10.0.19041.0 ElectronBot.Braincase Assets/WindowIcon.ico @@ -71,6 +71,7 @@ + @@ -242,14 +243,14 @@ - + - + @@ -338,6 +339,9 @@ MSBuild:Compile + + $(DefaultXamlRuntime) + MSBuild:Compile diff --git a/src/ElectronBot.Braincase/ViewModels/GestureAppConfigViewModel.cs b/src/ElectronBot.Braincase/ViewModels/GestureAppConfigViewModel.cs index 05ee370..44dd91b 100644 --- a/src/ElectronBot.Braincase/ViewModels/GestureAppConfigViewModel.cs +++ b/src/ElectronBot.Braincase/ViewModels/GestureAppConfigViewModel.cs @@ -4,16 +4,21 @@ using System.Collections.ObjectModel; using System.Diagnostics; using System.Text; using System.Windows.Input; +using Windows.ApplicationModel; using CommunityToolkit.Mvvm.ComponentModel; using CommunityToolkit.Mvvm.Input; using ElectronBot.Braincase.Contracts.Services; using ElectronBot.Braincase.Contracts.ViewModels; using ElectronBot.Braincase.Helpers; using ElectronBot.Braincase.Models; +using Windows.Management.Deployment; +using Controls; +using Microsoft.UI.Xaml.Controls; +using ElectronBot.Braincase.Core.Models; namespace ElectronBot.Braincase.ViewModels; -public partial class GestureAppConfigViewModel : ObservableRecipient +public partial class GestureAppConfigViewModel : ObservableRecipient,INavigationAware { private readonly ILocalSettingsService _localSettingsService; @@ -24,6 +29,8 @@ public partial class GestureAppConfigViewModel : ObservableRecipient set => SetProperty(ref _gestureAppConfigs, value); } + + private readonly PackageManager _packageManager = new(); public List GestureLabels { get; set; } = new() { Constants.Land, @@ -38,6 +45,7 @@ public partial class GestureAppConfigViewModel : ObservableRecipient Constants.ThirdFinger, }; + [ObservableProperty] private ObservableCollection _appPackages; public GestureAppConfigViewModel(ILocalSettingsService localSettingsService) { _localSettingsService = localSettingsService; @@ -114,4 +122,51 @@ public partial class GestureAppConfigViewModel : ObservableRecipient }; GestureAppConfigs.Add(gestureAppConfig); } + + [RelayCommand] + public async Task AddLaunchApp() + { + try + { + var theme = App.GetService(); + var addLaunchApDialog = new ContentDialog() + { + Title = "AddRandomContentTitle".GetLocalized(), + PrimaryButtonText = "AddEmojisOkBtnContent".GetLocalized(), + CloseButtonText = "AddEmojisCancelBtnContent".GetLocalized(), + DefaultButton = ContentDialogButton.Primary, + XamlRoot = App.MainWindow.Content.XamlRoot, + Content = new LaunchAppPage(), + RequestedTheme = theme.Theme + }; + + addLaunchApDialog.Closed += LaunchAppDialog_Closed; + + await addLaunchApDialog.ShowAsync(); + } + catch + { + + } + } + + private async void LaunchAppDialog_Closed(ContentDialog sender, ContentDialogClosedEventArgs args) + { + var list = (await _localSettingsService.ReadSettingAsync>(Constants.LaunchAppListKey)) ?? new List(); + } + + public void OnNavigatedTo(object parameter) + { + Task.Run(() => + { + var apps = _packageManager.FindPackagesForUser(string.Empty) + .Where(p => p.IsFramework == false && !string.IsNullOrEmpty(p.DisplayName)).ToList(); + App.MainWindow.DispatcherQueue.TryEnqueue(() => AppPackages = new ObservableCollection(apps)); + }); + } + + public void OnNavigatedFrom() + { + + } } diff --git a/src/ElectronBot.Braincase/ViewModels/LaunchAppViewModel.cs b/src/ElectronBot.Braincase/ViewModels/LaunchAppViewModel.cs new file mode 100644 index 0000000..4d9facb --- /dev/null +++ b/src/ElectronBot.Braincase/ViewModels/LaunchAppViewModel.cs @@ -0,0 +1,126 @@ +using System.Collections.ObjectModel; +using System.Runtime.InteropServices; +using System.Text.RegularExpressions; +using System.Windows.Input; +using Windows.ApplicationModel; +using CommunityToolkit.Mvvm.ComponentModel; +using CommunityToolkit.Mvvm.Input; +using ElectronBot.Braincase.Contracts.Services; +using ElectronBot.Braincase.Controls; +using ElectronBot.Braincase.Helpers; +using ElectronBot.Braincase.Models; +using Microsoft.UI.Xaml; +using Microsoft.UI.Xaml.Controls; +using Microsoft.UI.Xaml.Media.Imaging; +using Services; +using Windows.Management.Deployment; +using Windows.Storage; + + +namespace ElectronBot.Braincase.ViewModels; + +public partial class LaunchAppViewModel : ObservableRecipient +{ + private readonly PackageManager _packageManager = new(); + + [ObservableProperty] private ObservableCollection _appPackages; + + [ObservableProperty] private string _win32Path; + + [ObservableProperty] private string _VoiceText; + + [ObservableProperty] private Package _selectPackage; + + private readonly ILocalSettingsService _localSettingsService; + + public LaunchAppViewModel(ILocalSettingsService localSettingsService) + { + _localSettingsService = localSettingsService; + } + + public void ComboBox_OnTextSubmitted(ComboBox sender, ComboBoxTextSubmittedEventArgs args) + { + if(args.Text is not null) + { + var text = args.Text; + var apps = _packageManager.FindPackagesForUser(string.Empty) + .Where(p => p.IsFramework == false && p.DisplayName.Contains(text)).ToList(); + + App.MainWindow.DispatcherQueue.TryEnqueue(() => AppPackages = new ObservableCollection(apps)); + } + else + { + Task.Run(() => + { + var apps = _packageManager.FindPackagesForUser(string.Empty) + .Where(p => p.IsFramework == false && !string.IsNullOrEmpty(p.DisplayName)).ToList(); + App.MainWindow.DispatcherQueue.TryEnqueue(() => AppPackages = new ObservableCollection(apps)); + }); + } + } + + public void RadioButtons_SelectionChanged(object sender, SelectionChangedEventArgs e) + { + var radioButtons = (RadioButtons)sender; + + if (radioButtons.SelectedIndex == 0) + { + Win32PathVisibility = Visibility.Collapsed; + MsixVisibility = Visibility.Visible; + Task.Run(() => + { + var apps = _packageManager.FindPackagesForUser(string.Empty) + .Where(p => p.IsFramework == false && !string.IsNullOrEmpty(p.DisplayName)).ToList(); + App.MainWindow.DispatcherQueue.TryEnqueue(() => AppPackages = new ObservableCollection(apps)); + }); + } + else + { + Win32PathVisibility = Visibility.Visible; + MsixVisibility = Visibility.Collapsed; + } + } + + + [ObservableProperty] private Visibility _win32PathVisibility = Visibility.Collapsed; + + [ObservableProperty] private Visibility _msixVisibility = Visibility.Visible; + + [RelayCommand] + private async Task AddWin32AppPath() + { + var hwnd = WinRT.Interop.WindowNative.GetWindowHandle(App.MainWindow); + + var picker = new Windows.Storage.Pickers.FileOpenPicker + { + ViewMode = Windows.Storage.Pickers.PickerViewMode.Thumbnail, + + SuggestedStartLocation = Windows.Storage.Pickers.PickerLocationId.PicturesLibrary + }; + + picker.FileTypeFilter.Add(".exe"); + + WinRT.Interop.InitializeWithWindow.Initialize(picker, hwnd); + + var file = await picker.PickSingleFileAsync(); + + if (file is null) + { + return; + } + var propList = await file.GetBasicPropertiesAsync(); + + Win32Path = file.Path; + } + + public void Selector_OnSelectionChanged(object sender, SelectionChangedEventArgs e) + { + + } + + [RelayCommand] + private async Task SaveLaunchApp() + { + + } +} diff --git a/src/ElectronBot.Braincase/Views/GestureAppConfigPage.xaml b/src/ElectronBot.Braincase/Views/GestureAppConfigPage.xaml index 478ec3e..9aae044 100644 --- a/src/ElectronBot.Braincase/Views/GestureAppConfigPage.xaml +++ b/src/ElectronBot.Braincase/Views/GestureAppConfigPage.xaml @@ -9,19 +9,90 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:local="using:Views" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" - Background="{ThemeResource ApplicationPageBackgroundThemeBrush}" + xmlns:package="using:Windows.ApplicationModel" mc:Ignorable="d"> - + - + - -