Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Plugins/Flow.Launcher.Plugin.WebSearch/Languages/en.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
<system:String x:Key="flowlauncher_plugin_websearch_edit">Edit</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_add">Add</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_enabled_label">Enabled</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_tag">Tag</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_true">Enabled</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_false">Disabled</system:String>
<system:String x:Key="flowlauncher_plugin_websearch_confirm">Confirm</system:String>
Expand Down
3 changes: 3 additions & 0 deletions Plugins/Flow.Launcher.Plugin.WebSearch/SearchSource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ public string IconPath
}

public string Url { get; set; }

public string Tag { get; set; }

[JsonIgnore]
public bool Status => Enabled;
Expand All @@ -45,6 +47,7 @@ public SearchSource DeepCopy()
Url = Url,
Icon = Icon,
CustomIcon = CustomIcon,
Tag = Tag,
Enabled = Enabled
};
return webSearch;
Expand Down
18 changes: 17 additions & 1 deletion Plugins/Flow.Launcher.Plugin.WebSearch/SearchSourceSetting.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@
<RowDefinition />
<RowDefinition />
<RowDefinition />
<RowDefinition />
</Grid.RowDefinitions>
<TextBlock
Grid.Row="0"
Expand Down Expand Up @@ -175,13 +176,28 @@
<TextBlock
Grid.Row="4"
Grid.Column="0"
Margin="10,15,15,10"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="14"
Text="{DynamicResource flowlauncher_plugin_websearch_tag}" />
<TextBox
Grid.Row="4"
Grid.Column="1"
Margin="10,10,10,0"
HorizontalAlignment="Stretch"
VerticalAlignment="Center"
Text="{Binding SearchSource.Tag}" />
<TextBlock
Grid.Row="5"
Grid.Column="0"
Margin="10,10,15,15"
HorizontalAlignment="Left"
VerticalAlignment="Center"
FontSize="14"
Text="{DynamicResource flowlauncher_plugin_websearch_enabled_label}" />
<CheckBox
Grid.Row="4"
Grid.Row="5"
Grid.Column="1"
Margin="10,10,10,15"
VerticalAlignment="Center"
Expand Down
4 changes: 4 additions & 0 deletions Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,10 @@
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn
Width="130"
DisplayMemberBinding="{Binding Tag}"
Header="{DynamicResource flowlauncher_plugin_websearch_tag}" />
</GridView>
</ListView.View>
</ListView>
Expand Down
99 changes: 97 additions & 2 deletions Plugins/Flow.Launcher.Plugin.WebSearch/SettingsControl.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
using System.Windows.Controls;
using System.ComponentModel;
using System.Windows.Data;
using System;
using System.Collections;
using System.Collections.Generic;

namespace Flow.Launcher.Plugin.WebSearch
{
Expand All @@ -19,6 +22,40 @@ public SettingsControl(PluginInitContext context, SettingsViewModel viewModel)
_context = context;
_settings = viewModel.Settings;
DataContext = viewModel;
this.Loaded += SettingsControl_Loaded;
}

private void SettingsControl_Loaded(object sender, RoutedEventArgs e)
{
// After the ListView is loaded, sort by Tag in ascending order
if (SearchSourcesListView.ItemsSource != null)
{
// Apply initial sorting by Tag column
Sort("Tag", ListSortDirection.Ascending);

// Display an arrow on the sorted column (optional)
var tagColumn = GetColumnByHeader("Tag");
if (tagColumn != null)
{
tagColumn.HeaderTemplate = Resources["HeaderTemplateArrowUp"] as DataTemplate;
_lastHeaderClicked = tagColumn.Header as GridViewColumnHeader;
_lastDirection = ListSortDirection.Ascending;
}
}
}

// Find column by header name
private GridViewColumn GetColumnByHeader(string header)
{
if (SearchSourcesListView.View is GridView gridView)
{
foreach (var column in gridView.Columns)
{
if (column.Header != null && column.Header.ToString() == header)
return column;
}
}
return null;
}

private void OnAddSearchSearchClick(object sender, RoutedEventArgs e)
Expand Down Expand Up @@ -122,8 +159,23 @@ private void Sort(string sortBy, ListSortDirection direction)
{
ICollectionView dataView = CollectionViewSource.GetDefaultView(SearchSourcesListView.ItemsSource);
dataView.SortDescriptions.Clear();
SortDescription sd = new(sortBy, direction);
dataView.SortDescriptions.Add(sd);

// Special handling for Tag sorting
if (sortBy == "Tag")
{
// Apply custom sorting (using TagComparer)
if (dataView is ListCollectionView listView)
{
listView.CustomSort = new TagComparer(direction);
}
}
else
{
// Normal sorting
SortDescription sd = new SortDescription(sortBy, direction);
dataView.SortDescriptions.Add(sd);
}

dataView.Refresh();
}

Expand All @@ -139,5 +191,48 @@ private void MouseDoubleClickItem(object sender, System.Windows.Input.MouseButto
webSearch.ShowDialog();
}
}


public class TagComparer : IComparer
{
private readonly ListSortDirection _direction;

public TagComparer(ListSortDirection direction)
{
_direction = direction;
}

public int Compare(object x, object y)
{
if (x is SearchSource sourceX && y is SearchSource sourceY)
{
string tagX = sourceX.Tag;
string tagY = sourceY.Tag;

bool isEmptyX = string.IsNullOrWhiteSpace(tagX);
bool isEmptyY = string.IsNullOrWhiteSpace(tagY);

// If both are empty tags, they are equal
if (isEmptyX && isEmptyY)
return 0;

// If only x is an empty tag, it always goes to the back
if (isEmptyX)
return 1;

// If only y is an empty tag, it always goes to the front
if (isEmptyY)
return -1;

// If both have tags, compare as normal strings
int result = string.Compare(tagX, tagY, StringComparison.OrdinalIgnoreCase);

// Reverse the result according to the sorting direction
return _direction == ListSortDirection.Ascending ? result : -result;
}

return 0;
}
}
}
}
Loading