Skip to content

Commit 3bba8dd

Browse files
Jack251970TBM13
authored andcommitted
Merge pull request Flow-Launcher#4023 from Flow-Launcher/shell_setting_panel_enhancement
Refactor ShellSettings with Binding & Fix IsEnabled logic
1 parent 7dbc7e8 commit 3bba8dd

File tree

7 files changed

+269
-152
lines changed

7 files changed

+269
-152
lines changed
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace Flow.Launcher.Plugin.Shell.Converters;
6+
7+
public class LeaveShellOpenOrCloseShellAfterPressEnabledConverter : IMultiValueConverter
8+
{
9+
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
if (
12+
values.Length != 2 ||
13+
values[0] is not bool closeShellAfterPressOrLeaveShellOpen ||
14+
values[1] is not Shell shell
15+
)
16+
return Binding.DoNothing;
17+
18+
return (!closeShellAfterPressOrLeaveShellOpen) && shell != Shell.RunCommand;
19+
}
20+
21+
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
22+
{
23+
throw new NotImplementedException();
24+
}
25+
}

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
using System.IO;
66
using System.Linq;
77
using Flow.Launcher.Plugin.SharedCommands;
8+
using Flow.Launcher.Plugin.Shell.Views;
89
using Control = System.Windows.Controls.Control;
910

1011
namespace Flow.Launcher.Plugin.Shell
@@ -376,6 +377,14 @@ public void Init(PluginInitContext context)
376377
{
377378
Context = context;
378379
_settings = context.API.LoadSettingJsonStorage<Settings>();
380+
381+
// Since the old Settings class set default value of ShowOnlyMostUsedCMDsNumber to 0 which is a wrong value,
382+
// we need to fix it here to make sure the default value is 5
383+
// todo: remove this code block after release v2.2.0
384+
if (_settings.ShowOnlyMostUsedCMDsNumber == 0)
385+
{
386+
_settings.ShowOnlyMostUsedCMDsNumber = 5;
387+
}
379388
}
380389

381390
public Control CreateSettingPanel()
Lines changed: 101 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,107 @@
11
using System.Collections.Generic;
2+
using Flow.Launcher.Localization.Attributes;
23

34
namespace Flow.Launcher.Plugin.Shell
45
{
5-
public class Settings
6+
public class Settings : BaseModel
67
{
7-
public Shell Shell { get; set; } = Shell.Cmd;
8+
private Shell _shell = Shell.Cmd;
9+
public Shell Shell
10+
{
11+
get => _shell;
12+
set
13+
{
14+
if (_shell != value)
15+
{
16+
_shell = value;
17+
OnPropertyChanged();
18+
}
19+
}
20+
}
821

9-
public bool CloseShellAfterPress { get; set; } = false;
22+
private bool _closeShellAfterPress = false;
23+
public bool CloseShellAfterPress
24+
{
25+
get => _closeShellAfterPress;
26+
set
27+
{
28+
if (_closeShellAfterPress != value)
29+
{
30+
_closeShellAfterPress = value;
31+
OnPropertyChanged();
32+
}
33+
}
34+
}
1035

11-
public bool LeaveShellOpen { get; set; }
36+
private bool _leaveShellOpen;
37+
public bool LeaveShellOpen
38+
{
39+
get => _leaveShellOpen;
40+
set
41+
{
42+
if (_leaveShellOpen != value)
43+
{
44+
_leaveShellOpen = value;
45+
OnPropertyChanged();
46+
}
47+
}
48+
}
1249

13-
public bool RunAsAdministrator { get; set; } = true;
50+
private bool _runAsAdministrator = true;
51+
public bool RunAsAdministrator
52+
{
53+
get => _runAsAdministrator;
54+
set
55+
{
56+
if (_runAsAdministrator != value)
57+
{
58+
_runAsAdministrator = value;
59+
OnPropertyChanged();
60+
}
61+
}
62+
}
1463

15-
public bool UseWindowsTerminal { get; set; } = false;
64+
private bool _useWindowsTerminal = false;
65+
public bool UseWindowsTerminal
66+
{
67+
get => _useWindowsTerminal;
68+
set
69+
{
70+
if (_useWindowsTerminal != value)
71+
{
72+
_useWindowsTerminal = value;
73+
OnPropertyChanged();
74+
}
75+
}
76+
}
1677

17-
public bool ShowOnlyMostUsedCMDs { get; set; }
78+
private bool _showOnlyMostUsedCMDs;
79+
public bool ShowOnlyMostUsedCMDs
80+
{
81+
get => _showOnlyMostUsedCMDs;
82+
set
83+
{
84+
if (_showOnlyMostUsedCMDs != value)
85+
{
86+
_showOnlyMostUsedCMDs = value;
87+
OnPropertyChanged();
88+
}
89+
}
90+
}
1891

19-
public int ShowOnlyMostUsedCMDsNumber { get; set; }
92+
private int _showOnlyMostUsedCMDsNumber = 5;
93+
public int ShowOnlyMostUsedCMDsNumber
94+
{
95+
get => _showOnlyMostUsedCMDsNumber;
96+
set
97+
{
98+
if (_showOnlyMostUsedCMDsNumber != value)
99+
{
100+
_showOnlyMostUsedCMDsNumber = value;
101+
OnPropertyChanged();
102+
}
103+
}
104+
}
20105

21106
public Dictionary<string, int> CommandHistory { get; set; } = [];
22107

@@ -29,11 +114,19 @@ public void AddCmdHistory(string cmdName)
29114
}
30115
}
31116

117+
[EnumLocalize]
32118
public enum Shell
33119
{
120+
[EnumLocalizeValue("CMD")]
34121
Cmd = 0,
122+
123+
[EnumLocalizeValue("PowerShell")]
35124
Powershell = 1,
125+
126+
[EnumLocalizeValue("RunCommand")]
36127
RunCommand = 2,
128+
129+
[EnumLocalizeValue("Pwsh")]
37130
Pwsh = 3,
38131
}
39132
}

Plugins/Flow.Launcher.Plugin.Shell/ShellSetting.xaml.cs

Lines changed: 0 additions & 130 deletions
This file was deleted.
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using System.Collections.Generic;
2+
3+
namespace Flow.Launcher.Plugin.Shell.ViewModels;
4+
5+
public class ShellSettingViewModel : BaseModel
6+
{
7+
public Settings Settings { get; }
8+
9+
public List<ShellLocalized> AllShells { get; } = ShellLocalized.GetValues();
10+
11+
public Shell SelectedShell
12+
{
13+
get => Settings.Shell;
14+
set
15+
{
16+
if (Settings.Shell != value)
17+
{
18+
Settings.Shell = value;
19+
OnPropertyChanged();
20+
}
21+
}
22+
}
23+
24+
public List<int> OnlyMostUsedCMDsNumbers { get; } = [5, 10, 20];
25+
public int SelectedOnlyMostUsedCMDsNumber
26+
{
27+
get => Settings.ShowOnlyMostUsedCMDsNumber;
28+
set
29+
{
30+
if (Settings.ShowOnlyMostUsedCMDsNumber != value)
31+
{
32+
Settings.ShowOnlyMostUsedCMDsNumber = value;
33+
OnPropertyChanged();
34+
}
35+
}
36+
}
37+
38+
public bool CloseShellAfterPress
39+
{
40+
get => Settings.CloseShellAfterPress;
41+
set
42+
{
43+
if (Settings.CloseShellAfterPress != value)
44+
{
45+
Settings.CloseShellAfterPress = value;
46+
OnPropertyChanged();
47+
// Only allow CloseShellAfterPress to be true when LeaveShellOpen is false
48+
if (value)
49+
{
50+
LeaveShellOpen = false;
51+
}
52+
}
53+
}
54+
}
55+
56+
public bool LeaveShellOpen
57+
{
58+
get => Settings.LeaveShellOpen;
59+
set
60+
{
61+
if (Settings.LeaveShellOpen != value)
62+
{
63+
Settings.LeaveShellOpen = value;
64+
OnPropertyChanged();
65+
// Only allow LeaveShellOpen to be true when CloseShellAfterPress is false
66+
if (value)
67+
{
68+
CloseShellAfterPress = false;
69+
}
70+
}
71+
}
72+
}
73+
74+
public ShellSettingViewModel(Settings settings)
75+
{
76+
Settings = settings;
77+
}
78+
}

0 commit comments

Comments
 (0)