Skip to content

Commit

Permalink
支持隐藏侧边栏 (#43)
Browse files Browse the repository at this point in the history
* 支持隐藏侧边栏

* 更新迁移根目录配置
  • Loading branch information
Richasy authored Jun 29, 2024
1 parent ea1ff5a commit 4cfaab0
Show file tree
Hide file tree
Showing 20 changed files with 428 additions and 122 deletions.
13 changes: 11 additions & 2 deletions src/Core/RodelAgent.Context/MigrationUtils.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,16 @@ namespace RodelAgent.Context;
/// <summary>
/// 数据库迁移工具.
/// </summary>
internal static class MigrationUtils
public static class MigrationUtils
{
private static string _rootPath;

/// <summary>
/// 设置根目录.
/// </summary>
/// <param name="rootPath">应用安装目录.</param>
public static void SetRootPath(string rootPath) => _rootPath = rootPath;

/// <summary>
/// 获取密钥数据库.
/// </summary>
Expand Down Expand Up @@ -71,7 +79,8 @@ private static async Task CheckDatabaseExistInternalAsync(string dbName, string
var targetDbPath = Path.Combine(workDir, dbName);
if (!File.Exists(targetDbPath))
{
var emptyDb = Path.Combine(AppContext.BaseDirectory, "Assets", dbName);
var rootPath = string.IsNullOrEmpty(_rootPath) ? AppContext.BaseDirectory : _rootPath;
var emptyDb = Path.Combine(rootPath, "Assets", dbName);
await Task.Run(() => File.Copy(emptyDb, targetDbPath));
}
}
Expand Down
4 changes: 4 additions & 0 deletions src/Desktop/RodelAgent.UI.Models/Constants/SettingNames.cs
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,8 @@ public enum SettingNames
LastSelectedGroup,
ChatGroupPanelType,
AppLanguage,
IsChatServicePageServiceColumnManualHide,
IsChatServicePageExtraColumnManualHide,
IsDrawHistoryColumnManualHide,
IsAudioHistoryColumnManualHide,
}
1 change: 1 addition & 0 deletions src/Desktop/RodelAgent.UI/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ public partial class App : Application
public App()
{
InitializeComponent();
RodelAgent.Context.MigrationUtils.SetRootPath(Windows.ApplicationModel.Package.Current.InstalledPath);
var language = SettingsToolkit.ReadLocalSetting(SettingNames.AppLanguage, "default");
ApplicationLanguages.PrimaryLanguageOverride = language != "default"
? language
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?xml version="1.0" encoding="utf-8" ?>
<UserControl
x:Class="RodelAgent.UI.Controls.VisibilityToggleButton"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:ic="using:FluentIcons.WinUI"
xmlns:local="using:RodelAgent.UI.Controls"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
HorizontalAlignment="Center"
VerticalAlignment="Center"
mc:Ignorable="d">

<Grid
Width="20"
VerticalAlignment="Stretch"
Background="{ThemeResource SubtleFillColorTransparent}">
<Grid
x:Name="BackGrid"
Width="20"
Height="30"
VerticalAlignment="Center"
Background="{ThemeResource SolidBackgroundFillColorBaseBrush}"
BorderBrush="{ThemeResource DividerStrokeColorDefaultBrush}"
BorderThickness="1"
CornerRadius="{x:Bind CornerRadius, Mode=OneWay}"
Visibility="Collapsed" />
<Button
x:Name="Btn"
Width="20"
Height="30"
Padding="0"
HorizontalAlignment="Center"
VerticalAlignment="Center"
HorizontalContentAlignment="Center"
VerticalContentAlignment="Center"
BorderThickness="0"
Click="OnBtnClick"
CornerRadius="{x:Bind CornerRadius, Mode=OneWay}"
Visibility="Collapsed">
<ic:SymbolIcon
x:Name="Icon"
FontSize="10"
Symbol="ChevronRight" />
</Button>
</Grid>
</UserControl>
132 changes: 132 additions & 0 deletions src/Desktop/RodelAgent.UI/Controls/Base/VisibilityToggleButton.xaml.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
// Copyright (c) Rodel. All rights reserved.

using Microsoft.UI.Xaml.Automation;
using Microsoft.UI.Xaml.Media;
using RodelAgent.UI.Toolkits;

namespace RodelAgent.UI.Controls;

/// <summary>
/// 可见性切换按钮.
/// </summary>
public sealed partial class VisibilityToggleButton : UserControl
{
/// <summary>
/// <see cref="Direction"/> 依赖属性.
/// </summary>
public static readonly DependencyProperty DirectionProperty =
DependencyProperty.Register(nameof(Direction), typeof(VisibilityToggleButtonDirection), typeof(VisibilityToggleButton), new PropertyMetadata(VisibilityToggleButtonDirection.LeftToRightVisible));

/// <summary>
/// <see cref="IsHide"/> 依赖属性.
/// </summary>
public static readonly DependencyProperty IsHideProperty =
DependencyProperty.Register(nameof(IsHide), typeof(bool), typeof(VisibilityToggleButton), new PropertyMetadata(default, new PropertyChangedCallback(OnIsHideChanged)));

/// <summary>
/// Initializes a new instance of the <see cref="VisibilityToggleButton"/> class.
/// </summary>
public VisibilityToggleButton()
{
InitializeComponent();
Loaded += OnLoaded;
}

/// <summary>
/// 点击事件.
/// </summary>
public event EventHandler Click;

/// <summary>
/// 获取或设置方向.
/// </summary>
public VisibilityToggleButtonDirection Direction
{
get => (VisibilityToggleButtonDirection)GetValue(DirectionProperty);
set => SetValue(DirectionProperty, value);
}

/// <summary>
/// 是否已经隐藏指定目标.
/// </summary>
public bool IsHide
{
get => (bool)GetValue(IsHideProperty);
set => SetValue(IsHideProperty, value);
}

/// <inheritdoc/>
protected override void OnPointerEntered(PointerRoutedEventArgs e)
=> ShowButton();

/// <inheritdoc/>
protected override void OnPointerMoved(PointerRoutedEventArgs e)
=> ShowButton();

/// <inheritdoc/>
protected override void OnPointerExited(PointerRoutedEventArgs e)
=> HideButton();

/// <inheritdoc/>
protected override void OnPointerCanceled(PointerRoutedEventArgs e)
=> HideButton();

private static void OnIsHideChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
var instance = d as VisibilityToggleButton;
instance?.CheckButtonStates();
}

private void OnLoaded(object sender, RoutedEventArgs e)
=> CheckButtonStates();

private void CheckButtonStates()
{
var symbol = IsHide
? Direction == VisibilityToggleButtonDirection.LeftToRightVisible ? FluentIcons.Common.Symbol.ChevronRight : FluentIcons.Common.Symbol.ChevronLeft
: Direction == VisibilityToggleButtonDirection.LeftToRightVisible ? FluentIcons.Common.Symbol.ChevronLeft : FluentIcons.Common.Symbol.ChevronRight;
Icon.Symbol = symbol;
var tip = IsHide ? ResourceToolkit.GetLocalizedString(Models.Constants.StringNames.Show) : ResourceToolkit.GetLocalizedString(Models.Constants.StringNames.Hide);
ToolTipService.SetToolTip(Btn, tip);
AutomationProperties.SetName(Btn, tip);

var container = VisualTreeHelper.GetParent(this) as FrameworkElement;
if (container is not null)
{
container.Margin = IsHide
? Direction == VisibilityToggleButtonDirection.LeftToRightVisible ? new Thickness(-4, 0, 0, 0) : new Thickness(0, 0, -4, 0)
: new Thickness(0);
}
}

private void OnBtnClick(object sender, RoutedEventArgs e)
=> Click?.Invoke(this, EventArgs.Empty);

private void ShowButton()
{
BackGrid.Visibility = Visibility.Visible;
Btn.Visibility = Visibility.Visible;
}

private void HideButton()
{
BackGrid.Visibility = Visibility.Collapsed;
Btn.Visibility = Visibility.Collapsed;
}
}

/// <summary>
/// 可见性切换按钮方向.
/// </summary>
public enum VisibilityToggleButtonDirection
{
/// <summary>
/// 需要控制的内容在左侧.
/// </summary>
LeftToRightVisible,

/// <summary>
/// 需要控制的内容在右侧.
/// </summary>
RightToLeftVisible,
}
18 changes: 14 additions & 4 deletions src/Desktop/RodelAgent.UI/Pages/AudioServicePage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@
Grid.Column="2"
Width="{x:Bind ViewModel.HistoryColumnWidth, Mode=OneWay}"
Margin="-6,0,0,0"
Background="{ThemeResource LayerFillColorDefaultBrush}">
Background="{ThemeResource LayerFillColorDefaultBrush}"
Visibility="{x:Bind ViewModel.IsHistoryColumnManualHide, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}">
<audio:AudioHistorySection />
</Grid>

Expand All @@ -103,13 +104,22 @@
Width="1"
HorizontalAlignment="Center"
VerticalAlignment="Stretch"
Background="{ThemeResource DividerStrokeColorDefaultBrush}" />
Background="{ThemeResource DividerStrokeColorDefaultBrush}"
Visibility="{x:Bind ViewModel.IsHistoryColumnManualHide, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}" />
<community:PropertySizer
HorizontalAlignment="Left"
HorizontalAlignment="Center"
Binding="{x:Bind ViewModel.HistoryColumnWidth, Mode=TwoWay}"
IsDragInverted="True"
Maximum="360"
Minimum="240" />
Minimum="240"
Visibility="{x:Bind ViewModel.IsHistoryColumnManualHide, Mode=OneWay, Converter={StaticResource BoolToVisibilityReverseConverter}}" />
<controls:VisibilityToggleButton
Margin="-20,0,0,0"
VerticalAlignment="Stretch"
Click="OnHistoryVisibilityButtonClick"
CornerRadius="4,0,0,4"
Direction="RightToLeftVisible"
IsHide="{x:Bind ViewModel.IsHistoryColumnManualHide, Mode=OneWay}" />
</Grid>
</Grid>
</Grid>
Expand Down
3 changes: 3 additions & 0 deletions src/Desktop/RodelAgent.UI/Pages/AudioServicePage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ protected override void OnPageLoaded()
ViewModel.ResetAvailableAudioServicesCommand.Execute(default);
}
}

private void OnHistoryVisibilityButtonClick(object sender, EventArgs e)
=> ViewModel.IsHistoryColumnManualHide = !ViewModel.IsHistoryColumnManualHide;
}

/// <summary>
Expand Down
Loading

0 comments on commit 4cfaab0

Please sign in to comment.