Skip to content

Commit

Permalink
feat: 添加 '上次使用查询' 的记录保存
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimeNull committed Jan 13, 2024
1 parent e6f4487 commit b16bced
Show file tree
Hide file tree
Showing 8 changed files with 78 additions and 10 deletions.
13 changes: 9 additions & 4 deletions CurvaLauncher.Plugins.RunApplication/RunApplicationPlugin.cs
Original file line number Diff line number Diff line change
Expand Up @@ -510,10 +510,15 @@ public override IEnumerable<IQueryResult> Query(string query)
if (_apps == null)
yield break;

var results = _apps
.Select(app => (App: app, Weight: HostContext.StringApi.Match(app.Name.ToLower(), query.ToLower())))
.OrderByDescending(kvw => kvw.Weight)
.Take(ResultCount);
IEnumerable<(AppInfo App, float Weight)> results;

lock (_apps)
{
results = _apps
.Select(app => (App: app, Weight: HostContext.StringApi.Match(app.Name.ToLower(), query.ToLower())))
.OrderByDescending(kvw => kvw.Weight)
.Take(ResultCount);
}

foreach (var result in results)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,9 @@ public RunWin32ApplicationQueryResult(CurvaLauncherContext context, Win32AppInfo

public void Invoke()
{
var process = Process.Start(
try
{
var process = Process.Start(
new ProcessStartInfo()
{
FileName = AppInfo.FilePath,
Expand All @@ -56,5 +58,10 @@ public void Invoke()
Verb = AppInfo.IsUAC ? "runas" : null,
UseShellExecute = true,
});
}
catch (Exception ex)
{

}
}
}
3 changes: 3 additions & 0 deletions CurvaLauncher/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Windows;
using System.Windows.Threading;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CurvaLauncher.Apis;
using CurvaLauncher.Services;
using CurvaLauncher.Utilities;
Expand Down Expand Up @@ -53,6 +54,8 @@ private static IServiceProvider BuildServiceProvider()
services.AddSingleton<I18nService>();
services.AddTransient<PageService>();

services.AddSingleton<IMessenger>(WeakReferenceMessenger.Default);

return services.BuildServiceProvider();
}

Expand Down
3 changes: 2 additions & 1 deletion CurvaLauncher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,8 @@
FontSize="{DynamicResource QueryBoxTextSize}"
FocusVisualStyle="{x:Null}"
ClearButtonEnabled="True"
PlaceholderText="Hello CurvaLauncher">
PlaceholderText="Hello CurvaLauncher"
PreviewKeyDown="QueryBox_PreviewKeyDown">
<ui:TextBox.InputBindings>
<KeyBinding Key="Up" Command="{Binding ViewModel.SelectPrevCommand}"/>
<KeyBinding Key="Down" Command="{Binding ViewModel.SelectNextCommand}"/>
Expand Down
14 changes: 13 additions & 1 deletion CurvaLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,18 @@ private void UiWindow_Loaded(object sender, RoutedEventArgs e)
Wpf.Ui.Appearance.Watcher.Watch(this, BackgroundType.Mica, true);
}


private void QueryBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Up &&
string.IsNullOrWhiteSpace(ViewModel.QueryText) &&
ViewModel.LastInvokedQueryText is string lastInvokedQueryText)
{
SetQueryText(lastInvokedQueryText);
e.Handled = true;
}
}

[RelayCommand]
public void ScrollToSelectedQueryResult()
{
Expand All @@ -57,7 +69,7 @@ public void ScrollToSelectedQueryResult()

public void SetQueryText(string text)
{
ViewModel.QueryText = text;
QueryBox.Text = text;
QueryBox.SelectionStart = text.Length;
QueryBox.SelectionLength = 0;
}
Expand Down
15 changes: 15 additions & 0 deletions CurvaLauncher/Messages/SaveQueryMessage.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CurvaLauncher.Messages
{
public class SaveQueryMessage
{
private SaveQueryMessage() { }

public static readonly SaveQueryMessage Instance = new();
}
}
9 changes: 8 additions & 1 deletion CurvaLauncher/Models/QueryResultModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@
using System.Windows.Media.Imaging;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CurvaLauncher.Messages;
using CurvaLauncher.Services;
using Microsoft.Extensions.DependencyInjection;

namespace CurvaLauncher.Models;

Expand Down Expand Up @@ -76,7 +80,10 @@ public async Task Invoke()
}
}

//MainWindow mainWindow = App.ServiceProvider.GetRequiredService<MainWindow>();
App.ServiceProvider
.GetRequiredService<IMessenger>()
.Send(SaveQueryMessage.Instance);

App.CloseLauncher();
}

Expand Down
22 changes: 20 additions & 2 deletions CurvaLauncher/ViewModels/MainViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,25 +5,33 @@
using System.Windows.Threading;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CommunityToolkit.Mvvm.Messaging;
using CurvaLauncher.Messages;
using CurvaLauncher.Models;
using CurvaLauncher.Services;
using CurvaLauncher.Utilities;

namespace CurvaLauncher.ViewModels;

public partial class MainViewModel : ObservableObject
public partial class MainViewModel : ObservableObject, IRecipient<SaveQueryMessage>
{
private readonly PluginService _pluginService;
private readonly ConfigService _configService;

public MainViewModel(
PluginService pluginService,
ConfigService configService)
ConfigService configService,
IMessenger messenger)
{
_pluginService = pluginService;
_configService = configService;

messenger.Register(this);
}

[ObservableProperty]
string? _lastInvokedQueryText;

[ObservableProperty]
private string queryText = string.Empty;

Expand Down Expand Up @@ -140,12 +148,22 @@ public void SelectNext()
public void SelectPrev()
{
if (QueryResults.Count == 0)
{
if (_lastInvokedQueryText != null && string.IsNullOrWhiteSpace(QueryText))
QueryText = _lastInvokedQueryText;

return;
}

int newIndex = (SelectedQueryResultIndex - 1) % QueryResults.Count;
if (newIndex == -1)
newIndex = QueryResults.Count - 1;

SelectedQueryResultIndex = newIndex;
}

void IRecipient<SaveQueryMessage>.Receive(SaveQueryMessage message)
{
LastInvokedQueryText = QueryText;
}
}

0 comments on commit b16bced

Please sign in to comment.