Skip to content

Commit 0be17e7

Browse files
committed
Feat: Configure Terminal Applications
1 parent 20c4529 commit 0be17e7

File tree

8 files changed

+176
-12
lines changed

8 files changed

+176
-12
lines changed

Files/App.xaml.cs

Lines changed: 35 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@
3030
using Windows.Storage.Streams;
3131
using Windows.System;
3232
using Microsoft.UI.Xaml.Controls;
33+
using System.Threading.Tasks;
34+
using Windows.ApplicationModel.AppService;
35+
using Windows.ApplicationModel.AppExtensions;
36+
using Files.DataModels;
37+
using Newtonsoft.Json;
3338

3439
namespace Files
3540
{
@@ -52,9 +57,9 @@ public static ProHome OccupiedInstance
5257
}
5358
set
5459
{
55-
if(value != occupiedInstance)
60+
if (value != occupiedInstance)
5661
{
57-
occupiedInstance = value;
62+
occupiedInstance = value;
5863
}
5964
}
6065
}
@@ -68,6 +73,7 @@ public static ProHome OccupiedInstance
6873
public static ObservableCollection<WSLDistroItem> linuxDistroItems = new ObservableCollection<WSLDistroItem>();
6974
public static FormFactorMode FormFactor { get; set; } = FormFactorMode.Regular;
7075
ApplicationDataContainer localSettings;
76+
public static IList<TerminalModel> Terminals {get; set;}
7177

7278
public App()
7379
{
@@ -87,6 +93,33 @@ public App()
8793
PopulatePinnedSidebarItems();
8894
DetectWSLDistros();
8995
QuickLookIntegration();
96+
LoadTerminalApps();
97+
}
98+
99+
private async void LoadTerminalApps()
100+
{
101+
ApplicationData.Current.LocalSettings.Values["terminal_id"] = 1;
102+
103+
var localFolder = ApplicationData.Current.LocalFolder;
104+
var localSettingsFolder = await localFolder.CreateFolderAsync("settings", CreationCollisionOption.OpenIfExists);
105+
StorageFile file;
106+
try
107+
{
108+
file = await localSettingsFolder.GetFileAsync("terminal.json");
109+
}
110+
catch (FileNotFoundException ex)
111+
{
112+
var defaultFile = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/terminal/terminal.json"));
113+
114+
file = await localSettingsFolder.CreateFileAsync("terminal.json");
115+
await FileIO.WriteBufferAsync(file, await FileIO.ReadBufferAsync(await defaultFile));
116+
}
117+
118+
var content = await FileIO.ReadTextAsync(file);
119+
120+
var terminals = JsonConvert.DeserializeObject<TerminalFileModel>(content).Terminals;
121+
122+
Terminals = terminals;
90123
}
91124

92125
public void CloseOpenPopups()

Files/Assets/terminal/terminal.json

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
{
2+
"version": 1,
3+
"terminals": [
4+
{
5+
"id": 1,
6+
"name": "CMD",
7+
"path": "cmd.exe",
8+
"arguments": "/k \"cd /d {0} && title Command Prompt\"",
9+
"icon": "C:\\Users\\felix\\Pictures\\BingImageOfTheDay_20190730.jpg"
10+
}
11+
/*
12+
{
13+
"id": 2,
14+
"name": "PowerShell Core 6",
15+
"path": "pwsh.exe",
16+
"arguments": "-WorkingDirectory {0}",
17+
"icon": "C:\\Users\\felix\\Pictures\\BingImageOfTheDay_20190730.jpg"
18+
}
19+
*/
20+
]
21+
}

Files/DataModels/TerminalFileModel.cs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Files.DataModels
8+
{
9+
class TerminalFileModel
10+
{
11+
public int Version { get; set; }
12+
13+
public List<TerminalModel> Terminals { get; set; }
14+
}
15+
}

Files/DataModels/TerminalModel.cs

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.Linq;
4+
using System.Text;
5+
using System.Threading.Tasks;
6+
7+
namespace Files.DataModels
8+
{
9+
public class TerminalModel
10+
{
11+
public int Id { get; set; }
12+
13+
public string Name { get; set; }
14+
15+
public string Path { get; set; }
16+
17+
public string icon { get; set; }
18+
19+
public string arguments { get; set; }
20+
}
21+
}

Files/Files.csproj

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,8 @@
145145
<Compile Include="Controls\RibbonPage.xaml.cs">
146146
<DependentUpon>RibbonPage.xaml</DependentUpon>
147147
</Compile>
148+
<Compile Include="DataModels\TerminalFileModel.cs" />
149+
<Compile Include="DataModels\TerminalModel.cs" />
148150
<Compile Include="Dialogs\AddItemDialog.xaml.cs">
149151
<DependentUpon>AddItemDialog.xaml</DependentUpon>
150152
</Compile>
@@ -232,6 +234,9 @@
232234
<Content Include="Assets\FilesHome.png" />
233235
<Content Include="Assets\FilesDrive.png" />
234236
<Content Include="Assets\logo.bmp" />
237+
<None Include="Assets\terminal\terminal.json">
238+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
239+
</None>
235240
<None Include="Package.StoreAssociation.xml" />
236241
<Content Include="Assets\QuickLook\quicklook_icon_black.png" />
237242
<Content Include="Assets\StoreLogo.png" />
@@ -384,6 +389,9 @@
384389
<PackageReference Include="MvvmLight">
385390
<Version>5.4.1.1</Version>
386391
</PackageReference>
392+
<PackageReference Include="Newtonsoft.Json">
393+
<Version>12.0.3</Version>
394+
</PackageReference>
387395
</ItemGroup>
388396
<ItemGroup>
389397
<SDKReference Include="WindowsDesktop, Version=10.0.18362.0">

Files/Interacts/Interaction.cs

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -93,16 +93,12 @@ public void OpenDirectoryInNewTab_Click(object sender, RoutedEventArgs e)
9393

9494
public async void OpenDirectoryInTerminal(object sender, RoutedEventArgs e)
9595
{
96+
var localSettings = ApplicationData.Current.LocalSettings;
97+
var terminalId = (int)localSettings.Values["terminal_id"];
98+
var terminal = App.Terminals.Single(p => p.Id == terminalId);
9699

97-
ApplicationData.Current.LocalSettings.Values["Application"] = "cmd.exe";
98-
if(App.OccupiedInstance.ItemDisplayFrame.SourcePageType == typeof(GenericFileBrowser))
99-
{
100-
ApplicationData.Current.LocalSettings.Values["Arguments"] = "/k \"cd /d "+ currentInstance.instanceViewModel.Universal.path + "&& title Command Prompt" + "\"";
101-
}
102-
else if(App.OccupiedInstance.ItemDisplayFrame.SourcePageType == typeof(PhotoAlbum))
103-
{
104-
ApplicationData.Current.LocalSettings.Values["Arguments"] = "/k \"cd /d " + currentInstance.instanceViewModel.Universal.path + "&& title Command Prompt" + "\"";
105-
}
100+
localSettings.Values["Application"] = terminal.Path;
101+
localSettings.Values["Arguments"] = String.Format(terminal.arguments, currentInstance.instanceViewModel.Universal.path);
106102

107103
await FullTrustProcessLauncher.LaunchFullTrustProcessForCurrentAppAsync();
108104
}

Files/SettingsPages/Preferences.xaml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
55
xmlns:local="using:Files.SettingsPages"
66
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
7-
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
7+
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:datamodels="using:Files.DataModels"
88
mc:Ignorable="d"
99
Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
1010

@@ -53,6 +53,26 @@
5353
</StackPanel>
5454
</StackPanel>
5555
</Grid>
56+
57+
<Grid Margin="0,18,0,0">
58+
<StackPanel Orientation="Vertical">
59+
<TextBlock FontSize="20" Text="Terminal Applications"/>
60+
61+
<ComboBox
62+
Header="Choose Terminal Application"
63+
ItemsSource="{x:Bind Terminals}"
64+
SelectionChanged="TerminalApp_SelectionChanged">
65+
<ComboBox.ItemTemplate>
66+
<DataTemplate x:DataType="datamodels:TerminalModel">
67+
<StackPanel Orientation="Horizontal">
68+
<TextBlock Text="{x:Bind Name}"/>
69+
</StackPanel>
70+
</DataTemplate>
71+
</ComboBox.ItemTemplate>
72+
</ComboBox>
73+
<HyperlinkButton Content="Edit Terminal Applications" Click="EditTerminalApplications_Click"/>
74+
</StackPanel>
75+
</Grid>
5676
</StackPanel>
5777
</ScrollViewer>
5878
</Grid>

Files/SettingsPages/Preferences.xaml.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,18 @@
55
using Windows.UI;
66
using System.IO;
77
using Files.Filesystem;
8+
using Newtonsoft.Json;
9+
using Files.DataModels;
10+
using System.Collections.ObjectModel;
11+
using Windows.System;
812

913
namespace Files.SettingsPages
1014
{
1115

1216
public sealed partial class Preferences : Page
1317
{
18+
ObservableCollection<TerminalModel> Terminals { get; } = new ObservableCollection<TerminalModel>();
19+
1420
ApplicationDataContainer localSettings = ApplicationData.Current.LocalSettings;
1521
StorageFolder localFolder = ApplicationData.Current.LocalFolder;
1622
public Preferences()
@@ -72,6 +78,31 @@ public Preferences()
7278
OneDriveL.IsEnabled = false;
7379
}
7480
SuccessMark.Visibility = Windows.UI.Xaml.Visibility.Collapsed;
81+
82+
LoadTerminalsAsync();
83+
}
84+
85+
private async System.Threading.Tasks.Task LoadTerminalsAsync()
86+
{
87+
var localSettingsFolder = await localFolder.CreateFolderAsync("settings", CreationCollisionOption.OpenIfExists);
88+
StorageFile file;
89+
try
90+
{
91+
file = await localSettingsFolder.GetFileAsync("terminal.json");
92+
}
93+
catch (FileNotFoundException ex)
94+
{
95+
var defaultFile = StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appx:///Assets/terminal/terminal.json"));
96+
97+
file = await localSettingsFolder.CreateFileAsync("terminal.json");
98+
await FileIO.WriteBufferAsync(file, await FileIO.ReadBufferAsync(await defaultFile));
99+
}
100+
101+
var content = await FileIO.ReadTextAsync(file);
102+
103+
var terminals = JsonConvert.DeserializeObject<TerminalFileModel>(content).Terminals;
104+
105+
foreach (var terminal in terminals) Terminals.Add(terminal);
75106
}
76107

77108
private void ToggleSwitch_Toggled(object sender, Windows.UI.Xaml.RoutedEventArgs e)
@@ -276,5 +307,24 @@ private async void SaveCustomL_Click(object sender, Windows.UI.Xaml.RoutedEventA
276307
SuccessMark.Visibility = Windows.UI.Xaml.Visibility.Visible;
277308
}
278309
}
310+
311+
private async void EditTerminalApplications_Click(object sender, Windows.UI.Xaml.RoutedEventArgs e)
312+
{
313+
LaunchTerminalsConfigFile();
314+
}
315+
316+
private async void LaunchTerminalsConfigFile()
317+
{
318+
Launcher.LaunchFileAsync(await StorageFile.GetFileFromApplicationUriAsync(new Uri("ms-appdata:///local/settings/terminal.json")));
319+
}
320+
321+
private void TerminalApp_SelectionChanged(object sender, SelectionChangedEventArgs e)
322+
{
323+
var comboBox = (ComboBox)sender;
324+
325+
var selectedTerminal = (TerminalModel)comboBox.SelectedItem;
326+
327+
localSettings.Values["terminal_id"] = selectedTerminal.Id;
328+
}
279329
}
280330
}

0 commit comments

Comments
 (0)