-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathMainPage.EditDialogControls.cs
More file actions
299 lines (269 loc) · 16.2 KB
/
Copy pathMainPage.EditDialogControls.cs
File metadata and controls
299 lines (269 loc) · 16.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
using Microsoft.UI.Xaml;
using Microsoft.UI.Xaml.Input;
using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Controls.Primitives;
using Microsoft.UI.Xaml.Media;
using System.Collections.ObjectModel;
using System.Collections.Generic;
using FluentTaskScheduler.Models;
using FluentTaskScheduler.Models.Enums;
using FluentTaskScheduler.Services;
using Microsoft.Extensions.DependencyInjection;
using System.Threading.Tasks;
using System.Linq;
using Microsoft.UI.Dispatching;
using FluentTaskScheduler.ViewModels;
using System;
using Windows.ApplicationModel.DataTransfer;
namespace FluentTaskScheduler
{
/// <summary>Create/edit task dialog: trigger, action and option control handlers.</summary>
public sealed partial class MainPage
{
// ========================================================================================================
// UI Logic (Dialogs)
// ========================================================================================================
private static string FormatScheduleInfo(DateTime value) => DurationUtil.FormatScheduleInfo(value);
private static DateTime? TryParseScheduleInfo(string? value) => DurationUtil.TryParseScheduleInfo(value);
private void ActionList_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (ActionList.SelectedItem is TaskActionModel act)
{
ActionDetailsPanel.Visibility = Visibility.Visible;
EditTaskActionCommand.Text = act.Command ?? "";
EditTaskArguments.Text = act.Arguments ?? "";
EditTaskWorkingDirectory.Text = act.WorkingDirectory ?? "";
}
else
{
ActionDetailsPanel.Visibility = Visibility.Collapsed;
}
}
private void EditTaskActionCommand_TextChanged(object sender, TextChangedEventArgs e) { if (ActionList.SelectedItem is TaskActionModel m) m.Command = EditTaskActionCommand.Text; }
private void EditTaskArguments_TextChanged(object sender, TextChangedEventArgs e) { if (ActionList.SelectedItem is TaskActionModel m) m.Arguments = EditTaskArguments.Text; }
private void EditTaskWorkingDirectory_TextChanged(object sender, TextChangedEventArgs e) { if (ActionList.SelectedItem is TaskActionModel m) m.WorkingDirectory = EditTaskWorkingDirectory.Text; }
private async void BrowseAction_Click(object sender, RoutedEventArgs e)
{
string? filePath = await Helpers.FilePickerHelper.PickOpenFileAsync(
App.m_window!, "Select File", "All Files", "*");
if (!string.IsNullOrEmpty(filePath)) EditTaskActionCommand.Text = filePath;
}
private void PopulateNetworkList()
{
bool isAdmin = Helpers.ElevationHelper.IsElevated();
if (!isAdmin)
{
// Non-admin: disable dropdown and show explanation notice
EditTaskNetworkSelection.IsEnabled = false;
EditTaskNetworkSelection.Items.Clear();
EditTaskNetworkSelection.Items.Add(new ComboBoxItem { Content = L("Main.Network.Any", "Any network"), Tag = "" });
EditTaskNetworkSelection.SelectedIndex = 0;
NetworkAdminNotice.IsOpen = true;
return;
}
// Admin: populate from registry (exact NLM profile GUIDs)
NetworkAdminNotice.IsOpen = false;
EditTaskNetworkSelection.IsEnabled = true;
EditTaskNetworkSelection.Items.Clear();
EditTaskNetworkSelection.Items.Add(new ComboBoxItem { Content = L("Main.Network.Any", "Any network"), Tag = "" });
try
{
using var profilesKey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(
@"SOFTWARE\Microsoft\Windows NT\CurrentVersion\NetworkList\Profiles");
if (profilesKey != null)
{
foreach (var subKeyName in profilesKey.GetSubKeyNames())
{
using var profileKey = profilesKey.OpenSubKey(subKeyName);
var name = profileKey?.GetValue("ProfileName") as string;
if (!string.IsNullOrWhiteSpace(name))
EditTaskNetworkSelection.Items.Add(new ComboBoxItem { Content = name, Tag = subKeyName });
}
}
}
catch (Exception ex) { Serilog.Log.Warning("{Message}", $"Could not populate network list: {ex.Message}"); }
}
private void BtnAddAction_Click(object sender, RoutedEventArgs e) { _tempActions.Add(new TaskActionModel { Command="notepad.exe" }); ActionList.SelectedIndex = _tempActions.Count - 1; }
private void AddAction_SendEmail_Click(object sender, RoutedEventArgs e)
{
string ps = "powershell.exe";
string args = "-ExecutionPolicy Bypass -Command \"Send-MailMessage -To 'recipient@example.com' -From 'scheduler@example.com' -Subject 'Task Started' -Body 'The task has started.' -SmtpServer 'smtp.example.com'\"";
_tempActions.Add(new TaskActionModel { Command = ps, Arguments = args });
ActionList.SelectedIndex = _tempActions.Count - 1;
}
private void AddAction_ShowNotification_Click(object sender, RoutedEventArgs e)
{
string ps = "powershell.exe";
string args = "-WindowStyle Hidden -Command \"& {Add-Type -AssemblyName System.Windows.Forms; [System.Windows.Forms.MessageBox]::Show('Task Notification', 'FluentTaskScheduler')}\"";
_tempActions.Add(new TaskActionModel { Command = ps, Arguments = args });
ActionList.SelectedIndex = _tempActions.Count - 1;
}
private void BtnRemoveAction_Click(object sender, RoutedEventArgs e) { if (ActionList.SelectedItem is TaskActionModel t) _tempActions.Remove(t); }
private void BtnMoveActionUp_Click(object sender, RoutedEventArgs e)
{
int idx = ActionList.SelectedIndex;
if (idx > 0) {
var item = _tempActions[idx];
_tempActions.RemoveAt(idx);
_tempActions.Insert(idx - 1, item);
ActionList.SelectedIndex = idx - 1;
}
}
private void BtnMoveActionDown_Click(object sender, RoutedEventArgs e)
{
int idx = ActionList.SelectedIndex;
if (idx >= 0 && idx < _tempActions.Count - 1) {
var item = _tempActions[idx];
_tempActions.RemoveAt(idx);
_tempActions.Insert(idx + 1, item);
ActionList.SelectedIndex = idx + 1;
}
}
private void EditTaskRepetitionInterval_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_isPopulatingDetails) return;
if (TriggerEditor.SelectedTrigger is TaskTriggerModel tr && EditTaskRepetitionInterval.SelectedItem is ComboBoxItem item)
tr.RepetitionInterval = item.Tag?.ToString() ?? "";
}
private void EditTaskRepetitionDuration_SelectionChanged(object sender, SelectionChangedEventArgs e)
{
if (_isPopulatingDetails) return;
if (TriggerEditor.SelectedTrigger is TaskTriggerModel tr && EditTaskRepetitionDuration.SelectedItem is ComboBoxItem item)
tr.RepetitionDuration = item.Tag?.ToString() ?? "";
}
// Repetition lives outside the trigger editor, so the page loads it when a trigger is selected there.
private void TriggerEditor_SelectedTriggerChanged(object? sender, TaskTriggerModel tr)
{
_isPopulatingDetails = true;
foreach (var item in EditTaskRepetitionInterval.Items.Cast<ComboBoxItem>())
if (item.Tag?.ToString() == (tr.RepetitionInterval ?? "")) { EditTaskRepetitionInterval.SelectedItem = item; break; }
foreach (var item in EditTaskRepetitionDuration.Items.Cast<ComboBoxItem>())
if (item.Tag?.ToString() == (tr.RepetitionDuration ?? "")) { EditTaskRepetitionDuration.SelectedItem = item; break; }
_isPopulatingDetails = false;
}
private void UserContextRadio_Checked(object sender, RoutedEventArgs e)
{
if (EditTaskRunAsUser != null) EditTaskRunAsUser.IsEnabled = RunAsSpecificUser.IsChecked == true;
if (SystemUserWarning != null)
{
bool isElevated = Helpers.ElevationHelper.IsElevated();
SystemUserWarning.IsOpen = (!isElevated) && (RunAsSystem.IsChecked == true);
}
}
private void RunAsSystem_Click(object sender, RoutedEventArgs e) => RunAsSystem.IsChecked = true;
private void DialogScrollViewer_PointerPressed(object sender, PointerRoutedEventArgs e)
{
// When the user clicks on empty space (no interactive element), WinUI shifts focus to
// the ScrollViewer and calls BringIntoView on it, which resets the scroll position to
// the top. We prevent this by capturing the current vertical offset and restoring it
// on the next dispatcher frame (after BringIntoView has already fired).
if (sender is not ScrollViewer sv) return;
double savedOffset = sv.VerticalOffset;
DispatcherQueue.TryEnqueue(() => sv.ChangeView(null, savedOffset, null, true));
}
private void InfoIcon_Tapped(object sender, TappedRoutedEventArgs e)
{
e.Handled = true; // Don't bubble to ScrollViewer
if (sender is FrameworkElement icon) Helpers.InfoFlyoutHelper.Show(icon, EditScrollViewer);
}
// Batch
private void UpdateBatchActionsState()
{
ViewModel.UpdateBatchSelection(SelectedTasks());
}
private void BatchCancel_Click(object sender, RoutedEventArgs e) => TaskListView.SelectedItems.Clear();
private async void BatchRun_Click(object sender, RoutedEventArgs e)
{
// Snapshot selection before anything changes.
// Set IsRunning=true BEFORE calling RunTask so the ring appears immediately,
// independently of the volatile State string.
var tasks = TaskListView.SelectedItems.Cast<ScheduledTaskModel>().ToList();
if (SnoozeService.IsActive)
{
foreach (var t in tasks) SnoozeService.RecordSuppressedRun(t.Path, "Manual");
await ShowErrorDialog(string.Format(
L("Snooze.Error.BatchBlocked", "{0} task(s) were not started because Global Snooze is active."),
tasks.Count));
return;
}
foreach (var t in tasks)
{
t.State = TaskState.Running;
t.IsRunning = true; // show the ring immediately
try
{
ViewModel.TaskService.RunTask(t.Path);
}
catch (Exception ex)
{
// The watcher below corrects IsRunning; log so a silent failure is traceable.
Serilog.Log.Error(ex, "{Message}", $"Batch run could not start task '{t.Path}'.");
}
WatchTaskUntilFinished(t);
}
}
private void BatchStop_Click(object sender, RoutedEventArgs e) => ViewModel.BatchStop(SelectedTasks());
private async void BatchEnable_Click(object sender, RoutedEventArgs e) { var denied = ViewModel.BatchSetEnabled(SelectedTasks(), true); UpdateBatchActionsState(); if (denied.Count > 0) await ShowErrorDialog($"The user account under which you are performing this action does not have permission to enable the following task(s):\n\n{string.Join("\n", denied)}\n\nThese tasks are protected and cannot be modified, even with administrator privileges."); }
private async void BatchDisable_Click(object sender, RoutedEventArgs e) { var denied = ViewModel.BatchSetEnabled(SelectedTasks(), false); UpdateBatchActionsState(); if (denied.Count > 0) await ShowErrorDialog($"The user account under which you are performing this action does not have permission to disable the following task(s):\n\n{string.Join("\n", denied)}\n\nThese tasks are protected and cannot be modified, even with administrator privileges."); }
private async void BatchDelete_Click(object sender, RoutedEventArgs e)
{
var tasks = TaskListView.SelectedItems.Cast<ScheduledTaskModel>().ToList();
bool confirmed = !Settings.ConfirmDelete;
if (!confirmed)
{
var dialog = new ContentDialog
{
Title = L("Dialog.ConfirmDelete.Title", "Confirm Delete"),
Content = string.Format(L("Dialog.BatchDelete.ContentFormat", "Delete {0} tasks?"), tasks.Count),
PrimaryButtonText = L("Dialog.Common.Delete", "Delete"),
CloseButtonText = L("Dialog.Common.Cancel", "Cancel"),
DefaultButton = ContentDialogButton.Close,
XamlRoot = this.XamlRoot
};
confirmed = await dialog.ShowAsync() == ContentDialogResult.Primary;
}
if (confirmed)
{
ViewModel.BatchDelete(tasks);
}
}
private List<ScheduledTaskModel> SelectedTasks() => TaskListView.SelectedItems.Cast<ScheduledTaskModel>().ToList();
// Keyboard Accelerators
protected override void OnKeyDown(Microsoft.UI.Xaml.Input.KeyRoutedEventArgs e)
{
if (e.Key == Windows.System.VirtualKey.F5) { e.Handled = true; _ = ViewModel.LoadTasksAsync(); return; }
base.OnKeyDown(e);
}
private void NewTaskAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { args.Handled = true; NewTaskButton_Click(sender, new RoutedEventArgs()); }
private void EditTaskAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { args.Handled = true; if (ViewModel.SelectedTask != null) EditTask_Click(sender, new RoutedEventArgs()); }
private void RunTaskAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { args.Handled = true; if (ViewModel.SelectedTask != null) RunTask_Click(sender, new RoutedEventArgs()); }
private void DeleteTaskAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { args.Handled = true; if (FocusManager.GetFocusedElement() is not TextBox) DeleteTask_Click(sender, new RoutedEventArgs()); }
private void EscapeAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { args.Handled = true; try { TaskDetailsDialog.Hide(); } catch { } try { TaskEditDialog.Hide(); } catch { } }
private void ShortcutsAccelerator_Invoked(KeyboardAccelerator sender, KeyboardAcceleratorInvokedEventArgs args) { args.Handled = true; ShowShortcutsDialog(); }
// Feature 1: Keyboard shortcuts dialog
private void ShortcutsButton_Click(object sender, RoutedEventArgs e) => ShowShortcutsDialog();
private async void ShowShortcutsDialog()
{
ShortcutsDialog.XamlRoot = this.XamlRoot;
try { await ShortcutsDialog.ShowAsync(); } catch { }
}
// Feature 2: Sort button with flyout
private void SortButton_Click(object sender, RoutedEventArgs e)
{
var flyout = new MenuFlyout();
void AddItem(string label, SortColumn col)
{
var item = new MenuFlyoutItem { Text = label + ViewModel.GetSortIndicator(col), Command = ViewModel.SortByCommand, CommandParameter = col };
flyout.Items.Add(item);
}
AddItem(L("Main.Sort.Name", "Name"), SortColumn.Name);
AddItem(L("Main.Sort.Status", "Status"), SortColumn.Status);
AddItem(L("Main.Sort.NextRun", "Next Run"), SortColumn.NextRun);
AddItem(L("Main.Sort.LastRun", "Last Run"), SortColumn.LastRun);
flyout.Items.Add(new MenuFlyoutSeparator());
var clear = new MenuFlyoutItem { Text = L("Main.Sort.Clear", "Clear Sort"), Command = ViewModel.ClearSortCommand };
flyout.Items.Add(clear);
flyout.ShowAt(SortButton);
}
}
}