|
1 | | -using System.Windows; |
| 1 | +using System; |
| 2 | +using System.Windows; |
2 | 3 | using System.Windows.Navigation; |
3 | 4 | using CommunityToolkit.Mvvm.DependencyInjection; |
4 | | -using Flow.Launcher.Infrastructure; |
| 5 | +using Flow.Launcher.Helper; |
5 | 6 | using Flow.Launcher.Infrastructure.UserSettings; |
6 | 7 | using Flow.Launcher.ViewModel; |
7 | | -using Microsoft.Win32; |
8 | 8 |
|
9 | 9 | namespace Flow.Launcher.Resources.Pages |
10 | 10 | { |
11 | 11 | public partial class WelcomePage5 |
12 | 12 | { |
13 | 13 | public Settings Settings { get; private set; } |
14 | | - |
15 | | - private const string StartupPath = "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run"; |
16 | | - public bool HideOnStartup { get; set; } |
| 14 | + private WelcomeViewModel _viewModel; |
17 | 15 |
|
18 | 16 | protected override void OnNavigatedTo(NavigationEventArgs e) |
19 | 17 | { |
20 | 18 | if (!IsInitialized) |
21 | 19 | { |
22 | 20 | Settings = Ioc.Default.GetRequiredService<Settings>(); |
| 21 | + _viewModel = Ioc.Default.GetRequiredService<WelcomeViewModel>(); |
23 | 22 | InitializeComponent(); |
24 | 23 | } |
25 | 24 | // Sometimes the navigation is not triggered by button click, |
26 | 25 | // so we need to reset the page number |
27 | | - Ioc.Default.GetRequiredService<WelcomeViewModel>().PageNum = 5; |
| 26 | + _viewModel.PageNum = 5; |
28 | 27 | base.OnNavigatedTo(e); |
29 | 28 | } |
30 | 29 |
|
31 | 30 | private void OnAutoStartupChecked(object sender, RoutedEventArgs e) |
32 | 31 | { |
33 | | - SetStartup(); |
| 32 | + ChangeAutoStartup(true); |
34 | 33 | } |
| 34 | + |
35 | 35 | private void OnAutoStartupUncheck(object sender, RoutedEventArgs e) |
36 | 36 | { |
37 | | - RemoveStartup(); |
| 37 | + ChangeAutoStartup(false); |
| 38 | + } |
| 39 | + |
| 40 | + private void ChangeAutoStartup(bool value) |
| 41 | + { |
| 42 | + Settings.StartFlowLauncherOnSystemStartup = value; |
| 43 | + try |
| 44 | + { |
| 45 | + if (value) |
| 46 | + { |
| 47 | + if (Settings.UseLogonTaskForStartup) |
| 48 | + { |
| 49 | + AutoStartup.ChangeToViaLogonTask(); |
| 50 | + } |
| 51 | + else |
| 52 | + { |
| 53 | + AutoStartup.ChangeToViaRegistry(); |
| 54 | + } |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + AutoStartup.DisableViaLogonTaskAndRegistry(); |
| 59 | + } |
| 60 | + } |
| 61 | + catch (Exception e) |
| 62 | + { |
| 63 | + App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message); |
| 64 | + } |
| 65 | + } |
| 66 | + |
| 67 | + private void OnUseLogonTaskChecked(object sender, RoutedEventArgs e) |
| 68 | + { |
| 69 | + ChangeUseLogonTask(true); |
38 | 70 | } |
39 | 71 |
|
40 | | - private void RemoveStartup() |
| 72 | + private void OnUseLogonTaskUncheck(object sender, RoutedEventArgs e) |
41 | 73 | { |
42 | | - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); |
43 | | - key?.DeleteValue(Constant.FlowLauncher, false); |
44 | | - Settings.StartFlowLauncherOnSystemStartup = false; |
| 74 | + ChangeUseLogonTask(false); |
45 | 75 | } |
46 | | - private void SetStartup() |
| 76 | + |
| 77 | + private void ChangeUseLogonTask(bool value) |
47 | 78 | { |
48 | | - using var key = Registry.CurrentUser.OpenSubKey(StartupPath, true); |
49 | | - key?.SetValue(Constant.FlowLauncher, Constant.ExecutablePath); |
50 | | - Settings.StartFlowLauncherOnSystemStartup = true; |
| 79 | + Settings.UseLogonTaskForStartup = value; |
| 80 | + if (Settings.StartFlowLauncherOnSystemStartup) |
| 81 | + { |
| 82 | + try |
| 83 | + { |
| 84 | + if (value) |
| 85 | + { |
| 86 | + AutoStartup.ChangeToViaLogonTask(); |
| 87 | + } |
| 88 | + else |
| 89 | + { |
| 90 | + AutoStartup.ChangeToViaRegistry(); |
| 91 | + } |
| 92 | + } |
| 93 | + catch (Exception e) |
| 94 | + { |
| 95 | + App.API.ShowMsg(App.API.GetTranslation("setAutoStartFailed"), e.Message); |
| 96 | + } |
| 97 | + } |
51 | 98 | } |
52 | 99 |
|
53 | 100 | private void OnHideOnStartupChecked(object sender, RoutedEventArgs e) |
54 | 101 | { |
55 | 102 | Settings.HideOnStartup = true; |
56 | 103 | } |
| 104 | + |
57 | 105 | private void OnHideOnStartupUnchecked(object sender, RoutedEventArgs e) |
58 | 106 | { |
59 | 107 | Settings.HideOnStartup = false; |
|
0 commit comments