Skip to content

Commit

Permalink
fix #1633
Browse files Browse the repository at this point in the history
  • Loading branch information
qhy040404 committed Jun 28, 2024
1 parent f1bcef4 commit 8cf71e0
Show file tree
Hide file tree
Showing 16 changed files with 2,904 additions and 30 deletions.
2 changes: 1 addition & 1 deletion src/Snap.Hutao/Snap.Hutao/App.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
<ResourceDictionary.MergedDictionaries>
<XamlControlsResources/>
<ResourceDictionary Source="ms-appx:///CommunityToolkit.WinUI.Controls.SettingsControls/SettingsCard/SettingsCard.xaml"/>
<ResourceDictionary Source="ms-appx:///CommunityToolkit.WinUI.Controls.TokenizingTextBox/TokenizingTextBox.xaml"/>
<ResourceDictionary Source="ms-appx:///CommunityToolkit.Labs.WinUI.TokenView/TokenItem/TokenItem.xaml"/>
<ResourceDictionary Source="ms-appx:///UI/Xaml/Control/Elevation.xaml"/>
<ResourceDictionary Source="ms-appx:///UI/Xaml/Control/ItemIcon.xaml"/>
Expand Down Expand Up @@ -38,6 +37,7 @@
<ResourceDictionary Source="ms-appx:///UI/Xaml/Control/Theme/TransitionCollection.xaml"/>
<ResourceDictionary Source="ms-appx:///UI/Xaml/Control/Theme/Uri.xaml"/>
<ResourceDictionary Source="ms-appx:///UI/Xaml/Control/Theme/WindowOverride.xaml"/>
<ResourceDictionary Source="ms-appx:///UI/Xaml/Control/TokenizingTextBox/TokenizingTextBox.xaml"/>
</ResourceDictionary.MergedDictionaries>

<Style
Expand Down
6 changes: 6 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Resource/Localization/SH.resx
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,12 @@
<data name="ControlPanelPanelSelectorDropdownListName" xml:space="preserve">
<value>列表</value>
</data>
<data name="ControlTokenizingTextBoxRemoveMenuItem" xml:space="preserve">
<value>删除</value>
</data>
<data name="ControlTokenizingTextBoxSelectAllMenuItem" xml:space="preserve">
<value>选择全部</value>
</data>
<data name="CoreExceptionServiceDatabaseCorruptedMessage" xml:space="preserve">
<value>数据库已损坏:{0}</value>
</data>
Expand Down
6 changes: 6 additions & 0 deletions src/Snap.Hutao/Snap.Hutao/Snap.Hutao.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
<None Remove="UI\Xaml\Control\Theme\TransitionCollection.xaml" />
<None Remove="UI\Xaml\Control\Theme\Uri.xaml" />
<None Remove="UI\Xaml\Control\Theme\WindowOverride.xaml" />
<None Remove="UI\Xaml\Control\TokenizingTextBox\TokenizingTextBox.xaml" />
<None Remove="GuideWindow.xaml" />
<None Remove="IdentifyMonitorWindow.xaml" />
<None Remove="IdentityStructs.json" />
Expand Down Expand Up @@ -415,4 +416,9 @@
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
<ItemGroup>
<Page Update="UI\Xaml\Control\TokenizingTextBox\TokenizingTextBox.xaml">
<Generator>MSBuild:Compile</Generator>
</Page>
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,60 +1,49 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

using CommunityToolkit.WinUI;
using CommunityToolkit.WinUI.Controls;
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Snap.Hutao.UI.Input;
using Snap.Hutao.UI.Xaml.Control.TokenizingTextBox;
using System.Collections;

namespace Snap.Hutao.UI.Xaml.Control.AutoSuggestBox;

[DependencyProperty("FilterCommand", typeof(ICommand))]
[DependencyProperty("FilterCommandParameter", typeof(object))]
[DependencyProperty("AvailableTokens", typeof(IReadOnlyDictionary<string, SearchToken>))]
internal sealed partial class AutoSuggestTokenBox : TokenizingTextBox
internal sealed partial class AutoSuggestTokenBox : TokenizingTextBox.TokenizingTextBox
{
public AutoSuggestTokenBox()
{
DefaultStyleKey = typeof(TokenizingTextBox);
TextChanged += OnFilterSuggestionRequested;
QuerySubmitted += OnQuerySubmitted;
TokenItemAdding += OnTokenItemAdding;
TokenItemAdded += OnTokenItemCollectionChanged;
TokenItemRemoved += OnTokenItemCollectionChanged;
Loaded += OnLoaded;
DefaultStyleKey = typeof(TokenizingTextBox.TokenizingTextBox);
}

private void OnLoaded(object sender, RoutedEventArgs e)
public IEnumerable<SearchToken> Tokens
{
if (this.FindDescendant("SuggestionsPopup") is Popup { Child: Border { Child: ListView listView } border })
{
IAppResourceProvider appResourceProvider = this.ServiceProvider().GetRequiredService<IAppResourceProvider>();

listView.Background = null;
listView.Margin = appResourceProvider.GetResource<Thickness>("AutoSuggestListPadding");

border.Background = appResourceProvider.GetResource<Microsoft.UI.Xaml.Media.Brush>("AutoSuggestBoxSuggestionsListBackground");
CornerRadius overlayCornerRadius = appResourceProvider.GetResource<CornerRadius>("OverlayCornerRadius");
CornerRadiusFilterConverter cornerRadiusFilterConverter = new() { Filter = CornerRadiusFilterKind.Bottom };
border.CornerRadius = (CornerRadius)cornerRadiusFilterConverter.Convert(overlayCornerRadius, typeof(CornerRadius), default, default);
}
get => ((IList)ItemsSource).OfType<SearchToken>();
}

private void OnFilterSuggestionRequested(Microsoft.UI.Xaml.Controls.AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
public override void OnTextChanged(Microsoft.UI.Xaml.Controls.AutoSuggestBox sender, AutoSuggestBoxTextChangedEventArgs args)
{
if (IsTokenLimitReached())
{
return;
}

if (string.IsNullOrWhiteSpace(Text))
{
sender.ItemsSource = AvailableTokens
.ExceptBy(Tokens, kvp => kvp.Value)
.OrderBy(kvp => kvp.Value.Kind)
.ThenBy(kvp => kvp.Value.Order)
.Select(kvp => kvp.Value);
}

if (args.Reason == AutoSuggestionBoxTextChangeReason.UserInput)
{
sender.ItemsSource = AvailableTokens
.ExceptBy(Tokens, kvp => kvp.Value)
.Where(kvp => kvp.Value.Value.Contains(Text, StringComparison.OrdinalIgnoreCase))
.OrderBy(kvp => kvp.Value.Kind)
.ThenBy(kvp => kvp.Value.Order)
Expand All @@ -63,7 +52,7 @@ private void OnFilterSuggestionRequested(Microsoft.UI.Xaml.Controls.AutoSuggestB
}
}

private void OnQuerySubmitted(Microsoft.UI.Xaml.Controls.AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
public override void OnQuerySubmitted(Microsoft.UI.Xaml.Controls.AutoSuggestBox sender, AutoSuggestBoxQuerySubmittedEventArgs args)
{
if (args.ChosenSuggestion is not null)
{
Expand All @@ -73,7 +62,7 @@ private void OnQuerySubmitted(Microsoft.UI.Xaml.Controls.AutoSuggestBox sender,
CommandInvocation.TryExecute(FilterCommand, FilterCommandParameter);
}

private void OnTokenItemAdding(TokenizingTextBox sender, TokenItemAddingEventArgs args)
public override void OnTokenItemAdding(TokenizingTextBox.TokenizingTextBox sender, TokenItemAddingEventArgs args)
{
if (string.IsNullOrWhiteSpace(args.TokenText))
{
Expand All @@ -90,13 +79,21 @@ private void OnTokenItemAdding(TokenizingTextBox sender, TokenItemAddingEventArg
}
}

private void OnTokenItemCollectionChanged(TokenizingTextBox sender, object args)
public override void OnTokenItemAdded(TokenizingTextBox.TokenizingTextBox sender, object args)
{
if (args is SearchToken { Kind: SearchTokenKind.None } token)
{
((IList)sender.ItemsSource).Remove(token);
((IList)ItemsSource).Remove(token);
}

base.OnTokenItemAdded(sender, args);

FilterCommand.TryExecute(FilterCommandParameter);
}

public override void OnTokenItemRemoved(TokenizingTextBox.TokenizingTextBox sender, object args)
{
base.OnTokenItemRemoved(sender, args);
FilterCommand.TryExecute(FilterCommandParameter);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Copyright (c) DGP Studio. All rights reserved.
// Licensed under the MIT license.

namespace Snap.Hutao.UI.Xaml.Control.TokenizingTextBox;

internal interface ITokenStringContainer
{
string Text { get; set; }

bool IsLast { get; }
}
Loading

0 comments on commit 8cf71e0

Please sign in to comment.