Skip to content

Commit 75792f8

Browse files
authored
Feature: Added actions to open log file and folder (#17433)
1 parent c6654f4 commit 75792f8

File tree

7 files changed

+105
-1
lines changed

7 files changed

+105
-1
lines changed
Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using Microsoft.UI.Xaml.Controls;
5+
using Microsoft.UI.Xaml.Media;
6+
using Windows.Foundation.Metadata;
7+
using Windows.Storage;
8+
using Windows.System;
9+
10+
namespace Files.App.Actions
11+
{
12+
internal sealed partial class OpenLogFileAction : IAction
13+
{
14+
public string Label
15+
=> Strings.OpenLogFile.GetLocalizedResource();
16+
17+
public string Description
18+
=> Strings.OpenLogFileDescription.GetLocalizedResource();
19+
20+
public HotKey HotKey
21+
=> new(Keys.OemPeriod, KeyModifiers.Ctrl);
22+
23+
public async Task ExecuteAsync(object? parameter = null)
24+
{
25+
try
26+
{
27+
var debugFile = await ApplicationData.Current.LocalFolder.TryGetItemAsync("debug.log") as StorageFile;
28+
29+
if (debugFile != null && !await Launcher.LaunchFileAsync(debugFile))
30+
{
31+
// Fallback to Process.Start if Launcher fails
32+
using var process = Process.Start(new ProcessStartInfo(debugFile.Path)
33+
{
34+
UseShellExecute = true,
35+
Verb = "open"
36+
});
37+
}
38+
}
39+
catch (Exception ex)
40+
{
41+
// Only show the error dialog if no other popups are open
42+
if (!VisualTreeHelper.GetOpenPopupsForXamlRoot(MainWindow.Instance.Content.XamlRoot).Any())
43+
{
44+
var errorDialog = new ContentDialog()
45+
{
46+
Title = Strings.FailedToOpenLogFile.GetLocalizedResource(),
47+
Content = ex.Message,
48+
PrimaryButtonText = Strings.OK.GetLocalizedResource(),
49+
};
50+
51+
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
52+
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
53+
54+
await errorDialog.TryShowAsync();
55+
}
56+
}
57+
}
58+
}
59+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Copyright (c) Files Community
2+
// Licensed under the MIT License.
3+
4+
using Windows.Storage;
5+
using Windows.System;
6+
7+
namespace Files.App.Actions
8+
{
9+
internal sealed partial class OpenLogFileLocationAction : IAction
10+
{
11+
public string Label
12+
=> Strings.OpenLogLocation.GetLocalizedResource();
13+
14+
public string Description
15+
=> Strings.OpenLogFileLocationDescription.GetLocalizedResource();
16+
17+
public HotKey HotKey
18+
=> new(Keys.OemPeriod, KeyModifiers.CtrlShift);
19+
20+
public async Task ExecuteAsync(object? parameter = null)
21+
{
22+
await Launcher.LaunchFolderAsync(ApplicationData.Current.LocalFolder).AsTask();
23+
}
24+
}
25+
}

src/Files.App/Data/Commands/Manager/CommandCodes.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,8 @@ public enum CommandCodes
130130
OpenTerminalFromHome,
131131
OpenCommandPalette,
132132
EditInNotepad,
133+
OpenLogFile,
134+
OpenLogFileLocation,
133135

134136
// Layout
135137
LayoutDecreaseSize,

src/Files.App/Data/Commands/Manager/CommandManager.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,6 +131,8 @@ public IRichCommand this[HotKey hotKey]
131131
public IRichCommand OpenTerminalFromHome => commands[CommandCodes.OpenTerminalFromHome];
132132
public IRichCommand OpenCommandPalette => commands[CommandCodes.OpenCommandPalette];
133133
public IRichCommand EditInNotepad => commands[CommandCodes.EditInNotepad];
134+
public IRichCommand OpenLogFile => commands[CommandCodes.OpenLogFile];
135+
public IRichCommand OpenLogFileLocation => commands[CommandCodes.OpenLogFileLocation];
134136
public IRichCommand LayoutDecreaseSize => commands[CommandCodes.LayoutDecreaseSize];
135137
public IRichCommand LayoutIncreaseSize => commands[CommandCodes.LayoutIncreaseSize];
136138
public IRichCommand LayoutDetails => commands[CommandCodes.LayoutDetails];
@@ -339,6 +341,8 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
339341
[CommandCodes.OpenTerminalFromHome] = new OpenTerminalFromHomeAction(),
340342
[CommandCodes.OpenCommandPalette] = new OpenCommandPaletteAction(),
341343
[CommandCodes.EditInNotepad] = new EditInNotepadAction(),
344+
[CommandCodes.OpenLogFile] = new OpenLogFileAction(),
345+
[CommandCodes.OpenLogFileLocation] = new OpenLogFileLocationAction(),
342346
[CommandCodes.LayoutDecreaseSize] = new LayoutDecreaseSizeAction(),
343347
[CommandCodes.LayoutIncreaseSize] = new LayoutIncreaseSizeAction(),
344348
[CommandCodes.LayoutDetails] = new LayoutDetailsAction(),

src/Files.App/Data/Commands/Manager/ICommandManager.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,8 @@ public interface ICommandManager : IEnumerable<IRichCommand>
119119
IRichCommand OpenTerminalFromHome { get; }
120120
IRichCommand OpenCommandPalette { get; }
121121
IRichCommand EditInNotepad { get; }
122+
IRichCommand OpenLogFile { get; }
123+
IRichCommand OpenLogFileLocation { get; }
122124

123125
IRichCommand LayoutDecreaseSize { get; }
124126
IRichCommand LayoutIncreaseSize { get; }

src/Files.App/Strings/en-US/Resources.resw

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4313,4 +4313,16 @@
43134313
<data name="ShowOpenTerminal" xml:space="preserve">
43144314
<value>Show option to open folders in Windows Terminal</value>
43154315
</data>
4316+
<data name="OpenLogFile" xml:space="preserve">
4317+
<value>Open log file</value>
4318+
</data>
4319+
<data name="OpenLogFileDescription" xml:space="preserve">
4320+
<value>Open log file in your default editor</value>
4321+
</data>
4322+
<data name="OpenLogFileLocationDescription" xml:space="preserve">
4323+
<value>Open log file location in your default file manager</value>
4324+
</data>
4325+
<data name="FailedToOpenLogFile" xml:space="preserve">
4326+
<value>Unable to open the log file</value>
4327+
</data>
43164328
</root>

src/Files.App/ViewModels/Settings/AboutViewModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ private async Task<bool> OpenLogLocation()
107107
using var subkey = Registry.ClassesRoot.OpenSubKey(@"Folder\shell\open\command");
108108
var command = (string?)subkey?.GetValue(string.Empty);
109109

110-
// Close the settings dialog if Files is the deault file manager
110+
// Close the settings dialog if Files is the default file manager
111111
if (!string.IsNullOrEmpty(command) && command.Contains("Files.App.Launcher.exe"))
112112
UIHelpers.CloseAllDialogs();
113113

0 commit comments

Comments
 (0)