Skip to content

Commit 833bded

Browse files
ferrariofilippoyaira2
authored andcommitted
Use default IDE
1 parent 0a0564d commit 833bded

File tree

6 files changed

+129
-8
lines changed

6 files changed

+129
-8
lines changed

src/Files.App/Actions/Open/OpenInIDEAction.cs

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Microsoft.UI.Xaml.Controls;
5+
46
namespace Files.App.Actions
57
{
68
internal sealed class OpenInIDEAction : ObservableObject, IAction
@@ -31,12 +33,15 @@ public OpenInIDEAction()
3133
_devToolsSettingsService.PropertyChanged += DevSettings_PropertyChanged;
3234
}
3335

34-
public Task ExecuteAsync(object? parameter = null)
36+
public async Task ExecuteAsync(object? parameter = null)
3537
{
36-
return Win32Helper.RunPowershellCommandAsync(
38+
var res = await Win32Helper.RunPowershellCommandAsync(
3739
$"& \'{_devToolsSettingsService.IDEPath}\' \'{_context.ShellPage?.ShellViewModel.WorkingDirectory}\'",
3840
PowerShellExecutionOptions.Hidden
3941
);
42+
43+
if (!res)
44+
await ShowErrorDialog();
4045
}
4146

4247
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
@@ -57,5 +62,24 @@ private void DevSettings_PropertyChanged(object? sender, PropertyChangedEventArg
5762
OnPropertyChanged(nameof(Description));
5863
}
5964
}
65+
66+
private async Task ShowErrorDialog()
67+
{
68+
var commands = Ioc.Default.GetRequiredService<ICommandManager>();
69+
var errorDialog = new ContentDialog()
70+
{
71+
Title = Strings.IDEError.GetLocalizedResource(),
72+
Content = Strings.SelectedIDENotValid.GetLocalizedResource(),
73+
PrimaryButtonText = Strings.OK.GetLocalizedResource(),
74+
SecondaryButtonText = Strings.EditInSettings.GetLocalizedResource(),
75+
};
76+
77+
if (await errorDialog.TryShowAsync() == ContentDialogResult.Secondary)
78+
{
79+
await commands.OpenSettings.ExecuteAsync(
80+
new SettingsNavigationParams() { PageKind = SettingsPageKind.DevToolsPage }
81+
);
82+
}
83+
}
6084
}
6185
}

src/Files.App/Actions/Open/OpenRepoInIDEAction.cs

Lines changed: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
// Copyright (c) Files Community
22
// Licensed under the MIT License.
33

4+
using Microsoft.UI.Xaml.Controls;
5+
46
namespace Files.App.Actions
5-
{
7+
{
68
internal sealed class OpenRepoInIDEAction : ObservableObject, IAction
79
{
810
private readonly IDevToolsSettingsService _devToolsSettingsService;
@@ -28,12 +30,15 @@ public OpenRepoInIDEAction()
2830
_devToolsSettingsService.PropertyChanged += DevSettings_PropertyChanged;
2931
}
3032

31-
public Task ExecuteAsync(object? parameter = null)
33+
public async Task ExecuteAsync(object? parameter = null)
3234
{
33-
return Win32Helper.RunPowershellCommandAsync(
35+
var res = await Win32Helper.RunPowershellCommandAsync(
3436
$"& \'{_devToolsSettingsService.IDEPath}\' \'{_context.ShellPage!.InstanceViewModel.GitRepositoryPath}\'",
3537
PowerShellExecutionOptions.Hidden
3638
);
39+
40+
if (!res)
41+
await ShowErrorDialog();
3742
}
3843

3944
private void Context_PropertyChanged(object? sender, PropertyChangedEventArgs e)
@@ -54,5 +59,24 @@ private void DevSettings_PropertyChanged(object? sender, PropertyChangedEventArg
5459
OnPropertyChanged(nameof(Description));
5560
}
5661
}
62+
63+
private async Task ShowErrorDialog()
64+
{
65+
var commands = Ioc.Default.GetRequiredService<ICommandManager>();
66+
var errorDialog = new ContentDialog()
67+
{
68+
Title = Strings.IDEError.GetLocalizedResource(),
69+
Content = Strings.SelectedIDENotValid.GetLocalizedResource(),
70+
PrimaryButtonText = Strings.OK.GetLocalizedResource(),
71+
SecondaryButtonText = Strings.EditInSettings.GetLocalizedResource(),
72+
};
73+
74+
if (await errorDialog.TryShowAsync() == ContentDialogResult.Secondary)
75+
{
76+
await commands.OpenSettings.ExecuteAsync(
77+
new SettingsNavigationParams() { PageKind = SettingsPageKind.DevToolsPage }
78+
);
79+
}
80+
}
5781
}
5882
}
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.Win32;
5+
using System.Security;
6+
7+
namespace Files.App.Helpers
8+
{
9+
internal static class SoftwareHelpers
10+
{
11+
private const string UninstallRegistryKey = @"Software\Microsoft\Windows\CurrentVersion\Uninstall";
12+
13+
private const string VsCodeName = "Microsoft Visual Studio Code";
14+
15+
public static bool IsVSCodeInstalled()
16+
{
17+
try
18+
{
19+
return
20+
ContainsName(Registry.CurrentUser.OpenSubKey(UninstallRegistryKey), VsCodeName) ||
21+
ContainsName(Registry.LocalMachine.OpenSubKey(UninstallRegistryKey), VsCodeName);
22+
}
23+
catch (SecurityException)
24+
{
25+
// Handle edge case where OpenSubKey results in SecurityException
26+
return false;
27+
}
28+
}
29+
30+
private static bool ContainsName(RegistryKey? key, string find)
31+
{
32+
if (key is null)
33+
return false;
34+
35+
try
36+
{
37+
foreach (var subKey in key.GetSubKeyNames().Select(key.OpenSubKey))
38+
{
39+
var displayName = subKey?.GetValue("DisplayName") as string;
40+
if (!string.IsNullOrWhiteSpace(displayName) && displayName.StartsWith(find))
41+
{
42+
key.Close();
43+
44+
return true;
45+
}
46+
}
47+
48+
key.Close();
49+
50+
return false;
51+
}
52+
catch (SecurityException)
53+
{
54+
// Handle edge case where OpenSubKey results in SecurityException
55+
return false;
56+
}
57+
}
58+
}
59+
}

src/Files.App/Services/Settings/DevToolsSettingsService.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace Files.App.Services.Settings
55
{
66
internal sealed partial class DevToolsSettingsService : BaseObservableJsonSettings, IDevToolsSettingsService
77
{
8+
private bool _isVSCodeInstalled = SoftwareHelpers.IsVSCodeInstalled();
9+
810
public DevToolsSettingsService(ISettingsSharingContext settingsSharingContext)
911
{
1012
// Register root
@@ -21,14 +23,14 @@ public OpenInIDEOption OpenInIDEOption
2123
/// <inheritdoc/>
2224
public string IDEPath
2325
{
24-
get => Get("") ?? "";
26+
get => Get(_isVSCodeInstalled ? "code" : string.Empty) ?? string.Empty;
2527
set => Set(value);
2628
}
2729

2830
/// <inheritdoc/>
2931
public string FriendlyIDEName
3032
{
31-
get => Get("") ?? "";
33+
get => Get(_isVSCodeInstalled ? Strings.VisualStudioCode.GetLocalizedResource() : string.Empty) ?? string.Empty;
3234
set => Set(value);
3335
}
3436

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

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4055,4 +4055,16 @@
40554055
<data name="Test" xml:space="preserve">
40564056
<value>Test</value>
40574057
</data>
4058+
<data name="SelectedIDENotValid" xml:space="preserve">
4059+
<value>Selected IDE is not valid</value>
4060+
</data>
4061+
<data name="IDEError" xml:space="preserve">
4062+
<value>IDE Error</value>
4063+
</data>
4064+
<data name="EditInSettings" xml:space="preserve">
4065+
<value>Edit in settings</value>
4066+
</data>
4067+
<data name="VisualStudioCode" xml:space="preserve">
4068+
<value>Visual Studio Code</value>
4069+
</data>
40584070
</root>

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -247,7 +247,7 @@
247247
<Button
248248
Command="{x:Bind ViewModel.SaveIDEChangesCommand, Mode=OneWay}"
249249
Content="{helpers:ResourceString Name=Save}"
250-
IsEnabled="{x:Bind ViewModel.CanSaveIDEChanges, Mode=OneWay}"
250+
IsEnabled="{x:Bind ViewModel.CanSaveIDEChanges, Mode=OneWay, FallbackValue=False}"
251251
Style="{StaticResource AccentButtonStyle}" />
252252
</StackPanel>
253253
</StackPanel>

0 commit comments

Comments
 (0)