Skip to content

Feature: Added option to open settings file when right clicking settings button #17004

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Apr 1, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/Files.App/Actions/Open/OpenSettingsFileAction.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
// Copyright (c) Files Community
// Licensed under the MIT License.

using Microsoft.UI.Xaml.Controls;
using Microsoft.UI.Xaml.Media;
using Windows.Foundation.Metadata;
using Windows.Storage;
using Windows.System;

namespace Files.App.Actions
{
internal sealed partial class OpenSettingsFileAction : IAction
{
public string Label
=> Strings.EditSettingsFile.GetLocalizedResource();

public string Description
=> Strings.EditSettingsFileDescription.GetLocalizedResource();

public HotKey HotKey
=> new(Keys.OemComma, KeyModifiers.CtrlShift);

public RichGlyph Glyph
=> new("\uE8DA");

public async Task ExecuteAsync(object? parameter = null)
{
try
{
var settingsJsonFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri($"ms-appdata:///local/{Constants.LocalSettings.SettingsFolderName}/{Constants.LocalSettings.UserSettingsFileName}"));
if (!await Launcher.LaunchFileAsync(settingsJsonFile))
await ContextMenu.InvokeVerb("open", settingsJsonFile.Path);
}
catch (Exception ex)
{
// Only show the error dialog if no other popups are open
if (!VisualTreeHelper.GetOpenPopupsForXamlRoot(MainWindow.Instance.Content.XamlRoot).Any())
{
var errorDialog = new ContentDialog()
{
Title = Strings.FailedToOpenSettingsFile.GetLocalizedResource(),
Content = ex.Message,
PrimaryButtonText = Strings.OK.GetLocalizedResource(),
};

if (ApiInformation.IsApiContractPresent("Windows.Foundation.UniversalApiContract", 8))
errorDialog.XamlRoot = MainWindow.Instance.Content.XamlRoot;

await errorDialog.TryShowAsync();
}
}
}
}
}
1 change: 1 addition & 0 deletions src/Files.App/Data/Commands/Manager/CommandCodes.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,7 @@ public enum CommandCodes
OpenReleaseNotes,
OpenClassicProperties,
OpenSettings,
OpenSettingsFile,
OpenStorageSense,
OpenStorageSenseFromHome,
OpenStorageSenseFromSidebar,
Expand Down
2 changes: 2 additions & 0 deletions src/Files.App/Data/Commands/Manager/CommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,7 @@ public IRichCommand this[HotKey hotKey]
public IRichCommand OpenStorageSenseFromHome => commands[CommandCodes.OpenStorageSenseFromHome];
public IRichCommand OpenStorageSenseFromSidebar => commands[CommandCodes.OpenStorageSenseFromSidebar];
public IRichCommand OpenSettings => commands[CommandCodes.OpenSettings];
public IRichCommand OpenSettingsFile => commands[CommandCodes.OpenSettingsFile];
public IRichCommand OpenTerminal => commands[CommandCodes.OpenTerminal];
public IRichCommand OpenTerminalAsAdmin => commands[CommandCodes.OpenTerminalAsAdmin];
public IRichCommand OpenTerminalFromSidebar => commands[CommandCodes.OpenTerminalFromSidebar];
Expand Down Expand Up @@ -327,6 +328,7 @@ public IEnumerator<IRichCommand> GetEnumerator() =>
[CommandCodes.OpenStorageSenseFromHome] = new OpenStorageSenseFromHomeAction(),
[CommandCodes.OpenStorageSenseFromSidebar] = new OpenStorageSenseFromSidebarAction(),
[CommandCodes.OpenSettings] = new OpenSettingsAction(),
[CommandCodes.OpenSettingsFile] = new OpenSettingsFileAction(),
[CommandCodes.OpenTerminal] = new OpenTerminalAction(),
[CommandCodes.OpenTerminalAsAdmin] = new OpenTerminalAsAdminAction(),
[CommandCodes.OpenTerminalFromSidebar] = new OpenTerminalFromSidebarAction(),
Expand Down
1 change: 1 addition & 0 deletions src/Files.App/Data/Commands/Manager/ICommandManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ public interface ICommandManager : IEnumerable<IRichCommand>
IRichCommand OpenStorageSenseFromHome { get; }
IRichCommand OpenStorageSenseFromSidebar { get; }
IRichCommand OpenSettings { get; }
IRichCommand OpenSettingsFile { get; }
IRichCommand OpenTerminal { get; }
IRichCommand OpenTerminalAsAdmin { get; }
IRichCommand OpenTerminalFromSidebar { get; }
Expand Down
6 changes: 6 additions & 0 deletions src/Files.App/Strings/en-US/Resources.resw
Original file line number Diff line number Diff line change
Expand Up @@ -2195,6 +2195,9 @@
<data name="EditSettingsFile" xml:space="preserve">
<value>Edit settings file</value>
</data>
<data name="EditSettingsFileDescription" xml:space="preserve">
<value>Open settings file in your default editor</value>
</data>
<data name="WhatsNew" xml:space="preserve">
<value>What's new</value>
</data>
Expand Down Expand Up @@ -3694,6 +3697,9 @@
<data name="FailedToSetBackground" xml:space="preserve">
<value>Failed to set the background wallpaper</value>
</data>
<data name="FailedToOpenSettingsFile" xml:space="preserve">
<value>Failed to open the settings file</value>
</data>
<data name="GitDeleteBranch" xml:space="preserve">
<value>Delete Git branch</value>
</data>
Expand Down
13 changes: 1 addition & 12 deletions src/Files.App/ViewModels/Settings/AdvancedViewModel.cs
Original file line number Diff line number Diff line change
Expand Up @@ -19,14 +19,14 @@ public sealed partial class AdvancedViewModel : ObservableObject
{
private IUserSettingsService UserSettingsService { get; } = Ioc.Default.GetRequiredService<IUserSettingsService>();
private ICommonDialogService CommonDialogService { get; } = Ioc.Default.GetRequiredService<ICommonDialogService>();
public ICommandManager Commands { get; } = Ioc.Default.GetRequiredService<ICommandManager>();

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

public ICommand SetAsDefaultExplorerCommand { get; }
public ICommand SetAsOpenFileDialogCommand { get; }
public ICommand ExportSettingsCommand { get; }
public ICommand ImportSettingsCommand { get; }
public ICommand OpenSettingsJsonCommand { get; }
public AsyncRelayCommand OpenFilesOnWindowsStartupCommand { get; }


Expand All @@ -39,22 +39,11 @@ public AdvancedViewModel()
SetAsOpenFileDialogCommand = new AsyncRelayCommand(SetAsOpenFileDialogAsync);
ExportSettingsCommand = new AsyncRelayCommand(ExportSettingsAsync);
ImportSettingsCommand = new AsyncRelayCommand(ImportSettingsAsync);
OpenSettingsJsonCommand = new AsyncRelayCommand(OpenSettingsJsonAsync);
OpenFilesOnWindowsStartupCommand = new AsyncRelayCommand(OpenFilesOnWindowsStartupAsync);

_ = DetectOpenFilesAtStartupAsync();
}

private async Task OpenSettingsJsonAsync()
{
await SafetyExtensions.IgnoreExceptions(async () =>
{
var settingsJsonFile = await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/settings/user_settings.json"));
if (!await Launcher.LaunchFileAsync(settingsJsonFile))
await ContextMenu.InvokeVerb("open", settingsJsonFile.Path);
});
}

private async Task SetAsDefaultExplorerAsync()
{
// Make sure IsSetAsDefaultFileManager is updated
Expand Down
12 changes: 10 additions & 2 deletions src/Files.App/Views/MainPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -280,8 +280,7 @@
Background="Transparent"
BorderThickness="0"
Command="{x:Bind Commands.OpenSettings, Mode=OneWay}"
ToolTipService.Placement="Bottom"
ToolTipService.ToolTip="{x:Bind Commands.OpenSettings.LabelWithHotKey, Mode=OneWay}">
ToolTipService.Placement="Bottom">
<Button.Resources>
<SolidColorBrush x:Key="ButtonBackgroundPointerOver" Color="{ThemeResource SubtleFillColorSecondary}" />
<SolidColorBrush x:Key="ButtonForegroundDisabled" Color="{ThemeResource TextFillColorPrimary}" />
Expand All @@ -308,6 +307,15 @@
Margin="12,0,0,0"
Text="{x:Bind Commands.OpenSettings.Label, Mode=OneWay}" />
</Grid>

<Button.ContextFlyout>
<MenuFlyout>
<MenuFlyoutItem
Command="{x:Bind Commands.OpenSettingsFile, Mode=OneWay}"
Icon="{x:Bind Commands.OpenSettingsFile.FontIcon, Mode=OneWay}"
Text="{x:Bind Commands.OpenSettingsFile.Label, Mode=OneWay}" />
</MenuFlyout>
</Button.ContextFlyout>
</Button>
</StackPanel>
</controls:SidebarView.Footer>
Expand Down
3 changes: 1 addition & 2 deletions src/Files.App/Views/MainPage.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public sealed partial class MainPage : Page
private IGeneralSettingsService generalSettingsService { get; } = Ioc.Default.GetRequiredService<IGeneralSettingsService>();
public IUserSettingsService UserSettingsService { get; }
private readonly IWindowContext WindowContext = Ioc.Default.GetRequiredService<IWindowContext>();
public ICommandManager Commands { get; }
private readonly ICommandManager Commands = Ioc.Default.GetRequiredService<ICommandManager>();
public SidebarViewModel SidebarAdaptiveViewModel { get; }
public MainPageViewModel ViewModel { get; }

Expand All @@ -42,7 +42,6 @@ public MainPage()

// Dependency Injection
UserSettingsService = Ioc.Default.GetRequiredService<IUserSettingsService>();
Commands = Ioc.Default.GetRequiredService<ICommandManager>();
SidebarAdaptiveViewModel = Ioc.Default.GetRequiredService<SidebarViewModel>();
SidebarAdaptiveViewModel.PaneFlyout = (MenuFlyout)Resources["SidebarContextMenu"];
ViewModel = Ioc.Default.GetRequiredService<MainPageViewModel>();
Expand Down
2 changes: 1 addition & 1 deletion src/Files.App/Views/Settings/AdvancedPage.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@

<!-- Edit Settings File -->
<wctcontrols:SettingsCard
Command="{x:Bind ViewModel.OpenSettingsJsonCommand}"
Command="{x:Bind ViewModel.Commands.OpenSettingsFile, Mode=OneWay}"
Header="{helpers:ResourceString Name=EditSettingsFile}"
IsClickEnabled="True">
<wctcontrols:SettingsCard.HeaderIcon>
Expand Down
Loading