Skip to content

Commit e0f02a0

Browse files
committed
Initialize localized grid & Edit button
1 parent 8335821 commit e0f02a0

File tree

7 files changed

+301
-45
lines changed

7 files changed

+301
-45
lines changed
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System.Text.Json.Serialization;
2+
3+
namespace Flow.Launcher.Plugin.Sys
4+
{
5+
public class Command : BaseModel
6+
{
7+
public string Key { get; set; }
8+
9+
[JsonIgnore]
10+
public string Name { get; set; }
11+
12+
[JsonIgnore]
13+
public string Description { get; set; }
14+
15+
public string Keyword { get; set; }
16+
}
17+
}

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,9 @@
44
xmlns:system="clr-namespace:System;assembly=mscorlib">
55

66
<!-- Command List -->
7-
<system:String x:Key="flowlauncher_plugin_sys_command">Command</system:String>
7+
<system:String x:Key="flowlauncher_plugin_sys_name">Name</system:String>
88
<system:String x:Key="flowlauncher_plugin_sys_desc">Description</system:String>
9+
<system:String x:Key="flowlauncher_plugin_sys_command">Command</system:String>
910

1011
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer_cmd">Shutdown</system:String>
1112
<system:String x:Key="flowlauncher_plugin_sys_restart_computer_cmd">Restart</system:String>
@@ -29,6 +30,8 @@
2930
<system:String x:Key="flowlauncher_plugin_sys_toggle_game_mode_cmd">Toggle Game Mode</system:String>
3031
<system:String x:Key="flowlauncher_plugin_sys_theme_selector_cmd">Set the Flow Launcher Theme</system:String>
3132

33+
<system:String x:Key="flowlauncher_plugin_sys_edit">Edit</system:String>
34+
3235
<!-- Command Descriptions -->
3336
<system:String x:Key="flowlauncher_plugin_sys_shutdown_computer">Shutdown Computer</system:String>
3437
<system:String x:Key="flowlauncher_plugin_sys_restart_computer">Restart Computer</system:String>

Plugins/Flow.Launcher.Plugin.Sys/Main.cs

Lines changed: 73 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,45 @@ namespace Flow.Launcher.Plugin.Sys
1919
public class Main : IPlugin, ISettingProvider, IPluginI18n
2020
{
2121
private PluginInitContext context;
22+
private Settings settings;
2223
private ThemeSelector themeSelector;
23-
private Dictionary<string, string> KeywordTitleMappings = new Dictionary<string, string>();
24+
25+
private readonly Dictionary<string, string> KeywordTitleMappings = new()
26+
{
27+
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
28+
{"Restart", "flowlauncher_plugin_sys_restart_computer_cmd"},
29+
{"Restart With Advanced Boot Options", "flowlauncher_plugin_sys_restart_advanced_cmd"},
30+
{"Log Off/Sign Out", "flowlauncher_plugin_sys_log_off_cmd"},
31+
{"Lock", "flowlauncher_plugin_sys_lock_cmd"},
32+
{"Sleep", "flowlauncher_plugin_sys_sleep_cmd"},
33+
{"Hibernate", "flowlauncher_plugin_sys_hibernate_cmd"},
34+
{"Index Option", "flowlauncher_plugin_sys_indexoption_cmd"},
35+
{"Empty Recycle Bin", "flowlauncher_plugin_sys_emptyrecyclebin_cmd"},
36+
{"Open Recycle Bin", "flowlauncher_plugin_sys_openrecyclebin_cmd"},
37+
{"Exit", "flowlauncher_plugin_sys_exit_cmd"},
38+
{"Save Settings", "flowlauncher_plugin_sys_save_all_settings_cmd"},
39+
{"Restart Flow Launcher", "flowlauncher_plugin_sys_restart_cmd"},
40+
{"Settings", "flowlauncher_plugin_sys_setting_cmd"},
41+
{"Reload Plugin Data", "flowlauncher_plugin_sys_reload_plugin_data_cmd"},
42+
{"Check For Update", "flowlauncher_plugin_sys_check_for_update_cmd"},
43+
{"Open Log Location", "flowlauncher_plugin_sys_open_log_location_cmd"},
44+
{"Flow Launcher Tips", "flowlauncher_plugin_sys_open_docs_tips_cmd"},
45+
{"Flow Launcher UserData Folder", "flowlauncher_plugin_sys_open_userdata_location_cmd"},
46+
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"},
47+
{"Set Flow Launcher Theme", "flowlauncher_plugin_sys_theme_selector_cmd"}
48+
};
49+
private readonly Dictionary<string, string> KeywordDescriptionMappings = new();
50+
51+
private SettingsViewModel _viewModel;
2452

2553
// SHTDN_REASON_MAJOR_OTHER indicates a generic shutdown reason that isn't categorized under hardware failure, software updates, or other predefined reasons.
2654
// SHTDN_REASON_FLAG_PLANNED marks the shutdown as planned rather than an unexpected shutdown or failure
2755
private const SHUTDOWN_REASON REASON = SHUTDOWN_REASON.SHTDN_REASON_MAJOR_OTHER | SHUTDOWN_REASON.SHTDN_REASON_FLAG_PLANNED;
2856

2957
public Control CreateSettingPanel()
3058
{
31-
var commands = Commands();
32-
foreach (var c in commands)
33-
{
34-
c.Title = GetDynamicTitle(null, c);
35-
}
36-
return new SysSettings(commands);
59+
UpdateLocalizedNameDescription(false);
60+
return new SysSettings(_viewModel);
3761
}
3862

3963
public List<Result> Query(Query query)
@@ -67,6 +91,29 @@ public List<Result> Query(Query query)
6791
return results;
6892
}
6993

94+
private string GetTitle(string key)
95+
{
96+
if (!KeywordTitleMappings.TryGetValue(key, out var translationKey))
97+
{
98+
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Title not found for: {key}");
99+
return "Title Not Found";
100+
}
101+
102+
return context.API.GetTranslation(translationKey);
103+
}
104+
105+
private string GetDescription(string key)
106+
{
107+
if (!KeywordDescriptionMappings.TryGetValue(key, out var translationKey))
108+
{
109+
Log.Error("Flow.Launcher.Plugin.Sys.Main", $"Description not found for: {key}");
110+
return "Description Not Found";
111+
}
112+
113+
return context.API.GetTranslation(translationKey);
114+
}
115+
116+
[Obsolete]
70117
private string GetDynamicTitle(Query query, Result result)
71118
{
72119
if (!KeywordTitleMappings.TryGetValue(result.Title, out var translationKey))
@@ -96,30 +143,26 @@ private string GetDynamicTitle(Query query, Result result)
96143
public void Init(PluginInitContext context)
97144
{
98145
this.context = context;
146+
settings = context.API.LoadSettingJsonStorage<Settings>();
147+
_viewModel = new SettingsViewModel(settings);
99148
themeSelector = new ThemeSelector(context);
100-
KeywordTitleMappings = new Dictionary<string, string>{
101-
{"Shutdown", "flowlauncher_plugin_sys_shutdown_computer_cmd"},
102-
{"Restart", "flowlauncher_plugin_sys_restart_computer_cmd"},
103-
{"Restart With Advanced Boot Options", "flowlauncher_plugin_sys_restart_advanced_cmd"},
104-
{"Log Off/Sign Out", "flowlauncher_plugin_sys_log_off_cmd"},
105-
{"Lock", "flowlauncher_plugin_sys_lock_cmd"},
106-
{"Sleep", "flowlauncher_plugin_sys_sleep_cmd"},
107-
{"Hibernate", "flowlauncher_plugin_sys_hibernate_cmd"},
108-
{"Index Option", "flowlauncher_plugin_sys_indexoption_cmd"},
109-
{"Empty Recycle Bin", "flowlauncher_plugin_sys_emptyrecyclebin_cmd"},
110-
{"Open Recycle Bin", "flowlauncher_plugin_sys_openrecyclebin_cmd"},
111-
{"Exit", "flowlauncher_plugin_sys_exit_cmd"},
112-
{"Save Settings", "flowlauncher_plugin_sys_save_all_settings_cmd"},
113-
{"Restart Flow Launcher", "flowlauncher_plugin_sys_restart_cmd"},
114-
{"Settings", "flowlauncher_plugin_sys_setting_cmd"},
115-
{"Reload Plugin Data", "flowlauncher_plugin_sys_reload_plugin_data_cmd"},
116-
{"Check For Update", "flowlauncher_plugin_sys_check_for_update_cmd"},
117-
{"Open Log Location", "flowlauncher_plugin_sys_open_log_location_cmd"},
118-
{"Flow Launcher Tips", "flowlauncher_plugin_sys_open_docs_tips_cmd"},
119-
{"Flow Launcher UserData Folder", "flowlauncher_plugin_sys_open_userdata_location_cmd"},
120-
{"Toggle Game Mode", "flowlauncher_plugin_sys_toggle_game_mode_cmd"},
121-
{"Set Flow Launcher Theme", "flowlauncher_plugin_sys_theme_selector_cmd"}
122-
};
149+
foreach (string key in KeywordTitleMappings.Keys)
150+
{
151+
// Remove _cmd in the last of the strings
152+
KeywordDescriptionMappings[key] = KeywordTitleMappings[key][..^4];
153+
}
154+
}
155+
156+
private void UpdateLocalizedNameDescription(bool force)
157+
{
158+
if (string.IsNullOrEmpty(settings.Commands[0].Name) || force)
159+
{
160+
foreach (var c in settings.Commands)
161+
{
162+
c.Name = GetTitle(c.Key);
163+
c.Description = GetDescription(c.Key);
164+
}
165+
}
123166
}
124167

125168
private static unsafe bool EnableShutdownPrivilege()
Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
using System.Collections.ObjectModel;
2+
using System.Text.Json.Serialization;
3+
4+
namespace Flow.Launcher.Plugin.Sys;
5+
6+
public class Settings : BaseModel
7+
{
8+
public Settings()
9+
{
10+
if (Commands.Count > 0)
11+
{
12+
SelectedCommand = Commands[0];
13+
}
14+
}
15+
16+
public ObservableCollection<Command> Commands { get; set; } = new ObservableCollection<Command>
17+
{
18+
new()
19+
{
20+
Key = "Shutdown",
21+
Keyword = "Shutdown"
22+
},
23+
new()
24+
{
25+
Key = "Restart",
26+
Keyword = "Restart"
27+
},
28+
new()
29+
{
30+
Key = "Restart With Advanced Boot Options",
31+
Keyword = "Restart With Advanced Boot Options"
32+
},
33+
new()
34+
{
35+
Key = "Log Off/Sign Out",
36+
Keyword = "Log Off/Sign Out"
37+
},
38+
new()
39+
{
40+
Key = "Lock",
41+
Keyword = "Lock"
42+
},
43+
new()
44+
{
45+
Key = "Sleep",
46+
Keyword = "Sleep"
47+
},
48+
new()
49+
{
50+
Key = "Hibernate",
51+
Keyword = "Hibernate"
52+
},
53+
new()
54+
{
55+
Key = "Index Option",
56+
Keyword = "Index Option"
57+
},
58+
new()
59+
{
60+
Key = "Empty Recycle Bin",
61+
Keyword = "Empty Recycle Bin"
62+
},
63+
new()
64+
{
65+
Key = "Open Recycle Bin",
66+
Keyword = "Open Recycle Bin"
67+
},
68+
new()
69+
{
70+
Key = "Exit",
71+
Keyword = "Exit"
72+
},
73+
new()
74+
{
75+
Key = "Save Settings",
76+
Keyword = "Save Settings"
77+
},
78+
new()
79+
{
80+
Key = "Restart Flow Launcher",
81+
Keyword = "Restart Flow Launcher"
82+
},
83+
new()
84+
{
85+
Key = "Settings",
86+
Keyword = "Settings"
87+
},
88+
new()
89+
{
90+
Key = "Reload Plugin Data",
91+
Keyword = "Reload Plugin Data"
92+
},
93+
new()
94+
{
95+
Key = "Check For Update",
96+
Keyword = "Check For Update"
97+
},
98+
new()
99+
{
100+
Key = "Open Log Location",
101+
Keyword = "Open Log Location"
102+
},
103+
new()
104+
{
105+
Key = "Flow Launcher Tips",
106+
Keyword = "Flow Launcher Tips"
107+
},
108+
new()
109+
{
110+
Key = "Flow Launcher UserData Folder",
111+
Keyword = "Flow Launcher UserData Folder"
112+
},
113+
new()
114+
{
115+
Key = "Toggle Game Mode",
116+
Keyword = "Toggle Game Mode"
117+
},
118+
new()
119+
{
120+
Key = "Set Flow Launcher Theme",
121+
Keyword = "Set Flow Launcher Theme"
122+
}
123+
};
124+
125+
[JsonIgnore]
126+
public Command SelectedCommand { get; set; }
127+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
namespace Flow.Launcher.Plugin.Sys
2+
{
3+
public class SettingsViewModel
4+
{
5+
public SettingsViewModel(Settings settings)
6+
{
7+
Settings = settings;
8+
}
9+
10+
public Settings Settings { get; }
11+
}
12+
}

Plugins/Flow.Launcher.Plugin.Sys/SysSettings.xaml

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,36 +4,66 @@
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
66
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:vm="clr-namespace:Flow.Launcher.Plugin.Sys"
8+
d:DataContext="{d:DesignInstance vm:SettingsViewModel}"
79
d:DesignHeight="300"
810
d:DesignWidth="300"
911
mc:Ignorable="d">
10-
<Grid Margin="70,18,18,18">
12+
<Grid Margin="70 18 18 18">
13+
<Grid.RowDefinitions>
14+
<RowDefinition />
15+
<RowDefinition Height="Auto" />
16+
</Grid.RowDefinitions>
17+
1118
<ListView
1219
x:Name="lbxCommands"
1320
Grid.Row="0"
1421
Margin="0"
1522
BorderBrush="DarkGray"
1623
BorderThickness="1"
24+
ItemsSource="{Binding Settings.Commands}"
25+
MouseDoubleClick="MouseDoubleClickItem"
26+
SelectedItem="{Binding Settings.SelectedCommand}"
1727
SizeChanged="ListView_SizeChanged"
1828
Style="{StaticResource {x:Static GridView.GridViewStyleKey}}">
1929
<ListView.View>
2030
<GridView>
21-
<GridViewColumn Width="150" Header="{DynamicResource flowlauncher_plugin_sys_command}">
31+
<GridViewColumn Width="150" Header="{DynamicResource flowlauncher_plugin_sys_name}">
2232
<GridViewColumn.CellTemplate>
2333
<DataTemplate>
24-
<TextBlock Text="{Binding Title}" TextTrimming="CharacterEllipsis" />
34+
<TextBlock Text="{Binding Name}" TextTrimming="CharacterEllipsis" />
2535
</DataTemplate>
2636
</GridViewColumn.CellTemplate>
2737
</GridViewColumn>
38+
2839
<GridViewColumn Width="379" Header="{DynamicResource flowlauncher_plugin_sys_desc}">
2940
<GridViewColumn.CellTemplate>
3041
<DataTemplate>
31-
<TextBlock Text="{Binding SubTitle}" TextTrimming="CharacterEllipsis" />
42+
<TextBlock Text="{Binding Description}" TextTrimming="CharacterEllipsis" />
43+
</DataTemplate>
44+
</GridViewColumn.CellTemplate>
45+
</GridViewColumn>
46+
47+
<GridViewColumn Width="150" Header="{DynamicResource flowlauncher_plugin_sys_command}">
48+
<GridViewColumn.CellTemplate>
49+
<DataTemplate>
50+
<TextBlock Text="{Binding Keyword}" TextTrimming="CharacterEllipsis" />
3251
</DataTemplate>
3352
</GridViewColumn.CellTemplate>
3453
</GridViewColumn>
3554
</GridView>
3655
</ListView.View>
3756
</ListView>
57+
58+
<StackPanel
59+
Grid.Row="1"
60+
HorizontalAlignment="Right"
61+
Orientation="Horizontal">
62+
<Button
63+
Width="100"
64+
Margin="10"
65+
Click="OnEditCommandKeywordClick"
66+
Content="{DynamicResource flowlauncher_plugin_sys_edit}" />
67+
</StackPanel>
3868
</Grid>
3969
</UserControl>

0 commit comments

Comments
 (0)