mirror of
https://fastgit.cc/github.com/sourcegit-scm/sourcegit
synced 2026-04-23 10:22:13 +08:00
feature: add Open File command palette to quick open repo's file with default editor
Signed-off-by: leo <longshuang@msn.cn>
This commit is contained in:
@@ -542,6 +542,7 @@
|
||||
<x:String x:Key="Text.Open" xml:space="preserve">Open</x:String>
|
||||
<x:String x:Key="Text.Open.SystemDefaultEditor" xml:space="preserve">Default Editor (System)</x:String>
|
||||
<x:String x:Key="Text.OpenAppDataDir" xml:space="preserve">Open Data Storage Directory</x:String>
|
||||
<x:String x:Key="Text.OpenFile" xml:space="preserve">Open File</x:String>
|
||||
<x:String x:Key="Text.OpenInExternalMergeTool" xml:space="preserve">Open in Merge Tool</x:String>
|
||||
<x:String x:Key="Text.Optional" xml:space="preserve">Optional.</x:String>
|
||||
<x:String x:Key="Text.PageTabBar.New" xml:space="preserve">Create New Tab</x:String>
|
||||
|
||||
@@ -546,6 +546,7 @@
|
||||
<x:String x:Key="Text.Open" xml:space="preserve">打开</x:String>
|
||||
<x:String x:Key="Text.Open.SystemDefaultEditor" xml:space="preserve">系统默认编辑器</x:String>
|
||||
<x:String x:Key="Text.OpenAppDataDir" xml:space="preserve">浏览应用数据目录</x:String>
|
||||
<x:String x:Key="Text.OpenFile" xml:space="preserve">打开文件</x:String>
|
||||
<x:String x:Key="Text.OpenInExternalMergeTool" xml:space="preserve">使用外部对比工具查看</x:String>
|
||||
<x:String x:Key="Text.Optional" xml:space="preserve">选填。</x:String>
|
||||
<x:String x:Key="Text.PageTabBar.New" xml:space="preserve">新建空白页</x:String>
|
||||
|
||||
@@ -546,6 +546,7 @@
|
||||
<x:String x:Key="Text.Open" xml:space="preserve">開啟</x:String>
|
||||
<x:String x:Key="Text.Open.SystemDefaultEditor" xml:space="preserve">系統預設編輯器</x:String>
|
||||
<x:String x:Key="Text.OpenAppDataDir" xml:space="preserve">瀏覽程式資料目錄</x:String>
|
||||
<x:String x:Key="Text.OpenFile" xml:space="preserve">開啟檔案</x:String>
|
||||
<x:String x:Key="Text.OpenInExternalMergeTool" xml:space="preserve">使用外部比對工具檢視</x:String>
|
||||
<x:String x:Key="Text.Optional" xml:space="preserve">選填。</x:String>
|
||||
<x:String x:Key="Text.PageTabBar.New" xml:space="preserve">新增分頁</x:String>
|
||||
|
||||
111
src/ViewModels/OpenFileCommandPalette.cs
Normal file
111
src/ViewModels/OpenFileCommandPalette.cs
Normal file
@@ -0,0 +1,111 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Threading.Tasks;
|
||||
using Avalonia.Threading;
|
||||
|
||||
namespace SourceGit.ViewModels
|
||||
{
|
||||
public class OpenFileCommandPalette : ICommandPalette
|
||||
{
|
||||
public bool IsLoading
|
||||
{
|
||||
get => _isLoading;
|
||||
private set => SetProperty(ref _isLoading, value);
|
||||
}
|
||||
|
||||
public List<string> VisibleFiles
|
||||
{
|
||||
get => _visibleFiles;
|
||||
private set => SetProperty(ref _visibleFiles, value);
|
||||
}
|
||||
|
||||
public string Filter
|
||||
{
|
||||
get => _filter;
|
||||
set
|
||||
{
|
||||
if (SetProperty(ref _filter, value))
|
||||
UpdateVisible();
|
||||
}
|
||||
}
|
||||
|
||||
public string SelectedFile
|
||||
{
|
||||
get => _selectedFile;
|
||||
set => SetProperty(ref _selectedFile, value);
|
||||
}
|
||||
|
||||
public OpenFileCommandPalette(Launcher launcher, string repo)
|
||||
{
|
||||
_launcher = launcher;
|
||||
_repo = repo;
|
||||
_isLoading = true;
|
||||
|
||||
Task.Run(async () =>
|
||||
{
|
||||
var files = await new Commands.QueryRevisionFileNames(_repo, "HEAD")
|
||||
.GetResultAsync()
|
||||
.ConfigureAwait(false);
|
||||
|
||||
Dispatcher.UIThread.Post(() =>
|
||||
{
|
||||
IsLoading = false;
|
||||
_repoFiles = files;
|
||||
UpdateVisible();
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
public override void Cleanup()
|
||||
{
|
||||
_launcher = null;
|
||||
_repo = null;
|
||||
_repoFiles.Clear();
|
||||
_filter = null;
|
||||
_visibleFiles.Clear();
|
||||
_selectedFile = null;
|
||||
}
|
||||
|
||||
public void ClearFilter()
|
||||
{
|
||||
Filter = string.Empty;
|
||||
}
|
||||
|
||||
public void Launch()
|
||||
{
|
||||
if (!string.IsNullOrEmpty(_selectedFile))
|
||||
Native.OS.OpenWithDefaultEditor(Native.OS.GetAbsPath(_repo, _selectedFile));
|
||||
|
||||
_launcher.CancelCommandPalette();
|
||||
}
|
||||
|
||||
private void UpdateVisible()
|
||||
{
|
||||
if (_repoFiles is { Count: > 0 })
|
||||
{
|
||||
if (string.IsNullOrEmpty(_filter))
|
||||
{
|
||||
VisibleFiles = _repoFiles;
|
||||
}
|
||||
else
|
||||
{
|
||||
var visible = new List<string>();
|
||||
foreach (var f in _repoFiles)
|
||||
{
|
||||
if (f.Contains(_filter, StringComparison.OrdinalIgnoreCase))
|
||||
visible.Add(f);
|
||||
}
|
||||
VisibleFiles = visible;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private Launcher _launcher = null;
|
||||
private string _repo = null;
|
||||
private bool _isLoading = false;
|
||||
private List<string> _repoFiles = null;
|
||||
private string _filter = string.Empty;
|
||||
private List<string> _visibleFiles = [];
|
||||
private string _selectedFile = null;
|
||||
}
|
||||
}
|
||||
@@ -45,6 +45,12 @@ namespace SourceGit.ViewModels
|
||||
_launcher = launcher;
|
||||
_repo = repo;
|
||||
|
||||
_cmds.Add(new("OpenFile", () =>
|
||||
{
|
||||
var sub = new OpenFileCommandPalette(_launcher, _repo.FullPath);
|
||||
_launcher.OpenCommandPalette(sub);
|
||||
}));
|
||||
|
||||
_cmds.Add(new("FileHistory", () =>
|
||||
{
|
||||
var sub = new FileHistoryCommandPalette(_launcher, _repo.FullPath);
|
||||
|
||||
115
src/Views/OpenFileCommandPalette.axaml
Normal file
115
src/Views/OpenFileCommandPalette.axaml
Normal file
@@ -0,0 +1,115 @@
|
||||
<UserControl xmlns="https://github.com/avaloniaui"
|
||||
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
|
||||
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
|
||||
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
|
||||
xmlns:vm="using:SourceGit.ViewModels"
|
||||
xmlns:v="using:SourceGit.Views"
|
||||
xmlns:c="using:SourceGit.Converters"
|
||||
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
|
||||
x:Class="SourceGit.Views.OpenFileCommandPalette"
|
||||
x:DataType="vm:OpenFileCommandPalette">
|
||||
<Grid RowDefinitions="Auto,Auto">
|
||||
<v:RepositoryCommandPaletteTextBox Grid.Row="0"
|
||||
x:Name="FilterTextBox"
|
||||
Height="24"
|
||||
Margin="4,8,4,0"
|
||||
BorderThickness="1"
|
||||
CornerRadius="12"
|
||||
Text="{Binding Filter, Mode=TwoWay}"
|
||||
BorderBrush="{DynamicResource Brush.Border2}"
|
||||
VerticalContentAlignment="Center">
|
||||
<TextBox.InnerLeftContent>
|
||||
<StackPanel Orientation="Horizontal">
|
||||
<Path Width="14" Height="14"
|
||||
Margin="6,0,0,0"
|
||||
Fill="{DynamicResource Brush.FG2}"
|
||||
Data="{StaticResource Icons.Search}"/>
|
||||
<Border BorderThickness="0"
|
||||
Background="{DynamicResource Brush.Badge}"
|
||||
Height="18"
|
||||
CornerRadius="4"
|
||||
Margin="4,0,0,0" Padding="4,0">
|
||||
<TextBlock Text="{DynamicResource Text.OpenFile}"
|
||||
Foreground="Black"
|
||||
FontWeight="Bold"/>
|
||||
</Border>
|
||||
</StackPanel>
|
||||
</TextBox.InnerLeftContent>
|
||||
|
||||
<TextBox.InnerRightContent>
|
||||
<Button Classes="icon_button"
|
||||
Width="16"
|
||||
Margin="0,0,6,0"
|
||||
Command="{Binding ClearFilter}"
|
||||
IsVisible="{Binding Filter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
|
||||
HorizontalAlignment="Right">
|
||||
<Path Width="14" Height="14"
|
||||
Margin="0,1,0,0"
|
||||
Fill="{DynamicResource Brush.FG1}"
|
||||
Data="{StaticResource Icons.Clear}"/>
|
||||
</Button>
|
||||
</TextBox.InnerRightContent>
|
||||
</v:RepositoryCommandPaletteTextBox>
|
||||
|
||||
<ListBox Grid.Row="1"
|
||||
x:Name="FileListBox"
|
||||
MaxHeight="250"
|
||||
Margin="4,8,4,0"
|
||||
BorderThickness="0"
|
||||
SelectionMode="Single"
|
||||
Background="Transparent"
|
||||
Focusable="True"
|
||||
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
|
||||
ScrollViewer.VerticalScrollBarVisibility="Auto"
|
||||
ItemsSource="{Binding VisibleFiles, Mode=OneWay}"
|
||||
SelectedItem="{Binding SelectedFile, Mode=TwoWay}"
|
||||
IsVisible="{Binding VisibleFiles, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
|
||||
<ListBox.Styles>
|
||||
<Style Selector="ListBoxItem">
|
||||
<Setter Property="Padding" Value="8,0"/>
|
||||
<Setter Property="MinHeight" Value="26"/>
|
||||
<Setter Property="CornerRadius" Value="4"/>
|
||||
</Style>
|
||||
|
||||
<Style Selector="ListBox">
|
||||
<Setter Property="FocusAdorner">
|
||||
<FocusAdornerTemplate>
|
||||
<Grid/>
|
||||
</FocusAdornerTemplate>
|
||||
</Setter>
|
||||
</Style>
|
||||
</ListBox.Styles>
|
||||
|
||||
<ListBox.ItemsPanel>
|
||||
<ItemsPanelTemplate>
|
||||
<VirtualizingStackPanel Orientation="Vertical"/>
|
||||
</ItemsPanelTemplate>
|
||||
</ListBox.ItemsPanel>
|
||||
|
||||
<ListBox.ItemTemplate>
|
||||
<DataTemplate DataType="x:String">
|
||||
<Grid ColumnDefinitions="Auto,*" Background="Transparent" Tapped="OnItemTapped">
|
||||
<Path Grid.Column="0"
|
||||
Width="12" Height="12"
|
||||
Data="{StaticResource Icons.File}"
|
||||
IsHitTestVisible="False"/>
|
||||
<TextBlock Grid.Column="1"
|
||||
Margin="4,0,0,0"
|
||||
VerticalAlignment="Center"
|
||||
IsHitTestVisible="False">
|
||||
<Run Text="{Binding Converter={x:Static c:PathConverters.PureFileName}, Mode=OneWay}"/>
|
||||
<Run Text=" "/>
|
||||
<Run Text="{Binding Converter={x:Static c:PathConverters.PureDirectoryName}, Mode=OneWay}" Foreground="{DynamicResource Brush.FG2}"/>
|
||||
</TextBlock>
|
||||
</Grid>
|
||||
</DataTemplate>
|
||||
</ListBox.ItemTemplate>
|
||||
</ListBox>
|
||||
|
||||
<v:LoadingIcon Grid.Row="1"
|
||||
Width="48" Height="48"
|
||||
Margin="0,12,0,8"
|
||||
HorizontalAlignment="Center" VerticalAlignment="Center"
|
||||
IsVisible="{Binding IsLoading}"/>
|
||||
</Grid>
|
||||
</UserControl>
|
||||
66
src/Views/OpenFileCommandPalette.axaml.cs
Normal file
66
src/Views/OpenFileCommandPalette.axaml.cs
Normal file
@@ -0,0 +1,66 @@
|
||||
using Avalonia.Controls;
|
||||
using Avalonia.Input;
|
||||
|
||||
namespace SourceGit.Views
|
||||
{
|
||||
public partial class OpenFileCommandPalette : UserControl
|
||||
{
|
||||
public OpenFileCommandPalette()
|
||||
{
|
||||
InitializeComponent();
|
||||
}
|
||||
|
||||
protected override void OnKeyDown(KeyEventArgs e)
|
||||
{
|
||||
base.OnKeyDown(e);
|
||||
|
||||
if (DataContext is not ViewModels.OpenFileCommandPalette vm)
|
||||
return;
|
||||
|
||||
if (e.Key == Key.Enter)
|
||||
{
|
||||
vm.Launch();
|
||||
e.Handled = true;
|
||||
}
|
||||
else if (e.Key == Key.Up)
|
||||
{
|
||||
if (FileListBox.IsKeyboardFocusWithin)
|
||||
{
|
||||
FilterTextBox.Focus(NavigationMethod.Directional);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
else if (e.Key == Key.Down || e.Key == Key.Tab)
|
||||
{
|
||||
if (FilterTextBox.IsKeyboardFocusWithin)
|
||||
{
|
||||
if (vm.VisibleFiles.Count > 0)
|
||||
{
|
||||
FileListBox.Focus(NavigationMethod.Directional);
|
||||
vm.SelectedFile = vm.VisibleFiles[0];
|
||||
}
|
||||
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
|
||||
if (FileListBox.IsKeyboardFocusWithin && e.Key == Key.Tab)
|
||||
{
|
||||
FilterTextBox.Focus(NavigationMethod.Directional);
|
||||
e.Handled = true;
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void OnItemTapped(object sender, TappedEventArgs e)
|
||||
{
|
||||
if (DataContext is ViewModels.OpenFileCommandPalette vm)
|
||||
{
|
||||
vm.Launch();
|
||||
e.Handled = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user