Skip to content

Commit 52160e1

Browse files
committed
feature: add Open File command palette to quick open repo's file with default editor
Signed-off-by: leo <[email protected]>
1 parent 795f031 commit 52160e1

File tree

7 files changed

+301
-0
lines changed

7 files changed

+301
-0
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -542,6 +542,7 @@
542542
<x:String x:Key="Text.Open" xml:space="preserve">Open</x:String>
543543
<x:String x:Key="Text.Open.SystemDefaultEditor" xml:space="preserve">Default Editor (System)</x:String>
544544
<x:String x:Key="Text.OpenAppDataDir" xml:space="preserve">Open Data Storage Directory</x:String>
545+
<x:String x:Key="Text.OpenFile" xml:space="preserve">Open File</x:String>
545546
<x:String x:Key="Text.OpenInExternalMergeTool" xml:space="preserve">Open in Merge Tool</x:String>
546547
<x:String x:Key="Text.Optional" xml:space="preserve">Optional.</x:String>
547548
<x:String x:Key="Text.PageTabBar.New" xml:space="preserve">Create New Tab</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
<x:String x:Key="Text.Open" xml:space="preserve">打开</x:String>
547547
<x:String x:Key="Text.Open.SystemDefaultEditor" xml:space="preserve">系统默认编辑器</x:String>
548548
<x:String x:Key="Text.OpenAppDataDir" xml:space="preserve">浏览应用数据目录</x:String>
549+
<x:String x:Key="Text.OpenFile" xml:space="preserve">打开文件</x:String>
549550
<x:String x:Key="Text.OpenInExternalMergeTool" xml:space="preserve">使用外部对比工具查看</x:String>
550551
<x:String x:Key="Text.Optional" xml:space="preserve">选填。</x:String>
551552
<x:String x:Key="Text.PageTabBar.New" xml:space="preserve">新建空白页</x:String>

src/Resources/Locales/zh_TW.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -546,6 +546,7 @@
546546
<x:String x:Key="Text.Open" xml:space="preserve">開啟</x:String>
547547
<x:String x:Key="Text.Open.SystemDefaultEditor" xml:space="preserve">系統預設編輯器</x:String>
548548
<x:String x:Key="Text.OpenAppDataDir" xml:space="preserve">瀏覽程式資料目錄</x:String>
549+
<x:String x:Key="Text.OpenFile" xml:space="preserve">開啟檔案</x:String>
549550
<x:String x:Key="Text.OpenInExternalMergeTool" xml:space="preserve">使用外部比對工具檢視</x:String>
550551
<x:String x:Key="Text.Optional" xml:space="preserve">選填。</x:String>
551552
<x:String x:Key="Text.PageTabBar.New" xml:space="preserve">新增分頁</x:String>
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Threading.Tasks;
4+
using Avalonia.Threading;
5+
6+
namespace SourceGit.ViewModels
7+
{
8+
public class OpenFileCommandPalette : ICommandPalette
9+
{
10+
public bool IsLoading
11+
{
12+
get => _isLoading;
13+
private set => SetProperty(ref _isLoading, value);
14+
}
15+
16+
public List<string> VisibleFiles
17+
{
18+
get => _visibleFiles;
19+
private set => SetProperty(ref _visibleFiles, value);
20+
}
21+
22+
public string Filter
23+
{
24+
get => _filter;
25+
set
26+
{
27+
if (SetProperty(ref _filter, value))
28+
UpdateVisible();
29+
}
30+
}
31+
32+
public string SelectedFile
33+
{
34+
get => _selectedFile;
35+
set => SetProperty(ref _selectedFile, value);
36+
}
37+
38+
public OpenFileCommandPalette(Launcher launcher, string repo)
39+
{
40+
_launcher = launcher;
41+
_repo = repo;
42+
_isLoading = true;
43+
44+
Task.Run(async () =>
45+
{
46+
var files = await new Commands.QueryRevisionFileNames(_repo, "HEAD")
47+
.GetResultAsync()
48+
.ConfigureAwait(false);
49+
50+
Dispatcher.UIThread.Post(() =>
51+
{
52+
IsLoading = false;
53+
_repoFiles = files;
54+
UpdateVisible();
55+
});
56+
});
57+
}
58+
59+
public override void Cleanup()
60+
{
61+
_launcher = null;
62+
_repo = null;
63+
_repoFiles.Clear();
64+
_filter = null;
65+
_visibleFiles.Clear();
66+
_selectedFile = null;
67+
}
68+
69+
public void ClearFilter()
70+
{
71+
Filter = string.Empty;
72+
}
73+
74+
public void Launch()
75+
{
76+
if (!string.IsNullOrEmpty(_selectedFile))
77+
Native.OS.OpenWithDefaultEditor(Native.OS.GetAbsPath(_repo, _selectedFile));
78+
79+
_launcher.CancelCommandPalette();
80+
}
81+
82+
private void UpdateVisible()
83+
{
84+
if (_repoFiles is { Count: > 0 })
85+
{
86+
if (string.IsNullOrEmpty(_filter))
87+
{
88+
VisibleFiles = _repoFiles;
89+
}
90+
else
91+
{
92+
var visible = new List<string>();
93+
foreach (var f in _repoFiles)
94+
{
95+
if (f.Contains(_filter, StringComparison.OrdinalIgnoreCase))
96+
visible.Add(f);
97+
}
98+
VisibleFiles = visible;
99+
}
100+
}
101+
}
102+
103+
private Launcher _launcher = null;
104+
private string _repo = null;
105+
private bool _isLoading = false;
106+
private List<string> _repoFiles = null;
107+
private string _filter = string.Empty;
108+
private List<string> _visibleFiles = [];
109+
private string _selectedFile = null;
110+
}
111+
}

src/ViewModels/RepositoryCommandPalette.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,12 @@ public RepositoryCommandPalette(Launcher launcher, Repository repo)
4545
_launcher = launcher;
4646
_repo = repo;
4747

48+
_cmds.Add(new("OpenFile", () =>
49+
{
50+
var sub = new OpenFileCommandPalette(_launcher, _repo.FullPath);
51+
_launcher.OpenCommandPalette(sub);
52+
}));
53+
4854
_cmds.Add(new("FileHistory", () =>
4955
{
5056
var sub = new FileHistoryCommandPalette(_launcher, _repo.FullPath);
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
<UserControl xmlns="https://github.com/avaloniaui"
2+
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
3+
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
4+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
5+
xmlns:vm="using:SourceGit.ViewModels"
6+
xmlns:v="using:SourceGit.Views"
7+
xmlns:c="using:SourceGit.Converters"
8+
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
9+
x:Class="SourceGit.Views.OpenFileCommandPalette"
10+
x:DataType="vm:OpenFileCommandPalette">
11+
<Grid RowDefinitions="Auto,Auto">
12+
<v:RepositoryCommandPaletteTextBox Grid.Row="0"
13+
x:Name="FilterTextBox"
14+
Height="24"
15+
Margin="4,8,4,0"
16+
BorderThickness="1"
17+
CornerRadius="12"
18+
Text="{Binding Filter, Mode=TwoWay}"
19+
BorderBrush="{DynamicResource Brush.Border2}"
20+
VerticalContentAlignment="Center">
21+
<TextBox.InnerLeftContent>
22+
<StackPanel Orientation="Horizontal">
23+
<Path Width="14" Height="14"
24+
Margin="6,0,0,0"
25+
Fill="{DynamicResource Brush.FG2}"
26+
Data="{StaticResource Icons.Search}"/>
27+
<Border BorderThickness="0"
28+
Background="{DynamicResource Brush.Badge}"
29+
Height="18"
30+
CornerRadius="4"
31+
Margin="4,0,0,0" Padding="4,0">
32+
<TextBlock Text="{DynamicResource Text.OpenFile}"
33+
Foreground="Black"
34+
FontWeight="Bold"/>
35+
</Border>
36+
</StackPanel>
37+
</TextBox.InnerLeftContent>
38+
39+
<TextBox.InnerRightContent>
40+
<Button Classes="icon_button"
41+
Width="16"
42+
Margin="0,0,6,0"
43+
Command="{Binding ClearFilter}"
44+
IsVisible="{Binding Filter, Converter={x:Static StringConverters.IsNotNullOrEmpty}}"
45+
HorizontalAlignment="Right">
46+
<Path Width="14" Height="14"
47+
Margin="0,1,0,0"
48+
Fill="{DynamicResource Brush.FG1}"
49+
Data="{StaticResource Icons.Clear}"/>
50+
</Button>
51+
</TextBox.InnerRightContent>
52+
</v:RepositoryCommandPaletteTextBox>
53+
54+
<ListBox Grid.Row="1"
55+
x:Name="FileListBox"
56+
MaxHeight="250"
57+
Margin="4,8,4,0"
58+
BorderThickness="0"
59+
SelectionMode="Single"
60+
Background="Transparent"
61+
Focusable="True"
62+
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
63+
ScrollViewer.VerticalScrollBarVisibility="Auto"
64+
ItemsSource="{Binding VisibleFiles, Mode=OneWay}"
65+
SelectedItem="{Binding SelectedFile, Mode=TwoWay}"
66+
IsVisible="{Binding VisibleFiles, Converter={x:Static c:ListConverters.IsNotNullOrEmpty}}">
67+
<ListBox.Styles>
68+
<Style Selector="ListBoxItem">
69+
<Setter Property="Padding" Value="8,0"/>
70+
<Setter Property="MinHeight" Value="26"/>
71+
<Setter Property="CornerRadius" Value="4"/>
72+
</Style>
73+
74+
<Style Selector="ListBox">
75+
<Setter Property="FocusAdorner">
76+
<FocusAdornerTemplate>
77+
<Grid/>
78+
</FocusAdornerTemplate>
79+
</Setter>
80+
</Style>
81+
</ListBox.Styles>
82+
83+
<ListBox.ItemsPanel>
84+
<ItemsPanelTemplate>
85+
<VirtualizingStackPanel Orientation="Vertical"/>
86+
</ItemsPanelTemplate>
87+
</ListBox.ItemsPanel>
88+
89+
<ListBox.ItemTemplate>
90+
<DataTemplate DataType="x:String">
91+
<Grid ColumnDefinitions="Auto,*" Background="Transparent" Tapped="OnItemTapped">
92+
<Path Grid.Column="0"
93+
Width="12" Height="12"
94+
Data="{StaticResource Icons.File}"
95+
IsHitTestVisible="False"/>
96+
<TextBlock Grid.Column="1"
97+
Margin="4,0,0,0"
98+
VerticalAlignment="Center"
99+
IsHitTestVisible="False">
100+
<Run Text="{Binding Converter={x:Static c:PathConverters.PureFileName}, Mode=OneWay}"/>
101+
<Run Text=" "/>
102+
<Run Text="{Binding Converter={x:Static c:PathConverters.PureDirectoryName}, Mode=OneWay}" Foreground="{DynamicResource Brush.FG2}"/>
103+
</TextBlock>
104+
</Grid>
105+
</DataTemplate>
106+
</ListBox.ItemTemplate>
107+
</ListBox>
108+
109+
<v:LoadingIcon Grid.Row="1"
110+
Width="48" Height="48"
111+
Margin="0,12,0,8"
112+
HorizontalAlignment="Center" VerticalAlignment="Center"
113+
IsVisible="{Binding IsLoading}"/>
114+
</Grid>
115+
</UserControl>
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
using Avalonia.Controls;
2+
using Avalonia.Input;
3+
4+
namespace SourceGit.Views
5+
{
6+
public partial class OpenFileCommandPalette : UserControl
7+
{
8+
public OpenFileCommandPalette()
9+
{
10+
InitializeComponent();
11+
}
12+
13+
protected override void OnKeyDown(KeyEventArgs e)
14+
{
15+
base.OnKeyDown(e);
16+
17+
if (DataContext is not ViewModels.OpenFileCommandPalette vm)
18+
return;
19+
20+
if (e.Key == Key.Enter)
21+
{
22+
vm.Launch();
23+
e.Handled = true;
24+
}
25+
else if (e.Key == Key.Up)
26+
{
27+
if (FileListBox.IsKeyboardFocusWithin)
28+
{
29+
FilterTextBox.Focus(NavigationMethod.Directional);
30+
e.Handled = true;
31+
return;
32+
}
33+
}
34+
else if (e.Key == Key.Down || e.Key == Key.Tab)
35+
{
36+
if (FilterTextBox.IsKeyboardFocusWithin)
37+
{
38+
if (vm.VisibleFiles.Count > 0)
39+
{
40+
FileListBox.Focus(NavigationMethod.Directional);
41+
vm.SelectedFile = vm.VisibleFiles[0];
42+
}
43+
44+
e.Handled = true;
45+
return;
46+
}
47+
48+
if (FileListBox.IsKeyboardFocusWithin && e.Key == Key.Tab)
49+
{
50+
FilterTextBox.Focus(NavigationMethod.Directional);
51+
e.Handled = true;
52+
return;
53+
}
54+
}
55+
}
56+
57+
private void OnItemTapped(object sender, TappedEventArgs e)
58+
{
59+
if (DataContext is ViewModels.OpenFileCommandPalette vm)
60+
{
61+
vm.Launch();
62+
e.Handled = true;
63+
}
64+
}
65+
}
66+
}

0 commit comments

Comments
 (0)