Skip to content

Commit 17130e1

Browse files
committed
工程树:仓库可逐层展开到文件 + 平台字体默认值 + 外部程序打开
- 文件层:仓库首次展开时执行一次 ls-files,一次性建成完整节点树, 避免按层懒加载导致空集合节点无法展开;单层不设数量上限, 「全部展开」跳过文件层,仅在右键「用外部程序打开」时打开文件 - 新增 QueryRepositoryFiles 命令与 RepositoryFileIndex 模型,按忽略规则过滤 - 外部打开:Native.OS 增加 OpenWithChooser,Windows 走 openas 谓词, Linux/macOS 回退默认程序并给出提示 - 字体:PlatformFonts 按平台给出 UI 与等宽字体链;Linux 不设置 FontManagerOptions,避免 Noto Sans 别名导致 glyph typeface 构造失败
1 parent 594389d commit 17130e1

12 files changed

Lines changed: 396 additions & 13 deletions

File tree

‎src/App.axaml.cs‎

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,10 +62,16 @@ public static AppBuilder BuildAvaloniaApp()
6262
builder.UsePlatformDetect();
6363
builder.LogToTrace();
6464
builder.WithInterFont();
65-
builder.With(new FontManagerOptions()
65+
// Only set when the platform has a family that is guaranteed to exist; an
66+
// unresolvable name makes Avalonia fail to build the default glyph typeface.
67+
var defaultFamily = Models.PlatformFonts.PrimaryFamily;
68+
if (!string.IsNullOrEmpty(defaultFamily))
6669
{
67-
DefaultFamilyName = Models.PlatformFonts.PrimaryFamily
68-
});
70+
builder.With(new FontManagerOptions()
71+
{
72+
DefaultFamilyName = defaultFamily
73+
});
74+
}
6975
builder.ConfigureFonts(manager =>
7076
{
7177
var monospace = new EmbeddedFontCollection(
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
using System.Collections.Generic;
2+
using System.Threading.Tasks;
3+
4+
namespace SourceGit.Commands
5+
{
6+
/// <summary>
7+
/// Lists every path git can see in a working copy: tracked files (index) plus untracked
8+
/// ones, with ignored paths filtered out by --exclude-standard. That is exactly the
9+
/// set the project tree should show, and it is why build output covered by .gitignore
10+
/// never shows up in the file layer.
11+
///
12+
/// Paths are printed as-is because CreateGitStartInfo already passes core.quotepath=off,
13+
/// so non-ASCII names come back readable instead of as octal escapes.
14+
/// </summary>
15+
public class QueryRepositoryFiles : Command
16+
{
17+
public QueryRepositoryFiles(string repo)
18+
{
19+
WorkingDirectory = repo;
20+
Context = repo;
21+
Args = "ls-files -c -o --exclude-standard";
22+
}
23+
24+
public async Task<List<string>> GetPathsAsync()
25+
{
26+
var paths = new List<string>();
27+
28+
var result = await ReadToEndAsync().ConfigureAwait(false);
29+
if (!result.IsSuccess)
30+
return paths;
31+
32+
foreach (var line in result.StdOut.Split('\n'))
33+
{
34+
var path = line.TrimEnd('\r');
35+
if (path.Length > 0)
36+
paths.Add(path);
37+
}
38+
39+
return paths;
40+
}
41+
}
42+
}

‎src/Models/PlatformFonts.cs‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,9 @@ public static string DefaultFamily
2727
if (OperatingSystem.IsMacOS())
2828
return "Helvetica Neue,PingFang SC";
2929

30-
return "Noto Sans,Noto Sans CJK SC,Source Han Sans SC,DejaVu Sans";
30+
// Linux: only family names that really exist in fontconfig are listed. "Noto Sans"
31+
// is a fontconfig alias, not a family, so it must not lead the chain.
32+
return "Noto Sans CJK SC,Source Han Sans SC,DejaVu Sans";
3133
}
3234
}
3335

@@ -50,13 +52,23 @@ public static string MonospaceFallback
5052
}
5153

5254
/// <summary>
53-
/// Family used for controls that do not set a font themselves (Avalonia's
54-
/// FontManagerOptions only takes a single name, so this is the first family above).
55+
/// Family handed to Avalonia's FontManagerOptions, which is the default for controls
56+
/// that set no font of their own.
57+
///
58+
/// Windows and macOS ship one guaranteed system family, so a single name is safe there.
59+
/// Linux has no such guarantee: "Noto Sans" is not a real family on hosts that only
60+
/// install Noto Sans CJK, and Avalonia then fails to build a glyph typeface for the
61+
/// default (the failure surfaces deep inside the compositor and kills the window). So
62+
/// Linux returns empty and Avalonia falls back to the fontconfig default, while the
63+
/// UI keeps its explicit family chain through the Fonts.Default resource.
5564
/// </summary>
5665
public static string PrimaryFamily
5766
{
5867
get
5968
{
69+
if (OperatingSystem.IsLinux())
70+
return string.Empty;
71+
6072
var comma = DefaultFamily.IndexOf(',');
6173
return comma < 0 ? DefaultFamily : DefaultFamily.Substring(0, comma).Trim();
6274
}

‎src/Models/RepositoryFileIndex.cs‎

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Threading;
5+
using System.Threading.Tasks;
6+
7+
namespace SourceGit.Models
8+
{
9+
/// <summary>
10+
/// One entry of the file layer: either a real file or a directory that git reported a
11+
/// file below. Because the directory list is derived from the file list, a directory
12+
/// with no visible file under it can never appear.
13+
/// </summary>
14+
public class RepositoryFileNode
15+
{
16+
public string Name { get; set; } = string.Empty;
17+
public string FullPath { get; set; } = string.Empty;
18+
public bool IsDirectory { get; set; } = false;
19+
public List<RepositoryFileNode> Children { get; } = new();
20+
21+
internal RepositoryFileNode FindChild(string name)
22+
{
23+
if (_lookup == null)
24+
_lookup = new Dictionary<string, RepositoryFileNode>(StringComparer.OrdinalIgnoreCase);
25+
26+
return _lookup.TryGetValue(name, out var node) ? node : null;
27+
}
28+
29+
internal void AddChild(RepositoryFileNode child)
30+
{
31+
if (_lookup == null)
32+
_lookup = new Dictionary<string, RepositoryFileNode>(StringComparer.OrdinalIgnoreCase);
33+
34+
_lookup[child.Name] = child;
35+
Children.Add(child);
36+
}
37+
38+
internal void SortRecursive()
39+
{
40+
Children.Sort((a, b) =>
41+
{
42+
if (a.IsDirectory != b.IsDirectory)
43+
return a.IsDirectory ? -1 : 1;
44+
45+
return string.Compare(a.Name, b.Name, StringComparison.OrdinalIgnoreCase);
46+
});
47+
48+
foreach (var child in Children)
49+
{
50+
if (child.IsDirectory)
51+
child.SortRecursive();
52+
}
53+
}
54+
55+
private Dictionary<string, RepositoryFileNode> _lookup;
56+
}
57+
58+
/// <summary>
59+
/// Reads the whole visible file list of a repository once and folds the paths into a
60+
/// directory tree. Nothing is truncated: the caller decides how deep to materialise
61+
/// view-model nodes, so a repository with thousands of files still costs one git
62+
/// process and no UI nodes until a branch is actually expanded.
63+
/// </summary>
64+
public static class RepositoryFileIndex
65+
{
66+
public static async Task<RepositoryFileNode> BuildAsync(string repoPath, CancellationToken cancellationToken)
67+
{
68+
var root = new RepositoryFileNode
69+
{
70+
Name = Path.GetFileName(repoPath.TrimEnd(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar)),
71+
FullPath = repoPath,
72+
IsDirectory = true,
73+
};
74+
75+
var command = new Commands.QueryRepositoryFiles(repoPath) { CancellationToken = cancellationToken };
76+
var paths = await command.GetPathsAsync().ConfigureAwait(false);
77+
78+
foreach (var path in paths)
79+
{
80+
var segments = path.Split('/');
81+
var cursor = root;
82+
83+
for (var i = 0; i < segments.Length; i++)
84+
{
85+
var segment = segments[i];
86+
if (segment.Length == 0)
87+
continue;
88+
89+
var isLeaf = i == segments.Length - 1;
90+
var existing = cursor.FindChild(segment);
91+
if (existing == null)
92+
{
93+
existing = new RepositoryFileNode
94+
{
95+
Name = segment,
96+
FullPath = Path.Combine(cursor.FullPath, segment),
97+
IsDirectory = !isLeaf,
98+
};
99+
cursor.AddChild(existing);
100+
}
101+
else if (!isLeaf)
102+
{
103+
// The same directory can be reached by many files; the first one to
104+
// arrive may have been a leaf, so promote it once a real child appears.
105+
existing.IsDirectory = true;
106+
}
107+
108+
cursor = existing;
109+
}
110+
}
111+
112+
root.SortRecursive();
113+
return root;
114+
}
115+
}
116+
}

‎src/Native/Linux.cs‎

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -174,6 +174,15 @@ public void OpenWithDefaultEditor(string file)
174174
}
175175
}
176176

177+
public void OpenWithChooser(string file)
178+
{
179+
// xdg-open has no equivalent of the Windows "Open with" dialog. There is no portable
180+
// way to list candidate applications from here, so open the registered default and
181+
// state plainly what happened instead of pretending a chooser appeared.
182+
Models.Notification.Send(string.Empty, "Linux 没有系统级“打开方式”对话框,已使用默认程序打开;如需其它程序请用文件管理器。");
183+
OpenWithDefaultEditor(file);
184+
}
185+
177186
public bool SupportSetSid()
178187
{
179188
return true;

‎src/Native/MacOS.cs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ public void OpenWithDefaultEditor(string file)
132132
Process.Start("open", file.Quoted());
133133
}
134134

135+
public void OpenWithChooser(string file)
136+
{
137+
// macOS has no per-file chooser, only the "Open With" list inside Finder's context
138+
// menu. Reveal the file and tell the user where to pick the program.
139+
Models.Notification.Send(string.Empty, "macOS 没有“打开方式”对话框,已在 Finder 中显示该文件;请在其右键菜单里选择程序。");
140+
OpenInFileManager(file);
141+
}
142+
135143
public bool SupportSetSid()
136144
{
137145
return File.Exists(_setsidExecutable);

‎src/Native/OS.cs‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ public interface IBackend
3434
void OpenInFileManager(string path);
3535
void OpenBrowser(string url);
3636
void OpenWithDefaultEditor(string file);
37+
void OpenWithChooser(string file);
3738

3839
bool SupportSetSid();
3940
string GetSetSidExecutable();
@@ -316,6 +317,16 @@ public static void OpenWithDefaultEditor(string file)
316317
_backend.OpenWithDefaultEditor(file);
317318
}
318319

320+
/// <summary>
321+
/// Opens a file with a program the user picks. Only Windows has a per-file chooser
322+
/// (the shell's "Open with" dialog); the other backends fall back to the registered
323+
/// default program and say so, because they cannot list candidate applications here.
324+
/// </summary>
325+
public static void OpenWithChooser(string file)
326+
{
327+
_backend.OpenWithChooser(file);
328+
}
329+
319330
public static string GetAbsPath(string root, string sub)
320331
{
321332
var fullpath = Path.Combine(root, sub);

‎src/Native/Windows.cs‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,27 @@ public void OpenWithDefaultEditor(string file)
220220
Process.Start(start);
221221
}
222222

223+
public void OpenWithChooser(string file)
224+
{
225+
// Windows is the only platform with a real per-file chooser: the shell's "Open with"
226+
// dialog (openas verb). If a shell registration is missing the verb cannot start,
227+
// so fall back to the registered default program instead of doing nothing.
228+
try
229+
{
230+
Process.Start(new ProcessStartInfo(new FileInfo(file).FullName)
231+
{
232+
UseShellExecute = true,
233+
Verb = "openas",
234+
CreateNoWindow = true,
235+
});
236+
}
237+
catch (Exception e)
238+
{
239+
Models.Notification.Send(string.Empty, $"无法打开“打开方式”对话框,改用默认程序: {e.Message}");
240+
OpenWithDefaultEditor(file);
241+
}
242+
}
243+
223244
public bool SupportSetSid()
224245
{
225246
return false;

0 commit comments

Comments
 (0)