Skip to content

Commit d950382

Browse files
committed
refactor: unify the behavior of Reveal in File Explorer when the selected is a folder across all platforms
Signed-off-by: leo <longshuang@msn.cn>
1 parent 85baddd commit d950382

File tree

10 files changed

+50
-70
lines changed

10 files changed

+50
-70
lines changed

src/Native/Linux.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ public void OpenBrowser(string url)
106106
Process.Start(browser, url.Quoted());
107107
}
108108

109-
public void OpenInFileManager(string path, bool select)
109+
public void OpenInFileManager(string path)
110110
{
111111
if (Directory.Exists(path))
112112
{

src/Native/MacOS.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void OpenBrowser(string url)
9191
Process.Start("open", url);
9292
}
9393

94-
public void OpenInFileManager(string path, bool select)
94+
public void OpenInFileManager(string path)
9595
{
9696
if (Directory.Exists(path))
9797
Process.Start("open", path.Quoted());

src/Native/OS.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public interface IBackend
2323
List<Models.ExternalTool> FindExternalTools();
2424

2525
void OpenTerminal(string workdir, string args);
26-
void OpenInFileManager(string path, bool select);
26+
void OpenInFileManager(string path);
2727
void OpenBrowser(string url);
2828
void OpenWithDefaultEditor(string file);
2929
}
@@ -198,9 +198,9 @@ public static void AutoSelectExternalMergeToolExecFile()
198198
}
199199
}
200200

201-
public static void OpenInFileManager(string path, bool select = false)
201+
public static void OpenInFileManager(string path)
202202
{
203-
_backend.OpenInFileManager(path, select);
203+
_backend.OpenInFileManager(path);
204204
}
205205

206206
public static void OpenBrowser(string url)

src/Native/Windows.cs

Lines changed: 33 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -177,32 +177,30 @@ public void OpenTerminal(string workdir, string args)
177177
Process.Start(startInfo);
178178
}
179179

180-
public void OpenInFileManager(string path, bool select)
180+
public void OpenInFileManager(string path)
181181
{
182-
string fullpath;
183182
if (File.Exists(path))
184183
{
185-
fullpath = new FileInfo(path).FullName;
186-
select = true;
187-
}
188-
else
189-
{
190-
fullpath = new DirectoryInfo(path!).FullName;
191-
fullpath += Path.DirectorySeparatorChar;
192-
}
184+
var pidl = ILCreateFromPathW(new FileInfo(path).FullName);
193185

194-
if (select)
195-
{
196-
OpenFolderAndSelectFile(fullpath);
197-
}
198-
else
199-
{
200-
Process.Start(new ProcessStartInfo(fullpath)
186+
try
187+
{
188+
SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
189+
}
190+
finally
201191
{
202-
UseShellExecute = true,
203-
CreateNoWindow = true,
204-
});
192+
ILFree(pidl);
193+
}
194+
195+
return;
205196
}
197+
198+
var dir = new DirectoryInfo(path).FullName + Path.DirectorySeparatorChar;
199+
Process.Start(new ProcessStartInfo(dir)
200+
{
201+
UseShellExecute = true,
202+
CreateNoWindow = true,
203+
});
206204
}
207205

208206
public void OpenWithDefaultEditor(string file)
@@ -213,6 +211,7 @@ public void OpenWithDefaultEditor(string file)
213211
Process.Start(start);
214212
}
215213

214+
#region HELPER_METHODS
216215
private void FixWindowFrameOnWin10(Window w)
217216
{
218217
// Schedule the DWM frame extension to run in the next render frame
@@ -228,11 +227,22 @@ private void FixWindowFrameOnWin10(Window w)
228227
}, DispatcherPriority.Render);
229228
}
230229

231-
private PixelPoint IntPtrToPixelPoint(IntPtr param)
230+
private List<Models.ExternalTool.LaunchOption> GenerateVSProjectLaunchOptions(string path)
232231
{
233-
var v = IntPtr.Size == 4 ? param.ToInt32() : (int)(param.ToInt64() & 0xFFFFFFFF);
234-
return new PixelPoint((short)(v & 0xffff), (short)(v >> 16));
232+
var root = new DirectoryInfo(path);
233+
if (!root.Exists)
234+
return null;
235+
236+
var options = new List<Models.ExternalTool.LaunchOption>();
237+
root.WalkFiles(f =>
238+
{
239+
if (f.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) ||
240+
f.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase))
241+
options.Add(new(root.GetRelativePath(f), f.Quoted()));
242+
});
243+
return options;
235244
}
245+
#endregion
236246

237247
#region EXTERNAL_EDITOR_FINDER
238248
private string FindVSCode()
@@ -385,35 +395,5 @@ private string FindZed()
385395
return string.Empty;
386396
}
387397
#endregion
388-
389-
private void OpenFolderAndSelectFile(string folderPath)
390-
{
391-
var pidl = ILCreateFromPathW(folderPath);
392-
393-
try
394-
{
395-
SHOpenFolderAndSelectItems(pidl, 0, 0, 0);
396-
}
397-
finally
398-
{
399-
ILFree(pidl);
400-
}
401-
}
402-
403-
private List<Models.ExternalTool.LaunchOption> GenerateVSProjectLaunchOptions(string path)
404-
{
405-
var root = new DirectoryInfo(path);
406-
if (!root.Exists)
407-
return null;
408-
409-
var options = new List<Models.ExternalTool.LaunchOption>();
410-
root.WalkFiles(f =>
411-
{
412-
if (f.EndsWith(".sln", StringComparison.OrdinalIgnoreCase) ||
413-
f.EndsWith(".slnx", StringComparison.OrdinalIgnoreCase))
414-
options.Add(new(root.GetRelativePath(f), f.Quoted()));
415-
});
416-
return options;
417-
}
418398
}
419399
}

src/Views/CommitDetail.axaml.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public ContextMenu CreateChangeContextMenuByFolder(ViewModels.ChangeTreeNode nod
2929
explore.IsEnabled = Directory.Exists(fullPath);
3030
explore.Click += (_, ev) =>
3131
{
32-
Native.OS.OpenInFileManager(fullPath, true);
32+
Native.OS.OpenInFileManager(fullPath);
3333
ev.Handled = true;
3434
};
3535

@@ -264,7 +264,7 @@ public ContextMenu CreateChangeContextMenu(Models.Change change)
264264
explore.IsEnabled = File.Exists(fullPath);
265265
explore.Click += (_, ev) =>
266266
{
267-
Native.OS.OpenInFileManager(fullPath, true);
267+
Native.OS.OpenInFileManager(fullPath);
268268
ev.Handled = true;
269269
};
270270

src/Views/Compare.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
7676
explore.IsEnabled = File.Exists(full);
7777
explore.Click += (_, ev) =>
7878
{
79-
Native.OS.OpenInFileManager(full, true);
79+
Native.OS.OpenInFileManager(full);
8080
ev.Handled = true;
8181
};
8282
menu.Items.Add(explore);

src/Views/RevisionCompare.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
7777
explore.IsEnabled = File.Exists(changeFullPath);
7878
explore.Click += (_, ev) =>
7979
{
80-
Native.OS.OpenInFileManager(changeFullPath, true);
80+
Native.OS.OpenInFileManager(changeFullPath);
8181
ev.Handled = true;
8282
};
8383
menu.Items.Add(explore);

src/Views/RevisionFileTreeView.axaml.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ private ContextMenu CreateRevisionFileContextMenuByFolder(ViewModels.Repository
446446
explore.IsEnabled = Directory.Exists(fullPath);
447447
explore.Click += (_, ev) =>
448448
{
449-
Native.OS.OpenInFileManager(fullPath, true);
449+
Native.OS.OpenInFileManager(fullPath);
450450
ev.Handled = true;
451451
};
452452

@@ -567,10 +567,10 @@ private ContextMenu CreateRevisionFileContextMenu(ViewModels.Repository repo, Vi
567567
var explore = new MenuItem();
568568
explore.Header = App.Text("RevealFile");
569569
explore.Icon = App.CreateMenuIcon("Icons.Explore");
570-
explore.IsEnabled = File.Exists(fullPath);
570+
explore.IsEnabled = File.Exists(fullPath) || Directory.Exists(fullPath);
571571
explore.Click += (_, ev) =>
572572
{
573-
Native.OS.OpenInFileManager(fullPath, file.Type == Models.ObjectType.Blob);
573+
Native.OS.OpenInFileManager(fullPath);
574574
ev.Handled = true;
575575
};
576576

src/Views/StashesPage.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ private void OnChangeContextRequested(object sender, ContextRequestedEventArgs e
157157
explore.IsEnabled = File.Exists(fullPath);
158158
explore.Click += (_, ev) =>
159159
{
160-
Native.OS.OpenInFileManager(fullPath, true);
160+
Native.OS.OpenInFileManager(fullPath);
161161
ev.Handled = true;
162162
};
163163

src/Views/WorkingCopy.axaml.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -285,7 +285,7 @@ private ContextMenu CreateContextMenuForUnstagedChanges(ViewModels.WorkingCopy v
285285
explore.Click += (_, e) =>
286286
{
287287
var target = hasSelectedFolder ? Native.OS.GetAbsPath(repo.FullPath, selectedSingleFolder) : path;
288-
Native.OS.OpenInFileManager(target, true);
288+
Native.OS.OpenInFileManager(target);
289289
e.Handled = true;
290290
};
291291
menu.Items.Add(explore);
@@ -764,7 +764,7 @@ private ContextMenu CreateContextMenuForUnstagedChanges(ViewModels.WorkingCopy v
764764
explore.IsEnabled = Directory.Exists(dir);
765765
explore.Click += (_, e) =>
766766
{
767-
Native.OS.OpenInFileManager(dir, true);
767+
Native.OS.OpenInFileManager(dir);
768768
e.Handled = true;
769769
};
770770
menu.Items.Add(explore);
@@ -960,7 +960,7 @@ public ContextMenu CreateContextMenuForStagedChanges(ViewModels.WorkingCopy vm,
960960
explore.Click += (_, e) =>
961961
{
962962
var target = hasSelectedFolder ? Native.OS.GetAbsPath(repo.FullPath, selectedSingleFolder) : path;
963-
Native.OS.OpenInFileManager(target, true);
963+
Native.OS.OpenInFileManager(target);
964964
e.Handled = true;
965965
};
966966

@@ -1173,7 +1173,7 @@ public ContextMenu CreateContextMenuForStagedChanges(ViewModels.WorkingCopy vm,
11731173
explore.Icon = App.CreateMenuIcon("Icons.Explore");
11741174
explore.Click += (_, e) =>
11751175
{
1176-
Native.OS.OpenInFileManager(dir, true);
1176+
Native.OS.OpenInFileManager(dir);
11771177
e.Handled = true;
11781178
};
11791179

0 commit comments

Comments
 (0)