Use api fuctions forr main project

This commit is contained in:
Jack251970 2025-04-13 17:50:44 +08:00
parent b13ab3b893
commit 4a7915b9ff
13 changed files with 67 additions and 56 deletions

View file

@ -18,12 +18,15 @@ namespace Flow.Launcher.Core.Resource
{
private static readonly string ClassName = nameof(Internationalization);
// We should not initialize API in static constructor because it will create another API instance
private static IPublicAPI api = null;
private static IPublicAPI API => api ??= Ioc.Default.GetRequiredService<IPublicAPI>();
private const string Folder = "Languages";
private const string DefaultLanguageCode = "en";
private const string DefaultFile = "en.xaml";
private const string Extension = ".xaml";
private readonly Settings _settings;
private readonly IPublicAPI _api;
private readonly List<string> _languageDirectories = new();
private readonly List<ResourceDictionary> _oldResources = new();
private readonly string SystemLanguageCode;
@ -31,7 +34,6 @@ namespace Flow.Launcher.Core.Resource
public Internationalization(Settings settings)
{
_settings = settings;
_api = Ioc.Default.GetRequiredService<IPublicAPI>();
AddFlowLauncherLanguageDirectory();
SystemLanguageCode = GetSystemLanguageCodeAtStartup();
}
@ -83,7 +85,7 @@ namespace Flow.Launcher.Core.Resource
}
else
{
_api.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
API.LogError(ClassName, $"Can't find plugin path <{location}> for <{plugin.Metadata.Name}>");
}
}
@ -147,13 +149,13 @@ namespace Flow.Launcher.Core.Resource
_settings.Language = isSystem ? Constant.SystemLanguageCode : language.LanguageCode;
}
private Language GetLanguageByLanguageCode(string languageCode)
private static Language GetLanguageByLanguageCode(string languageCode)
{
var lowercase = languageCode.ToLower();
var language = AvailableLanguages.GetAvailableLanguages().FirstOrDefault(o => o.LanguageCode.ToLower() == lowercase);
if (language == null)
{
_api.LogError(ClassName, $"Language code can't be found <{languageCode}>");
API.LogError(ClassName, $"Language code can't be found <{languageCode}>");
return AvailableLanguages.English;
}
else
@ -242,7 +244,7 @@ namespace Flow.Launcher.Core.Resource
return list;
}
public string GetTranslation(string key)
public static string GetTranslation(string key)
{
var translation = Application.Current.TryFindResource(key);
if (translation is string)
@ -251,7 +253,7 @@ namespace Flow.Launcher.Core.Resource
}
else
{
_api.LogError(ClassName, $"No Translation for key {key}");
API.LogError(ClassName, $"No Translation for key {key}");
return $"No Translation for key {key}";
}
}
@ -269,12 +271,12 @@ namespace Flow.Launcher.Core.Resource
}
catch (Exception e)
{
_api.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e);
API.LogException(ClassName, $"Failed for <{p.Metadata.Name}>", e);
}
}
}
private string LanguageFile(string folder, string language)
private static string LanguageFile(string folder, string language)
{
if (Directory.Exists(folder))
{
@ -285,7 +287,7 @@ namespace Flow.Launcher.Core.Resource
}
else
{
_api.LogError(ClassName, $"Language path can't be found <{path}>");
API.LogError(ClassName, $"Language path can't be found <{path}>");
var english = Path.Combine(folder, DefaultFile);
if (File.Exists(english))
{
@ -293,7 +295,7 @@ namespace Flow.Launcher.Core.Resource
}
else
{
_api.LogError(ClassName, $"Default English Language path can't be found <{path}>");
API.LogError(ClassName, $"Default English Language path can't be found <{path}>");
return string.Empty;
}
}

View file

@ -148,8 +148,8 @@ namespace Flow.Launcher
Ioc.Default.GetRequiredService<Portable>().PreStartCleanUpAfterPortabilityUpdate();
Log.Info("|App.OnStartup|Begin Flow Launcher startup ----------------------------------------------------");
Log.Info($"|App.OnStartup|Runtime info:{ErrorReporting.RuntimeInfo()}");
API.LogInfo(ClassName, "Begin Flow Launcher startup ----------------------------------------------------");
API.LogInfo(ClassName, "Runtime info:{ErrorReporting.RuntimeInfo()}");
RegisterAppDomainExceptions();
RegisterDispatcherUnhandledException();
@ -176,7 +176,7 @@ namespace Flow.Launcher
_mainWindow = new MainWindow();
Log.Info($"|App.OnStartup|Dependencies Info:{ErrorReporting.DependenciesInfo()}");
API.LogInfo(ClassName, "Dependencies Info:{ErrorReporting.DependenciesInfo()}");
Current.MainWindow = _mainWindow;
Current.MainWindow.Title = Constant.FlowLauncher;
@ -192,7 +192,7 @@ namespace Flow.Launcher
AutoUpdates();
API.SaveAppAllSettings();
Log.Info("|App.OnStartup|End Flow Launcher startup ----------------------------------------------------");
API.LogInfo(ClassName, "End Flow Launcher startup ----------------------------------------------------");
});
}
@ -250,19 +250,19 @@ namespace Flow.Launcher
{
AppDomain.CurrentDomain.ProcessExit += (s, e) =>
{
Log.Info("|App.RegisterExitEvents|Process Exit");
API.LogInfo(ClassName, "Process Exit");
Dispose();
};
Current.Exit += (s, e) =>
{
Log.Info("|App.RegisterExitEvents|Application Exit");
API.LogInfo(ClassName, "Application Exit");
Dispose();
};
Current.SessionEnding += (s, e) =>
{
Log.Info("|App.RegisterExitEvents|Session Ending");
API.LogInfo(ClassName, "Session Ending");
Dispose();
};
}
@ -326,7 +326,7 @@ namespace Flow.Launcher
API.StopwatchLogInfo(ClassName, "Dispose cost", () =>
{
Log.Info("|App.Dispose|Begin Flow Launcher dispose ----------------------------------------------------");
API.LogInfo(ClassName, "Begin Flow Launcher dispose ----------------------------------------------------");
if (disposing)
{
@ -336,7 +336,7 @@ namespace Flow.Launcher
_mainVM?.Dispose();
}
Log.Info("|App.Dispose|End Flow Launcher dispose ----------------------------------------------------");
API.LogInfo(ClassName, "End Flow Launcher dispose ----------------------------------------------------");
});
}

View file

@ -4,13 +4,14 @@ using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Media;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.ViewModel;
namespace Flow.Launcher.Converters;
public class QuerySuggestionBoxConverter : IMultiValueConverter
{
private static readonly string ClassName = nameof(QuerySuggestionBoxConverter);
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
// values[0] is TextBox: The textbox displaying the autocomplete suggestion
@ -64,7 +65,7 @@ public class QuerySuggestionBoxConverter : IMultiValueConverter
}
catch (Exception e)
{
Log.Exception(nameof(QuerySuggestionBoxConverter), "fail to convert text for suggestion box", e);
App.API.LogException(ClassName, "fail to convert text for suggestion box", e);
return string.Empty;
}
}

View file

@ -11,6 +11,8 @@ namespace Flow.Launcher.Helper;
public class AutoStartup
{
private static readonly string ClassName = nameof(AutoStartup);
private const string StartupPath = @"SOFTWARE\Microsoft\Windows\CurrentVersion\Run";
private const string LogonTaskName = $"{Constant.FlowLauncher} Startup";
private const string LogonTaskDesc = $"{Constant.FlowLauncher} Auto Startup";
@ -34,7 +36,7 @@ public class AutoStartup
}
catch (Exception e)
{
Log.Error("AutoStartup", $"Ignoring non-critical registry error (querying if enabled): {e}");
App.API.LogError(ClassName, $"Ignoring non-critical registry error (querying if enabled): {e}");
}
return false;
@ -61,7 +63,7 @@ public class AutoStartup
}
catch (Exception e)
{
Log.Error("AutoStartup", $"Failed to check logon task: {e}");
App.API.LogError(ClassName, $"Failed to check logon task: {e}");
}
}
@ -112,7 +114,7 @@ public class AutoStartup
}
catch (Exception e)
{
Log.Error("AutoStartup", $"Failed to disable auto-startup: {e}");
App.API.LogError(ClassName, $"Failed to disable auto-startup: {e}");
throw;
}
}
@ -133,7 +135,7 @@ public class AutoStartup
}
catch (Exception e)
{
Log.Error("AutoStartup", $"Failed to enable auto-startup: {e}");
App.API.LogError(ClassName, $"Failed to enable auto-startup: {e}");
throw;
}
}
@ -161,7 +163,7 @@ public class AutoStartup
}
catch (Exception e)
{
Log.Error("AutoStartup", $"Failed to schedule logon task: {e}");
App.API.LogError(ClassName, $"Failed to schedule logon task: {e}");
return false;
}
}
@ -176,7 +178,7 @@ public class AutoStartup
}
catch (Exception e)
{
Log.Error("AutoStartup", $"Failed to unschedule logon task: {e}");
App.API.LogError(ClassName, $"Failed to unschedule logon task: {e}");
return false;
}
}

View file

@ -12,6 +12,8 @@ namespace Flow.Launcher.Helper;
internal static class HotKeyMapper
{
private static readonly string ClassName = nameof(HotKeyMapper);
private static Settings _settings;
private static MainViewModel _mainViewModel;
@ -51,7 +53,7 @@ internal static class HotKeyMapper
}
catch (Exception e)
{
Log.Error(
App.API.LogError(ClassName,
string.Format("|HotkeyMapper.SetWithChefKeys|Error registering hotkey: {0} \nStackTrace:{1}",
e.Message,
e.StackTrace));
@ -76,7 +78,7 @@ internal static class HotKeyMapper
}
catch (Exception e)
{
Log.Error(
App.API.LogError(ClassName,
string.Format("|HotkeyMapper.SetHotkey|Error registering hotkey {2}: {0} \nStackTrace:{1}",
e.Message,
e.StackTrace,
@ -102,7 +104,7 @@ internal static class HotKeyMapper
}
catch (Exception e)
{
Log.Error(
App.API.LogError(ClassName,
string.Format("|HotkeyMapper.RemoveHotkey|Error removing hotkey: {0} \nStackTrace:{1}",
e.Message,
e.StackTrace));

View file

@ -4,13 +4,13 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger;
namespace Flow.Launcher
{
public partial class MessageBoxEx : Window
{
private static readonly string ClassName = nameof(MessageBoxEx);
private static MessageBoxEx msgBox;
private static MessageBoxResult _result = MessageBoxResult.None;
@ -59,7 +59,7 @@ namespace Flow.Launcher
}
catch (Exception e)
{
Log.Error($"|MessageBoxEx.Show|An error occurred: {e.Message}");
App.API.LogError(ClassName, $"An error occurred: {e.Message}");
msgBox = null;
return MessageBoxResult.None;
}

View file

@ -5,7 +5,6 @@ using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media.Animation;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Image;
namespace Flow.Launcher
{

View file

@ -1,5 +1,4 @@
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Microsoft.Toolkit.Uwp.Notifications;
using System;
using System.IO;
@ -9,6 +8,8 @@ namespace Flow.Launcher
{
internal static class Notification
{
private static readonly string ClassName = nameof(Notification);
internal static bool legacy = !Win32Helper.IsNotificationSupported();
internal static void Uninstall()
@ -51,12 +52,12 @@ namespace Flow.Launcher
{
// Temporary fix for the Windows 11 notification issue
// Possibly from 22621.1413 or 22621.1485, judging by post time of #2024
Log.Exception("Flow.Launcher.Notification|Notification InvalidOperationException Error", e);
App.API.LogException(ClassName, "Notification InvalidOperationException Error", e);
LegacyShow(title, subTitle, iconPath);
}
catch (Exception e)
{
Log.Exception("Flow.Launcher.Notification|Notification Error", e);
App.API.LogException(ClassName, "Notification Error", e);
LegacyShow(title, subTitle, iconPath);
}
}

View file

@ -2,12 +2,13 @@
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Input;
using Flow.Launcher.Infrastructure.Logger;
namespace Flow.Launcher
{
public partial class ProgressBoxEx : Window
{
private static readonly string ClassName = nameof(ProgressBoxEx);
private readonly Action _cancelProgress;
private ProgressBoxEx(Action cancelProgress)
@ -47,7 +48,7 @@ namespace Flow.Launcher
}
catch (Exception e)
{
Log.Error($"|ProgressBoxEx.Show|An error occurred: {e.Message}");
App.API.LogError(ClassName, $"An error occurred: {e.Message}");
await reportProgressAsync(null).ConfigureAwait(false);
}

View file

@ -435,16 +435,16 @@ namespace Flow.Launcher
PluginManager.UninstallPluginAsync(pluginMetadata, removePluginSettings);
public long StopwatchLogDebug(string className, string message, Action action, [CallerMemberName] string methodName = "") =>
Stopwatch.Debug($"|{className}.{methodName}|{message}", action);
Stopwatch.Debug(className, message, action, methodName);
public Task<long> StopwatchLogDebugAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "") =>
Stopwatch.DebugAsync($"|{className}.{methodName}|{message}", action);
Stopwatch.DebugAsync(className, message, action, methodName);
public long StopwatchLogInfo(string className, string message, Action action, [CallerMemberName] string methodName = "") =>
Stopwatch.Normal($"|{className}.{methodName}|{message}", action);
Stopwatch.Info(className, message, action, methodName);
public Task<long> StopwatchLogInfoAsync(string className, string message, Func<Task> action, [CallerMemberName] string methodName = "") =>
Stopwatch.NormalAsync($"|{className}.{methodName}|{message}", action);
Stopwatch.InfoAsync(className, message, action, methodName);
#endregion

View file

@ -8,13 +8,15 @@ using System.Windows;
using System.Windows.Documents;
using Flow.Launcher.Helper;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Plugin.SharedCommands;
using Flow.Launcher.Infrastructure.UserSettings;
namespace Flow.Launcher
{
internal partial class ReportWindow
{
private static readonly string ClassName = nameof(ReportWindow);
public ReportWindow(Exception exception)
{
InitializeComponent();
@ -38,7 +40,7 @@ namespace Flow.Launcher
private void SetException(Exception exception)
{
string path = Log.CurrentLogDirectory;
var path = DataLocation.VersionLogDirectory;
var directory = new DirectoryInfo(path);
var log = directory.GetFiles().OrderByDescending(f => f.LastWriteTime).First();

View file

@ -16,7 +16,6 @@ using CommunityToolkit.Mvvm.Input;
using Flow.Launcher.Core.Plugin;
using Flow.Launcher.Infrastructure;
using Flow.Launcher.Infrastructure.Hotkey;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.Storage;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
@ -30,6 +29,8 @@ namespace Flow.Launcher.ViewModel
{
#region Private Fields
private static readonly string ClassName = nameof(MainViewModel);
private bool _isQueryRunning;
private Query _lastQuery;
private string _queryTextBeforeLeaveResults;
@ -213,7 +214,7 @@ namespace Flow.Launcher.ViewModel
}
if (!_disposed)
Log.Error("MainViewModel", "Unexpected ResultViewUpdate ends");
App.API.LogError(ClassName, "Unexpected ResultViewUpdate ends");
}
void continueAction(Task t)
@ -257,7 +258,7 @@ namespace Flow.Launcher.ViewModel
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, pair.Metadata, e.Query,
token)))
{
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
}
};
}
@ -1303,7 +1304,7 @@ namespace Flow.Launcher.ViewModel
if (!_resultsUpdateChannelWriter.TryWrite(new ResultsForUpdate(resultsCopy, plugin.Metadata, query,
token, reSelect)))
{
Log.Error("MainViewModel", "Unable to add item to Result Update Queue");
App.API.LogError(ClassName, "Unable to add item to Result Update Queue");
}
}
}
@ -1347,8 +1348,8 @@ namespace Flow.Launcher.ViewModel
}
catch (Exception e)
{
Log.Exception(
$"{nameof(MainViewModel)}.{nameof(ConstructQuery)}|Error when expanding shortcut {shortcut.Key}",
App.API.LogException(ClassName,
$"Error when expanding shortcut {shortcut.Key}",
e);
}
}

View file

@ -1,5 +1,4 @@
using System;
using System.Collections;
using System.Collections.Generic;
using System.Drawing.Text;
using System.IO;
@ -7,7 +6,6 @@ using System.Threading.Tasks;
using System.Windows;
using System.Windows.Media;
using Flow.Launcher.Infrastructure.Image;
using Flow.Launcher.Infrastructure.Logger;
using Flow.Launcher.Infrastructure.UserSettings;
using Flow.Launcher.Plugin;
@ -15,6 +13,8 @@ namespace Flow.Launcher.ViewModel
{
public class ResultViewModel : BaseModel
{
private static readonly string ClassName = nameof(ResultsViewModel);
private static readonly PrivateFontCollection FontCollection = new();
private static readonly Dictionary<string, string> Fonts = new();
@ -232,8 +232,8 @@ namespace Flow.Launcher.ViewModel
}
catch (Exception e)
{
Log.Exception(
$"|ResultViewModel.LoadImageInternalAsync|IcoPath is empty and exception when calling IconDelegate for result <{Result.Title}> of plugin <{Result.PluginDirectory}>",
App.API.LogException(ClassName,
$"IcoPath is empty and exception when calling IconDelegate for result <{Result.Title}> of plugin <{Result.PluginDirectory}>",
e);
}
}