Skip to content

Commit

Permalink
feat: 支持子菜单
Browse files Browse the repository at this point in the history
  • Loading branch information
SlimeNull committed Dec 28, 2024
1 parent f13ed7b commit 6893293
Show file tree
Hide file tree
Showing 19 changed files with 284 additions and 131 deletions.
6 changes: 6 additions & 0 deletions src/CurvaLauncher.Common/IAsyncMenuQueryResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CurvaLauncher;

public interface IAsyncMenuQueryResult : IQueryResult
{
public Task<IEnumerable<IQueryResult>> GetMenuItemsAsync(CancellationToken cancellationToken);
}
6 changes: 6 additions & 0 deletions src/CurvaLauncher.Common/ISyncMenuQueryResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
namespace CurvaLauncher;

public interface ISyncMenuQueryResult : IQueryResult
{
public IEnumerable<IQueryResult> GetMenuItems();
}
73 changes: 69 additions & 4 deletions src/CurvaLauncher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -32,16 +32,18 @@

<Window.InputBindings>
<KeyBinding Key="Esc" Command="{Binding ViewModel.EscapeCommand}"/>
<KeyBinding Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
<KeyBinding Key="Up" Command="{Binding ViewModel.SelectPrevCommand}"/>
<KeyBinding Key="Down" Command="{Binding ViewModel.SelectNextCommand}"/>
<KeyBinding Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
<KeyBinding Modifiers="Ctrl" Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
<KeyBinding Modifiers="Alt" Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
<KeyBinding Modifiers="Ctrl+Alt" Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
</Window.InputBindings>

<Window.Resources>
<BooleanToVisibilityConverter x:Key="bool2visibility"/>
<ws:BindingProxy x:Key="WindowProxy"
Data="{Binding}"/>
<ws:BindingProxy x:Key="AppConfigProxy"
Data="{Binding AppConfig}"/>
<ws:BindingProxy x:Key="ViewModelProxy"
Expand Down Expand Up @@ -111,7 +113,6 @@
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ViewModel.ShowQueryResult}" Value="True"/>
<Condition Binding="{Binding ViewModel.InvokeCommand.IsRunning}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
Expand All @@ -128,7 +129,7 @@
</Grid>

<ws:ConditionalControl Condition="{Binding ViewModel.ShowQueryResult}">
<ListView Name="resultBox" Margin="0 5 0 0" MinHeight="0"
<ListView Name="ResultBox" Margin="0 5 0 0" MinHeight="0"
Height="Auto"
MaxHeight="{Binding AppConfig.LauncherResultViewHeight}"
BorderThickness="0"
Expand All @@ -139,7 +140,8 @@
SelectedIndex="{Binding ViewModel.SelectedQueryResultIndex}">
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="SelectionChanged">
<behaviors:InvokeCommandAction Command="{Binding ScrollToSelectedQueryResultCommand}"/>
<behaviors:InvokeCommandAction Command="{Binding ScrollToSelectedItemCommand}"
CommandParameter="{x:Reference Name=ResultBox}"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
<ListView.ItemTemplate>
Expand Down Expand Up @@ -193,6 +195,69 @@
<ContentControl Visibility="{Binding ViewModel.ShowImmediateResults,Converter={StaticResource bool2visibility}}"
Content="{Binding ViewModel.CurrentImmediateResult}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type models_imr:MenuResult}">
<ListView Name="MenuResultBox" Margin="0 5 0 0" MinHeight="0"
Height="Auto"
MaxHeight="{Binding Source={StaticResource AppConfigProxy},Path=Data.LauncherResultViewHeight}"
BorderThickness="0"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
ItemsSource="{Binding Items}" d:ItemsSource="{d:SampleData ItemCount=5}"
SelectedItem="{Binding SelectedItem,Mode=OneWayToSource}"
SelectedIndex="{Binding SelectedIndex}">
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="SelectionChanged">
<behaviors:InvokeCommandAction Command="{Binding Source={StaticResource WindowProxy},Path=Data.ScrollToSelectedItemCommand}"
CommandParameter="{x:Reference Name=MenuResultBox}"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
<ListView.ItemTemplate>
<DataTemplate DataType="models:QueryResultModel">
<Border Padding="{DynamicResource QueryResultPaddingThickness}">
<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="8"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<Border Width="{DynamicResource QueryResultIconSize}"
Height="{DynamicResource QueryResultIconSize}"
CornerRadius="{DynamicResource QueryResultIconCornerRadius}">
<Border.Background>
<ImageBrush Stretch="Uniform"
ImageSource="{Binding Icon}"/>
</Border.Background>
</Border>

<Grid Grid.Column="2">
<Grid.RowDefinitions>
<RowDefinition/>
<RowDefinition/>
</Grid.RowDefinitions>
<TextBlock Text="{Binding Title}"
FontSize="{DynamicResource QueryResultTitleTextSize}"
Foreground="{DynamicResource TextFillColorPrimaryBrush}"/>
<TextBlock Grid.Row="1"
TextTrimming="WordEllipsis"
Text="{Binding Description}"
FontSize="{DynamicResource QueryResultDescriptionTextSize}"
Foreground="{DynamicResource TextFillColorSecondaryBrush}"/>
</Grid>
</Grid>
</Border>
</DataTemplate>
</ListView.ItemTemplate>
<ListView.ItemContainerStyle>
<Style TargetType="{x:Type ListViewItem}" BasedOn="{StaticResource {x:Type ListViewItem}}">
<Setter Property="BorderThickness" Value="0" />
<Setter Property="HorizontalContentAlignment" Value="Stretch"/>
<EventSetter Event="PreviewMouseDown"
Handler="ResultBoxItemMouseDown"/>
</Style>
</ListView.ItemContainerStyle>
</ListView>
</DataTemplate>
<DataTemplate DataType="{x:Type models_imr:DocumentResult}">
<FlowDocumentScrollViewer Document="{Binding Document}"
MinHeight="{Binding Source={StaticResource AppConfigProxy},Path=Data.LauncherResultViewHeight}">
Expand Down
21 changes: 16 additions & 5 deletions src/CurvaLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -61,16 +61,22 @@ private void WindowDeactivated(object sender, EventArgs e)
ViewModel.QueryResults.Clear();
ViewModel.SelectedQueryResult = null;

resultBox.SelectedItem = null;
ResultBox.SelectedItem = null;

App.CloseLauncher();
}


private void QueryBox_PreviewKeyDown(object sender, KeyEventArgs e)
{
if (ViewModel.ImmediateResults.Count != 0)
{
return;
}

if (e.Key == Key.Up &&
string.IsNullOrWhiteSpace(ViewModel.QueryText) &&
!string.IsNullOrWhiteSpace(ViewModel.LastInvokedQueryText) &&
ViewModel.LastInvokedQueryText is string lastInvokedQueryText)
{
SetQueryText(lastInvokedQueryText);
Expand All @@ -79,17 +85,22 @@ private void QueryBox_PreviewKeyDown(object sender, KeyEventArgs e)
}

[RelayCommand]
public void ScrollToSelectedQueryResult()
public void ScrollToSelectedItem(ListView? listView)
{
if (resultBox.SelectedItem is null ||
resultBox.SelectedIndex < 0)
if (listView is null)
{
return;
}

if (listView.SelectedItem is null ||
listView.SelectedIndex < 0)
{
return;
}

try
{
resultBox.ScrollIntoView(resultBox.SelectedItem);
listView.ScrollIntoView(listView.SelectedItem);
}
catch { }
}
Expand Down
10 changes: 5 additions & 5 deletions src/CurvaLauncher/Models/CurvaLauncherPluginInstance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,13 @@

namespace CurvaLauncher.Models;

public partial class CurvaLauncherPluginInstance : ObservableObject
public partial class PluginInstance : ObservableObject
{
public IPlugin Plugin { get; }
public Task InitTask { get; private set; } = Task.CompletedTask;


private CurvaLauncherPluginInstance(IPlugin plugin)
private PluginInstance(IPlugin plugin)
{
Plugin = plugin;
if (plugin is II18nPlugin i18nPlugin)
Expand Down Expand Up @@ -78,7 +78,7 @@ public async IAsyncEnumerable<IQueryResult> QueryAsync(string query)
}
}

public static bool TryCreate(Type type, [NotNullWhen(true)] out CurvaLauncherPluginInstance? curvaLauncherPlugin)
public static bool TryCreate(Type type, [NotNullWhen(true)] out PluginInstance? curvaLauncherPlugin)
{
curvaLauncherPlugin = null;

Expand All @@ -90,9 +90,9 @@ public static bool TryCreate(Type type, [NotNullWhen(true)] out CurvaLauncherPlu
var plugin = Activator.CreateInstance(type, CurvaLauncherContextImpl.Instance);

if (plugin is IAsyncPlugin asyncPlugin)
curvaLauncherPlugin = new CurvaLauncherPluginInstance(asyncPlugin);
curvaLauncherPlugin = new PluginInstance(asyncPlugin);
else if (plugin is ISyncPlugin syncPlugin)
curvaLauncherPlugin = new CurvaLauncherPluginInstance(syncPlugin);
curvaLauncherPlugin = new PluginInstance(syncPlugin);

return curvaLauncherPlugin != null;
}
Expand Down
3 changes: 2 additions & 1 deletion src/CurvaLauncher/Models/ImmediateResults/ImmediateResult.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,11 @@
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using CommunityToolkit.Mvvm.ComponentModel;

namespace CurvaLauncher.Models.ImmediateResults
{
public abstract class ImmediateResult
public abstract class ImmediateResult : ObservableObject
{

}
Expand Down
25 changes: 25 additions & 0 deletions src/CurvaLauncher/Models/ImmediateResults/MenuResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System.Collections.Generic;
using System.Linq;
using CommunityToolkit.Mvvm.ComponentModel;

namespace CurvaLauncher.Models.ImmediateResults
{
public partial class MenuResult : ImmediateResult
{
[ObservableProperty]
private int _selectedIndex;

[ObservableProperty]
private QueryResultModel? _selectedItem;

public IReadOnlyList<QueryResultModel> Items { get; }

public MenuResult(PluginInstance pluginInstance, IReadOnlyList<IQueryResult> items)
{
Items = items
.Select(item => new QueryResultModel(pluginInstance, item))
.OrderByDescending(item => item.Weight)
.ToArray();
}
}
}
14 changes: 0 additions & 14 deletions src/CurvaLauncher/Models/ImmediateResults/SubResultListResult.cs

This file was deleted.

Loading

0 comments on commit 6893293

Please sign in to comment.