Initialize localized grid & Edit button

This commit is contained in:
Jack251970 2025-03-13 22:34:50 +08:00
parent 8335821ad0
commit e0f02a0d7b
7 changed files with 301 additions and 45 deletions

View file

@ -0,0 +1,17 @@
using System.Text.Json.Serialization;
namespace Flow.Launcher.Plugin.Sys
{
public class Command : BaseModel
{
public string Key { get; set; }
[JsonIgnore]
public string Name { get; set; }
[JsonIgnore]
public string Description { get; set; }
public string Keyword { get; set; }
}
}

View file

@ -4,8 +4,9 @@
xmlns:system="clr-namespace:System;assembly=mscorlib">
<!-- Command List -->
<system:String x:Key="flowlauncher_plugin_sys_command">Command</system:String>
<system:String x:Key="flowlauncher_plugin_sys_name">Name</system:String>
<system:String x:Key="flowlauncher_plugin_sys_desc">Description</system:String>
<system:String x:Key="flowlauncher_plugin_sys_command">Command</system:String>
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer_cmd">Shutdown</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_computer_cmd">Restart</system:String>
@ -29,6 +30,8 @@
<system:String x:Key="flowlauncher_plugin_sys_toggle_game_mode_cmd">Toggle Game Mode</system:String>
<system:String x:Key="flowlauncher_plugin_sys_theme_selector_cmd">Set the Flow Launcher Theme</system:String>
<system:String x:Key="flowlauncher_plugin_sys_edit">Edit</system:String>
<!-- Command Descriptions -->
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer">Shutdown Computer</system:String>
<system:String x:Key="flowlauncher_plugin_sys_restart_computer">Restart Computer</system:String>

View file

@ -19,8 +19,36 @@ namespace Flow.Launcher.Plugin.Sys
public class Main : IPlugin, ISettingProvider, IPluginI18n
{
private PluginInitContext context;
private Settings settings;
private ThemeSelector themeSelector;
private Dictionary<string, string> KeywordTitleMappings = new Dictionary<string, string>();
private readonly Dictionary<string, string> KeywordTitleMappings = new()
{
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
{"Restart", "flowlauncher_plugin_sys_restart_computer_cmd"},
{"Restart With Advanced Boot Options", "flowlauncher_plugin_sys_restart_advanced_cmd"},
{"Log Off/Sign Out", "flowlauncher_plugin_sys_log_off_cmd"},
{"Lock", "flowlauncher_plugin_sys_lock_cmd"},
{"Sleep", "flowlauncher_plugin_sys_sleep_cmd"},
{"Hibernate", "flowlauncher_plugin_sys_hibernate_cmd"},
{"Index Option", "flowlauncher_plugin_sys_indexoption_cmd"},
{"Empty Recycle Bin", "flowlauncher_plugin_sys_emptyrecyclebin_cmd"},
{"Open Recycle Bin", "flowlauncher_plugin_sys_openrecyclebin_cmd"},
{"Exit", "flowlauncher_plugin_sys_exit_cmd"},
{"Save Settings", "flowlauncher_plugin_sys_save_all_settings_cmd"},
{"Restart Flow Launcher", "flowlauncher_plugin_sys_restart_cmd"},
{"Settings", "flowlauncher_plugin_sys_setting_cmd"},
{"Reload Plugin Data", "flowlauncher_plugin_sys_reload_plugin_data_cmd"},
{"Check For Update", "flowlauncher_plugin_sys_check_for_update_cmd"},
{"Open Log Location", "flowlauncher_plugin_sys_open_log_location_cmd"},
{"Flow Launcher Tips", "flowlauncher_plugin_sys_open_docs_tips_cmd"},
{"Flow Launcher UserData Folder", "flowlauncher_plugin_sys_open_userdata_location_cmd"},
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"},
{"Set Flow Launcher Theme", "flowlauncher_plugin_sys_theme_selector_cmd"}
};
private readonly Dictionary<string, string> KeywordDescriptionMappings = new();
private SettingsViewModel _viewModel;
// SHTDN_REASON_MAJOR_OTHER indicates a generic shutdown reason that isn't categorized under hardware failure, software updates, or other predefined reasons.
// SHTDN_REASON_FLAG_PLANNED marks the shutdown as planned rather than an unexpected shutdown or failure
@ -28,12 +56,8 @@ namespace Flow.Launcher.Plugin.Sys
public Control CreateSettingPanel()
{
var commands = Commands();
foreach (var c in commands)
{
c.Title = GetDynamicTitle(null, c);
}
return new SysSettings(commands);
UpdateLocalizedNameDescription(false);
return new SysSettings(_viewModel);
}
public List<Result> Query(Query query)
@ -67,6 +91,29 @@ namespace Flow.Launcher.Plugin.Sys
return results;
}
private string GetTitle(string key)
{
if (!KeywordTitleMappings.TryGetValue(key, out var translationKey))
{
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Title not found for: {key}");
return "Title Not Found";
}
return context.API.GetTranslation(translationKey);
}
private string GetDescription(string key)
{
if (!KeywordDescriptionMappings.TryGetValue(key, out var translationKey))
{
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Description not found for: {key}");
return "Description Not Found";
}
return context.API.GetTranslation(translationKey);
}
[Obsolete]
private string GetDynamicTitle(Query query, Result result)
{
if (!KeywordTitleMappings.TryGetValue(result.Title, out var translationKey))
@ -96,30 +143,26 @@ namespace Flow.Launcher.Plugin.Sys
public void Init(PluginInitContext context)
{
this.context = context;
settings = context.API.LoadSettingJsonStorage<Settings>();
_viewModel = new SettingsViewModel(settings);
themeSelector = new ThemeSelector(context);
KeywordTitleMappings = new Dictionary<string, string>{
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
{"Restart", "flowlauncher_plugin_sys_restart_computer_cmd"},
{"Restart With Advanced Boot Options", "flowlauncher_plugin_sys_restart_advanced_cmd"},
{"Log Off/Sign Out", "flowlauncher_plugin_sys_log_off_cmd"},
{"Lock", "flowlauncher_plugin_sys_lock_cmd"},
{"Sleep", "flowlauncher_plugin_sys_sleep_cmd"},
{"Hibernate", "flowlauncher_plugin_sys_hibernate_cmd"},
{"Index Option", "flowlauncher_plugin_sys_indexoption_cmd"},
{"Empty Recycle Bin", "flowlauncher_plugin_sys_emptyrecyclebin_cmd"},
{"Open Recycle Bin", "flowlauncher_plugin_sys_openrecyclebin_cmd"},
{"Exit", "flowlauncher_plugin_sys_exit_cmd"},
{"Save Settings", "flowlauncher_plugin_sys_save_all_settings_cmd"},
{"Restart Flow Launcher", "flowlauncher_plugin_sys_restart_cmd"},
{"Settings", "flowlauncher_plugin_sys_setting_cmd"},
{"Reload Plugin Data", "flowlauncher_plugin_sys_reload_plugin_data_cmd"},
{"Check For Update", "flowlauncher_plugin_sys_check_for_update_cmd"},
{"Open Log Location", "flowlauncher_plugin_sys_open_log_location_cmd"},
{"Flow Launcher Tips", "flowlauncher_plugin_sys_open_docs_tips_cmd"},
{"Flow Launcher UserData Folder", "flowlauncher_plugin_sys_open_userdata_location_cmd"},
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"},
{"Set Flow Launcher Theme", "flowlauncher_plugin_sys_theme_selector_cmd"}
};
foreach (string key in KeywordTitleMappings.Keys)
{
// Remove _cmd in the last of the strings
KeywordDescriptionMappings[key] = KeywordTitleMappings[key][..^4];
}
}
private void UpdateLocalizedNameDescription(bool force)
{
if (string.IsNullOrEmpty(settings.Commands[0].Name) || force)
{
foreach (var c in settings.Commands)
{
c.Name = GetTitle(c.Key);
c.Description = GetDescription(c.Key);
}
}
}
private static unsafe bool EnableShutdownPrivilege()

View file

@ -0,0 +1,127 @@
using System.Collections.ObjectModel;
using System.Text.Json.Serialization;
namespace Flow.Launcher.Plugin.Sys;
public class Settings : BaseModel
{
public Settings()
{
if (Commands.Count > 0)
{
SelectedCommand = Commands[0];
}
}
public ObservableCollection<Command> Commands { get; set; } = new ObservableCollection<Command>
{
new()
{
Key = "Shutdown",
Keyword = "Shutdown"
},
new()
{
Key = "Restart",
Keyword = "Restart"
},
new()
{
Key = "Restart With Advanced Boot Options",
Keyword = "Restart With Advanced Boot Options"
},
new()
{
Key = "Log Off/Sign Out",
Keyword = "Log Off/Sign Out"
},
new()
{
Key = "Lock",
Keyword = "Lock"
},
new()
{
Key = "Sleep",
Keyword = "Sleep"
},
new()
{
Key = "Hibernate",
Keyword = "Hibernate"
},
new()
{
Key = "Index Option",
Keyword = "Index Option"
},
new()
{
Key = "Empty Recycle Bin",
Keyword = "Empty Recycle Bin"
},
new()
{
Key = "Open Recycle Bin",
Keyword = "Open Recycle Bin"
},
new()
{
Key = "Exit",
Keyword = "Exit"
},
new()
{
Key = "Save Settings",
Keyword = "Save Settings"
},
new()
{
Key = "Restart Flow Launcher",
Keyword = "Restart Flow Launcher"
},
new()
{
Key = "Settings",
Keyword = "Settings"
},
new()
{
Key = "Reload Plugin Data",
Keyword = "Reload Plugin Data"
},
new()
{
Key = "Check For Update",
Keyword = "Check For Update"
},
new()
{
Key = "Open Log Location",
Keyword = "Open Log Location"
},
new()
{
Key = "Flow Launcher Tips",
Keyword = "Flow Launcher Tips"
},
new()
{
Key = "Flow Launcher UserData Folder",
Keyword = "Flow Launcher UserData Folder"
},
new()
{
Key = "Toggle Game Mode",
Keyword = "Toggle Game Mode"
},
new()
{
Key = "Set Flow Launcher Theme",
Keyword = "Set Flow Launcher Theme"
}
};
[JsonIgnore]
public Command SelectedCommand { get; set; }
}

View file

@ -0,0 +1,12 @@
namespace Flow.Launcher.Plugin.Sys
{
public class SettingsViewModel
{
public SettingsViewModel(Settings settings)
{
Settings = settings;
}
public Settings Settings { get; }
}
}

View file

@ -4,36 +4,66 @@
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:vm="clr-namespace:Flow.Launcher.Plugin.Sys"
d:DataContext="{d:DesignInstance vm:SettingsViewModel}"
d:DesignHeight="300"
d:DesignWidth="300"
mc:Ignorable="d">
<Grid Margin="70,18,18,18">
<Grid Margin="70 18 18 18">
<Grid.RowDefinitions>
<RowDefinition />
<RowDefinition Height="Auto" />
</Grid.RowDefinitions>
<ListView
x:Name="lbxCommands"
Grid.Row="0"
Margin="0"
BorderBrush="DarkGray"
BorderThickness="1"
ItemsSource="{Binding Settings.Commands}"
MouseDoubleClick="MouseDoubleClickItem"
SelectedItem="{Binding Settings.SelectedCommand}"
SizeChanged="ListView_SizeChanged"
Style="{StaticResource {x:Static GridView.GridViewStyleKey}}">
<ListView.View>
<GridView>
<GridViewColumn Width="150" Header="{DynamicResource flowlauncher_plugin_sys_command}">
<GridViewColumn Width="150" Header="{DynamicResource flowlauncher_plugin_sys_name}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Title}" TextTrimming="CharacterEllipsis" />
<TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="379" Header="{DynamicResource flowlauncher_plugin_sys_desc}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding SubTitle}" TextTrimming="CharacterEllipsis" />
<TextBlock Text="{Binding Description}" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
<GridViewColumn Width="150" Header="{DynamicResource flowlauncher_plugin_sys_command}">
<GridViewColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Keyword}" TextTrimming="CharacterEllipsis" />
</DataTemplate>
</GridViewColumn.CellTemplate>
</GridViewColumn>
</GridView>
</ListView.View>
</ListView>
<StackPanel
Grid.Row="1"
HorizontalAlignment="Right"
Orientation="Horizontal">
<Button
Width="100"
Margin="10"
Click="OnEditCommandKeywordClick"
Content="{DynamicResource flowlauncher_plugin_sys_edit}" />
</StackPanel>
</Grid>
</UserControl>

View file

@ -1,28 +1,28 @@
using System.Collections.Generic;
using System.Windows;
using System.Windows;
using System.Windows.Controls;
namespace Flow.Launcher.Plugin.Sys
{
public partial class SysSettings : UserControl
{
public SysSettings(List<Result> Results)
private readonly Settings _settings;
public SysSettings(SettingsViewModel viewModel)
{
InitializeComponent();
foreach (var Result in Results)
{
lbxCommands.Items.Add(Result);
}
_settings = viewModel.Settings;
DataContext = viewModel;
}
private void ListView_SizeChanged(object sender, SizeChangedEventArgs e)
{
ListView listView = sender as ListView;
GridView gView = listView.View as GridView;
var workingWidth = listView.ActualWidth - SystemParameters.VerticalScrollBarWidth; // take into account vertical scrollbar
var col1 = 0.3;
var col2 = 0.7;
var col1 = 0.2;
var col2 = 0.6;
var col3 = 0.2;
if (workingWidth <= 0)
{
@ -31,6 +31,30 @@ namespace Flow.Launcher.Plugin.Sys
gView.Columns[0].Width = workingWidth * col1;
gView.Columns[1].Width = workingWidth * col2;
gView.Columns[2].Width = workingWidth * col3;
}
public void OnEditCommandKeywordClick(object sender, RoutedEventArgs e)
{
/*var webSearch = new SearchSourceSettingWindow
(
_settings.SearchSources, _context, _settings.SelectedSearchSource
);
webSearch.ShowDialog();*/
}
private void MouseDoubleClickItem(object sender, System.Windows.Input.MouseButtonEventArgs e)
{
if (((FrameworkElement)e.OriginalSource).DataContext is Command && _settings.SelectedCommand != null)
{
/*var webSearch = new SearchSourceSettingWindow
(
_settings.SearchSources, _context, _settings.SelectedSearchSource
);
webSearch.ShowDialog();*/
}
}
}
}