Skip to content

Commit 96366b2

Browse files
author
David Khristepher Santos
committed
Tag filtering and bug fixes
1 parent 5913bcb commit 96366b2

9 files changed

Lines changed: 169 additions & 21 deletions

Diffusion.Toolkit/Controls/MetadataPanel.xaml

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
</Grid.ContextMenu>
2626

2727
<TabControl Background="Transparent" BorderBrush="{DynamicResource SecondaryBrush}">
28-
<TabItem Header="Metadata" Background="Transparent" GotFocus="UIElement_OnGotFocus" HorizontalContentAlignment="Stretch">
28+
<TabItem Header="Metadata" Background="Transparent" HorizontalContentAlignment="Stretch">
2929
<ScrollViewer Background="Transparent" VerticalScrollBarVisibility="Auto">
3030
<StackPanel Background="Transparent">
3131

@@ -223,7 +223,7 @@
223223
</StackPanel>
224224
</ScrollViewer>
225225
</TabItem>
226-
<TabItem Header="Tags" Background="Transparent" GotFocus="UIElement_OnGotFocus" HorizontalContentAlignment="Stretch">
226+
<TabItem Header="Tags" Background="Transparent" HorizontalContentAlignment="Stretch">
227227

228228
<Grid Background="Transparent" Height="{Binding ActualHeight, RelativeSource={RelativeSource FindAncestor, AncestorType=TabControl, AncestorLevel=1}}">
229229
<Grid.RowDefinitions>
@@ -233,11 +233,18 @@
233233
<RowDefinition Height="36"></RowDefinition>
234234
</Grid.RowDefinitions>
235235
<Grid Background="Transparent" Grid.Row="0">
236-
<TextBox></TextBox>
236+
<Grid.ColumnDefinitions>
237+
<ColumnDefinition Width="*"/>
238+
<ColumnDefinition Width="20"/>
239+
</Grid.ColumnDefinitions>
240+
<TextBox x:Name="TagFilter" KeyDown="TagFilter_OnKeyDown" TextChanged="TagFilter_OnTextChanged"></TextBox>
241+
<Button Style="{StaticResource BorderlessButton}" Grid.Column="1" Click="ClearFilter_OnClick">
242+
<fa:FontAwesome Icon="Close"></fa:FontAwesome>
243+
</Button>
237244
</Grid>
238245
<Grid Background="Transparent" Grid.Row="1" ></Grid>
239246
<ScrollViewer Grid.Row="1" Background="Transparent" VerticalScrollBarVisibility="Auto">
240-
<ListView ItemsSource="{Binding ImageTags}">
247+
<ListView ItemsSource="{Binding FilteredTags}" PreviewMouseWheel="UIElement_OnPreviewMouseWheel">
241248
<ListView.ItemTemplate>
242249
<DataTemplate>
243250
<CheckBox IsChecked="{Binding IsTicked}" Content="{Binding Name}"></CheckBox>
@@ -263,26 +270,26 @@
263270
<ColumnDefinition Width="*"/>
264271
<ColumnDefinition Width="20"/>
265272
</Grid.ColumnDefinitions>
266-
<TextBox x:Name="AddTagText" Grid.Column="0"></TextBox>
273+
<TextBox x:Name="AddTagText" Grid.Column="0" KeyDown="AddTagText_OnKeyDown"></TextBox>
267274
<Button Style="{StaticResource BorderlessButton}" Grid.Column="1" Click="AddTagButton_OnClick">
268275
<fa:FontAwesome Icon="Plus"></fa:FontAwesome>
269276
</Button>
270277
</Grid>
271278
</Grid>
272279
</TabItem>
273-
<TabItem Header="Workflow" Background="Transparent" GotFocus="UIElement_OnGotFocus" HorizontalContentAlignment="Stretch">
280+
<TabItem Header="Workflow" Background="Transparent" HorizontalContentAlignment="Stretch">
274281
<ScrollViewer Background="Transparent" VerticalScrollBarVisibility="Auto">
275282
<local:ComfyNodeStack Nodes="{Binding Nodes}" Background="Transparent"></local:ComfyNodeStack>
276283
</ScrollViewer>
277284
</TabItem>
278-
<TabItem Header="Raw Metadata" Background="Transparent" GotFocus="UIElement_OnGotFocus" HorizontalContentAlignment="Stretch">
285+
<TabItem Header="Raw Metadata" Background="Transparent" HorizontalContentAlignment="Stretch">
279286
<ScrollViewer Grid.Row="0" Background="Transparent"
280287
VerticalAlignment="Stretch"
281288
VerticalScrollBarVisibility="Auto">
282289
<TextBox FontFamily="Consolas" Padding="5" TextWrapping="Wrap" IsReadOnly="True" Margin="5" Text="{Binding Workflow}" Foreground="{StaticResource ForegroundBrush}"></TextBox>
283290
</ScrollViewer>
284291
</TabItem>
285-
<TabItem Visibility="{Binding HasError, Converter={StaticResource boolToVis}}" Header="Errors" Background="Transparent" GotFocus="UIElement_OnGotFocus" HorizontalContentAlignment="Stretch">
292+
<TabItem Visibility="{Binding HasError, Converter={StaticResource boolToVis}}" Header="Errors" Background="Transparent" HorizontalContentAlignment="Stretch">
286293
<Grid>
287294
<ScrollViewer Grid.Row="0" Background="Transparent"
288295
VerticalAlignment="Stretch"

Diffusion.Toolkit/Controls/MetadataPanel.xaml.cs

Lines changed: 96 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
1-
using Diffusion.Toolkit.Models;
1+
using Diffusion.Common;
2+
using Diffusion.Common.Query;
3+
using Diffusion.Database.Models;
4+
using Diffusion.Toolkit.Configuration;
5+
using Diffusion.Toolkit.Models;
6+
using Diffusion.Toolkit.Services;
7+
using System.Linq;
28
using System.Threading;
39
using System.Windows;
410
using System.Windows.Controls;
511
using System.Windows.Input;
6-
using Diffusion.Common;
7-
using Diffusion.Database.Models;
8-
using Diffusion.Toolkit.Configuration;
9-
using Diffusion.Toolkit.Services;
12+
using System.Windows.Media;
1013

1114
namespace Diffusion.Toolkit.Controls
1215
{
@@ -19,9 +22,24 @@ public partial class MetadataPanel : UserControl
1922
nameof(CurrentImage),
2023
typeof(ImageViewModel),
2124
typeof(MetadataPanel),
22-
new PropertyMetadata(default(ImageEntry))
25+
new PropertyMetadata(default(ImageEntry), PropertyChangedCallback)
2326
);
2427

28+
private static void PropertyChangedCallback(DependencyObject d, DependencyPropertyChangedEventArgs e)
29+
{
30+
if (d is MetadataPanel panel)
31+
{
32+
panel.CurrentImage.PropertyChanged += (sender, args) =>
33+
{
34+
if (args.PropertyName == nameof(ImageViewModel.ImageTags))
35+
{
36+
panel.UpdateFilter();
37+
}
38+
};
39+
panel.UpdateFilter();
40+
}
41+
}
42+
2543
public ImageViewModel CurrentImage
2644
{
2745
get => (ImageViewModel)GetValue(CurrentImageProperty);
@@ -81,13 +99,85 @@ private void UIElement_OnGotFocus(object sender, RoutedEventArgs e)
8199
}
82100

83101
private void AddTagButton_OnClick(object sender, RoutedEventArgs e)
102+
{
103+
AddTag();
104+
}
105+
106+
private void AddTagText_OnKeyDown(object sender, KeyEventArgs e)
107+
{
108+
if (e.Key == Key.Enter)
109+
{
110+
AddTag();
111+
}
112+
}
113+
114+
private void AddTag()
84115
{
85116
var tagName = AddTagText.Text.Trim();
86117
if (tagName.Length > 0)
87118
{
88119
ServiceLocator.DataStore.CreateTag(tagName);
89120
AddTagText.Text = "";
90121
CurrentImage.ImageTags = ServiceLocator.TagService.GetImageTagViews(CurrentImage.Id);
122+
ServiceLocator.TagService.LoadTags();
123+
UpdateFilter();
124+
}
125+
}
126+
127+
private void UpdateFilter()
128+
{
129+
if (CurrentImage.ImageTags == null)
130+
{
131+
CurrentImage.FilteredTags = null;
132+
return;
133+
}
134+
if (TagFilter.Text is { Length: > 0 })
135+
{
136+
var filter = TagFilter.Text.ToLower().Trim();
137+
138+
CurrentImage.FilteredTags = CurrentImage.ImageTags.Where(d => d.Name.ToLower().Contains(filter)).ToList();
139+
}
140+
else
141+
{
142+
CurrentImage.FilteredTags = CurrentImage.ImageTags.ToList();
143+
}
144+
}
145+
146+
private void ClearFilter_OnClick(object sender, RoutedEventArgs e)
147+
{
148+
TagFilter.Text = "";
149+
UpdateFilter();
150+
}
151+
152+
private void TagFilter_OnTextChanged(object sender, TextChangedEventArgs e)
153+
{
154+
UpdateFilter();
155+
}
156+
157+
private void UIElement_OnPreviewMouseWheel(object sender, MouseWheelEventArgs e)
158+
{
159+
e.Handled = true; // Mark the event as handled to stop tunneling
160+
161+
// Create a new MouseWheelEventArgs for the bubbling event
162+
var eventArg = new MouseWheelEventArgs(e.MouseDevice, e.Timestamp, e.Delta)
163+
{
164+
RoutedEvent = UIElement.MouseWheelEvent,
165+
Source = sender
166+
};
167+
168+
// Raise the new bubbling event on the ListView itself
169+
if (VisualTreeHelper.GetParent((FrameworkElement)sender) is UIElement parent)
170+
{
171+
parent.RaiseEvent(eventArg);
172+
}
173+
}
174+
175+
private void TagFilter_OnKeyDown(object sender, KeyEventArgs e)
176+
{
177+
if (e.Key == Key.Escape)
178+
{
179+
TagFilter.Text = "";
180+
UpdateFilter();
91181
}
92182
}
93183
}

Diffusion.Toolkit/MainWindow.xaml.Events.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ private void InitEvents()
9393

9494
_model.SortAlbumCommand = new RelayCommand<object>((o) => SortAlbums());
9595
_model.ClearAlbumsCommand = new RelayCommand<object>((o) => ClearAlbums());
96+
_model.ClearTagsCommand = new RelayCommand<object>((o) => ClearTags());
9697
_model.ClearModelsCommand = new RelayCommand<object>((o) => ClearModels());
9798

9899
_model.ToggleNavigationPane = new RelayCommand<object>((o) => ToggleNavigationPane());

Diffusion.Toolkit/MainWindow.xaml.Scanning.cs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,18 @@ private void ClearAlbums()
236236
ServiceLocator.SearchService.ExecuteSearch();
237237
}
238238

239+
private void ClearTags()
240+
{
241+
foreach (var tag in _model.Tags)
242+
{
243+
tag.IsTicked = false;
244+
}
245+
ServiceLocator.MainModel.SelectedTagsCount = 0;
246+
ServiceLocator.MainModel.HasSelectedTags = false;
247+
248+
ServiceLocator.SearchService.ExecuteSearch();
249+
}
250+
239251
private void ClearModels()
240252
{
241253
foreach (var model in _model.ImageModels)

Diffusion.Toolkit/MainWindow.xaml.Tags.cs

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ private void InitTags()
2424
var title = GetLocalizedText("Actions.Tags.Create.Title");
2525

2626
var (result, name) = await ServiceLocator.MessageService.ShowInput(GetLocalizedText("Actions.Tags.Create.Message"), title);
27-
27+
2828
if (result == PopupResult.OK)
2929
{
3030
name = name.Trim();
@@ -41,7 +41,7 @@ private void InitTags()
4141
}
4242
});
4343

44-
44+
4545
_model.RenameTagCommand = new AsyncCommand<TagFilterView>(async (tag) =>
4646
{
4747
var title = GetLocalizedText("Actions.Tags.Rename.Title");
@@ -96,7 +96,22 @@ private void InitTags()
9696

9797
private void LoadTags()
9898
{
99+
HashSet<int> currentSelected = new HashSet<int>();
100+
101+
if (_model.Tags is { Count: > 0 })
102+
{
103+
currentSelected = _model.Tags.Where(d => d.IsTicked).Select(d => d.Id).ToHashSet();
104+
}
105+
99106
_model.Tags = ServiceLocator.TagService.GetTagFilterViews();
107+
108+
if (currentSelected.Any())
109+
{
110+
foreach (var tag in _model.Tags)
111+
{
112+
tag.IsTicked = currentSelected.Contains(tag.Id);
113+
}
114+
}
100115
}
101116

102117
public void UpdateTagName(int id, string name)

Diffusion.Toolkit/Models/ImageViewModel.cs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,14 @@ public IReadOnlyCollection<ImageTagView> ImageTags
287287
get;
288288
set => SetField(ref field, value);
289289
}
290-
290+
291+
public IReadOnlyCollection<ImageTagView> FilteredTags
292+
{
293+
get;
294+
set => SetField(ref field, value);
295+
}
296+
297+
291298
public string ErrorMessage
292299
{
293300
get;

Diffusion.Toolkit/Models/MainModel.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -381,6 +381,12 @@ public ICommand ClearAlbumsCommand
381381
set => SetField(ref field, value);
382382
}
383383

384+
public ICommand ClearTagsCommand
385+
{
386+
get;
387+
set => SetField(ref field, value);
388+
}
389+
384390
public bool HasSelectedAlbums
385391
{
386392
get;

Diffusion.Toolkit/Pages/Search.xaml

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@
111111
</StackPanel>
112112

113113
<StackPanel Grid.Column="2" VerticalAlignment="Center" Margin="0,0,10,0" Orientation="Horizontal">
114-
<Border Visibility="{Binding MainModel.HasSelectedModels, Converter={StaticResource boolToVisCol}}" Background="SeaGreen" CornerRadius="6" Height="24" Padding="6,0,6,0" Margin="0,0,10,0">
114+
<Border Margin="0,0,10,0" Visibility="{Binding MainModel.HasSelectedModels, Converter={StaticResource boolToVisCol}}" Background="SeaGreen" CornerRadius="6" Height="24" Padding="6,0,6,0">
115115
<StackPanel Orientation="Horizontal">
116116
<Label FontSize="10" Content="{lex:Loc Search.Navigation.Models}"></Label>
117117
<Label FontSize="12" Content="{Binding MainModel.SelectedModelsCount}"></Label>
@@ -120,7 +120,7 @@
120120
</Button>
121121
</StackPanel>
122122
</Border>
123-
<Border Visibility="{Binding MainModel.HasSelectedAlbums, Converter={StaticResource boolToVisCol}}" Background="SeaGreen" CornerRadius="6" Height="24" Padding="6,0,6,0">
123+
<Border Margin="0,0,10,0" Visibility="{Binding MainModel.HasSelectedAlbums, Converter={StaticResource boolToVisCol}}" Background="SeaGreen" CornerRadius="6" Height="24" Padding="6,0,6,0">
124124
<StackPanel Orientation="Horizontal">
125125
<Label FontSize="10">Albums:</Label>
126126
<Label FontSize="12" Content="{Binding MainModel.SelectedAlbumsCount}"></Label>
@@ -129,6 +129,16 @@
129129
</Button>
130130
</StackPanel>
131131
</Border>
132+
<Border Margin="0,0,10,0" Visibility="{Binding MainModel.HasSelectedTags, Converter={StaticResource boolToVisCol}}" Background="SeaGreen" CornerRadius="6" Height="24" Padding="6,0,6,0">
133+
<StackPanel Orientation="Horizontal">
134+
<Label FontSize="10">Tags:</Label>
135+
<Label FontSize="12" Content="{Binding MainModel.SelectedTagsCount}"></Label>
136+
<Button Style="{StaticResource StaticButton}" Command="{Binding MainModel.ClearTagsCommand}">
137+
<fa:FontAwesome FontSize="10" Icon="Close"></fa:FontAwesome>
138+
</Button>
139+
</StackPanel>
140+
</Border>
141+
132142
</StackPanel>
133143
<Label Grid.Column="3" FontSize="12" VerticalContentAlignment="Center" Content="{lex:Loc Search.SortBy}"></Label>
134144
<ComboBox Height="28" VerticalContentAlignment="Center" ItemsSource="{Binding SortOptions}" DisplayMemberPath="Name" SelectedValue="{Binding SortBy}" SelectedValuePath="Value" Grid.Column="4" Margin="0,0,10,0">

Diffusion.Toolkit/Pages/Search.xaml.Events.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ private void Tag_OnClick(object sender, RoutedEventArgs e)
4343
tagFilterView.IsTicked = true;
4444

4545
ServiceLocator.MainModel.SelectedTagsCount = 1;
46-
ServiceLocator.MainModel.HasSelectedAlbums = true;
46+
ServiceLocator.MainModel.HasSelectedTags = true;
4747

4848
SearchImages(null);
4949

@@ -62,7 +62,7 @@ private void TagCheck_OnClick(object sender, RoutedEventArgs e)
6262
{
6363
var selectedTags = ServiceLocator.MainModel.Tags.Where(d => d.IsTicked).ToList();
6464
ServiceLocator.MainModel.SelectedTagsCount = selectedTags.Count;
65-
ServiceLocator.MainModel.HasSelectedAlbums = selectedTags.Any();
65+
ServiceLocator.MainModel.HasSelectedTags = selectedTags.Any();
6666

6767
SearchImages(null);
6868
}

0 commit comments

Comments
 (0)