Skip to content

Commit

Permalink
即时保存设置
Browse files Browse the repository at this point in the history
  • Loading branch information
Poker-sang committed Nov 16, 2022
1 parent 5617ca7 commit 33e01da
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 48 deletions.
5 changes: 3 additions & 2 deletions TagsTree/Views/FilePropertiesPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Media.Animation;
using Microsoft.UI.Xaml.Navigation;
using Microsoft.VisualBasic.FileIO;
using TagsTree.Services;
using TagsTree.Services.ExtensionMethods;
using TagsTree.ViewModels;
using System.IO;

namespace TagsTree.Views;

Expand Down Expand Up @@ -45,7 +45,7 @@ private async void RenameBClick(object sender, RoutedEventArgs e)
}

var newFullName = FileViewModel.Path + @"\" + cd.Text;
if (FileViewModel.IsFolder ? FileSystem.DirectoryExists(newFullName) : FileSystem.FileExists(newFullName))
if (FileViewModel.IsFolder ? Directory.Exists(newFullName) : File.Exists(newFullName))
{
var isFolder = FileViewModel.IsFolder ? "夹" : "";
return $"新文件{isFolder}名与目录中其他文件{isFolder}同名!";
Expand All @@ -58,6 +58,7 @@ private async void RenameBClick(object sender, RoutedEventArgs e)
var newFullName = FileViewModel.Path + @"\" + InputName.Text;
FileViewModel.FileModel.Rename(newFullName);
FileViewModel.MoveOrRenameAndSave(newFullName);
// 相当于对FileViewModel的所有属性OnPropertyChanged
OnPropertyChanged(nameof(FileViewModel));
}
private async void MoveBClick(object sender, RoutedEventArgs e)
Expand Down
18 changes: 5 additions & 13 deletions TagsTree/Views/SettingsPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,18 @@
xmlns:local="using:TagsTree.Views"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:root="using:TagsTree"
xmlns:ui="using:CommunityToolkit.WinUI.UI.Controls"
Background="Transparent"
NavigationCacheMode="Required"
mc:Ignorable="d">
<ScrollViewer>
<StackPanel Margin="40,30">
<StackPanel>
<labs:SettingsExpander Description="应用的主题色" Header="主题">
<labs:SettingsExpander.HeaderIcon>
<FontIcon Glyph="&#xE771;" />
</labs:SettingsExpander.HeaderIcon>
<labs:SettingsExpander.Items>
<labs:SettingsCard HorizontalContentAlignment="Left" ContentAlignment="Vertical">
<RadioButtons x:Name="RbTheme">
<RadioButtons SelectedIndex="{x:Bind root:App.AppConfiguration.Theme}">
<RadioButton Checked="OnThemeRadioButtonChecked" Content="跟随系统">
<RadioButton.Tag>
<x:Int32>0</x:Int32>
Expand Down Expand Up @@ -49,7 +48,7 @@
IsClickEnabled="True"
Tapped="BLibraryPath_Click">
<labs:SettingsCard.Description>
<TextBox x:Name="TbLibraryPath" />
<TextBox x:Name="TbLibraryPath" KeyUp="TbLibraryPath_OnCharacterReceived" />
</labs:SettingsCard.Description>
</labs:SettingsCard>
</labs:SettingsExpander.Items>
Expand All @@ -58,13 +57,13 @@
Description="将所在路径文件夹名作为标签使用"
Header="文件夹标签"
HeaderIcon="Tag">
<ToggleSwitch x:Name="CbRootFoldersExist" />
<ToggleSwitch IsOn="{x:Bind root:App.AppConfiguration.PathTagsEnabled}" Toggled="PathTagsEnabled_OnToggled" />
</labs:SettingsCard>
<labs:SettingsCard
Description="监控文件路径文件下文件(夹)的变化"
Header="监控文件"
HeaderIcon="View">
<ToggleSwitch x:Name="TsFilesObserver" />
<ToggleSwitch IsOn="{x:Bind root:App.AppConfiguration.FilesObserverEnabled}" Toggled="FilesObserver_OnToggled" />
</labs:SettingsCard>
<labs:SettingsExpander
Description="指定文件和标签记录所在的文件夹并进行导入导出的操作"
Expand Down Expand Up @@ -103,13 +102,6 @@
</labs:SettingsCard>
</labs:SettingsExpander.Items>
</labs:SettingsExpander>
<ui:DockPanel>
<Button
ui:DockPanel.Dock="Left"
Content="保存"
Tapped="BSave_Click" />
<InfoBar x:Name="InfoBar" />
</ui:DockPanel>
</StackPanel>
</ScrollViewer>
</Page>
76 changes: 43 additions & 33 deletions TagsTree/Views/SettingsPage.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,22 +1,18 @@
using Microsoft.UI;
using System.IO;
using Microsoft.UI;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
using Windows.System;
using Microsoft.UI.Xaml.Input;
using TagsTree.Services;
using TagsTree.Services.ExtensionMethods;

namespace TagsTree.Views;

public partial class SettingsPage : Page
{
public SettingsPage()
{
InitializeComponent();
TbLibraryPath.Text = App.AppConfiguration.LibraryPath;
RbTheme.SelectedIndex = App.AppConfiguration.Theme;
TsFilesObserver.IsOn = App.AppConfiguration.FilesObserverEnabled;
CbRootFoldersExist.IsOn = App.AppConfiguration.PathTagsEnabled;
}
public SettingsPage() => InitializeComponent();

private void OnThemeRadioButtonChecked(object sender, RoutedEventArgs e)
{
Expand All @@ -27,6 +23,7 @@ private void OnThemeRadioButtonChecked(object sender, RoutedEventArgs e)
_ => ElementTheme.Default
};

// 内含 App.AppConfiguration.Theme = selectedTheme;
ThemeHelper.RootTheme = selectedTheme;
Application.Current.Resources["WindowCaptionForeground"] = selectedTheme switch
{
Expand All @@ -36,10 +33,14 @@ private void OnThemeRadioButtonChecked(object sender, RoutedEventArgs e)
};

TitleBarHelper.TriggerTitleBarRepaint();
AppContext.SaveConfiguration(App.AppConfiguration);
}

private async void BLibraryPath_Click(object sender, RoutedEventArgs e)
=> TbLibraryPath.Text = (await FileSystemHelper.GetStorageFolder())?.Path ?? TbLibraryPath.Text;
{
TbLibraryPath.Text = (await FileSystemHelper.GetStorageFolder())?.Path ?? TbLibraryPath.Text;
await ValidLibraryPathSet(TbLibraryPath.Text);
}

private async void BExport_Click(object sender, RoutedEventArgs e)
{
Expand All @@ -52,35 +53,44 @@ private async void BImport_Click(object sender, RoutedEventArgs e)
if (await FileSystemHelper.GetStorageFolder() is { } folder)
folder.Path.Copy(AppContext.AppLocalFolder);
}

private void BOpenDirectory_Click(object sender, RoutedEventArgs e) => AppContext.AppLocalFolder.Open();

private async void BSave_Click(object sender, RoutedEventArgs e)
private void FilesObserver_OnToggled(object sender, RoutedEventArgs e)
{
App.AppConfiguration.FilesObserverEnabled = ((ToggleSwitch)sender).IsOn;
AppContext.SaveConfiguration(App.AppConfiguration);
}

private void PathTagsEnabled_OnToggled(object sender, RoutedEventArgs e)
{
var legalPath = new Regex($@"^[a-zA-Z]:\\[^{FileSystemHelper.GetInvalidPathChars}]*$");
if (!legalPath.IsMatch(TbLibraryPath.Text))
App.AppConfiguration.PathTagsEnabled = ((ToggleSwitch)sender).IsOn;
AppContext.SaveConfiguration(App.AppConfiguration);
}

private async void TbLibraryPath_OnCharacterReceived(object sender, KeyRoutedEventArgs e)
{
if (e.Key is not VirtualKey.Enter)
return;
if (Directory.Exists(((TextBox)sender).Text))
{
InfoBar.Severity = InfoBarSeverity.Error;
InfoBar.Message = "错误";
InfoBar.Message = "路径错误!请填写正确完整的文件夹路径!";
await ValidLibraryPathSet(((TextBox)sender).Text);
((TextBox)sender).Description = "";
}
else
((TextBox)sender).Description = "路径错误!请填写正确、完整、存在的文件夹路径!";
}

private static async Task ValidLibraryPathSet(string path)
{
App.AppConfiguration.LibraryPath = path;
AppContext.SaveConfiguration(App.AppConfiguration);
if (!App.ConfigSet)
{
App.AppConfiguration.LibraryPath = TbLibraryPath.Text;
App.AppConfiguration.PathTagsEnabled = CbRootFoldersExist.IsOn;
App.AppConfiguration.FilesObserverEnabled = TsFilesObserver.IsOn;
AppContext.SaveConfiguration(App.AppConfiguration);
InfoBar.Severity = InfoBarSeverity.Success;
InfoBar.Title = "成功";
InfoBar.Message = "已保存";
if (!App.ConfigSet)
{
App.ConfigSet = true;
await WindowHelper.Window.ConfigIsSet();
}
else
((NavigationViewItem)App.RootNavigationView.FooterMenuItems[0]).IsEnabled = await App.ChangeFilesObserver();
App.ConfigSet = true;
await WindowHelper.Window.ConfigIsSet();
}

InfoBar.IsOpen = true;
else
((NavigationViewItem)App.RootNavigationView.FooterMenuItems[0]).IsEnabled = await App.ChangeFilesObserver();
}
}

0 comments on commit 33e01da

Please sign in to comment.