Skip to content

Commit

Permalink
feat: 优化了设置界面, 增加了插件权重设置, 允许自定义快捷键
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimeNull committed Jan 9, 2024
1 parent d95ffdf commit e0b3da9
Show file tree
Hide file tree
Showing 41 changed files with 1,094 additions and 499 deletions.
Binary file modified Assets/Icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Assets/Icon.psd
Binary file not shown.
Binary file modified Assets/Icon128.ico
Binary file not shown.
Binary file modified Assets/Icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Assets/Icon32.ico
Binary file not shown.
Binary file modified Assets/Icon32.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified Assets/Icon64.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
39 changes: 37 additions & 2 deletions CurvaLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@
using CommunityToolkit.Mvvm.ComponentModel.__Internals;
using System.Reflection;
using System.Diagnostics;
using CurvaLauncher.Views.Pages;
using Wpf.Ui.Mvvm.Contracts;
using Wpf.Ui.Mvvm.Services;

namespace CurvaLauncher
{
Expand All @@ -30,15 +33,28 @@ private static IServiceProvider BuildServiceProvider()
{
ServiceCollection services = new ServiceCollection();

// view
services.AddSingleton<MainWindow>();
services.AddSingleton<MainViewModel>();
services.AddSingleton<SettingsWindow>();
services.AddSingleton<SettingsGeneralPage>();
services.AddSingleton<SettingsPluginPage>();
services.AddSingleton<SettingsHotkeyPage>();
services.AddSingleton<SettingsAboutPage>();

// view model
services.AddSingleton<MainViewModel>();
services.AddSingleton<SettingsViewModel>();
services.AddSingleton<SettingsGeneralViewModel>();
services.AddSingleton<SettingsPluginViewModel>();
services.AddSingleton<SettingsHotkeyViewModel>();
services.AddSingleton<SettingsAboutViewModel>();

// services
services.AddSingleton<PathService>();
services.AddSingleton<HotkeyService>();
services.AddSingleton<PluginService>();
services.AddSingleton<ConfigService>();
services.AddSingleton<PageService>();

return services.BuildServiceProvider();
}
Expand All @@ -63,7 +79,7 @@ protected override void OnStartup(StartupEventArgs e)
ServiceProvider
.GetRequiredService<MainWindow>();

if (!hotkeyService.Registered)
if (!hotkeyService.IsLauncherHotkeyRegistered)
{
NativeMethods.MessageBox(
IntPtr.Zero,
Expand Down Expand Up @@ -139,6 +155,25 @@ public static void ShowLauncher()
mainWindow.QueryBox.Focus();
}

public static void ShowLauncherWithQuery(string queryText)
{
_currentCancellationTokenSource = new();

var mainWindow =
ServiceProvider.GetRequiredService<MainWindow>();
var configService =
ServiceProvider.GetRequiredService<ConfigService>();

mainWindow.Width = configService.Config.LauncherWidth;
mainWindow.Left = (SystemParameters.PrimaryScreenWidth - mainWindow.AppConfig.LauncherWidth) / 2;
mainWindow.Top = SystemParameters.PrimaryScreenHeight / 3;

mainWindow.Show();
mainWindow.Activate();
mainWindow.SetQueryText(queryText);
mainWindow.QueryBox.Focus();
}

public static void CloseLauncher()
{
var mainWindow =
Expand Down
19 changes: 17 additions & 2 deletions CurvaLauncher/AppConfig.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using CommunityToolkit.Mvvm.ComponentModel;
using CurvaLauncher.Models;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Text.Json.Nodes;
using System.Text.Json.Serialization;

Expand Down Expand Up @@ -27,11 +29,24 @@ public partial class AppConfig : ObservableObject
private string _launcherHotkey = "Alt+Space";

[ObservableProperty]
private JsonObject? _pluginsConfig;
private ObservableCollection<QueryHotkey> _customQueryHotkeys = new();

[ObservableProperty]
private HashSet<string>? _disabledPlugins;
private Dictionary<string, PluginConfig> _plugins = new();

[JsonIgnore]
public double LauncherResultViewHeight => LauncherResultViewCount * 57 + LauncherResultViewCount;


public partial class PluginConfig : ObservableObject
{
[ObservableProperty]
private bool _isEnabled;

[ObservableProperty]
private float _weight;

[ObservableProperty]
JsonObject? _options;
}
}
Binary file modified CurvaLauncher/Assets/Icon128.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file modified CurvaLauncher/Assets/Icon32.ico
Binary file not shown.
Binary file modified CurvaLauncher/Icon128.ico
Binary file not shown.
7 changes: 7 additions & 0 deletions CurvaLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,11 @@ public void ScrollToSelectedQueryResult()
{
resultBox.ScrollIntoView(resultBox.SelectedItem);
}

public void SetQueryText(string text)
{
ViewModel.QueryText = text;
QueryBox.SelectionStart = text.Length;
QueryBox.SelectionLength = 0;
}
}
3 changes: 3 additions & 0 deletions CurvaLauncher/Models/MicaLauncherPluginInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,9 @@ private CurvaLauncherPluginInstance(IPlugin plugin)
[ObservableProperty]
private bool _isEnabled = false;

[ObservableProperty]
private float _weight = 1;

partial void OnIsEnabledChanged(bool value)
{
if (value)
Expand Down
19 changes: 19 additions & 0 deletions CurvaLauncher/Models/QueryHotkey.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Text.Json.Serialization;
using CommunityToolkit.Mvvm.ComponentModel;
using CurvaLauncher.Utilities;

namespace CurvaLauncher.Models;

public sealed partial class QueryHotkey : ObservableObject
{
[ObservableProperty]
[NotifyPropertyChangedFor(nameof(IsValidHotkey))]
private string _hotkey = string.Empty;

[ObservableProperty]
private string _queryText = string.Empty;

[JsonIgnore]
public bool IsValidHotkey => HotkeyUtils.IsValidHotkey(Hotkey);
}
35 changes: 15 additions & 20 deletions CurvaLauncher/Models/QueryResultModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,41 +13,36 @@ public partial class QueryResultModel : ObservableObject
{
private readonly IQueryResult _rawQueryResult;

public QueryResultModel(float weight, string title, string description, ImageSource? icon, IQueryResult rawQueryResult)
private QueryResultModel(float weight, string title, string description, ImageSource? icon, IQueryResult rawQueryResult)
{
Weight = weight;
Title = title;
Description = description;
Icon = icon;
_icon = icon;
_rawQueryResult = rawQueryResult;
}

[ObservableProperty]
private float weight;
private ImageSource? _icon;

[ObservableProperty]
private string title = string.Empty;

[ObservableProperty]
private string description = string.Empty;

[ObservableProperty]
private ImageSource? icon;
public float Weight { get; }
public string Title { get; }
public string Description { get; }
public ImageSource? Icon => _icon;

public void SetFallbackIcon(Func<ImageSource> iconFactory)
{
if (Icon == null)
if (_icon == null)
{
Icon = iconFactory.Invoke();
SetProperty(ref _icon, iconFactory.Invoke(), nameof(Icon));
}
else if (Icon is BitmapImage bitmapImage && bitmapImage.IsDownloading)
else if (_icon is BitmapImage bitmapImage && bitmapImage.IsDownloading)
{
var originIcon = Icon;
var originIcon = _icon;

Icon = iconFactory.Invoke();
SetProperty(ref _icon, iconFactory.Invoke(), nameof(Icon));
bitmapImage.DownloadCompleted += (s, e) =>
{
Icon = originIcon;
SetProperty(ref _icon, originIcon, nameof(Icon));
};
}
}
Expand Down Expand Up @@ -86,8 +81,8 @@ public async Task Invoke()
App.CloseLauncher();
}

public static QueryResultModel FromQueryResult(IQueryResult queryResult)
public static QueryResultModel Create(CurvaLauncherPluginInstance pluginInstance, IQueryResult queryResult)
{
return new QueryResultModel(queryResult.Weight, queryResult.Title, queryResult.Description, queryResult.Icon, queryResult);
return new QueryResultModel(pluginInstance.Weight * queryResult.Weight, queryResult.Title, queryResult.Description, queryResult.Icon, queryResult);
}
}
40 changes: 17 additions & 23 deletions CurvaLauncher/Services/ConfigService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
using System.Text.Json.Nodes;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CurvaLauncher.Models;
using CurvaLauncher.Plugin;
using CurvaLauncher.Utilities;
using Microsoft.Extensions.DependencyInjection;
Expand Down Expand Up @@ -51,27 +52,19 @@ private void LoadConfig(out AppConfig config)
config = JsonSerializer.Deserialize<AppConfig>(fs, JsonUtils.Options) ?? new AppConfig();
}

private HashSet<string> GetDisabledPlugins()
private AppConfig.PluginConfig GetPluginConfig(CurvaLauncherPluginInstance pluginInstance)
{
PluginService pluginService = _serviceProvider
.GetRequiredService<PluginService>();

return pluginService.PluginInstances
.Where(pluginInstance => !pluginInstance.IsEnabled)
.Select(pluginInstance => pluginInstance.Plugin.GetType().FullName!)
.Where(pluginName => pluginName != null)
.ToHashSet();
}

private JsonObject GetPluginsConfig()
{
PluginService pluginService = _serviceProvider
.GetRequiredService<PluginService>();
return new()
{
IsEnabled = pluginInstance.IsEnabled,
Weight = pluginInstance.Weight,
Options = GetPluginOptions(pluginInstance.Plugin)
};

JsonObject config = new();
foreach (var pluginInstance in pluginService.PluginInstances)
JsonObject GetPluginOptions(IPlugin plugin)
{
var props = pluginInstance.Plugin.GetType().GetProperties()
var props = plugin.GetType()
.GetProperties()
.Where(p => p.GetCustomAttribute<PluginOptionAttribute>() is not null);

JsonObject json = new();
Expand All @@ -80,10 +73,8 @@ private JsonObject GetPluginsConfig()
json[property.Name] = JsonSerializer.SerializeToNode(property.GetValue(pluginInstance.Plugin));
}

config[pluginInstance.Plugin.GetType().FullName!] = json;
return json;
}

return config;
}

[RelayCommand]
Expand All @@ -96,8 +87,11 @@ public void Load()
[RelayCommand]
public void Save()
{
Config.DisabledPlugins = GetDisabledPlugins();
Config.PluginsConfig = GetPluginsConfig();
var pluginService = _serviceProvider
.GetRequiredService<PluginService>();

Config.Plugins = pluginService.PluginInstances
.ToDictionary(instance => instance.Plugin.GetType().FullName!, instance => GetPluginConfig(instance));

string fullPath = _pathService.GetPath(Path);
using FileStream fs = File.Create(fullPath);
Expand Down
Loading

0 comments on commit e0b3da9

Please sign in to comment.