Skip to content

Commit

Permalink
add: settings page
Browse files Browse the repository at this point in the history
  • Loading branch information
emako committed May 18, 2024
1 parent d3cbfa0 commit 648c066
Show file tree
Hide file tree
Showing 8 changed files with 313 additions and 21 deletions.
33 changes: 33 additions & 0 deletions src/Common/Fischless.Design/Controls/Card/CardAction.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
using Avalonia.Controls;
using Avalonia.Controls.Metadata;
using Avalonia.Controls.Presenters;
using Avalonia.Data;
using Avalonia.Input;
using Avalonia.Interactivity;
using Avalonia.Metadata;
using System.Windows.Input;

namespace Fischless.Design.Controls;

Expand All @@ -18,4 +22,33 @@ public class CardAction : ContentControl
get => GetValue(ContentProperty);
set => SetValue(ContentProperty, value);
}

public static readonly StyledProperty<ICommand?> CommandProperty = AvaloniaProperty.Register<CardAction, ICommand>("Command", null, inherits: false, BindingMode.OneWay, null, null, enableDataValidation: true)!;

public ICommand? Command
{
get => GetValue(CommandProperty);
set => SetValue(CommandProperty, value);
}

public static readonly StyledProperty<object?> CommandParameterProperty = AvaloniaProperty.Register<CardAction, object>("CommandParameter")!;

public object? CommandParameter
{
get => GetValue(CommandParameterProperty);
set => SetValue(CommandParameterProperty, value);
}

public CardAction()
{
this.AddHandler(PointerPressedEvent, OnPointerPressed, RoutingStrategies.Tunnel);
}

private void OnPointerPressed(object? sender, PointerPressedEventArgs e)
{
if (Command?.CanExecute(CommandParameter) == true)
{
Command.Execute(CommandParameter);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<Platforms>AnyCPU;x64</Platforms>
<Platforms>x64</Platforms>
</PropertyGroup>
<ItemGroup>
<PackageReference Include="Antelcat.I18N.Avalonia" Version="1.0.1" />
Expand Down
2 changes: 1 addition & 1 deletion src/Common/Fischless.Linq/Fischless.Linq.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<Platforms>AnyCPU;x64</Platforms>
<Platforms>x64</Platforms>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>
</PropertyGroup>
</Project>
56 changes: 56 additions & 0 deletions src/Common/Fischless.Linq/SmartEnum{T}.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
using System.ComponentModel;

namespace Fischless.Linq;

public abstract class SmartEnum<TEnum> where TEnum : struct
{
public static TEnum? FromName(string name, TEnum? @default = default)
{
if (Enum.TryParse(name, out TEnum @enum))
{
return @enum;
}
return @default;
}

public static TEnum? FromValue(int? value, TEnum? @default = default)
{
if (value != null)
{
if (Enum.TryParse(value.ToString(), out TEnum @enum))
{
return @enum;
}
}
return @default;
}

public static TEnum? FromDescriptionAttribute(string? description, TEnum? @default = default)
{
if (description != null)
{
foreach (TEnum enumValue in Enum.GetValues(typeof(TEnum)))
{
var enumMember = typeof(TEnum).GetMember(enumValue.ToString())[0];
if (enumMember.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() is DescriptionAttribute attribute && attribute.Description == description)
{
return enumValue;
}
}
}
return @default;
}

public static string? ToDescriptionAttribute(TEnum? value, string? @default = default)
{
if (value != null)
{
var enumMember = typeof(TEnum).GetMember(value.ToString())[0];
if (enumMember.GetCustomAttributes(typeof(DescriptionAttribute), false).FirstOrDefault() is DescriptionAttribute attribute)
{
return attribute.Description;
}
}
return @default;
}
}
2 changes: 1 addition & 1 deletion src/Common/Fischless.Win32/Fischless.Win32.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
<LangVersion>12.0</LangVersion>
<Platforms>AnyCPU;x64</Platforms>
<Platforms>x64</Platforms>
</PropertyGroup>
<PropertyGroup>
<AvaloniaVersion>11.0.10</AvaloniaVersion>
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Original file line number Diff line number Diff line change
@@ -1,10 +1,102 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using Fischless.Globalization;
using Fischless.Linq;
using FluentAvalonia.Core;
using LyricStudio.Core.Configuration;
using LyricStudio.Models;
using Serilog;
using System;
using System.Diagnostics;
using System.IO;

namespace LyricStudio.ViewModels;

public class SettingsPageViewModel : ObservableObject
public partial class SettingsPageViewModel : ObservableObject
{
[ObservableProperty]
private int languageIndex = (int)SmartEnum<LanguageIndex>.FromDescriptionAttribute(ConfigurationKeys.Language.Get(), Fischless.Globalization.LanguageIndex.Chinese)!;

partial void OnLanguageIndexChanged(int value)
{
string prev = SmartEnum<LanguageIndex>.ToDescriptionAttribute(SmartEnum<LanguageIndex>.FromValue(LanguageIndex));
string next = SmartEnum<LanguageIndex>.ToDescriptionAttribute(SmartEnum<LanguageIndex>.FromValue(value));

_ = prev;
MuiLanguage.SetLanguage(next);
ConfigurationKeys.Language.Set(next);
Config.Configer?.Save(AppConfig.SettingsFile);
}

public SettingsPageViewModel()
{
}

[RelayCommand]
private void CheckUpdate()
{
try
{
Process.Start(new ProcessStartInfo()
{
FileName = "https://github.com/lemutec/LyricStudio/releases",
UseShellExecute = true,
});
}
catch (Exception e)
{
Log.Error(e.Message);
}
}

[RelayCommand]
private void CheckLicense()
{
try
{
Process.Start(new ProcessStartInfo()
{
FileName = "https://github.com/lemutec/LyricStudio/blob/master/LICENSE",
UseShellExecute = true,
});
}
catch (Exception e)
{
Log.Error(e.Message);
}
}

[RelayCommand]
private void OpenSpecialFolder()
{
try
{
Process.Start(new ProcessStartInfo()
{
FileName = $"file://{new FileInfo(AppConfig.SettingsFile).DirectoryName}/",
UseShellExecute = true,
});
}
catch (Exception e)
{
Log.Error(e.Message);
}
}

[RelayCommand]
private void OpenLogsFolder()
{
try
{
Process.Start(new ProcessStartInfo()
{
FileName = $"file://{AppConfig.LogFolder}/",
UseShellExecute = true,
});
}
catch (Exception e)
{
Log.Error(e.Message);
}
}
}
145 changes: 128 additions & 17 deletions src/Desktop/LyricStudio/Views/Pages/SettingsPage.axaml
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,133 @@
d:DesignWidth="800"
mc:Ignorable="d">
<Grid>
<StackPanel Margin="8" Spacing="10">
<Expander MinHeight="46">
<Grid>
<TextBlock Text="a" />
</Grid>
</Expander>
<fui:Card MinHeight="46">
<Grid>
<TextBlock Text="a" />
</Grid>
</fui:Card>
<fui:CardAction MinHeight="46">
<Grid>
<TextBlock Text="a" />
</Grid>
</fui:CardAction>
</StackPanel>
<Image MaxWidth="200"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
Opacity="0.5"
RenderOptions.BitmapInterpolationMode="HighQuality"
Source="/Assets/Images/UI_SummerTimeV2_Img_Island05.png" />
<ScrollViewer>
<StackPanel Margin="8" Spacing="10">
<TextBlock Margin="3,0,0,0"
FontSize="14"
Foreground="{DynamicResource AccentTextFillColorPrimaryBrush}"
Text="关于程序" />
<fui:Card MinHeight="65">
<Grid ColumnDefinitions="Auto, *">
<Image Grid.Column="0"
Height="30"
Margin="16,0,0,0"
RenderOptions.BitmapInterpolationMode="HighQuality"
Source="/Assets/Images/Favicon.ico" />
<StackPanel Grid.Column="1" Orientation="Horizontal">
<StackPanel Margin="16,0,0,0" VerticalAlignment="Center">
<StackPanel Orientation="Horizontal">
<TextBlock FontSize="13" Text="Lyric Studio" />
<Border Margin="4,0,0,0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
Background="#30FFFFFF"
CornerRadius="4">
<TextBlock Padding="4,2,4,2"
VerticalAlignment="Center"
FontSize="12"
Opacity="0.9"
Text="预览版" />
</Border>
</StackPanel>
<TextBlock FontSize="13"
Foreground="{DynamicResource TextFillColorTertiaryBrush}"
Text="v0.0.1" />
</StackPanel>
<StackPanel Margin="16,0,0,0"
VerticalAlignment="Center"
Orientation="Horizontal">
<Button Padding="6,4"
Command="{Binding CheckUpdateCommand}"
Content="检查更新"
FontSize="12" />
<Button Margin="8,0,0,0"
Padding="6,4"
Command="{Binding CheckLicenseCommand}"
Content="许可信息"
FontSize="12" />
</StackPanel>
</StackPanel>
</Grid>
</fui:Card>
<TextBlock Margin="3,0,0,0"
FontSize="14"
Foreground="{DynamicResource AccentTextFillColorPrimaryBrush}"
Text="全局设定" />
<fui:Card MinHeight="65">
<Grid ColumnDefinitions="Auto, *, Auto">
<ui:FontIcon MinWidth="30"
Margin="16,0,0,0"
FontFamily="{StaticResource SymbolThemeFontFamily}"
FontSize="24"
Glyph="{x:Static fui:FontSymbols.LocaleLanguage}" />
<StackPanel Grid.Column="1"
Margin="16,0,0,0"
VerticalAlignment="Center">
<TextBlock Text="语言" />
<TextBlock Foreground="{DynamicResource TextFillColorTertiaryBrush}" Text="Language" />
</StackPanel>
<StackPanel Grid.Column="2"
Margin="0,0,24,0"
VerticalAlignment="Center">
<ComboBox SelectedIndex="{Binding LanguageIndex, Mode=TwoWay}">
<ComboBox.Items>
<ComboBoxItem Content="简体中文" Tag="zh" />
<ComboBoxItem Content="日本語" Tag="ja" />
<ComboBoxItem Content="English" Tag="en" />
</ComboBox.Items>
</ComboBox>
</StackPanel>
</Grid>
</fui:Card>
<TextBlock Margin="3,0,0,0"
FontSize="14"
Foreground="{DynamicResource AccentTextFillColorPrimaryBrush}"
Text="数据储存" />
<fui:CardAction MinHeight="65" Command="{Binding OpenSpecialFolderCommand}">
<Grid ColumnDefinitions="Auto, *">
<ui:FontIcon MinWidth="30"
Margin="16,0,0,0"
FontFamily="{StaticResource SymbolThemeFontFamily}"
FontSize="24"
Glyph="{x:Static fui:FontSymbols.OpenFolderHorizontal}" />
<StackPanel Grid.Column="1"
Margin="16,0,0,0"
VerticalAlignment="Center">
<TextBlock Text="用户数据" />
<TextBlock Foreground="{DynamicResource TextFillColorTertiaryBrush}" Text="本地记录的用户数据" />
</StackPanel>
</Grid>
</fui:CardAction>
<fui:CardAction MinHeight="65" Command="{Binding OpenLogsFolderCommand}">
<Grid ColumnDefinitions="Auto, *">
<ui:FontIcon MinWidth="30"
Margin="16,0,0,0"
FontFamily="{StaticResource SymbolThemeFontFamily}"
FontSize="24"
Glyph="{x:Static fui:FontSymbols.OpenFolderHorizontal}" />
<StackPanel Grid.Column="1"
Margin="16,0,0,0"
VerticalAlignment="Center">
<TextBlock Text="应用日志" />
<TextBlock Foreground="{DynamicResource TextFillColorTertiaryBrush}" Text="本地记录的应用日志" />
</StackPanel>
</Grid>
</fui:CardAction>
<!--
<Expander MinHeight="60">
<Grid>
<TextBlock Text="" />
</Grid>
</Expander>
-->
</StackPanel>
</ScrollViewer>
</Grid>
</UserControl>

0 comments on commit 648c066

Please sign in to comment.