Skip to content

Commit

Permalink
feat(settings): update settings management and UI enhancements (#39)
Browse files Browse the repository at this point in the history
- Updated `LangVersion` to `preview` in `Directory.Build.props`.
- Upgraded NuGet packages including `Microsoft.Extensions.DependencyInjection`, `Microsoft.Extensions.Hosting`, and `System.Text.Json` to version `9.0.2`.
- Added a new `<connectionStrings />` section in `App.config` and renamed settings to include the "Is" prefix.
- Introduced `CreateHostBuilder` method in `App.xaml.cs` for improved dependency injection.
- Refactored `MainWindow.xaml` and `MainWindow.xaml.cs` to enhance UI and settings handling.
- Added `Settings.cs` for managing application settings with `CommunityToolkit.Mvvm`.
- Updated resource files for localization, including a new "Visibility" string.
- Refactored `MainWindowViewModel.cs` to use `ObservableRecipient` for messaging.
- Introduced `SettingsViewModel.cs` to manage settings view and commands.
- Improved UI layout and bindings in `PageNavigationView.xaml`, `SettingsView.xaml`, and `ToolBarView.xaml`.
  • Loading branch information
realybin authored Feb 18, 2025
1 parent ac48240 commit 65e32a6
Show file tree
Hide file tree
Showing 20 changed files with 482 additions and 311 deletions.
2 changes: 1 addition & 1 deletion Directory.Build.props
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ See: https://learn.microsoft.com/visualstudio/msbuild/customize-your-build
<PropertyGroup>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12</LangVersion>
<LangVersion>preview</LangVersion>
<AccelerateBuildsInVisualStudio>true</AccelerateBuildsInVisualStudio>
<!--
If you you like to see source generated files saved to disk you can enable the following:
Expand Down
6 changes: 3 additions & 3 deletions Directory.Packages.props
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@
<PrivateAssets>all</PrivateAssets>
</PackageVersion>
<PackageVersion Include="MaterialDesignThemes" Version="5.2.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.1" />
<PackageVersion Include="Microsoft.Extensions.DependencyInjection" Version="9.0.2" />
<PackageVersion Include="Microsoft.Extensions.Hosting" Version="9.0.2" />
<PackageVersion Include="Microsoft.NET.Test.Sdk" Version="17.12.0" />
<PackageVersion Include="Moq" Version="4.20.72" />
<PackageVersion Include="Moq.AutoMock" Version="3.5.0" />
<PackageVersion Include="SingleInstanceCore" Version="2.2.2" />
<PackageVersion Include="System.Text.Json" Version="9.0.1" />
<PackageVersion Include="System.Text.Json" Version="9.0.2" />
<PackageVersion Include="ValueConverters" Version="3.1.22" />
<PackageVersion Include="Velopack" Version="0.0.1053" />
<PackageVersion Include="WPF.JoshSmith.Controls.DragCanvas" Version="2.0.3" />
Expand Down
10 changes: 7 additions & 3 deletions SketchNow/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@
<section name="SketchNow.Properties.Settings" type="System.Configuration.ClientSettingsSection, System, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089" allowExeDefinition="MachineToLocalUser" requirePermission="false" />
</sectionGroup>
</configSections>
<connectionStrings />
<userSettings>
<SketchNow.Properties.Settings>
<setting name="FitToCurve" serializeAs="String">
<setting name="IsFitToCurve" serializeAs="String">
<value>True</value>
</setting>
<setting name="IgnorePressure" serializeAs="String">
<setting name="IsIgnorePressure" serializeAs="String">
<value>False</value>
</setting>
<setting name="EraseByStroke" serializeAs="String">
<setting name="IsEraseByStroke" serializeAs="String">
<value>True</value>
</setting>
<setting name="StrokeSize" serializeAs="String">
<value>5</value>
</setting>
</SketchNow.Properties.Settings>
</userSettings>
</configuration>
15 changes: 9 additions & 6 deletions SketchNow/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
using System.Text;
using System.Reflection;
using System.Text;
using System.Windows;
using System.Windows.Media;
using System.Windows.Threading;

using CommunityToolkit.Mvvm.Messaging;

Expand All @@ -8,14 +12,11 @@
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;

using SingleInstanceCore;

using SketchNow.ViewModels;
using SketchNow.Views;

using System.Windows;
using System.Windows.Threading;

using SingleInstanceCore;

using Velopack;
using Velopack.Sources;

Expand Down Expand Up @@ -51,6 +52,7 @@ private static async Task MainAsync(string[] args)
//to suppress the CA2007 warning https://learn.microsoft.com/en-us/dotnet/fundamentals/code-analysis/quality-rules/ca2007
await host.StopAsync().ConfigureAwait(true);
}

public static IHostBuilder CreateHostBuilder(string[] args) =>
Host.CreateDefaultBuilder(args)
.ConfigureAppConfiguration((hostBuilderContext, configurationBuilder)
Expand All @@ -59,6 +61,7 @@ public static IHostBuilder CreateHostBuilder(string[] args) =>
{
services.AddSingleton<MainWindow>();
services.AddSingleton<MainWindowViewModel>();
services.AddSingleton<SettingsViewModel>();

services.AddSingleton<WeakReferenceMessenger>();
services.AddSingleton<IMessenger, WeakReferenceMessenger>(provider =>
Expand Down
30 changes: 7 additions & 23 deletions SketchNow/Views/MainWindow.xaml → SketchNow/MainWindow.xaml
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
<Window x:Class="SketchNow.Views.MainWindow"
<Window x:Class="SketchNow.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:controls="clr-namespace:SketchNow.Controls"
Expand Down Expand Up @@ -80,13 +80,13 @@
<Window.Resources>
<valueConverters:BoolToVisibilityConverter x:Key="BoolToVisibilityConverter" />
</Window.Resources>
<Window.CacheMode>
<BitmapCache
<Grid>
<Grid.CacheMode>
<BitmapCache
EnableClearType="True"
RenderAtScale="1"
SnapsToDevicePixels="True" />
</Window.CacheMode>
<Grid>
</Grid.CacheMode>
<materialDesign:DialogHost ApplyBlurBackground="True">
<materialDesign:DialogHost.DialogContent>
<Grid Margin="16">
Expand All @@ -97,7 +97,7 @@
<ScrollViewer Grid.Row="0"
HorizontalScrollBarVisibility="Auto"
VerticalScrollBarVisibility="Auto">
<views:SettingsView />
<views:SettingsView x:Name="SettingsView"/>
</ScrollViewer>
<StackPanel Grid.Row="1"
HorizontalAlignment="Right"
Expand All @@ -112,13 +112,7 @@
</Grid>
</materialDesign:DialogHost.DialogContent>
<Grid>
<Grid.CacheMode>
<BitmapCache
EnableClearType="True"
RenderAtScale="1"
SnapsToDevicePixels="True" />
</Grid.CacheMode>
<Canvas Background="{Binding SelectedBrush}" Visibility="{Binding IsMultiPageMode, Converter={StaticResource BoolToVisibilityConverter}}" />
<Canvas Background="{Binding SelectedBackground}" Visibility="{Binding IsMultiPageMode, Converter={StaticResource BoolToVisibilityConverter}}" />
<controls:CustomInkCanvas Background="Transparent"
Cursor="{Binding InkCanvasCursor}"
DefaultDrawingAttributes="{Binding CurrentDrawingAttributes}"
Expand All @@ -136,15 +130,5 @@
<views:ToolBarView />
</Grid>
</materialDesign:DialogHost>

<materialDesign:Snackbar HorizontalAlignment="Center"
VerticalAlignment="Bottom"
MessageQueue="{Binding MessageQueue}" />
<ProgressBar Height="10"
Margin="0,0,0,6"
VerticalAlignment="Bottom"
IsIndeterminate="{Binding Progress.IsIndeterminate}"
Visibility="{Binding Progress.IsVisible, Converter={StaticResource BoolToVisibilityConverter}}"
Value="{Binding Progress.Value}" />
</Grid>
</Window>
Original file line number Diff line number Diff line change
@@ -1,19 +1,26 @@
using System.Windows.Input;
using System.Diagnostics;
using System.Windows.Input;

using SketchNow.ViewModels;
using SketchNow.Views;

namespace SketchNow.Views;
namespace SketchNow;

/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow
{
public MainWindow(MainWindowViewModel viewModel)
public MainWindow(
MainWindowViewModel mainViewModel,
SettingsViewModel settingsViewModel)
{
DataContext = viewModel;
InitializeComponent();
Debug.WriteLine("Ctor=============");

DataContext = mainViewModel;
SettingsView.DataContext = settingsViewModel;

CommandBindings.Add(new CommandBinding(ApplicationCommands.Close, OnClose));
}

Expand Down
2 changes: 1 addition & 1 deletion SketchNow/Models/CustomCursors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public interface ICustomCursors
public class CustomCursors : ICustomCursors
{
/// <summary>
///
/// Load a cursor from the resources
/// </summary>
/// <param name="resourcePath"></param>
/// <returns></returns>
Expand Down
48 changes: 48 additions & 0 deletions SketchNow/Models/Settings.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
using System.Windows;
using System.Windows.Media;

using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;

namespace SketchNow.Models;

public partial class Settings : ObservableObject
{
[ObservableProperty]
private bool _isFitToCurve;

[ObservableProperty]
private bool _isIgnorePressure;

[ObservableProperty]
private bool _isEraseByStroke;

[ObservableProperty]
private Brush _selectedBackground;

public Settings()
{
IsFitToCurve = Properties.Settings.Default.IsFitToCurve;
IsIgnorePressure = Properties.Settings.Default.IsIgnorePressure;
IsEraseByStroke = Properties.Settings.Default.IsEraseByStroke;
SelectedBackground = Properties.Settings.Default.SelectedBackground;
}

public Settings(bool isFitToCurve, bool isIgnorePressure, bool isEraseByStroke, Brush selectedBackground)
{
IsFitToCurve = isFitToCurve;
IsIgnorePressure = isIgnorePressure;
IsEraseByStroke = isEraseByStroke;
SelectedBackground = selectedBackground;
}

[RelayCommand]
private void SaveSettings()
{
Properties.Settings.Default.IsFitToCurve = IsFitToCurve;
Properties.Settings.Default.IsIgnorePressure = IsIgnorePressure;
Properties.Settings.Default.IsEraseByStroke = IsEraseByStroke;
Properties.Settings.Default.SelectedBackground = SelectedBackground;
Properties.Settings.Default.Save();
}
}
9 changes: 9 additions & 0 deletions SketchNow/Properties/Resource.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions SketchNow/Properties/Resource.resx
Original file line number Diff line number Diff line change
Expand Up @@ -198,4 +198,7 @@
<data name="DownloadedUpdatesPleaseWait" xml:space="preserve">
<value>Downloaded updates. Please wait...</value>
</data>
<data name="Visibility" xml:space="preserve">
<value>Visibility</value>
</data>
</root>
9 changes: 6 additions & 3 deletions SketchNow/Properties/Resource.zh-CN.resx
Original file line number Diff line number Diff line change
Expand Up @@ -189,13 +189,16 @@
<data name="InstallAndUpdate" xml:space="preserve">
<value>安装并更新</value>
</data>
<data name="DownloadingUpdatesPleaseWait" xml:space="preserve">
<value>下载更新中,请稍等...</value>
</data>
<data name="UpdatesAreNotAvailable" xml:space="preserve">
<value>更新不可用。</value>
</data>
<data name="DownloadingUpdatesPleaseWait" xml:space="preserve">
<value>下载更新中,请稍等...</value>
</data>
<data name="DownloadedUpdatesPleaseWait" xml:space="preserve">
<value>更新下载完成,请稍等...</value>
</data>
<data name="Visibility" xml:space="preserve">
<value>可见性</value>
</data>
</root>
44 changes: 34 additions & 10 deletions SketchNow/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions SketchNow/Properties/Settings.settings
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,20 @@
<SettingsFile xmlns="http://schemas.microsoft.com/VisualStudio/2004/01/settings" CurrentProfile="(Default)" GeneratedClassNamespace="SketchNow.Properties" GeneratedClassName="Settings">
<Profiles />
<Settings>
<Setting Name="FitToCurve" Type="System.Boolean" Scope="User">
<Setting Name="IsFitToCurve" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="IgnorePressure" Type="System.Boolean" Scope="User">
<Setting Name="IsIgnorePressure" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">False</Value>
</Setting>
<Setting Name="EraseByStroke" Type="System.Boolean" Scope="User">
<Setting Name="IsEraseByStroke" Type="System.Boolean" Scope="User">
<Value Profile="(Default)">True</Value>
</Setting>
<Setting Name="SelectedBackground" Type="System.Windows.Media.Brush" Scope="User">
<Value Profile="(Default)">#FFFFFFFF</Value>
</Setting>
<Setting Name="StrokeSize" Type="System.Int32" Scope="User">
<Value Profile="(Default)">5</Value>
</Setting>
</Settings>
</SettingsFile>
Loading

0 comments on commit 65e32a6

Please sign in to comment.