refactor infobar view

This commit is contained in:
DismissedLight
2024-07-08 16:39:48 +08:00
parent 823ffdb5ad
commit bad60f1d65
7 changed files with 177 additions and 45 deletions

View File

@@ -1505,6 +1505,12 @@
<data name="ViewHutaoDatabaseHeader" xml:space="preserve">
<value>深渊统计</value>
</data>
<data name="ViewInfoBarPanelClearAllContent" xml:space="preserve">
<value>清除所有通知</value>
</data>
<data name="ViewInfoBarPanelContractContent" xml:space="preserve">
<value>收起</value>
</data>
<data name="ViewInfoBarToggleTitle" xml:space="preserve">
<value>有新的通知</value>
</data>

View File

@@ -2,9 +2,11 @@
x:Class="Snap.Hutao.UI.Xaml.View.InfoBarView"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:clw="using:CommunityToolkit.Labs.WinUI"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:mxi="using:Microsoft.Xaml.Interactivity"
xmlns:mxic="using:Microsoft.Xaml.Interactions.Core"
xmlns:shsn="using:Snap.Hutao.Service.Notification"
xmlns:shuxb="using:Snap.Hutao.UI.Xaml.Behavior"
xmlns:shuxc="using:Snap.Hutao.UI.Xaml.Control"
@@ -95,32 +97,34 @@
x:Key="InfoBarTemplateSelector"
ActionButtonDisabled="{StaticResource InfoBarTemplate}"
ActionButtonEnabled="{StaticResource InfoBarWithActionButtonTemplate}"/>
<clw:TransitionHelper
x:Key="InfoBarPanelTransitionHelper"
x:Name="InfoBarPanelTransitionHelper"
Duration="0:0:0.3">
<clw:TransitionConfig Id="Body" ScaleMode="Scale"/>
</clw:TransitionHelper>
</ResourceDictionary>
</UserControl.Resources>
<Grid>
<ItemsControl
MaxWidth="640"
Margin="32,48,32,32"
VerticalAlignment="Bottom"
ItemContainerTransitions="{StaticResource RepositionThemeTransitions}"
ItemTemplateSelector="{StaticResource InfoBarTemplateSelector}"
ItemsSource="{x:Bind InfoBars}"
Transitions="{ThemeResource AddDeleteThemeTransitions}"
Visibility="{x:Bind VisibilityButton.IsChecked, Converter={StaticResource BoolToVisibilityConverter}, Mode=OneWay}"/>
<Border
x:Name="ShowButtonBorder"
Margin="16"
HorizontalAlignment="Right"
VerticalAlignment="Bottom"
VerticalAlignment="Top"
clw:TransitionHelper.Id="Body"
Background="{ThemeResource SystemControlAcrylicElementBrush}"
CornerRadius="{ThemeResource ControlCornerRadius}"
Visibility="{x:Bind InfoBars.Count, Mode=OneWay, Converter={StaticResource Int32ToVisibilityConverter}}">
<ToggleButton
Name="VisibilityButton"
Checked="OnVisibilityButtonCheckedChanged"
Unchecked="OnVisibilityButtonCheckedChanged">
<ToggleButton.Content>
CornerRadius="{ThemeResource ControlCornerRadius}">
<Button>
<mxi:Interaction.Behaviors>
<mxic:EventTriggerBehavior EventName="Click">
<clw:StartTransitionAction
Source="{x:Bind ShowButtonBorder}"
Target="{x:Bind InfoBarItemsBorder}"
Transition="{StaticResource InfoBarPanelTransitionHelper}"/>
</mxic:EventTriggerBehavior>
</mxi:Interaction.Behaviors>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="auto"/>
@@ -134,8 +138,72 @@
Style="{ThemeResource CriticalValueInfoBadgeStyle}"
Value="{x:Bind InfoBars.Count, Mode=OneWay}"/>
</Grid>
</ToggleButton.Content>
</ToggleButton>
</Button>
</Border>
<Border
x:Name="InfoBarItemsBorder"
Margin="16"
Padding="16"
HorizontalAlignment="Right"
VerticalAlignment="Top"
clw:TransitionHelper.Id="Body"
Style="{ThemeResource AcrylicBorderCardStyle}"
Visibility="Collapsed">
<Grid RowSpacing="8">
<Grid.RowDefinitions>
<RowDefinition Height="auto"/>
<RowDefinition/>
</Grid.RowDefinitions>
<StackPanel
HorizontalAlignment="Right"
Orientation="Horizontal"
Spacing="8">
<Button Click="OnClearAllButtonClick">
<Grid ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<FontIcon
HorizontalAlignment="Center"
FontSize="14"
Glyph="&#xE74D;"/>
<TextBlock Grid.Column="1" Text="{shuxm:ResourceString Name=ViewInfoBarPanelClearAllContent}"/>
</Grid>
</Button>
<Button>
<mxi:Interaction.Behaviors>
<mxic:EventTriggerBehavior EventName="Click">
<clw:StartTransitionAction
Source="{x:Bind InfoBarItemsBorder}"
Target="{x:Bind ShowButtonBorder}"
Transition="{StaticResource InfoBarPanelTransitionHelper}"/>
</mxic:EventTriggerBehavior>
</mxi:Interaction.Behaviors>
<Grid ColumnSpacing="8">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="19"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<FontIcon
HorizontalAlignment="Center"
FontSize="14"
Glyph="&#xEE49;"/>
<TextBlock Grid.Column="1" Text="{shuxm:ResourceString Name=ViewInfoBarPanelContractContent}"/>
</Grid>
</Button>
</StackPanel>
<ScrollViewer Grid.Row="1">
<ItemsControl
MaxWidth="480"
ItemContainerTransitions="{StaticResource AddDeleteThemeTransitions}"
ItemTemplateSelector="{StaticResource InfoBarTemplateSelector}"
ItemsPanel="{StaticResource StackPanelSpacing8Template}"
ItemsSource="{x:Bind InfoBars}"/>
</ScrollViewer>
</Grid>
</Border>
</Grid>
</UserControl>

View File

@@ -3,10 +3,9 @@
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Snap.Hutao.Core.Setting;
using Snap.Hutao.Service.Notification;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
namespace Snap.Hutao.UI.Xaml.View;
@@ -22,16 +21,57 @@ internal sealed partial class InfoBarView : UserControl
IServiceProvider serviceProvider = Ioc.Default;
infoBarService = serviceProvider.GetRequiredService<IInfoBarService>();
InfoBars = infoBarService.Collection;
VisibilityButton.IsChecked = LocalSetting.Get(SettingKeys.IsInfoBarToggleChecked, true);
InfoBars.CollectionChanged += OnInfoBarsCollectionChanged;
Unloaded += OnUnloaded;
}
private void OnVisibilityButtonCheckedChanged(object sender, RoutedEventArgs e)
private void OnUnloaded(object sender, RoutedEventArgs e)
{
LocalSetting.Set(SettingKeys.IsInfoBarToggleChecked, ((ToggleButton)sender).IsChecked ?? false);
InfoBars.CollectionChanged -= OnInfoBarsCollectionChanged;
}
private void OnInfoBarsCollectionChanged(object? sender, NotifyCollectionChangedEventArgs args)
{
switch (args.Action)
{
case NotifyCollectionChangedAction.Add:
{
InfoBarPanelTransitionHelper.Source = ShowButtonBorder;
InfoBarPanelTransitionHelper.Target = InfoBarItemsBorder;
InfoBarPanelTransitionHelper.StartAsync();
break;
}
case NotifyCollectionChangedAction.Remove:
{
if (InfoBars.Count is 0)
{
InfoBarPanelTransitionHelper.Source = InfoBarItemsBorder;
InfoBarPanelTransitionHelper.Target = ShowButtonBorder;
InfoBarPanelTransitionHelper.StartAsync();
}
break;
}
}
}
private void OnInfoBarClosed(InfoBar sender, InfoBarClosedEventArgs args)
{
InfoBars.Remove((InfoBarOptions)sender.DataContext);
}
private void OnClearAllButtonClick(object sender, RoutedEventArgs e)
{
RemoveInfoBarsAsync().SafeForget();
async ValueTask RemoveInfoBarsAsync()
{
while (InfoBars.Count > 0)
{
InfoBars.RemoveAt(0);
await Task.Delay(150).ConfigureAwait(true);
}
}
}
}

View File

@@ -128,6 +128,6 @@
UseLayoutRounding="True"/>
</NavigationView>
<shuxv:InfoBarView/>
<shuxv:InfoBarView Margin="0,44,0,0" VerticalAlignment="Stretch"/>
</Grid>
</UserControl>

View File

@@ -137,11 +137,20 @@
Header="Rename Desktop TestFolder"
IsClickEnabled="True"/>
<cwc:SettingsCard Header="Crash">
<StackPanel Orientation="Horizontal">
<Button Command="{Binding ExceptionCommand}" Content="Activate"/>
</StackPanel>
</cwc:SettingsCard>
<cwc:SettingsCard
Command="{Binding FileOperationRenameCommand}"
Header="Rename Desktop TestFolder"
IsClickEnabled="True"/>
<cwc:SettingsCard
Command="{Binding ExceptionCommand}"
Header="Crash"
IsClickEnabled="True"/>
<cwc:SettingsCard
Command="{Binding SendRandomInfoBarNotificationCommand}"
Header="Random InfoBar"
IsClickEnabled="True"/>
<TextBlock Style="{ThemeResource SettingsSectionHeaderTextBlockStyle}" Text="Announcement"/>
<Expander

View File

@@ -5,7 +5,6 @@ using Microsoft.UI.Windowing;
using Microsoft.UI.Xaml;
using Microsoft.Web.WebView2.Core;
using Snap.Hutao.Core.Graphics;
using System.Diagnostics;
using Windows.Graphics;
using Windows.System;

View File

@@ -2,6 +2,7 @@
// Licensed under the MIT license.
using Microsoft.Extensions.Caching.Memory;
using Microsoft.UI.Xaml.Controls;
using Snap.Hutao.Core.Caching;
using Snap.Hutao.Core.ExceptionService;
using Snap.Hutao.Core.Graphics;
@@ -188,4 +189,13 @@ internal sealed partial class TestViewModel : Abstraction.ViewModel
}
}
}
[Command("SendRandomInfoBarNotificationCommand")]
private void SendRandomInfoBarNotification()
{
infoBarService.PrepareInfoBarAndShow(builder => builder
.SetSeverity((InfoBarSeverity)Random.Shared.Next((int)InfoBarSeverity.Error) + 1)
.SetTitle("Lorem ipsum dolor sit amet")
.SetMessage("Consectetur adipiscing elit. Nullam nec purus nec elit ultricies tincidunt. Donec nec sapien nec elit ultricies tincidunt. Donec nec sapien nec elit ultricies tincidunt."));
}
}