Skip to content

Commit 0aabcb6

Browse files
yaira20x5bfa
andauthored
Feature: Added option to open settings file when right clicking settings button (#17004)
Signed-off-by: Yair <[email protected]> Co-authored-by: 0x5BFA <[email protected]>
1 parent 2030228 commit 0aabcb6

File tree

9 files changed

+77
-17
lines changed

9 files changed

+77
-17
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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 OpenSettingsFileAction : IAction
13+
{
14+
public string Label
15+
=> Strings.EditSettingsFile.GetLocalizedResource();
16+
17+
public string Description
18+
=> Strings.EditSettingsFileDescription.GetLocalizedResource();
19+
20+
public HotKey HotKey
21+
=> new(Keys.OemComma, KeyModifiers.CtrlShift);
22+
23+
public RichGlyph Glyph
24+
=> new("\uE8DA");
25+
26+
public async Task ExecuteAsync(object? parameter = null)
27+
{
28+
try
29+
{
30+
var settingsJsonFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appdata:///local/{Constants.LocalSettings.SettingsFolderName}/{Constants.LocalSettings.UserSettingsFileName}"));
31+
if (!await Launcher.LaunchFileAsync(settingsJsonFile))
32+
await ContextMenu.InvokeVerb("open", settingsJsonFile.Path);
33+
}
34+
catch (Exception ex)
35+
{
36+
// Only show the error dialog if no other popups are open
37+
if (!VisualTreeHelper.GetOpenPopupsForXamlRoot(MainWindow.Instance.Content.XamlRoot).Any())
38+
{
39+
var errorDialog = new ContentDialog()
40+
{
41+
Title = Strings.FailedToOpenSettingsFile.GetLocalizedResource(),
42+
Content = ex.Message,
43+
PrimaryButtonText = Strings.OK.GetLocalizedResource(),
44+
};
45+
46+
if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
47+
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;
48+
49+
await errorDialog.TryShowAsync();
50+
}
51+
}
52+
}
53+
}
54+
}

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

+1
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ public enum CommandCodes
118118
OpenReleaseNotes,
119119
OpenClassicProperties,
120120
OpenSettings,
121+
OpenSettingsFile,
121122
OpenStorageSense,
122123
OpenStorageSenseFromHome,
123124
OpenStorageSenseFromSidebar,

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

+2
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,7 @@ public IRichCommand this[HotKey hotKey]
122122
public IRichCommand OpenStorageSenseFromHome => commands[CommandCodes.OpenStorageSenseFromHome];
123123
public IRichCommand OpenStorageSenseFromSidebar => commands[CommandCodes.OpenStorageSenseFromSidebar];
124124
public IRichCommand OpenSettings => commands[CommandCodes.OpenSettings];
125+
public IRichCommand OpenSettingsFile => commands[CommandCodes.OpenSettingsFile];
125126
public IRichCommand OpenTerminal => commands[CommandCodes.OpenTerminal];
126127
public IRichCommand OpenTerminalAsAdmin => commands[CommandCodes.OpenTerminalAsAdmin];
127128
public IRichCommand OpenTerminalFromSidebar => commands[CommandCodes.OpenTerminalFromSidebar];
@@ -327,6 +328,7 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
327328
[CommandCodes.OpenStorageSenseFromHome] = new OpenStorageSenseFromHomeAction(),
328329
[CommandCodes.OpenStorageSenseFromSidebar] = new OpenStorageSenseFromSidebarAction(),
329330
[CommandCodes.OpenSettings] = new OpenSettingsAction(),
331+
[CommandCodes.OpenSettingsFile] = new OpenSettingsFileAction(),
330332
[CommandCodes.OpenTerminal] = new OpenTerminalAction(),
331333
[CommandCodes.OpenTerminalAsAdmin] = new OpenTerminalAsAdminAction(),
332334
[CommandCodes.OpenTerminalFromSidebar] = new OpenTerminalFromSidebarAction(),

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

+1
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
110110
IRichCommand OpenStorageSenseFromHome { get; }
111111
IRichCommand OpenStorageSenseFromSidebar { get; }
112112
IRichCommand OpenSettings { get; }
113+
IRichCommand OpenSettingsFile { get; }
113114
IRichCommand OpenTerminal { get; }
114115
IRichCommand OpenTerminalAsAdmin { get; }
115116
IRichCommand OpenTerminalFromSidebar { get; }

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

+6
Original file line numberDiff line numberDiff line change
@@ -2195,6 +2195,9 @@
21952195
<data name="EditSettingsFile" xml:space="preserve">
21962196
<value>Edit settings file</value>
21972197
</data>
2198+
<data name="EditSettingsFileDescription" xml:space="preserve">
2199+
<value>Open settings file in your default editor</value>
2200+
</data>
21982201
<data name="WhatsNew" xml:space="preserve">
21992202
<value>What's new</value>
22002203
</data>
@@ -3694,6 +3697,9 @@
36943697
<data name="FailedToSetBackground" xml:space="preserve">
36953698
<value>Failed to set the background wallpaper</value>
36963699
</data>
3700+
<data name="FailedToOpenSettingsFile" xml:space="preserve">
3701+
<value>Failed to open the settings file</value>
3702+
</data>
36973703
<data name="GitDeleteBranch" xml:space="preserve">
36983704
<value>Delete Git branch</value>
36993705
</data>

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

+1-12
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ public sealed partial class AdvancedViewModel : ObservableObject
1919
{
2020
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
2121
private ICommonDialogService CommonDialogService { get; } = Ioc.Default.GetRequiredService<ICommonDialogService>();
22+
public ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();
2223

2324
private readonly IFileTagsSettingsService fileTagsSettingsService = Ioc.Default.GetRequiredService<IFileTagsSettingsService>();
2425

2526
public ICommand SetAsDefaultExplorerCommand { get; }
2627
public ICommand SetAsOpenFileDialogCommand { get; }
2728
public ICommand ExportSettingsCommand { get; }
2829
public ICommand ImportSettingsCommand { get; }
29-
public ICommand OpenSettingsJsonCommand { get; }
3030
public AsyncRelayCommand OpenFilesOnWindowsStartupCommand { get; }
3131

3232

@@ -39,22 +39,11 @@ public AdvancedViewModel()
3939
SetAsOpenFileDialogCommand = new AsyncRelayCommand(SetAsOpenFileDialogAsync);
4040
ExportSettingsCommand = new AsyncRelayCommand(ExportSettingsAsync);
4141
ImportSettingsCommand = new AsyncRelayCommand(ImportSettingsAsync);
42-
OpenSettingsJsonCommand = new AsyncRelayCommand(OpenSettingsJsonAsync);
4342
OpenFilesOnWindowsStartupCommand = new AsyncRelayCommand(OpenFilesOnWindowsStartupAsync);
4443

4544
_ = DetectOpenFilesAtStartupAsync();
4645
}
4746

48-
private async Task OpenSettingsJsonAsync()
49-
{
50-
await SafetyExtensions.IgnoreExceptions(async () =>
51-
{
52-
var settingsJsonFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/settings/user_settings.json"));
53-
if (!await Launcher.LaunchFileAsync(settingsJsonFile))
54-
await ContextMenu.InvokeVerb("open", settingsJsonFile.Path);
55-
});
56-
}
57-
5847
private async Task SetAsDefaultExplorerAsync()
5948
{
6049
// Make sure IsSetAsDefaultFileManager is updated

src/Files.App/Views/MainPage.xaml

+10-2
Original file line numberDiff line numberDiff line change
@@ -280,8 +280,7 @@
280280
Background="Transparent"
281281
BorderThickness="0"
282282
Command="{x:Bind Commands.OpenSettings, Mode=OneWay}"
283-
ToolTipService.Placement="Bottom"
284-
ToolTipService.ToolTip="{x:Bind Commands.OpenSettings.LabelWithHotKey, Mode=OneWay}">
283+
ToolTipService.Placement="Bottom">
285284
<Button.Resources>
286285
<SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="{ThemeResource SubtleFillColorSecondary}" />
287286
<SolidColorBrush x:Key="ButtonForegroundDisabled" Color="{ThemeResource TextFillColorPrimary}" />
@@ -308,6 +307,15 @@
308307
Margin="12,0,0,0"
309308
Text="{x:Bind Commands.OpenSettings.Label, Mode=OneWay}" />
310309
</Grid>
310+
311+
<Button.ContextFlyout>
312+
<MenuFlyout>
313+
<MenuFlyoutItem
314+
Command="{x:Bind Commands.OpenSettingsFile, Mode=OneWay}"
315+
Icon="{x:Bind Commands.OpenSettingsFile.FontIcon, Mode=OneWay}"
316+
Text="{x:Bind Commands.OpenSettingsFile.Label, Mode=OneWay}" />
317+
</MenuFlyout>
318+
</Button.ContextFlyout>
311319
</Button>
312320
</StackPanel>
313321
</controls:SidebarView.Footer>

src/Files.App/Views/MainPage.xaml.cs

+1-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public sealed partial class MainPage : Page
2828
private IGeneralSettingsService generalSettingsService { get; } = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
2929
public IUserSettingsService UserSettingsService { get; }
3030
private readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
31-
public ICommandManager Commands { get; }
31+
private readonly ICommandManager Commands = Ioc.Default.GetRequiredService<ICommandManager>();
3232
public SidebarViewModel SidebarAdaptiveViewModel { get; }
3333
public MainPageViewModel ViewModel { get; }
3434

@@ -42,7 +42,6 @@ public MainPage()
4242

4343
// Dependency Injection
4444
UserSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
45-
Commands = Ioc.Default.GetRequiredService<ICommandManager>();
4645
SidebarAdaptiveViewModel = Ioc.Default.GetRequiredService<SidebarViewModel>();
4746
SidebarAdaptiveViewModel.PaneFlyout = (MenuFlyout)Resources["SidebarContextMenu"];
4847
ViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();

src/Files.App/Views/Settings/AdvancedPage.xaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363

6464
<!-- Edit Settings File -->
6565
<wctcontrols:SettingsCard
66-
Command="{x:Bind ViewModel.OpenSettingsJsonCommand}"
66+
Command="{x:Bind ViewModel.Commands.OpenSettingsFile, Mode=OneWay}"
6767
Header="{helpers:ResourceString Name=EditSettingsFile}"
6868
IsClickEnabled="True">
6969
<wctcontrols:SettingsCard.HeaderIcon>

0 commit comments

Comments
 (0)