Skip to content

Commit 26a3a2b

Browse files
committed
feature: supports to scan custom directory (#1696)
Signed-off-by: leo <[email protected]>
1 parent 7e71ab2 commit 26a3a2b

File tree

6 files changed

+81
-4
lines changed

6 files changed

+81
-4
lines changed

src/Resources/Locales/en_US.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -733,6 +733,7 @@
733733
<x:String x:Key="Text.SaveAsPatchSuccess" xml:space="preserve">Patch has been saved successfully!</x:String>
734734
<x:String x:Key="Text.ScanRepositories" xml:space="preserve">Scan Repositories</x:String>
735735
<x:String x:Key="Text.ScanRepositories.RootDir" xml:space="preserve">Root Dir:</x:String>
736+
<x:String x:Key="Text.ScanRepositories.UseCustomDir" xml:space="preserve">Scan another custom directory</x:String>
736737
<x:String x:Key="Text.SelfUpdate" xml:space="preserve">Check for Updates...</x:String>
737738
<x:String x:Key="Text.SelfUpdate.Available" xml:space="preserve">New version of this software is available: </x:String>
738739
<x:String x:Key="Text.SelfUpdate.Error" xml:space="preserve">Check for updates failed!</x:String>

src/Resources/Locales/zh_CN.axaml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -737,6 +737,7 @@
737737
<x:String x:Key="Text.SaveAsPatchSuccess" xml:space="preserve">补丁已成功保存!</x:String>
738738
<x:String x:Key="Text.ScanRepositories" xml:space="preserve">扫描仓库</x:String>
739739
<x:String x:Key="Text.ScanRepositories.RootDir" xml:space="preserve">根路径 :</x:String>
740+
<x:String x:Key="Text.ScanRepositories.UseCustomDir" xml:space="preserve">扫描其他自定义路径</x:String>
740741
<x:String x:Key="Text.SelfUpdate" xml:space="preserve">检测更新...</x:String>
741742
<x:String x:Key="Text.SelfUpdate.Available" xml:space="preserve">检测到软件有版本更新: </x:String>
742743
<x:String x:Key="Text.SelfUpdate.Error" 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
@@ -737,6 +737,7 @@
737737
<x:String x:Key="Text.SaveAsPatchSuccess" xml:space="preserve">修補檔已成功儲存!</x:String>
738738
<x:String x:Key="Text.ScanRepositories" xml:space="preserve">掃描存放庫</x:String>
739739
<x:String x:Key="Text.ScanRepositories.RootDir" xml:space="preserve">頂層目錄:</x:String>
740+
<x:String x:Key="Text.ScanRepositories.UseCustomDir" xml:space="preserve">掃描其他自訂目錄</x:String>
740741
<x:String x:Key="Text.SelfUpdate" xml:space="preserve">檢查更新...</x:String>
741742
<x:String x:Key="Text.SelfUpdate.Available" xml:space="preserve">軟體有版本更新:</x:String>
742743
<x:String x:Key="Text.SelfUpdate.Error" xml:space="preserve">取得最新版本資訊失敗!</x:String>

src/ViewModels/ScanRepositories.cs

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,18 @@ namespace SourceGit.ViewModels
88
{
99
public class ScanRepositories : Popup
1010
{
11+
public bool UseCustomDir
12+
{
13+
get => _useCustomDir;
14+
set => SetProperty(ref _useCustomDir, value);
15+
}
16+
17+
public string CustomDir
18+
{
19+
get => _customDir;
20+
set => SetProperty(ref _customDir, value);
21+
}
22+
1123
public List<Models.ScanDir> ScanDirs
1224
{
1325
get;
@@ -33,16 +45,28 @@ public ScanRepositories()
3345

3446
if (ScanDirs.Count > 0)
3547
_selected = ScanDirs[0];
48+
else
49+
_useCustomDir = true;
3650

3751
GetManagedRepositories(Preferences.Instance.RepositoryNodes, _managed);
3852
}
3953

4054
public override async Task<bool> Sure()
4155
{
42-
ProgressDescription = $"Scan repositories under '{_selected.Path}' ...";
56+
var selectedDir = _useCustomDir ? _customDir : _selected?.Path;
57+
if (string.IsNullOrEmpty(selectedDir))
58+
{
59+
App.RaiseException(null, "Missing root directory to scan!");
60+
return false;
61+
}
62+
63+
if (!Directory.Exists(selectedDir))
64+
return true;
65+
66+
ProgressDescription = $"Scan repositories under '{selectedDir}' ...";
4367

4468
var minDelay = Task.Delay(500);
45-
var rootDir = new DirectoryInfo(_selected.Path);
69+
var rootDir = new DirectoryInfo(selectedDir);
4670
var found = new List<string>();
4771

4872
await GetUnmanagedRepositoriesAsync(rootDir, found, new EnumerationOptions()
@@ -162,6 +186,8 @@ private RepositoryNode FindOrCreateGroup(List<RepositoryNode> collection, string
162186
}
163187

164188
private HashSet<string> _managed = new();
189+
private bool _useCustomDir = false;
190+
private string _customDir = string.Empty;
165191
private Models.ScanDir _selected = null;
166192
}
167193
}

src/Views/ScanRepositories.axaml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
<TextBlock FontSize="18"
1212
Classes="bold"
1313
Text="{DynamicResource Text.ScanRepositories}"/>
14-
<Grid Margin="0,16,0,0" RowDefinitions="32" ColumnDefinitions="120,*">
14+
<Grid Margin="0,16,0,0" RowDefinitions="32,32" ColumnDefinitions="120,*">
1515
<TextBlock Grid.Row="0" Grid.Column="0"
1616
HorizontalAlignment="Right" VerticalAlignment="Center"
1717
Margin="0,0,8,0"
@@ -20,7 +20,8 @@
2020
Height="28" Padding="4,0"
2121
VerticalAlignment="Center" HorizontalAlignment="Stretch"
2222
ItemsSource="{Binding ScanDirs, Mode=OneWay}"
23-
SelectedItem="{Binding Selected, Mode=TwoWay}">
23+
SelectedItem="{Binding Selected, Mode=TwoWay}"
24+
IsVisible="{Binding !UseCustomDir, Mode=OneWay}">
2425
<ComboBox.ItemTemplate>
2526
<DataTemplate DataType="m:ScanDir">
2627
<Grid ColumnDefinitions="20,*,Auto">
@@ -38,6 +39,21 @@
3839
</DataTemplate>
3940
</ComboBox.ItemTemplate>
4041
</ComboBox>
42+
<TextBox Grid.Row="0" Grid.Column="1"
43+
Height="28"
44+
CornerRadius="3"
45+
Text="{Binding CustomDir, Mode=TwoWay}"
46+
IsVisible="{Binding UseCustomDir, Mode=OneWay}">
47+
<TextBox.InnerRightContent>
48+
<Button Classes="icon_button" Width="30" Height="30" Click="OnSelectRootDir">
49+
<Path Data="{StaticResource Icons.Folder.Open}" Fill="{DynamicResource Brush.FG1}"/>
50+
</Button>
51+
</TextBox.InnerRightContent>
52+
</TextBox>
53+
54+
<CheckBox Grid.Row="1" Grid.Column="1"
55+
Content="{DynamicResource Text.ScanRepositories.UseCustomDir}"
56+
IsChecked="{Binding UseCustomDir, Mode=TwoWay}"/>
4157
</Grid>
4258
</StackPanel>
4359
</UserControl>

src/Views/ScanRepositories.axaml.cs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1+
using System;
2+
13
using Avalonia.Controls;
4+
using Avalonia.Interactivity;
5+
using Avalonia.Platform.Storage;
26

37
namespace SourceGit.Views
48
{
@@ -8,5 +12,33 @@ public ScanRepositories()
812
{
913
InitializeComponent();
1014
}
15+
16+
private async void OnSelectRootDir(object _, RoutedEventArgs e)
17+
{
18+
var provider = TopLevel.GetTopLevel(this)?.StorageProvider;
19+
if (provider == null)
20+
return;
21+
22+
if (DataContext is not ViewModels.ScanRepositories vm)
23+
return;
24+
25+
var options = new FolderPickerOpenOptions() { AllowMultiple = false };
26+
try
27+
{
28+
var selected = await provider.OpenFolderPickerAsync(options);
29+
if (selected.Count == 1)
30+
{
31+
var folder = selected[0];
32+
var folderPath = folder is { Path: { IsAbsoluteUri: true } path } ? path.LocalPath : folder?.Path.ToString();
33+
vm.CustomDir = folderPath;
34+
}
35+
}
36+
catch (Exception ex)
37+
{
38+
App.RaiseException(string.Empty, $"Failed to select root scanning directory: {ex.Message}");
39+
}
40+
41+
e.Handled = true;
42+
}
1143
}
1244
}

0 commit comments

Comments
 (0)