From 237c5c9adabb43bb5e8aac1f2f1f46f65d92824d Mon Sep 17 00:00:00 2001 From: ikasmc <65895428+ikas-mc@users.noreply.github.com> Date: Fri, 25 Mar 2022 16:07:12 +0800 Subject: [PATCH] refactor ui --- .../ContextMenuCustomApp/App.xaml | 20 + .../ContextMenuCustomApp/App.xaml.cs | 10 +- .../ContextMenuCustomApp/Common/Settings.cs | 48 ++ .../ContextMenuCustomApp.csproj | 15 + .../ContextMenuCustomApp/Styles/Button.xaml | 602 ++++++++++++++++++ .../ContextMenuCustomApp/Themes/Generic.xaml | 6 + .../View/Controls/SettingItem.cs | 200 ++++++ .../View/Controls/SettingItem.xaml | 98 +++ .../View/Menu/MenuPage.xaml | 258 ++++---- .../View/Menu/MenuPage.xaml.cs | 8 +- .../View/Menu/MenuPageViewModel.cs | 115 ++-- 11 files changed, 1183 insertions(+), 197 deletions(-) create mode 100644 ContextMenuCustom/ContextMenuCustomApp/Common/Settings.cs create mode 100644 ContextMenuCustom/ContextMenuCustomApp/Styles/Button.xaml create mode 100644 ContextMenuCustom/ContextMenuCustomApp/Themes/Generic.xaml create mode 100644 ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.cs create mode 100644 ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.xaml diff --git a/ContextMenuCustom/ContextMenuCustomApp/App.xaml b/ContextMenuCustom/ContextMenuCustomApp/App.xaml index a2461e7..5312f6f 100644 --- a/ContextMenuCustom/ContextMenuCustomApp/App.xaml +++ b/ContextMenuCustom/ContextMenuCustomApp/App.xaml @@ -7,6 +7,7 @@ + @@ -14,6 +15,14 @@ #40FFFFFF #08000000 + + + + + + #FF0063b1 + + 1 @@ -21,7 +30,18 @@ #2C3A3A3A #19000000 + + + + + + #FF5fb2f2 + + 1 + + + diff --git a/ContextMenuCustom/ContextMenuCustomApp/App.xaml.cs b/ContextMenuCustom/ContextMenuCustomApp/App.xaml.cs index 9dd292b..c9dfb8c 100644 --- a/ContextMenuCustom/ContextMenuCustomApp/App.xaml.cs +++ b/ContextMenuCustom/ContextMenuCustomApp/App.xaml.cs @@ -23,10 +23,10 @@ public App() protected override void OnLaunched(LaunchActivatedEventArgs e) { //TODO check first run - ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.PreferredLaunchViewSize; - var size = new Size(800, 768); - ApplicationView.PreferredLaunchViewSize = size; - ApplicationView.GetForCurrentView().TryResizeView(size); + ApplicationView.PreferredLaunchWindowingMode = ApplicationViewWindowingMode.Auto; + //var size = new Size(800, 768); + //ApplicationView.PreferredLaunchViewSize = size; + //ApplicationView.GetForCurrentView().TryResizeView(size); //TODO use shell Frame rootFrame = Window.Current.Content as Frame; @@ -70,7 +70,7 @@ protected override void OnActivated(IActivatedEventArgs args) { var arguments = commandLineActivatedEventArgs.Operation.Arguments; MessageDialog dialog = new MessageDialog(arguments); - _=dialog.ShowAsync(); + _ = dialog.ShowAsync(); } } } diff --git a/ContextMenuCustom/ContextMenuCustomApp/Common/Settings.cs b/ContextMenuCustom/ContextMenuCustomApp/Common/Settings.cs new file mode 100644 index 0000000..3195bac --- /dev/null +++ b/ContextMenuCustom/ContextMenuCustomApp/Common/Settings.cs @@ -0,0 +1,48 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using Windows.Foundation.Collections; +using Windows.Storage; + +namespace ContextMenuCustomApp.Common +{ + + public class Settings + { + private readonly static IPropertySet settings; + + public readonly static Settings INS; + static Settings() { + settings = ApplicationData.Current.LocalSettings.CreateContainer("app-settings", ApplicationDataCreateDisposition.Always).Values; + INS = new Settings(); + } + + public bool CacheEnabled { + get { + return GetValue(nameof(CacheEnabled),false); + } + set { + SetValue(nameof(CacheEnabled), value); + } + } + + private T GetValue(string key,T defaultValue= default) { + if (settings[key] is T value) { + return value; + } + + return defaultValue; + } + + + private T SetValue(string key, T value) + { + settings[key] = value; + return value; + } + + + } +} diff --git a/ContextMenuCustom/ContextMenuCustomApp/ContextMenuCustomApp.csproj b/ContextMenuCustom/ContextMenuCustomApp/ContextMenuCustomApp.csproj index 624eae5..cc71970 100644 --- a/ContextMenuCustom/ContextMenuCustomApp/ContextMenuCustomApp.csproj +++ b/ContextMenuCustom/ContextMenuCustomApp/ContextMenuCustomApp.csproj @@ -79,6 +79,7 @@ PackageReference + App.xaml @@ -99,6 +100,7 @@ + MenuPage.xaml @@ -134,6 +136,18 @@ Designer MSBuild:Compile + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + + + MSBuild:Compile + Designer + MSBuild:Compile Designer @@ -155,6 +169,7 @@ + 14.0 diff --git a/ContextMenuCustom/ContextMenuCustomApp/Styles/Button.xaml b/ContextMenuCustom/ContextMenuCustomApp/Styles/Button.xaml new file mode 100644 index 0000000..411226f --- /dev/null +++ b/ContextMenuCustom/ContextMenuCustomApp/Styles/Button.xaml @@ -0,0 +1,602 @@ + + + + + diff --git a/ContextMenuCustom/ContextMenuCustomApp/Themes/Generic.xaml b/ContextMenuCustom/ContextMenuCustomApp/Themes/Generic.xaml new file mode 100644 index 0000000..70f38a2 --- /dev/null +++ b/ContextMenuCustom/ContextMenuCustomApp/Themes/Generic.xaml @@ -0,0 +1,6 @@ + + + + + + \ No newline at end of file diff --git a/ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.cs b/ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.cs new file mode 100644 index 0000000..e34f2cb --- /dev/null +++ b/ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.cs @@ -0,0 +1,200 @@ +using Windows.UI.Xaml; +using Windows.UI.Xaml.Controls; + +namespace ContextMenuCustomApp.View.Controls +{ + [TemplatePart(Name = PartIconPresenter, Type = typeof(ContentPresenter))] + [TemplatePart(Name = PartDescriptionPresenter, Type = typeof(ContentPresenter))] + [TemplatePart(Name = RightContentPresenter, Type = typeof(ContentPresenter))] + [TemplatePart(Name = ActionContentPresenter, Type = typeof(ContentPresenter))] + [TemplatePart(Name = BottomContentPresenter, Type = typeof(ContentPresenter))] + public class SettingItem : Control + { + private const string PartIconPresenter = "IconPresenter"; + private const string PartDescriptionPresenter = "DescriptionPresenter"; + private const string RightContentPresenter = "RightContentPresenter"; + private const string ActionContentPresenter = "ActionContentPresenter"; + private const string BottomContentPresenter = "BottomContentPresenter"; + + private ContentPresenter _iconPresenter; + private ContentPresenter _descriptionPresenter; + private ContentPresenter _rightContentPresenter; + private ContentPresenter _actionContentPresenter; + private ContentPresenter _bottomContentPresenter; + private SettingItem _setting; + + public SettingItem() + { + this.DefaultStyleKey = typeof(SettingItem); + } + + + protected override void OnApplyTemplate() + { + _setting = (SettingItem)this; + _iconPresenter = (ContentPresenter)_setting.GetTemplateChild(PartIconPresenter); + _descriptionPresenter = (ContentPresenter)_setting.GetTemplateChild(PartDescriptionPresenter); + _rightContentPresenter = (ContentPresenter)_setting.GetTemplateChild(RightContentPresenter); + _actionContentPresenter = (ContentPresenter)_setting.GetTemplateChild(ActionContentPresenter); + _bottomContentPresenter = (ContentPresenter)_setting.GetTemplateChild(BottomContentPresenter); + Update(); + base.OnApplyTemplate(); + } + + public string Header + { + get => (string)GetValue(HeaderProperty); + set => SetValue(HeaderProperty, value); + } + public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register( + "Header", + typeof(string), + typeof(SettingItem), + new PropertyMetadata(default(string), OnHeaderChanged)); + + public object Description + { + get => (object)GetValue(DescriptionProperty); + set => SetValue(DescriptionProperty, value); + } + public static readonly DependencyProperty DescriptionProperty = DependencyProperty.Register( + "Description", + typeof(object), + typeof(SettingItem), + new PropertyMetadata(null, OnDescriptionChanged)); + + public object Icon + { + get => (object)GetValue(IconProperty); + set => SetValue(IconProperty, value); + } + + public static readonly DependencyProperty IconProperty = DependencyProperty.Register( + "Icon", + typeof(object), + typeof(SettingItem), + new PropertyMetadata(default(string), OnIconChanged)); + + public object RightContent + { + get => (object)GetValue(RightContentProperty); + set => SetValue(RightContentProperty, value); + } + + public static readonly DependencyProperty RightContentProperty = DependencyProperty.Register( + "RightContent", + typeof(object), + typeof(SettingItem), + new PropertyMetadata(null, OnRightContentChanged)); + public object ActionContent + { + get => (object)GetValue(ActionContentProperty); + set => SetValue(ActionContentProperty, value); + } + public static readonly DependencyProperty ActionContentProperty = DependencyProperty.Register( + "ActionContent", + typeof(object), + typeof(SettingItem), + new PropertyMetadata(null, OnActionContentChanged)); + + + public object BottomContent + { + get => (object)GetValue(BottomContentProperty); + set => SetValue(BottomContentProperty, value); + } + + public static readonly DependencyProperty BottomContentProperty = DependencyProperty.Register( + "BottomContent", + typeof(object), + typeof(SettingItem), + new PropertyMetadata(null, OnBottomContentChanged)); + + private static void OnHeaderChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((SettingItem)d).Update(); + } + + private static void OnRightContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((SettingItem)d).Update(); + } + + private static void OnActionContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((SettingItem)d).Update(); + } + + + private static void OnBottomContentChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((SettingItem)d).Update(); + } + + private static void OnIconChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((SettingItem)d).Update(); + } + + private static void OnDescriptionChanged(DependencyObject d, DependencyPropertyChangedEventArgs e) + { + ((SettingItem)d).Update(); + } + + private void Update() + { + if (_setting == null) + { + return; + } + + if (_setting.Icon == null) + { + _setting._iconPresenter.Visibility = Visibility.Collapsed; + } + else + { + _setting._iconPresenter.Visibility = Visibility.Visible; + } + + if (_setting.Description == null) + { + _setting._descriptionPresenter.Visibility = Visibility.Collapsed; + } + else + { + _setting._descriptionPresenter.Visibility = Visibility.Visible; + } + + + if (_setting.RightContent == null) + { + _setting._rightContentPresenter.Visibility = Visibility.Collapsed; + } + else + { + _setting._rightContentPresenter.Visibility = Visibility.Visible; + } + + + if (_setting.ActionContent == null) + { + _setting._actionContentPresenter.Visibility = Visibility.Collapsed; + } + else + { + _setting._actionContentPresenter.Visibility = Visibility.Visible; + } + + + if (_setting.BottomContent == null) + { + _setting._bottomContentPresenter.Visibility = Visibility.Collapsed; + } + else + { + _setting._bottomContentPresenter.Visibility = Visibility.Visible; + } + } + } +} diff --git a/ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.xaml b/ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.xaml new file mode 100644 index 0000000..fbbdd80 --- /dev/null +++ b/ContextMenuCustom/ContextMenuCustomApp/View/Controls/SettingItem.xaml @@ -0,0 +1,98 @@ + + + \ No newline at end of file diff --git a/ContextMenuCustom/ContextMenuCustomApp/View/Menu/MenuPage.xaml b/ContextMenuCustom/ContextMenuCustomApp/View/Menu/MenuPage.xaml index 9d5cd59..1f78abf 100644 --- a/ContextMenuCustom/ContextMenuCustomApp/View/Menu/MenuPage.xaml +++ b/ContextMenuCustom/ContextMenuCustomApp/View/Menu/MenuPage.xaml @@ -3,6 +3,7 @@ xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:common="using:ContextMenuCustomApp.View.Common" + xmlns:controls="using:ContextMenuCustomApp.View.Controls" xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:muxc="using:Microsoft.UI.Xaml.Controls" @@ -20,6 +21,7 @@ + @@ -94,6 +96,7 @@ Grid.Row="2" Grid.Column="0" HorizontalAlignment="Right" + VerticalAlignment="Center" Orientation="Horizontal"> Cache: @@ -105,17 +108,12 @@ IsLightDismissEnabled="True" PlacementMargin="4" PreferredPlacement="Auto" - Target="{x:Bind BuildCacheButton}"> + Target="{x:Bind BuildCacheTipButton}"> - Preprocess the menu files to cache - - This can optimize the menu display speed - - Rebuild after modifying the menu ! + Cache menu content - - + This can optimize the menu loading speed Cache Time: @@ -135,23 +133,12 @@ Click="BuildCacheTipButton_Click"> - - + IsOn="{x:Bind _viewModel.CacheEnabled, Mode=TwoWay}" + Style="{ThemeResource ToggleSwitchContentLelft}" /> - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + MenuItems { get; } - private readonly MenuService _menuService; - private readonly Action _exceptionHandler; + public ObservableCollection MenuItems { get; } - public readonly int MultipleFilesFlagJOIN = (int) MultipleFilesFlag.JOIN; + public readonly int MultipleFilesFlagJOIN = (int)MultipleFilesFlag.JOIN; - public MenuPageViewModel(Action exceptionHandler) + public MenuPageViewModel() { - _exceptionHandler = exceptionHandler; - if (null == _exceptionHandler) - { - throw new ArgumentNullException(nameof(exceptionHandler), "exception handler is nul"); - } MenuItems = new ObservableCollection(); _menuService = MenuService.Ins; } + #region menu + public async Task LoadAsync() { MenuItems.Clear(); - await RunWith(async () => { + await RunWith(async () => + { var menus = await _menuService.QueryAllAsync(); menus.ForEach(MenuItems.Add); }); @@ -41,70 +38,107 @@ await RunWith(async () => { public MenuItem New() { - var item = new MenuItem() { Title = "new menu", Param = @"""{path}""", AcceptFile=true, AcceptDirectory=true}; + var item = new MenuItem() { Title = "new menu", Param = @"""{path}""", AcceptFile = true, AcceptDirectory = true }; MenuItems.Add(item); return item; } public async Task SaveAsync(MenuItem item) { - await RunWith(async () => { + await RunWith(async () => + { await _menuService.SaveAsync(item); await LoadAsync(); + await UpdateCache(); OnMessage("Save Successfully"); }); } public async Task DeleteAsync(MenuItem item) { - await RunWith(async () => { + await RunWith(async () => + { await _menuService.DeleteAsync(item); await LoadAsync(); + await UpdateCache(); OnMessage("Delete Successfully"); }); } public async Task OpenMenusFolderAsync() { - try + var folder = await _menuService.GetMenusFolderAsync(); + _ = await Launcher.LaunchFolderAsync(folder); + } + + public async Task OpenMenuFileAsync(MenuItem item) + { + if (item.File == null) { - var folder = await _menuService.GetMenusFolderAsync(); - _ = await Launcher.LaunchFolderAsync(folder); + return; } - catch (Exception e) + _ = await Launcher.LaunchFileAsync(item.File); + } + + #endregion menu + + + #region menu cache + public string CacheTime + { + get { - _exceptionHandler(e, e.Message); + var value = ApplicationData.Current.LocalSettings.Values["Cache_Time"]; + return (value as string) ?? "No Cache"; } } - public async Task OpenMenuFileAsync(MenuItem item) + public void UpdateCacheTime() + { + OnPropertyChanged(nameof(CacheTime)); + } + + public bool CacheEnabled { - try + get { - if (item.File == null) - { - return; - } - _ = await Launcher.LaunchFileAsync(item.File); + return Settings.INS.CacheEnabled; } - catch (Exception e) + set { - _exceptionHandler(e, e.Message); + Settings.INS.CacheEnabled = value; + OnPropertyChanged(nameof(CacheEnabled)); + _ = UpdateCache(); } } - public async Task Build() + private async Task UpdateCache() { - await RunWith(async() => { + if (Settings.INS.CacheEnabled) + { + await BuildCache(); + } + else + { + await ClearCache(); + } + } + + + public async Task BuildCache() + { + await RunWith(async () => + { await _menuService.BuildToCacheAsync(); ApplicationData.Current.LocalSettings.Values["Cache_Time"] = DateTime.Now.ToString(CultureInfo.CurrentCulture); OnMessage("Build Successfully"); }); } - public async void ClearCache() + public async Task ClearCache() { - await RunWith(() => { + await RunWith(() => + { return Task.Run(() => { _menuService.ClearCache(); @@ -114,19 +148,10 @@ await RunWith(() => { }); } - public string CacheTime - { - get - { - var value = ApplicationData.Current.LocalSettings.Values["Cache_Time"]; - return (value as string) ?? "No Cache"; - } - } + #endregion cache - public void UpdateCacheTime() - { - OnPropertyChanged(nameof(CacheTime)); - } + + #region setting public string Version() { @@ -147,5 +172,7 @@ await Task.Run(() => ApplicationData.Current.LocalSettings.Values["Custom_Menu_Name"] = name ?? "Open With"; }); } + + #endregion setting } }