feat: 优化触发器配置体验
- 根据触发器类型动态展示对应配置区域,减少无关字段干扰。 - 将单次、每日、每周、每月和生效时间范围改为日期选择器与时分秒下拉选择,避免手动输入时间格式。 - 为单次执行增加延后执行快捷设置,支持常用快捷按钮和自定义分钟、小时、天后执行。 - 移除开机自启设置、注册表写入逻辑和相关配置字段,降低对用户系统的影响。 - 同步优化部分任务状态、触发摘要和设置界面文案。
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
using System.Collections.ObjectModel;
|
||||
using System.Windows;
|
||||
using System.Windows.Controls;
|
||||
using Button = System.Windows.Controls.Button;
|
||||
using ComboBox = System.Windows.Controls.ComboBox;
|
||||
|
||||
namespace OmniScheduler;
|
||||
|
||||
@@ -9,8 +10,40 @@ public partial class TaskEditorWindow : Window
|
||||
private readonly ScheduledTask _task;
|
||||
private readonly AppSettings _settings;
|
||||
private readonly NotifyClient _client;
|
||||
private readonly List<Option<int>> _hours = Enumerable.Range(0, 24).Select(v => new Option<int>(v, v.ToString("00"))).ToList();
|
||||
private readonly List<Option<int>> _minutesAndSeconds = Enumerable.Range(0, 60).Select(v => new Option<int>(v, v.ToString("00"))).ToList();
|
||||
private readonly List<Option<DelayUnit>> _delayUnits =
|
||||
[
|
||||
new(DelayUnit.Minutes, "分钟"),
|
||||
new(DelayUnit.Hours, "小时"),
|
||||
new(DelayUnit.Days, "天")
|
||||
];
|
||||
private bool _loading;
|
||||
|
||||
private readonly List<Option<TriggerKind>> _triggerKinds =
|
||||
[
|
||||
new(TriggerKind.OneTime, "单次执行"),
|
||||
new(TriggerKind.Interval, "固定间隔"),
|
||||
new(TriggerKind.Daily, "每日定时"),
|
||||
new(TriggerKind.Weekly, "每周定时"),
|
||||
new(TriggerKind.Monthly, "每月定时"),
|
||||
new(TriggerKind.Cron, "Cron 表达式")
|
||||
];
|
||||
|
||||
private readonly List<Option<IntervalUnit>> _intervalUnits =
|
||||
[
|
||||
new(IntervalUnit.Seconds, "秒"),
|
||||
new(IntervalUnit.Minutes, "分钟"),
|
||||
new(IntervalUnit.Hours, "小时"),
|
||||
new(IntervalUnit.Days, "天")
|
||||
];
|
||||
|
||||
private readonly List<Option<MisfireStrategy>> _misfireStrategies =
|
||||
[
|
||||
new(MisfireStrategy.DoNothing, "忽略,等待下一次"),
|
||||
new(MisfireStrategy.FireOnceNow, "立即补偿一次")
|
||||
];
|
||||
|
||||
private TaskEditorWindow(Window owner, ScheduledTask task, AppSettings settings, NotifyClient client)
|
||||
{
|
||||
InitializeComponent();
|
||||
@@ -19,13 +52,16 @@ public partial class TaskEditorWindow : Window
|
||||
_settings = settings;
|
||||
_client = client;
|
||||
|
||||
KindBox.ItemsSource = Enum.GetValues<TriggerKind>();
|
||||
IntervalUnitBox.ItemsSource = Enum.GetValues<IntervalUnit>();
|
||||
MisfireBox.ItemsSource = Enum.GetValues<MisfireStrategy>();
|
||||
KindBox.ItemsSource = _triggerKinds;
|
||||
IntervalUnitBox.ItemsSource = _intervalUnits;
|
||||
OneTimeDelayUnitBox.ItemsSource = _delayUnits;
|
||||
OneTimeDelayUnitBox.SelectedItem = _delayUnits.First(o => o.Value == DelayUnit.Minutes);
|
||||
MisfireBox.ItemsSource = _misfireStrategies;
|
||||
ChannelBox.ItemsSource = owner is MainWindow main
|
||||
? main.State.Tasks.Select(t => t.Channel).Where(c => !string.IsNullOrWhiteSpace(c)).Distinct().ToList()
|
||||
: null;
|
||||
|
||||
InitializeTimeSelectors();
|
||||
LoadTask();
|
||||
}
|
||||
|
||||
@@ -44,6 +80,16 @@ public partial class TaskEditorWindow : Window
|
||||
return false;
|
||||
}
|
||||
|
||||
private void InitializeTimeSelectors()
|
||||
{
|
||||
SetTimeItems(OneTimeHourBox, OneTimeMinuteBox, OneTimeSecondBox);
|
||||
SetTimeItems(DailyHourBox, DailyMinuteBox, DailySecondBox);
|
||||
SetTimeItems(WeeklyHourBox, WeeklyMinuteBox, WeeklySecondBox);
|
||||
SetTimeItems(MonthlyHourBox, MonthlyMinuteBox, MonthlySecondBox);
|
||||
SetTimeItems(StartsAtHourBox, StartsAtMinuteBox, StartsAtSecondBox);
|
||||
SetTimeItems(EndsAtHourBox, EndsAtMinuteBox, EndsAtSecondBox);
|
||||
}
|
||||
|
||||
private void LoadTask()
|
||||
{
|
||||
NameBox.Text = _task.Name;
|
||||
@@ -53,9 +99,10 @@ public partial class TaskEditorWindow : Window
|
||||
ChannelBox.Text = _task.Channel;
|
||||
TitleBox.Text = _task.Title;
|
||||
BodyBox.Text = _task.Body;
|
||||
MisfireBox.SelectedItem = _task.MisfireStrategy;
|
||||
MisfireBox.SelectedItem = _misfireStrategies.First(o => o.Value == _task.MisfireStrategy);
|
||||
TriggersList.ItemsSource = _task.Triggers;
|
||||
TriggersList.SelectedIndex = _task.Triggers.Count > 0 ? 0 : -1;
|
||||
UpdateEditorAvailability();
|
||||
}
|
||||
|
||||
private void LoadTrigger(TaskTrigger? trigger)
|
||||
@@ -65,16 +112,23 @@ public partial class TaskEditorWindow : Window
|
||||
{
|
||||
if (trigger is null)
|
||||
{
|
||||
UpdateEditorAvailability();
|
||||
return;
|
||||
}
|
||||
|
||||
KindBox.SelectedItem = trigger.Kind;
|
||||
OneTimeBox.Text = trigger.OneTimeAt.ToString("yyyy-MM-dd HH:mm:ss");
|
||||
KindBox.SelectedItem = _triggerKinds.First(o => o.Value == trigger.Kind);
|
||||
SetDateTime(OneTimeDatePicker, OneTimeHourBox, OneTimeMinuteBox, OneTimeSecondBox, trigger.OneTimeAt);
|
||||
IntervalValueBox.Text = trigger.IntervalValue.ToString();
|
||||
IntervalUnitBox.SelectedItem = trigger.IntervalUnit;
|
||||
StartsAtBox.Text = trigger.StartsAt?.ToString("yyyy-MM-dd HH:mm:ss") ?? "";
|
||||
EndsAtBox.Text = trigger.EndsAt?.ToString("yyyy-MM-dd HH:mm:ss") ?? "";
|
||||
TimeOfDayBox.Text = trigger.TimeOfDay.ToString(@"hh\:mm\:ss");
|
||||
IntervalUnitBox.SelectedItem = _intervalUnits.First(o => o.Value == trigger.IntervalUnit);
|
||||
|
||||
UseStartsAtBox.IsChecked = trigger.StartsAt.HasValue;
|
||||
SetOptionalDateTime(StartsAtDatePicker, StartsAtHourBox, StartsAtMinuteBox, StartsAtSecondBox, trigger.StartsAt);
|
||||
UseEndsAtBox.IsChecked = trigger.EndsAt.HasValue;
|
||||
SetOptionalDateTime(EndsAtDatePicker, EndsAtHourBox, EndsAtMinuteBox, EndsAtSecondBox, trigger.EndsAt);
|
||||
|
||||
SetTimeOnly(DailyHourBox, DailyMinuteBox, DailySecondBox, trigger.TimeOfDay);
|
||||
SetTimeOnly(WeeklyHourBox, WeeklyMinuteBox, WeeklySecondBox, trigger.TimeOfDay);
|
||||
SetTimeOnly(MonthlyHourBox, MonthlyMinuteBox, MonthlySecondBox, trigger.TimeOfDay);
|
||||
MonthDayBox.Text = trigger.MonthDay.ToString();
|
||||
LastBusinessDayBox.IsChecked = trigger.LastBusinessDay;
|
||||
CronBox.Text = trigger.CronExpression;
|
||||
@@ -86,6 +140,10 @@ public partial class TaskEditorWindow : Window
|
||||
ThuBox.IsChecked = trigger.WeekDays.HasFlag(DayOfWeekFlags.Thursday);
|
||||
FriBox.IsChecked = trigger.WeekDays.HasFlag(DayOfWeekFlags.Friday);
|
||||
SatBox.IsChecked = trigger.WeekDays.HasFlag(DayOfWeekFlags.Saturday);
|
||||
|
||||
UpdateVisibleSections(trigger.Kind);
|
||||
UpdateWindowControlState();
|
||||
UpdateEditorAvailability();
|
||||
}
|
||||
finally
|
||||
{
|
||||
@@ -101,8 +159,8 @@ public partial class TaskEditorWindow : Window
|
||||
return true;
|
||||
}
|
||||
|
||||
trigger.Kind = KindBox.SelectedItem is TriggerKind kind ? kind : TriggerKind.Interval;
|
||||
trigger.IntervalUnit = IntervalUnitBox.SelectedItem is IntervalUnit unit ? unit : IntervalUnit.Minutes;
|
||||
trigger.Kind = KindBox.SelectedItem is Option<TriggerKind> kind ? kind.Value : TriggerKind.Interval;
|
||||
trigger.IntervalUnit = IntervalUnitBox.SelectedItem is Option<IntervalUnit> unit ? unit.Value : IntervalUnit.Minutes;
|
||||
trigger.LastBusinessDay = LastBusinessDayBox.IsChecked == true;
|
||||
trigger.CronExpression = CronBox.Text.Trim();
|
||||
|
||||
@@ -116,18 +174,24 @@ public partial class TaskEditorWindow : Window
|
||||
trigger.MonthDay = Math.Clamp(monthDay, 1, 31);
|
||||
}
|
||||
|
||||
if (DateTime.TryParse(OneTimeBox.Text, out var oneTime))
|
||||
if (GetDateTime(OneTimeDatePicker, OneTimeHourBox, OneTimeMinuteBox, OneTimeSecondBox) is { } oneTime)
|
||||
{
|
||||
trigger.OneTimeAt = oneTime;
|
||||
}
|
||||
|
||||
trigger.StartsAt = DateTime.TryParse(StartsAtBox.Text, out var startsAt) ? startsAt : null;
|
||||
trigger.EndsAt = DateTime.TryParse(EndsAtBox.Text, out var endsAt) ? endsAt : null;
|
||||
trigger.StartsAt = UseStartsAtBox.IsChecked == true
|
||||
? GetDateTime(StartsAtDatePicker, StartsAtHourBox, StartsAtMinuteBox, StartsAtSecondBox)
|
||||
: null;
|
||||
trigger.EndsAt = UseEndsAtBox.IsChecked == true
|
||||
? GetDateTime(EndsAtDatePicker, EndsAtHourBox, EndsAtMinuteBox, EndsAtSecondBox)
|
||||
: null;
|
||||
|
||||
if (TimeSpan.TryParse(TimeOfDayBox.Text, out var time))
|
||||
trigger.TimeOfDay = trigger.Kind switch
|
||||
{
|
||||
trigger.TimeOfDay = time;
|
||||
}
|
||||
TriggerKind.Weekly => GetTimeOnly(WeeklyHourBox, WeeklyMinuteBox, WeeklySecondBox),
|
||||
TriggerKind.Monthly => GetTimeOnly(MonthlyHourBox, MonthlyMinuteBox, MonthlySecondBox),
|
||||
_ => GetTimeOnly(DailyHourBox, DailyMinuteBox, DailySecondBox)
|
||||
};
|
||||
|
||||
var days = DayOfWeekFlags.None;
|
||||
if (SunBox.IsChecked == true) days |= DayOfWeekFlags.Sunday;
|
||||
@@ -138,6 +202,9 @@ public partial class TaskEditorWindow : Window
|
||||
if (FriBox.IsChecked == true) days |= DayOfWeekFlags.Friday;
|
||||
if (SatBox.IsChecked == true) days |= DayOfWeekFlags.Saturday;
|
||||
trigger.WeekDays = days;
|
||||
|
||||
UpdateVisibleSections(trigger.Kind);
|
||||
UpdateWindowControlState();
|
||||
trigger.Notify(nameof(TaskTrigger.Summary));
|
||||
TriggersList.Items.Refresh();
|
||||
RefreshPreview();
|
||||
@@ -162,6 +229,18 @@ public partial class TaskEditorWindow : Window
|
||||
ValidationText.Text = "固定间隔不能小于 1";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (trigger.Kind == TriggerKind.Weekly && trigger.WeekDays == DayOfWeekFlags.None)
|
||||
{
|
||||
ValidationText.Text = "每周定时至少选择一个星期";
|
||||
return false;
|
||||
}
|
||||
|
||||
if (trigger.StartsAt.HasValue && trigger.EndsAt.HasValue && trigger.StartsAt >= trigger.EndsAt)
|
||||
{
|
||||
ValidationText.Text = "结束失效时间必须晚于起始生效时间";
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
_task.Name = NameBox.Text.Trim();
|
||||
@@ -170,7 +249,7 @@ public partial class TaskEditorWindow : Window
|
||||
_task.Channel = ChannelBox.Text.Trim();
|
||||
_task.Title = TitleBox.Text.Trim();
|
||||
_task.Body = BodyBox.Text;
|
||||
_task.MisfireStrategy = MisfireBox.SelectedItem is MisfireStrategy strategy ? strategy : MisfireStrategy.DoNothing;
|
||||
_task.MisfireStrategy = MisfireBox.SelectedItem is Option<MisfireStrategy> strategy ? strategy.Value : MisfireStrategy.DoNothing;
|
||||
return true;
|
||||
}
|
||||
|
||||
@@ -183,6 +262,7 @@ public partial class TaskEditorWindow : Window
|
||||
_task.Triggers.Add(trigger);
|
||||
TriggersList.SelectedItem = trigger;
|
||||
TriggersList.Items.Refresh();
|
||||
UpdateEditorAvailability();
|
||||
}
|
||||
|
||||
private void RemoveTrigger_Click(object sender, RoutedEventArgs e)
|
||||
@@ -191,6 +271,7 @@ public partial class TaskEditorWindow : Window
|
||||
{
|
||||
_task.Triggers.Remove(trigger);
|
||||
TriggersList.Items.Refresh();
|
||||
UpdateEditorAvailability();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -202,6 +283,50 @@ public partial class TaskEditorWindow : Window
|
||||
}
|
||||
}
|
||||
|
||||
private void WindowOption_Changed(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (!_loading)
|
||||
{
|
||||
EnsureOptionalDateDefaults();
|
||||
}
|
||||
|
||||
UpdateWindowControlState();
|
||||
Field_Changed(sender, e);
|
||||
}
|
||||
|
||||
private void ApplyOneTimeDelay_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (!int.TryParse(OneTimeDelayValueBox.Text, out var value) || value < 1)
|
||||
{
|
||||
OneTimeDelayValueBox.Text = "1";
|
||||
value = 1;
|
||||
}
|
||||
|
||||
var unit = OneTimeDelayUnitBox.SelectedItem is Option<DelayUnit> option ? option.Value : DelayUnit.Minutes;
|
||||
ApplyOneTimeDelay(value, unit);
|
||||
}
|
||||
|
||||
private void QuickOneTimeDelay_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (sender is Button { Tag: string minutesText } && int.TryParse(minutesText, out var minutes))
|
||||
{
|
||||
ApplyOneTimeDelay(minutes, DelayUnit.Minutes);
|
||||
}
|
||||
}
|
||||
|
||||
private void ApplyOneTimeDelay(int value, DelayUnit unit)
|
||||
{
|
||||
var delay = unit switch
|
||||
{
|
||||
DelayUnit.Hours => TimeSpan.FromHours(value),
|
||||
DelayUnit.Days => TimeSpan.FromDays(value),
|
||||
_ => TimeSpan.FromMinutes(value)
|
||||
};
|
||||
|
||||
SetDateTime(OneTimeDatePicker, OneTimeHourBox, OneTimeMinuteBox, OneTimeSecondBox, DateTime.Now.Add(delay));
|
||||
SaveTriggerFields();
|
||||
}
|
||||
|
||||
private async void TestSend_Click(object sender, RoutedEventArgs e)
|
||||
{
|
||||
if (!SaveTaskFields())
|
||||
@@ -210,7 +335,7 @@ public partial class TaskEditorWindow : Window
|
||||
}
|
||||
|
||||
TestResultText.Text = "发送中...";
|
||||
var log = await _client.SendAsync(_task, "Test", _settings.OmniNotifyUrl);
|
||||
var log = await _client.SendAsync(_task, "测试发送", _settings.OmniNotifyUrl);
|
||||
TestResultText.Text = log.Level == "INFO" ? "测试发送成功" : log.Message;
|
||||
TestResultText.Foreground = log.Level == "INFO"
|
||||
? System.Windows.Media.Brushes.ForestGreen
|
||||
@@ -243,4 +368,102 @@ public partial class TaskEditorWindow : Window
|
||||
? "无法计算未来执行时间,请检查规则。"
|
||||
: string.Join(Environment.NewLine, preview.Select(d => d.ToString("yyyy-MM-dd HH:mm:ss")));
|
||||
}
|
||||
|
||||
private void UpdateEditorAvailability()
|
||||
{
|
||||
var hasTrigger = TriggersList.SelectedItem is TaskTrigger;
|
||||
TriggerEmptyText.Visibility = hasTrigger ? Visibility.Collapsed : Visibility.Visible;
|
||||
TriggerEditorPanel.Visibility = hasTrigger ? Visibility.Visible : Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void UpdateVisibleSections(TriggerKind kind)
|
||||
{
|
||||
OneTimeSection.Visibility = kind == TriggerKind.OneTime ? Visibility.Visible : Visibility.Collapsed;
|
||||
IntervalSection.Visibility = kind == TriggerKind.Interval ? Visibility.Visible : Visibility.Collapsed;
|
||||
DailySection.Visibility = kind == TriggerKind.Daily ? Visibility.Visible : Visibility.Collapsed;
|
||||
WeeklySection.Visibility = kind == TriggerKind.Weekly ? Visibility.Visible : Visibility.Collapsed;
|
||||
MonthlySection.Visibility = kind == TriggerKind.Monthly ? Visibility.Visible : Visibility.Collapsed;
|
||||
CronSection.Visibility = kind == TriggerKind.Cron ? Visibility.Visible : Visibility.Collapsed;
|
||||
WindowSection.Visibility = kind is TriggerKind.Interval or TriggerKind.Daily or TriggerKind.Weekly or TriggerKind.Monthly or TriggerKind.Cron
|
||||
? Visibility.Visible
|
||||
: Visibility.Collapsed;
|
||||
}
|
||||
|
||||
private void UpdateWindowControlState()
|
||||
{
|
||||
StartsAtPanel.IsEnabled = UseStartsAtBox.IsChecked == true;
|
||||
EndsAtPanel.IsEnabled = UseEndsAtBox.IsChecked == true;
|
||||
}
|
||||
|
||||
private void EnsureOptionalDateDefaults()
|
||||
{
|
||||
if (UseStartsAtBox.IsChecked == true && StartsAtDatePicker.SelectedDate is null)
|
||||
{
|
||||
SetDateTime(StartsAtDatePicker, StartsAtHourBox, StartsAtMinuteBox, StartsAtSecondBox, DateTime.Now);
|
||||
}
|
||||
|
||||
if (UseEndsAtBox.IsChecked == true && EndsAtDatePicker.SelectedDate is null)
|
||||
{
|
||||
SetDateTime(EndsAtDatePicker, EndsAtHourBox, EndsAtMinuteBox, EndsAtSecondBox, DateTime.Now.AddDays(1));
|
||||
}
|
||||
}
|
||||
|
||||
private void SetTimeItems(ComboBox hourBox, ComboBox minuteBox, ComboBox secondBox)
|
||||
{
|
||||
hourBox.ItemsSource = _hours;
|
||||
minuteBox.ItemsSource = _minutesAndSeconds;
|
||||
secondBox.ItemsSource = _minutesAndSeconds;
|
||||
}
|
||||
|
||||
private void SetDateTime(DatePicker datePicker, ComboBox hourBox, ComboBox minuteBox, ComboBox secondBox, DateTime value)
|
||||
{
|
||||
datePicker.SelectedDate = value.Date;
|
||||
SetTimeOnly(hourBox, minuteBox, secondBox, value.TimeOfDay);
|
||||
}
|
||||
|
||||
private void SetOptionalDateTime(DatePicker datePicker, ComboBox hourBox, ComboBox minuteBox, ComboBox secondBox, DateTime? value)
|
||||
{
|
||||
if (value.HasValue)
|
||||
{
|
||||
SetDateTime(datePicker, hourBox, minuteBox, secondBox, value.Value);
|
||||
return;
|
||||
}
|
||||
|
||||
datePicker.SelectedDate = null;
|
||||
SetTimeOnly(hourBox, minuteBox, secondBox, TimeSpan.Zero);
|
||||
}
|
||||
|
||||
private void SetTimeOnly(ComboBox hourBox, ComboBox minuteBox, ComboBox secondBox, TimeSpan value)
|
||||
{
|
||||
hourBox.SelectedItem = _hours.First(o => o.Value == value.Hours);
|
||||
minuteBox.SelectedItem = _minutesAndSeconds.First(o => o.Value == value.Minutes);
|
||||
secondBox.SelectedItem = _minutesAndSeconds.First(o => o.Value == value.Seconds);
|
||||
}
|
||||
|
||||
private DateTime? GetDateTime(DatePicker datePicker, ComboBox hourBox, ComboBox minuteBox, ComboBox secondBox)
|
||||
{
|
||||
if (datePicker.SelectedDate is not { } date)
|
||||
{
|
||||
return null;
|
||||
}
|
||||
|
||||
return date.Date.Add(GetTimeOnly(hourBox, minuteBox, secondBox));
|
||||
}
|
||||
|
||||
private static TimeSpan GetTimeOnly(ComboBox hourBox, ComboBox minuteBox, ComboBox secondBox)
|
||||
{
|
||||
var hour = hourBox.SelectedItem is Option<int> h ? h.Value : 0;
|
||||
var minute = minuteBox.SelectedItem is Option<int> m ? m.Value : 0;
|
||||
var second = secondBox.SelectedItem is Option<int> s ? s.Value : 0;
|
||||
return new TimeSpan(hour, minute, second);
|
||||
}
|
||||
}
|
||||
|
||||
public sealed record Option<T>(T Value, string Label);
|
||||
|
||||
public enum DelayUnit
|
||||
{
|
||||
Minutes,
|
||||
Hours,
|
||||
Days
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user