diff --git a/Flow.Launcher/Helper/AutoStartup.cs b/Flow.Launcher.Infrastructure/AutoStartup.cs similarity index 95% rename from Flow.Launcher/Helper/AutoStartup.cs rename to Flow.Launcher.Infrastructure/AutoStartup.cs index c5e20504b7d..42797237eb7 100644 --- a/Flow.Launcher/Helper/AutoStartup.cs +++ b/Flow.Launcher.Infrastructure/AutoStartup.cs @@ -2,12 +2,11 @@ using System.IO; using System.Linq; using System.Security.Principal; -using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.Logger; using Microsoft.Win32; using Microsoft.Win32.TaskScheduler; -namespace Flow.Launcher.Helper; +namespace Flow.Launcher.Infrastructure; public class AutoStartup { @@ -32,7 +31,7 @@ public static bool IsEnabled var path = key?.GetValue(Constant.FlowLauncher) as string; return path == Constant.ExecutablePath; } - catch (Exception e) + catch (System.Exception e) { Log.Error("AutoStartup", $"Ignoring non-critical registry error (querying if enabled): {e}"); } @@ -59,7 +58,7 @@ private static bool CheckLogonTask() return true; } - catch (Exception e) + catch (System.Exception e) { Log.Error("AutoStartup", $"Failed to check logon task: {e}"); } @@ -110,7 +109,7 @@ private static void Disable(bool logonTask) key?.DeleteValue(Constant.FlowLauncher, false); } } - catch (Exception e) + catch (System.Exception e) { Log.Error("AutoStartup", $"Failed to disable auto-startup: {e}"); throw; @@ -131,7 +130,7 @@ private static void Enable(bool logonTask) key?.SetValue(Constant.FlowLauncher, $"\"{Constant.ExecutablePath}\""); } } - catch (Exception e) + catch (System.Exception e) { Log.Error("AutoStartup", $"Failed to enable auto-startup: {e}"); throw; @@ -159,7 +158,7 @@ private static bool ScheduleLogonTask() TaskService.Instance.RootFolder.RegisterTaskDefinition(LogonTaskName, td); return true; } - catch (Exception e) + catch (System.Exception e) { Log.Error("AutoStartup", $"Failed to schedule logon task: {e}"); return false; @@ -174,7 +173,7 @@ private static bool UnscheduleLogonTask() taskService.RootFolder.DeleteTask(LogonTaskName); return true; } - catch (Exception e) + catch (System.Exception e) { Log.Error("AutoStartup", $"Failed to unschedule logon task: {e}"); return false; diff --git a/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj b/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj index 5d8b264251a..645edfcf0a8 100644 --- a/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj +++ b/Flow.Launcher.Infrastructure/Flow.Launcher.Infrastructure.csproj @@ -1,4 +1,4 @@ - + net7.0-windows @@ -68,6 +68,7 @@ + diff --git a/Flow.Launcher/App.xaml.cs b/Flow.Launcher/App.xaml.cs index 8374fc9fe9a..1d871aecd12 100644 --- a/Flow.Launcher/App.xaml.cs +++ b/Flow.Launcher/App.xaml.cs @@ -35,31 +35,64 @@ public partial class App : IDisposable, ISingleInstanceApp public App() { // Initialize settings - var storage = new FlowLauncherJsonStorage(); - _settings = storage.Load(); - _settings.SetStorage(storage); - _settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled(); + try + { + var storage = new FlowLauncherJsonStorage(); + _settings = storage.Load(); + _settings.SetStorage(storage); + _settings.WMPInstalled = WindowsMediaPlayerHelper.IsWindowsMediaPlayerInstalled(); + } + catch (Exception e) + { + ShowErrorMsgBoxAndFailFast("Cannot load setting storage, please check local data directory", e); + return; + } // Configure the dependency injection container - var host = Host.CreateDefaultBuilder() - .UseContentRoot(AppContext.BaseDirectory) - .ConfigureServices(services => services - .AddSingleton(_ => _settings) - .AddSingleton(sp => new Updater(sp.GetRequiredService(), Launcher.Properties.Settings.Default.GithubRepo)) - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - .AddSingleton() - ).Build(); - Ioc.Default.ConfigureServices(host.Services); + try + { + var host = Host.CreateDefaultBuilder() + .UseContentRoot(AppContext.BaseDirectory) + .ConfigureServices(services => services + .AddSingleton(_ => _settings) + .AddSingleton(sp => new Updater(sp.GetRequiredService(), Launcher.Properties.Settings.Default.GithubRepo)) + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + .AddSingleton() + ).Build(); + Ioc.Default.ConfigureServices(host.Services); + } + catch (Exception e) + { + ShowErrorMsgBoxAndFailFast("Cannot configure dependency injection container, please open new issue in Flow.Launcher", e); + return; + } // Initialize the public API and Settings first - API = Ioc.Default.GetRequiredService(); - _settings.Initialize(); + try + { + API = Ioc.Default.GetRequiredService(); + _settings.Initialize(); + } + catch (Exception e) + { + ShowErrorMsgBoxAndFailFast("Cannot initialize api and settings, please open new issue in Flow.Launcher", e); + return; + } + } + + private static void ShowErrorMsgBoxAndFailFast(string message, Exception e) + { + // Firstly show users the message + MessageBox.Show(e.ToString(), message, MessageBoxButton.OK, MessageBoxImage.Error); + + // Flow cannot construct its App instance, so ensure Flow crashes w/ the exception info. + Environment.FailFast(message, e); } [STAThread] @@ -102,8 +135,8 @@ await Stopwatch.NormalAsync("|App.OnStartup|Startup cost", async () => await imageLoadertask; var mainVM = Ioc.Default.GetRequiredService(); + ((PublicAPIInstance)API).Initialize(mainVM); var window = new MainWindow(_settings, mainVM); - Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}"); Current.MainWindow = window; @@ -132,17 +165,17 @@ private void AutoStartup() { // we try to enable auto-startup on first launch, or reenable if it was removed // but the user still has the setting set - if (_settings.StartFlowLauncherOnSystemStartup && !Helper.AutoStartup.IsEnabled) + if (_settings.StartFlowLauncherOnSystemStartup && !Infrastructure.AutoStartup.IsEnabled) { try { if (_settings.UseLogonTaskForStartup) { - Helper.AutoStartup.EnableViaLogonTask(); + Infrastructure.AutoStartup.EnableViaLogonTask(); } else { - Helper.AutoStartup.EnableViaRegistry(); + Infrastructure.AutoStartup.EnableViaRegistry(); } } catch (Exception e) diff --git a/Flow.Launcher/Flow.Launcher.csproj b/Flow.Launcher/Flow.Launcher.csproj index 0baa1bef503..f8ace91f871 100644 --- a/Flow.Launcher/Flow.Launcher.csproj +++ b/Flow.Launcher/Flow.Launcher.csproj @@ -104,7 +104,6 @@ - diff --git a/Flow.Launcher/PublicAPIInstance.cs b/Flow.Launcher/PublicAPIInstance.cs index 1a08150e591..207a3150b90 100644 --- a/Flow.Launcher/PublicAPIInstance.cs +++ b/Flow.Launcher/PublicAPIInstance.cs @@ -34,17 +34,22 @@ namespace Flow.Launcher public class PublicAPIInstance : IPublicAPI { private readonly Settings _settings; - private readonly MainViewModel _mainVM; + private MainViewModel _mainVM; - #region Constructor + #region Constructor & Initialization - public PublicAPIInstance(Settings settings, MainViewModel mainVM) + public PublicAPIInstance(Settings settings) { _settings = settings; - _mainVM = mainVM; GlobalHotkey.hookedKeyboardCallback = KListener_hookedKeyboardCallback; WebRequest.RegisterPrefix("data", new DataWebRequestFactory()); } + + // We must initialize mainVM later to avoid dispatcher thread issue + public void Initialize(MainViewModel mainVM) + { + _mainVM = mainVM; + } #endregion diff --git a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs index dddaa99d46e..894906cd501 100644 --- a/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs +++ b/Flow.Launcher/SettingPages/ViewModels/SettingsPaneGeneralViewModel.cs @@ -6,7 +6,7 @@ using Flow.Launcher.Core; using Flow.Launcher.Core.Configuration; using Flow.Launcher.Core.Resource; -using Flow.Launcher.Helper; +using Flow.Launcher.Infrastructure; using Flow.Launcher.Infrastructure.UserSettings; using Flow.Launcher.Plugin; using Flow.Launcher.Plugin.SharedModels;