Skip to content

Commit f965112

Browse files
committed
- Set default sorting by tag column
- Add Tag Column
1 parent 6ebb73b commit f965112

File tree

5 files changed

+122
-3
lines changed

5 files changed

+122
-3
lines changed

Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
<system:String x:Key="flowlauncher_plugin_websearch_edit">Edit</system:String>
1414
<system:String x:Key="flowlauncher_plugin_websearch_add">Add</system:String>
1515
<system:String x:Key="flowlauncher_plugin_websearch_enabled_label">Enabled</system:String>
16+
<system:String x:Key="flowlauncher_plugin_websearch_tag">Tag</system:String>
1617
<system:String x:Key="flowlauncher_plugin_websearch_true">Enabled</system:String>
1718
<system:String x:Key="flowlauncher_plugin_websearch_false">Disabled</system:String>
1819
<system:String x:Key="flowlauncher_plugin_websearch_confirm">Confirm</system:String>

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,8 @@ public string IconPath
3131
}
3232

3333
public string Url { get; set; }
34+
35+
public string Tag { get; set; }
3436

3537
[JsonIgnore]
3638
public bool Status => Enabled;
@@ -45,6 +47,7 @@ public SearchSource DeepCopy()
4547
Url = Url,
4648
Icon = Icon,
4749
CustomIcon = CustomIcon,
50+
Tag = Tag,
4851
Enabled = Enabled
4952
};
5053
return webSearch;

Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<RowDefinition />
102102
<RowDefinition />
103103
<RowDefinition />
104+
<RowDefinition />
104105
</Grid.RowDefinitions>
105106
<TextBlock
106107
Grid.Row="0"
@@ -175,13 +176,28 @@
175176
<TextBlock
176177
Grid.Row="4"
177178
Grid.Column="0"
179+
Margin="10,15,15,10"
180+
HorizontalAlignment="Left"
181+
VerticalAlignment="Center"
182+
FontSize="14"
183+
Text="{DynamicResource flowlauncher_plugin_websearch_tag}" />
184+
<TextBox
185+
Grid.Row="4"
186+
Grid.Column="1"
187+
Margin="10,10,10,0"
188+
HorizontalAlignment="Stretch"
189+
VerticalAlignment="Center"
190+
Text="{Binding SearchSource.Tag}" />
191+
<TextBlock
192+
Grid.Row="5"
193+
Grid.Column="0"
178194
Margin="10,10,15,15"
179195
HorizontalAlignment="Left"
180196
VerticalAlignment="Center"
181197
FontSize="14"
182198
Text="{DynamicResource flowlauncher_plugin_websearch_enabled_label}" />
183199
<CheckBox
184-
Grid.Row="4"
200+
Grid.Row="5"
185201
Grid.Column="1"
186202
Margin="10,10,10,15"
187203
VerticalAlignment="Center"

Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,10 @@
9696
</DataTemplate>
9797
</GridViewColumn.CellTemplate>
9898
</GridViewColumn>
99+
<GridViewColumn
100+
Width="130"
101+
DisplayMemberBinding="{Binding Tag}"
102+
Header="{DynamicResource flowlauncher_plugin_websearch_tag}" />
99103
</GridView>
100104
</ListView.View>
101105
</ListView>

Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml.cs

Lines changed: 97 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@
22
using System.Windows.Controls;
33
using System.ComponentModel;
44
using System.Windows.Data;
5+
using System;
6+
using System.Collections;
7+
using System.Collections.Generic;
58

69
namespace Flow.Launcher.Plugin.WebSearch
710
{
@@ -19,6 +22,40 @@ public SettingsControl(PluginInitContext context, SettingsViewModel viewModel)
1922
_context = context;
2023
_settings = viewModel.Settings;
2124
DataContext = viewModel;
25+
this.Loaded += SettingsControl_Loaded;
26+
}
27+
28+
private void SettingsControl_Loaded(object sender, RoutedEventArgs e)
29+
{
30+
// After the ListView is loaded, sort by Tag in ascending order
31+
if (SearchSourcesListView.ItemsSource != null)
32+
{
33+
// Apply initial sorting by Tag column
34+
Sort("Tag", ListSortDirection.Ascending);
35+
36+
// Display an arrow on the sorted column (optional)
37+
var tagColumn = GetColumnByHeader("Tag");
38+
if (tagColumn != null)
39+
{
40+
tagColumn.HeaderTemplate = Resources["HeaderTemplateArrowUp"] as DataTemplate;
41+
_lastHeaderClicked = tagColumn.Header as GridViewColumnHeader;
42+
_lastDirection = ListSortDirection.Ascending;
43+
}
44+
}
45+
}
46+
47+
// Find column by header name
48+
private GridViewColumn GetColumnByHeader(string header)
49+
{
50+
if (SearchSourcesListView.View is GridView gridView)
51+
{
52+
foreach (var column in gridView.Columns)
53+
{
54+
if (column.Header != null && column.Header.ToString() == header)
55+
return column;
56+
}
57+
}
58+
return null;
2259
}
2360

2461
private void OnAddSearchSearchClick(object sender, RoutedEventArgs e)
@@ -122,8 +159,23 @@ private void Sort(string sortBy, ListSortDirection direction)
122159
{
123160
ICollectionView dataView = CollectionViewSource.GetDefaultView(SearchSourcesListView.ItemsSource);
124161
dataView.SortDescriptions.Clear();
125-
SortDescription sd = new(sortBy, direction);
126-
dataView.SortDescriptions.Add(sd);
162+
163+
// Special handling for Tag sorting
164+
if (sortBy == "Tag")
165+
{
166+
// Apply custom sorting (using TagComparer)
167+
if (dataView is ListCollectionView listView)
168+
{
169+
listView.CustomSort = new TagComparer(direction);
170+
}
171+
}
172+
else
173+
{
174+
// Normal sorting
175+
SortDescription sd = new SortDescription(sortBy, direction);
176+
dataView.SortDescriptions.Add(sd);
177+
}
178+
127179
dataView.Refresh();
128180
}
129181

@@ -139,5 +191,48 @@ private void MouseDoubleClickItem(object sender, System.Windows.Input.MouseButto
139191
webSearch.ShowDialog();
140192
}
141193
}
194+
195+
196+
public class TagComparer : IComparer
197+
{
198+
private readonly ListSortDirection _direction;
199+
200+
public TagComparer(ListSortDirection direction)
201+
{
202+
_direction = direction;
203+
}
204+
205+
public int Compare(object x, object y)
206+
{
207+
if (x is SearchSource sourceX && y is SearchSource sourceY)
208+
{
209+
string tagX = sourceX.Tag;
210+
string tagY = sourceY.Tag;
211+
212+
bool isEmptyX = string.IsNullOrWhiteSpace(tagX);
213+
bool isEmptyY = string.IsNullOrWhiteSpace(tagY);
214+
215+
// If both are empty tags, they are equal
216+
if (isEmptyX && isEmptyY)
217+
return 0;
218+
219+
// If only x is an empty tag, it always goes to the back
220+
if (isEmptyX)
221+
return 1;
222+
223+
// If only y is an empty tag, it always goes to the front
224+
if (isEmptyY)
225+
return -1;
226+
227+
// If both have tags, compare as normal strings
228+
int result = string.Compare(tagX, tagY, StringComparison.OrdinalIgnoreCase);
229+
230+
// Reverse the result according to the sorting direction
231+
return _direction == ListSortDirection.Ascending ? result : -result;
232+
}
233+
234+
return 0;
235+
}
236+
}
142237
}
143238
}

0 commit comments

Comments
 (0)