-
Notifications
You must be signed in to change notification settings - Fork 383
Expand file tree
/
Copy pathLinux.cs
More file actions
182 lines (160 loc) · 6.51 KB
/
Linux.cs
File metadata and controls
182 lines (160 loc) · 6.51 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Runtime.Versioning;
using Avalonia;
using Avalonia.Controls;
using Avalonia.Platform;
namespace SourceGit.Native
{
[SupportedOSPlatform("linux")]
internal class Linux : OS.IBackend
{
private static readonly string LOCAL_APP_DATA_DIR = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
public void SetupApp(AppBuilder builder)
{
builder.With(new X11PlatformOptions() { EnableIme = true });
}
public void SetupWindow(Window window)
{
if (OS.UseSystemWindowFrame)
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.Default;
window.ExtendClientAreaToDecorationsHint = false;
}
else
{
window.ExtendClientAreaChromeHints = ExtendClientAreaChromeHints.NoChrome;
window.ExtendClientAreaToDecorationsHint = true;
window.Classes.Add("custom_window_frame");
}
}
public string FindGitExecutable()
{
return FindExecutable("git");
}
public string FindTerminal(Models.ShellOrTerminal shell)
{
if (shell.Type.Equals("custom", StringComparison.Ordinal))
return string.Empty;
return FindExecutable(shell.Exec);
}
public List<Models.ExternalTool> FindExternalTools()
{
var finder = new Models.ExternalToolsFinder();
finder.VSCode(() => FindExecutable("code", "com.visualstudio.code"));
finder.VSCodeInsiders(() => FindExecutable("code-insiders", "com.vscodium.codium-insiders"));
finder.VSCodium(() => FindExecutable("codium", "com.vscodium.codium"));
finder.Cursor(() => FindExecutable("cursor"));
finder.Fleet(FindJetBrainsFleet);
finder.FindJetBrainsFromToolbox(() => Path.Combine(LOCAL_APP_DATA_DIR, "JetBrains/Toolbox"));
FindJetBrainsFromFlatpak(finder);
finder.SublimeText(() => FindExecutable("subl", "com.sublimetext.three"));
finder.Zed(() => FindExecutable("zeditor", "dev.zed.Zed"));
return finder.Tools;
}
public void OpenBrowser(string url)
{
Process.Start("xdg-open", $"\"{url}\"");
}
public void OpenInFileManager(string path, bool select)
{
if (Directory.Exists(path))
{
Process.Start("xdg-open", $"\"{path}\"");
}
else
{
var dir = Path.GetDirectoryName(path);
if (Directory.Exists(dir))
Process.Start("xdg-open", $"\"{dir}\"");
}
}
public void OpenTerminal(string workdir)
{
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
var cwd = string.IsNullOrEmpty(workdir) ? home : workdir;
var terminal = OS.ShellOrTerminal;
var startInfo = new ProcessStartInfo();
startInfo.WorkingDirectory = cwd;
startInfo.FileName = terminal;
if (terminal.EndsWith("wezterm", StringComparison.OrdinalIgnoreCase))
startInfo.Arguments = $"start --cwd \"{cwd}\"";
else if (terminal.EndsWith("ptyxis", StringComparison.OrdinalIgnoreCase))
startInfo.Arguments = $"--new-window --working-directory=\"{cwd}\"";
try
{
Process.Start(startInfo);
}
catch (Exception e)
{
App.RaiseException(workdir, $"Failed to start '{OS.ShellOrTerminal}'. Reason: {e.Message}");
}
}
public void OpenWithDefaultEditor(string file)
{
var proc = Process.Start("xdg-open", $"\"{file}\"");
if (proc != null)
{
proc.WaitForExit();
if (proc.ExitCode != 0)
App.RaiseException("", $"Failed to open \"{file}\"");
proc.Close();
}
}
private static string FindExecutable(string filename, string flatpakAppId = null)
{
var pathVariable = Environment.GetEnvironmentVariable("PATH") ?? string.Empty;
var paths = pathVariable.Split(Path.PathSeparator, StringSplitOptions.RemoveEmptyEntries);
foreach (var path in paths)
{
var test = Path.Combine(path, filename);
if (File.Exists(test))
return test;
}
if (flatpakAppId != null)
{
foreach (var path in new[] { "/var/lib", LOCAL_APP_DATA_DIR })
{
var test = Path.Combine(path, "flatpak/exports/bin", flatpakAppId);
if (File.Exists(test))
return test;
}
}
return string.Empty;
}
private static string FindJetBrainsFleet()
{
var path = Path.Combine(LOCAL_APP_DATA_DIR, "JetBrains/Toolbox/apps/fleet/bin/Fleet");
return File.Exists(path) ? path : FindExecutable("fleet");
}
private static void FindJetBrainsFromFlatpak(Models.ExternalToolsFinder finder)
{
foreach (var basePath in new[] { "/var/lib", LOCAL_APP_DATA_DIR })
{
var binPath = Path.Combine(basePath, "flatpak/exports/bin");
if (Directory.Exists(binPath))
{
foreach (var file in Directory.GetFiles(binPath, "com.jetbrains.*"))
{
var fileName = Path.GetFileName(file);
var appName = fileName[14..].Replace("-", " ");
var icon = new string(Array.FindAll(fileName.ToCharArray(), char.IsUpper));
if (icon.Length > 2)
icon = icon[..2];
icon = icon switch
{
"DG" => "DB", // DataGrip
"GL" => "GO", // GoLand
"IJ" => "JB", // IntelliJ
"R" => "RD", // Rider
_ => icon
};
finder.Tools.Add(new Models.ExternalTool(appName, "JetBrains/" + icon, file));
}
}
}
}
}
}