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 ca5223d commit f13ed7b
Show file tree
Hide file tree
Showing 27 changed files with 383 additions and 170 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CurvaLauncher;

public interface IAsyncQueryResult : IQueryResult
public interface IAsyncActionQueryResult : IQueryResult
{
public Task InvokeAsync(CancellationToken cancellationToken);
}
8 changes: 8 additions & 0 deletions src/CurvaLauncher.Common/IAsyncDocumentQueryResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Windows.Documents;

namespace CurvaLauncher;

public interface IAsyncDocumentQueryResult : IQueryResult
{
public Task<FlowDocument> GenerateDocumentAsync(CancellationToken cancellationToken);
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
namespace CurvaLauncher;

public interface ISyncQueryResult : IQueryResult
public interface ISyncActionQueryResult : IQueryResult
{
public void Invoke();
}
8 changes: 8 additions & 0 deletions src/CurvaLauncher.Common/ISyncDocumentQueryResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
using System.Windows.Documents;

namespace CurvaLauncher;

public interface ISyncDocumentQueryResult : IQueryResult
{
public FlowDocument GenerateDocument();
}
6 changes: 4 additions & 2 deletions src/CurvaLauncher/CurvaLauncher.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<ApplicationManifest>app.manifest</ApplicationManifest>
<ApplicationIcon>Assets\Icon.ico</ApplicationIcon>

<Version>0.6.1-beta</Version>
<Version>0.7.1-beta</Version>

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
<EnableWindowsTargeting>true</EnableWindowsTargeting>
Expand All @@ -26,7 +26,9 @@
<ItemGroup>
<PackageReference Include="CommunityToolkit.Mvvm" Version="8.2.1" />
<PackageReference Include="EleCho.GlobalHotkey.Windows" Version="1.0.2" />
<PackageReference Include="EleCho.WpfSuite" Version="0.8.1" />
<PackageReference Include="EleCho.WpfSuite.Common" Version="0.9.3" />
<PackageReference Include="EleCho.WpfSuite.Controls" Version="0.9.14" />
<PackageReference Include="EleCho.WpfSuite.Layouts" Version="0.9.0.1" />
<PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="7.0.0" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" Version="1.1.39" />
<PackageReference Include="SharpVectors.Wpf" Version="1.8.2" />
Expand Down
170 changes: 98 additions & 72 deletions src/CurvaLauncher/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
xmlns:behaviors="http://schemas.microsoft.com/xaml/behaviors"
xmlns:utils="clr-namespace:CurvaLauncher.Utilities"
xmlns:models="clr-namespace:CurvaLauncher.Models"
xmlns:models_imr="clr-namespace:CurvaLauncher.Models.ImmediateResults"
xmlns:ws="https://schemas.elecho.dev/wpfsuite"
xmlns:tb="http://www.hardcodet.net/taskbar"
xmlns:ui="http://schemas.lepo.co/wpfui/2022/xaml"
Expand All @@ -30,7 +31,7 @@
Deactivated="WindowDeactivated">

<Window.InputBindings>
<KeyBinding Key="Esc" Command="{x:Static local:App.CloseLauncherCommand}"/>
<KeyBinding Key="Esc" Command="{Binding ViewModel.EscapeCommand}"/>
<KeyBinding Key="Up" Command="{Binding ViewModel.SelectPrevCommand}"/>
<KeyBinding Key="Down" Command="{Binding ViewModel.SelectNextCommand}"/>
<KeyBinding Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
Expand All @@ -41,6 +42,10 @@

<Window.Resources>
<BooleanToVisibilityConverter x:Key="bool2visibility"/>
<ws:BindingProxy x:Key="AppConfigProxy"
Data="{Binding AppConfig}"/>
<ws:BindingProxy x:Key="ViewModelProxy"
Data="{Binding ViewModel}"/>
</Window.Resources>

<Grid>
Expand All @@ -52,35 +57,52 @@
<ColumnDefinition Width="Auto"/>
</Grid.ColumnDefinitions>

<ui:TextBox Name="QueryBox"
Text="{Binding ViewModel.QueryText,UpdateSourceTrigger=PropertyChanged}"
FontSize="{DynamicResource QueryBoxTextSize}"
FocusVisualStyle="{x:Null}"
ClearButtonEnabled="True"
PlaceholderText="Hello CurvaLauncher"
PreviewKeyDown="QueryBox_PreviewKeyDown">
<ui:TextBox.InputBindings>
<KeyBinding Key="Up" Command="{Binding ViewModel.SelectPrevCommand}"/>
<KeyBinding Key="Down" Command="{Binding ViewModel.SelectNextCommand}"/>
<KeyBinding Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
</ui:TextBox.InputBindings>
<ui:TextBox.ContextMenu>
<ContextMenu FontSize="14" HasDropShadow="True"
BorderBrush="#80777777">
<ui:MenuItem Header="{DynamicResource StrCut}" Command="ApplicationCommands.Cut"/>
<ui:MenuItem Header="{DynamicResource StrCopy}" Command="ApplicationCommands.Copy"/>
<ui:MenuItem Header="{DynamicResource StrPaste}" Command="ApplicationCommands.Paste"/>
<Separator/>
<ui:MenuItem Header="{DynamicResource StrSettings}" Command="{x:Static local:App.ShowLauncherSettingsCommand}"/>
<ui:MenuItem Header="{DynamicResource StrExit}" Command="{x:Static local:App.ShutdownCommand}"/>
</ContextMenu>
</ui:TextBox.ContextMenu>
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="TextChanged">
<behaviors:InvokeCommandAction Command="{Binding ViewModel.QueryCommand}"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
</ui:TextBox>
<Grid>
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>

<ui:Button Content="&lt;"
VerticalAlignment="Stretch"
Width="{Binding RelativeSource={RelativeSource Self},Path=ActualHeight}"
Margin="0 0 4 0"
FontSize="20"
FontWeight="Bold"
Visibility="{Binding ViewModel.ShowImmediateResults,Converter={StaticResource bool2visibility}}"
Command="{Binding ViewModel.EscapeCommand}"/>

<ui:TextBox Name="QueryBox"
Grid.Column="1"
Text="{Binding ViewModel.QueryText,UpdateSourceTrigger=PropertyChanged}"
FontSize="{DynamicResource QueryBoxTextSize}"
FocusVisualStyle="{x:Null}"
ClearButtonEnabled="True"
PlaceholderText="Hello CurvaLauncher"
PreviewKeyDown="QueryBox_PreviewKeyDown">
<ui:TextBox.InputBindings>
<KeyBinding Key="Up" Command="{Binding ViewModel.SelectPrevCommand}"/>
<KeyBinding Key="Down" Command="{Binding ViewModel.SelectNextCommand}"/>
<KeyBinding Key="Return" Command="{Binding ViewModel.InvokeSelectedCommand}"/>
</ui:TextBox.InputBindings>
<ui:TextBox.ContextMenu>
<ContextMenu FontSize="14" HasDropShadow="True"
BorderBrush="#80777777">
<ui:MenuItem Header="{DynamicResource StrCut}" Command="ApplicationCommands.Cut"/>
<ui:MenuItem Header="{DynamicResource StrCopy}" Command="ApplicationCommands.Copy"/>
<ui:MenuItem Header="{DynamicResource StrPaste}" Command="ApplicationCommands.Paste"/>
<Separator/>
<ui:MenuItem Header="{DynamicResource StrSettings}" Command="{x:Static local:App.ShowLauncherSettingsCommand}"/>
<ui:MenuItem Header="{DynamicResource StrExit}" Command="{x:Static local:App.ShutdownCommand}"/>
</ContextMenu>
</ui:TextBox.ContextMenu>
<behaviors:Interaction.Triggers>
<behaviors:EventTrigger EventName="TextChanged">
<behaviors:InvokeCommandAction Command="{Binding ViewModel.QueryCommand}"/>
</behaviors:EventTrigger>
</behaviors:Interaction.Triggers>
</ui:TextBox>
</Grid>

<ui:ProgressRing Grid.Column="1" Margin="7 0 0 0" IsIndeterminate="True">
<ui:ProgressRing.Style>
Expand All @@ -89,8 +111,8 @@
<Style.Triggers>
<MultiDataTrigger>
<MultiDataTrigger.Conditions>
<Condition Binding="{Binding ViewModel.HasQueryResult}" Value="True"/>
<Condition Binding="{Binding ViewModel.SelectedQueryResult.InvokeCommand.IsRunning}" Value="True"/>
<Condition Binding="{Binding ViewModel.ShowQueryResult}" Value="True"/>
<Condition Binding="{Binding ViewModel.InvokeCommand.IsRunning}" Value="True"/>
</MultiDataTrigger.Conditions>
<MultiDataTrigger.Setters>
<Setter Property="Visibility" Value="Visible"/>
Expand All @@ -104,7 +126,8 @@
</ui:ProgressRing.LayoutTransform>
</ui:ProgressRing>
</Grid>
<ws:ConditionalControl Condition="{Binding ViewModel.HasQueryResult}">

<ws:ConditionalControl Condition="{Binding ViewModel.ShowQueryResult}">
<ListView Name="resultBox" Margin="0 5 0 0" MinHeight="0"
Height="Auto"
MaxHeight="{Binding AppConfig.LauncherResultViewHeight}"
Expand All @@ -122,59 +145,62 @@
<ListView.ItemTemplate>
<DataTemplate DataType="models:QueryResultModel">
<Border Padding="{DynamicResource QueryResultPaddingThickness}">
<Border.Resources>
<ws:BindingProxy x:Key="Proxy" Data="{Binding}"/>
</Border.Resources>
<Button Command="{Binding InvokeCommand}">
<Button.Template>
<ControlTemplate TargetType="Button">
<ContentPresenter Content="{TemplateBinding Content}"/>
</ControlTemplate>
</Button.Template>

<Grid VerticalAlignment="Stretch" HorizontalAlignment="Stretch">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto"/>
<ColumnDefinition Width="8"/>
<ColumnDefinition/>
</Grid.ColumnDefinitions>
<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 Source={StaticResource Proxy}, Path=Data.Icon}"/>
</Border.Background>
</Border>
<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 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>
</Button>
</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>
</ws:ConditionalControl>

<ContentControl Visibility="{Binding ViewModel.ShowImmediateResults,Converter={StaticResource bool2visibility}}"
Content="{Binding ViewModel.CurrentImmediateResult}">
<ContentControl.Resources>
<DataTemplate DataType="{x:Type models_imr:DocumentResult}">
<FlowDocumentScrollViewer Document="{Binding Document}"
MinHeight="{Binding Source={StaticResource AppConfigProxy},Path=Data.LauncherResultViewHeight}">

</FlowDocumentScrollViewer>
</DataTemplate>
</ContentControl.Resources>
</ContentControl>
</StackPanel>
</Border>

Expand Down
31 changes: 30 additions & 1 deletion src/CurvaLauncher/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using CommunityToolkit.Mvvm.ComponentModel;
using CommunityToolkit.Mvvm.Input;
using CurvaLauncher.Models;
using CurvaLauncher.Services;
using CurvaLauncher.ViewModels;
using Wpf.Ui.Appearance;
Expand Down Expand Up @@ -47,6 +49,7 @@ private void WindowClosing(object sender, System.ComponentModel.CancelEventArgs
private void WindowActivated(object sender, EventArgs e)
{
FocusManager.SetFocusedElement(this, QueryBox);
ViewModel.QueryCommand.Execute(null);
Focus();
}

Expand All @@ -55,6 +58,11 @@ private void WindowDeactivated(object sender, EventArgs e)
if (AppConfig.KeepLauncherWhenFocusLost)
return;

ViewModel.QueryResults.Clear();
ViewModel.SelectedQueryResult = null;

resultBox.SelectedItem = null;

App.CloseLauncher();
}

Expand All @@ -73,7 +81,17 @@ private void QueryBox_PreviewKeyDown(object sender, KeyEventArgs e)
[RelayCommand]
public void ScrollToSelectedQueryResult()
{
resultBox.ScrollIntoView(resultBox.SelectedItem);
if (resultBox.SelectedItem is null ||
resultBox.SelectedIndex < 0)
{
return;
}

try
{
resultBox.ScrollIntoView(resultBox.SelectedItem);
}
catch { }
}

public void SetQueryText(string text)
Expand All @@ -82,4 +100,15 @@ public void SetQueryText(string text)
QueryBox.SelectionStart = text.Length;
QueryBox.SelectionLength = 0;
}

private void ResultBoxItemMouseDown(object sender, MouseButtonEventArgs e)
{
if (sender is not ListBoxItem lbi ||
lbi.Content is not QueryResultModel queryResult)
{
return;
}

ViewModel.InvokeCommand.Execute(queryResult);
}
}
14 changes: 14 additions & 0 deletions src/CurvaLauncher/Models/ImmediateResults/DocumentResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
using System.Windows.Documents;

namespace CurvaLauncher.Models.ImmediateResults
{
public class DocumentResult : ImmediateResult
{
public FlowDocument Document { get; }

public DocumentResult(FlowDocument document)
{
Document = document;
}
}
}
12 changes: 12 additions & 0 deletions src/CurvaLauncher/Models/ImmediateResults/ImmediateResult.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

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

}
}
Loading

0 comments on commit f13ed7b

Please sign in to comment.