|
1 | | -using System; |
2 | | -using System.Collections.ObjectModel; |
3 | | -using System.ComponentModel; |
4 | | -using System.Diagnostics; |
5 | | -using System.IO; |
6 | | -using System.Linq; |
7 | | -using System.Windows; |
| 1 | +using System.Windows; |
8 | 2 | using System.Windows.Controls; |
9 | | -using System.Windows.Documents; |
10 | 3 | using System.Windows.Navigation; |
11 | | -using CommunityToolkit.Mvvm.ComponentModel; |
12 | | -using Flow.Launcher.Infrastructure.UserSettings; |
| 4 | +using CommunityToolkit.Mvvm.DependencyInjection; |
13 | 5 | using Flow.Launcher.ViewModel; |
14 | | -using ModernWpf.Controls; |
15 | 6 |
|
16 | 7 | namespace Flow.Launcher |
17 | 8 | { |
18 | | - [INotifyPropertyChanged] |
19 | 9 | public partial class SelectFileManagerWindow : Window |
20 | 10 | { |
21 | | - private readonly Settings _settings; |
| 11 | + private readonly SelectFileManagerViewModel _viewModel; |
22 | 12 |
|
23 | | - private int selectedCustomExplorerIndex; |
24 | | - |
25 | | - public int SelectedCustomExplorerIndex |
| 13 | + public SelectFileManagerWindow() |
26 | 14 | { |
27 | | - get => selectedCustomExplorerIndex; |
28 | | - set |
29 | | - { |
30 | | - selectedCustomExplorerIndex = value; |
31 | | - OnPropertyChanged(nameof(CustomExplorer)); |
32 | | - } |
33 | | - } |
34 | | - |
35 | | - public ObservableCollection<CustomExplorerViewModel> CustomExplorers { get; } |
36 | | - |
37 | | - public CustomExplorerViewModel CustomExplorer => CustomExplorers[SelectedCustomExplorerIndex]; |
38 | | - |
39 | | - public SelectFileManagerWindow(Settings settings) |
40 | | - { |
41 | | - _settings = settings; |
42 | | - CustomExplorers = new ObservableCollection<CustomExplorerViewModel>(_settings.CustomExplorerList.Select(x => x.Copy())); |
43 | | - SelectedCustomExplorerIndex = _settings.CustomExplorerIndex; |
| 15 | + _viewModel = Ioc.Default.GetRequiredService<SelectFileManagerViewModel>(); |
| 16 | + DataContext = _viewModel; |
44 | 17 | InitializeComponent(); |
45 | 18 | } |
46 | 19 |
|
47 | 20 | private void btnCancel_Click(object sender, RoutedEventArgs e) |
48 | 21 | { |
49 | 22 | Close(); |
50 | 23 | } |
51 | | - |
52 | | - private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) |
53 | | - { |
54 | | - App.API.OpenUrl(e.Uri.AbsoluteUri); |
55 | | - e.Handled = true; |
56 | | - } |
57 | 24 |
|
58 | 25 | private void btnDone_Click(object sender, RoutedEventArgs e) |
59 | 26 | { |
60 | | - // Check if the selected file manager path is valid |
61 | | - if (!IsFileManagerValid(CustomExplorer.Path)) |
| 27 | + if (_viewModel.SaveSettings()) |
62 | 28 | { |
63 | | - var result = App.API.ShowMsgBox( |
64 | | - string.Format(App.API.GetTranslation("fileManagerPathNotFound"), |
65 | | - CustomExplorer.Name, CustomExplorer.Path), |
66 | | - App.API.GetTranslation("fileManagerPathError"), |
67 | | - MessageBoxButton.YesNo, |
68 | | - MessageBoxImage.Warning); |
69 | | - |
70 | | - if (result == MessageBoxResult.No) |
71 | | - { |
72 | | - return; |
73 | | - } |
| 29 | + Close(); |
74 | 30 | } |
75 | | - |
76 | | - _settings.CustomExplorerList = CustomExplorers.ToList(); |
77 | | - _settings.CustomExplorerIndex = SelectedCustomExplorerIndex; |
78 | | - Close(); |
79 | 31 | } |
80 | 32 |
|
81 | | - private bool IsFileManagerValid(string path) |
82 | | - { |
83 | | - if (string.Equals(path, "explorer", StringComparison.OrdinalIgnoreCase)) |
84 | | - return true; |
85 | | - |
86 | | - if (Path.IsPathRooted(path)) |
87 | | - { |
88 | | - return File.Exists(path); |
89 | | - } |
90 | | - |
91 | | - try |
92 | | - { |
93 | | - var process = new Process |
94 | | - { |
95 | | - StartInfo = new ProcessStartInfo |
96 | | - { |
97 | | - FileName = "where", |
98 | | - Arguments = path, |
99 | | - RedirectStandardOutput = true, |
100 | | - UseShellExecute = false, |
101 | | - CreateNoWindow = true |
102 | | - } |
103 | | - }; |
104 | | - process.Start(); |
105 | | - string output = process.StandardOutput.ReadToEnd(); |
106 | | - process.WaitForExit(); |
107 | | - |
108 | | - return !string.IsNullOrEmpty(output); |
109 | | - } |
110 | | - catch |
111 | | - { |
112 | | - return false; |
113 | | - } |
114 | | - } |
115 | | - |
116 | | - [System.Diagnostics.CodeAnalysis.SuppressMessage("Usage", "VSTHRD100:Avoid async void methods", Justification = "<Pending>")] |
117 | | - private async void btnTips_Click(object sender, RoutedEventArgs e) |
118 | | - { |
119 | | - var tipText = (string)Application.Current.Resources["fileManager_files_tips"]; |
120 | | - var url = "https://files.community/docs/contributing/updates"; |
121 | | - |
122 | | - var textBlock = new TextBlock |
123 | | - { |
124 | | - FontSize = 14, |
125 | | - TextWrapping = TextWrapping.Wrap, |
126 | | - Margin = new Thickness(0, 0, 0, 0) |
127 | | - }; |
128 | | - |
129 | | - textBlock.Inlines.Add(tipText); |
130 | | - |
131 | | - var hyperlink = new Hyperlink |
132 | | - { |
133 | | - NavigateUri = new Uri(url) |
134 | | - }; |
135 | | - hyperlink.Inlines.Add(url); |
136 | | - hyperlink.RequestNavigate += (s, args) => |
137 | | - { |
138 | | - App.API.OpenUrl(args.Uri.AbsoluteUri); |
139 | | - args.Handled = true; |
140 | | - }; |
141 | | - |
142 | | - textBlock.Inlines.Add(hyperlink); |
143 | | - |
144 | | - var tipsDialog = new ContentDialog() |
145 | | - { |
146 | | - Owner = Window.GetWindow((DependencyObject)sender), |
147 | | - Title = (string)Application.Current.Resources["fileManager_files_btn"], |
148 | | - Content = textBlock, |
149 | | - PrimaryButtonText = (string)Application.Current.Resources["commonOK"], |
150 | | - CornerRadius = new CornerRadius(8), |
151 | | - Style = (Style)Application.Current.Resources["ContentDialog"] |
152 | | - }; |
153 | | - |
154 | | - await tipsDialog.ShowAsync(); |
155 | | - } |
156 | | - |
157 | | - private void btnAdd_Click(object sender, RoutedEventArgs e) |
158 | | - { |
159 | | - CustomExplorers.Add(new() |
160 | | - { |
161 | | - Name = "New Profile" |
162 | | - }); |
163 | | - SelectedCustomExplorerIndex = CustomExplorers.Count - 1; |
164 | | - } |
165 | | - |
166 | | - private void btnDelete_Click(object sender, RoutedEventArgs e) |
| 33 | + private void Hyperlink_RequestNavigate(object sender, RequestNavigateEventArgs e) |
167 | 34 | { |
168 | | - CustomExplorers.RemoveAt(SelectedCustomExplorerIndex--); |
| 35 | + App.API.OpenUrl(e.Uri.AbsoluteUri); |
| 36 | + e.Handled = true; |
169 | 37 | } |
170 | 38 |
|
171 | 39 | private void btnBrowseFile_Click(object sender, RoutedEventArgs e) |
172 | 40 | { |
173 | | - Microsoft.Win32.OpenFileDialog dlg = new Microsoft.Win32.OpenFileDialog(); |
| 41 | + var dlg = new Microsoft.Win32.OpenFileDialog(); |
174 | 42 | var result = dlg.ShowDialog(); |
175 | 43 | if (result == true) |
176 | 44 | { |
177 | | - TextBox path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("PathTextBox"); |
| 45 | + var path = (TextBox)(((FrameworkElement)sender).Parent as FrameworkElement).FindName("PathTextBox"); |
178 | 46 | path.Text = dlg.FileName; |
179 | 47 | path.Focus(); |
180 | 48 | ((Button)sender).Focus(); |
|
0 commit comments