Skip to content

Commit 175571a

Browse files
committed
Refactor ShellSettings with Binding logic
1 parent d363cf8 commit 175571a

File tree

7 files changed

+180
-157
lines changed

7 files changed

+180
-157
lines changed
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
using System;
2+
using System.Globalization;
3+
using System.Windows.Data;
4+
5+
namespace Flow.Launcher.Plugin.Shell.Converters;
6+
7+
public class CloseShellAfterPressEnabledConverter : IValueConverter
8+
{
9+
public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
10+
{
11+
if (value is not bool)
12+
return Binding.DoNothing;
13+
14+
var leaveShellOpen = (bool)value;
15+
return !leaveShellOpen;
16+
}
17+
18+
public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture)
19+
{
20+
throw new NotImplementedException();
21+
}
22+
}
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 LeaveShellOpenEnabledConverter : 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 closeShellAfterPress ||
14+
values[1] is not Shell shell
15+
)
16+
return Binding.DoNothing;
17+
18+
return (!closeShellAfterPress) && 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: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Linq;
77
using System.Threading.Tasks;
88
using Flow.Launcher.Plugin.SharedCommands;
9+
using Flow.Launcher.Plugin.Shell.Views;
910
using WindowsInput;
1011
using WindowsInput.Native;
1112
using Control = System.Windows.Controls.Control;

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

Lines changed: 0 additions & 142 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+
}
Lines changed: 38 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
11
<UserControl
2-
x:Class="Flow.Launcher.Plugin.Shell.CMDSetting"
2+
x:Class="Flow.Launcher.Plugin.Shell.Views.CMDSetting"
33
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
5+
xmlns:converters="clr-namespace:Flow.Launcher.Plugin.Shell.Converters"
56
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
67
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
8+
xmlns:vm="clr-namespace:Flow.Launcher.Plugin.Shell.ViewModels"
9+
d:DataContext="{d:DesignInstance vm:ShellSettingViewModel}"
710
d:DesignHeight="300"
811
d:DesignWidth="300"
9-
Loaded="CMDSetting_OnLoaded"
1012
mc:Ignorable="d">
13+
<UserControl.Resources>
14+
<converters:CloseShellAfterPressEnabledConverter x:Key="CloseShellAfterPressEnabledConverter" />
15+
<converters:LeaveShellOpenEnabledConverter x:Key="LeaveShellOpenEnabledConverter" />
16+
</UserControl.Resources>
17+
1118
<Grid Margin="{StaticResource SettingPanelMargin}" VerticalAlignment="Top">
1219
<Grid.RowDefinitions>
1320
<RowDefinition />
@@ -23,50 +30,66 @@
2330
Grid.Row="0"
2431
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
2532
HorizontalAlignment="Left"
26-
Content="{DynamicResource flowlauncher_plugin_cmd_relace_winr}" />
33+
Content="{DynamicResource flowlauncher_plugin_cmd_relace_winr}"
34+
IsChecked="{Binding Settings.ReplaceWinR, Mode=TwoWay}" />
2735
<CheckBox
2836
x:Name="CloseShellAfterPress"
2937
Grid.Row="1"
3038
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
3139
HorizontalAlignment="Left"
32-
Content="{DynamicResource flowlauncher_plugin_cmd_close_cmd_after_press}" />
40+
Content="{DynamicResource flowlauncher_plugin_cmd_close_cmd_after_press}"
41+
IsChecked="{Binding CloseShellAfterPress, Mode=TwoWay}"
42+
IsEnabled="{Binding LeaveShellOpen, Converter={StaticResource CloseShellAfterPressEnabledConverter}, Mode=OneWay}" />
3343
<CheckBox
3444
x:Name="LeaveShellOpen"
3545
Grid.Row="2"
3646
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
3747
HorizontalAlignment="Left"
38-
Content="{DynamicResource flowlauncher_plugin_cmd_leave_cmd_open}" />
48+
Content="{DynamicResource flowlauncher_plugin_cmd_leave_cmd_open}"
49+
IsChecked="{Binding LeaveShellOpen, Mode=TwoWay}">
50+
<CheckBox.IsEnabled>
51+
<MultiBinding Converter="{StaticResource LeaveShellOpenEnabledConverter}">
52+
<Binding Mode="OneWay" Path="CloseShellAfterPress" />
53+
<Binding Mode="OneWay" Path="SelectedShell" />
54+
</MultiBinding>
55+
</CheckBox.IsEnabled>
56+
</CheckBox>
3957
<CheckBox
4058
x:Name="AlwaysRunAsAdministrator"
4159
Grid.Row="3"
4260
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
4361
HorizontalAlignment="Left"
44-
Content="{DynamicResource flowlauncher_plugin_cmd_always_run_as_administrator}" />
62+
Content="{DynamicResource flowlauncher_plugin_cmd_always_run_as_administrator}"
63+
IsChecked="{Binding Settings.RunAsAdministrator, Mode=TwoWay}" />
4564
<CheckBox
4665
x:Name="UseWindowsTerminal"
4766
Grid.Row="4"
4867
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
4968
HorizontalAlignment="Left"
50-
Content="{DynamicResource flowlauncher_plugin_cmd_use_windows_terminal}" />
69+
Content="{DynamicResource flowlauncher_plugin_cmd_use_windows_terminal}"
70+
IsChecked="{Binding Settings.UseWindowsTerminal, Mode=TwoWay}" />
5171
<ComboBox
5272
x:Name="ShellComboBox"
5373
Grid.Row="5"
5474
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
55-
HorizontalAlignment="Left">
56-
<ComboBoxItem>CMD</ComboBoxItem>
57-
<ComboBoxItem>PowerShell</ComboBoxItem>
58-
<ComboBoxItem>Pwsh</ComboBoxItem>
59-
<ComboBoxItem>RunCommand</ComboBoxItem>
60-
</ComboBox>
75+
HorizontalAlignment="Left"
76+
DisplayMemberPath="Display"
77+
ItemsSource="{Binding AllShells, Mode=OneTime}"
78+
SelectedValue="{Binding SelectedShell, Mode=TwoWay}"
79+
SelectedValuePath="Value" />
6180
<StackPanel Grid.Row="6" Orientation="Horizontal">
6281
<CheckBox
6382
x:Name="ShowOnlyMostUsedCMDs"
6483
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
65-
Content="{DynamicResource flowlauncher_plugin_cmd_history}" />
84+
Content="{DynamicResource flowlauncher_plugin_cmd_history}"
85+
IsChecked="{Binding Settings.ShowOnlyMostUsedCMDs, Mode=TwoWay}" />
6686
<ComboBox
6787
x:Name="ShowOnlyMostUsedCMDsNumber"
6888
Margin="{StaticResource SettingPanelItemRightTopBottomMargin}"
69-
HorizontalAlignment="Left" />
89+
HorizontalAlignment="Left"
90+
IsEnabled="{Binding Settings.ShowOnlyMostUsedCMDs, Mode=OneWay}"
91+
ItemsSource="{Binding OnlyMostUsedCMDsNumbers, Mode=OneTime}"
92+
SelectedItem="{Binding SelectedOnlyMostUsedCMDsNumber, Mode=TwoWay}" />
7093
</StackPanel>
7194
</Grid>
7295
</UserControl>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
using System.Windows.Controls;
2+
using Flow.Launcher.Plugin.Shell.ViewModels;
3+
4+
namespace Flow.Launcher.Plugin.Shell.Views
5+
{
6+
public partial class CMDSetting : UserControl
7+
{
8+
public CMDSetting(Settings settings)
9+
{
10+
var viewModel = new ShellSettingViewModel(settings);
11+
DataContext = viewModel;
12+
InitializeComponent();
13+
DataContext = viewModel;
14+
}
15+
}
16+
}

0 commit comments

Comments
 (0)