Skip to content

Commit 3387f31

Browse files
committed
Implemented input to change the Process started
1 parent 7f942a0 commit 3387f31

File tree

3 files changed

+83
-23
lines changed

3 files changed

+83
-23
lines changed

Background-Terminal/BackgroundTerminalSettings.cs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
using System;
1+
using Newtonsoft.Json;
2+
using System;
23
using System.Collections.Generic;
4+
using System.ComponentModel;
35
using System.IO;
46
using System.Text;
57

68
namespace Background_Terminal
79
{
810
public class BackgroundTerminalSettings
911
{
12+
[DefaultValue("cmd.exe")]
13+
[JsonProperty(DefaultValueHandling = DefaultValueHandling.Populate)]
14+
public string ProcessPath { get; set; }
1015
public int Key1 { get; set; }
1116
public int Key2 { get; set; }
1217
public double FontSize { get; set; }

Background-Terminal/MainWindow.xaml

Lines changed: 38 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
xmlns:tb="http://www.hardcodet.net/taskbar"
88
Title="Background Terminal"
99
Width="400"
10-
Height="410"
10+
Height="450"
1111
AllowsTransparency="True"
1212
Background="Transparent"
1313
Closed="MainWindow_Closed"
@@ -98,7 +98,6 @@
9898
</Setter>
9999
</Style>
100100

101-
102101
<Style x:Key="PrimaryButtonRight" TargetType="Button">
103102
<Setter Property="Template">
104103
<Setter.Value>
@@ -176,6 +175,8 @@
176175

177176
<Grid Grid.Row="1">
178177
<Grid.RowDefinitions>
178+
<RowDefinition Height="30" />
179+
<RowDefinition Height="10" />
179180
<RowDefinition Height="30" />
180181
<RowDefinition Height="30" />
181182
<RowDefinition Height="30" />
@@ -187,8 +188,32 @@
187188
<RowDefinition Height="100" />
188189
<RowDefinition Height="30" />
189190
</Grid.RowDefinitions>
190-
<Border Grid.RowSpan="9" Background="#F5F5F5" />
191-
<Grid Grid.Row="0" VerticalAlignment="Center">
191+
<Border Grid.RowSpan="11" Background="#F5F5F5" />
192+
<Grid Grid.Row="0">
193+
<Grid.ColumnDefinitions>
194+
<ColumnDefinition Width="1*" />
195+
<ColumnDefinition Width="1*" />
196+
</Grid.ColumnDefinitions>
197+
<Label
198+
Grid.Column="0"
199+
VerticalAlignment="Center"
200+
HorizontalContentAlignment="Center"
201+
Content="Process:"
202+
FontFamily="Yu Gothic UI Light" />
203+
<TextBox
204+
x:Name="Process_TextBox"
205+
Grid.Column="1"
206+
Margin="10,5,10,5"
207+
VerticalContentAlignment="Center" />
208+
</Grid>
209+
<Grid Grid.Row="1">
210+
<Border
211+
Height="1"
212+
Margin="10,0,10,0"
213+
BorderBrush="Gray"
214+
BorderThickness="1" />
215+
</Grid>
216+
<Grid Grid.Row="2" VerticalAlignment="Center">
192217
<Grid.ColumnDefinitions>
193218
<ColumnDefinition Width="1*" />
194219
<ColumnDefinition Width="1*" />
@@ -230,7 +255,7 @@
230255
Style="{StaticResource StandardButton1}" />
231256
</Grid>
232257
</Grid>
233-
<Grid Grid.Row="1">
258+
<Grid Grid.Row="3">
234259
<Grid.ColumnDefinitions>
235260
<ColumnDefinition Width="1*" />
236261
<ColumnDefinition Width="1*" />
@@ -247,7 +272,7 @@
247272
Margin="10,5,10,5"
248273
VerticalContentAlignment="Center" />
249274
</Grid>
250-
<Grid Grid.Row="2">
275+
<Grid Grid.Row="4">
251276
<Grid.ColumnDefinitions>
252277
<ColumnDefinition Width="1*" />
253278
<ColumnDefinition Width="1*" />
@@ -264,14 +289,14 @@
264289
Margin="10,5,10,5"
265290
VerticalContentAlignment="Center" />
266291
</Grid>
267-
<Grid Grid.Row="3">
292+
<Grid Grid.Row="5">
268293
<Border
269294
Height="1"
270295
Margin="10,0,10,0"
271296
BorderBrush="Gray"
272297
BorderThickness="1" />
273298
</Grid>
274-
<Grid Grid.Row="4">
299+
<Grid Grid.Row="6">
275300
<Grid.ColumnDefinitions>
276301
<ColumnDefinition Width="1*" />
277302
<ColumnDefinition Width="1*" />
@@ -292,7 +317,7 @@
292317
Style="{StaticResource StandardButton1}" />
293318
</Grid>
294319
</Grid>
295-
<Grid Grid.Row="5">
320+
<Grid Grid.Row="7">
296321
<Grid.ColumnDefinitions>
297322
<ColumnDefinition Width="1*" />
298323
<ColumnDefinition Width="1*" />
@@ -335,7 +360,7 @@
335360
FontFamily="Yu Gothic UI" />
336361
</Grid>
337362
</Grid>
338-
<Grid Grid.Row="6">
363+
<Grid Grid.Row="8">
339364
<Grid.ColumnDefinitions>
340365
<ColumnDefinition Width="1*" />
341366
<ColumnDefinition Width="1*" />
@@ -378,14 +403,14 @@
378403
FontFamily="Yu Gothic UI" />
379404
</Grid>
380405
</Grid>
381-
<Grid Grid.Row="7">
406+
<Grid Grid.Row="9">
382407
<Border
383408
Height="1"
384409
Margin="10,0,10,0"
385410
BorderBrush="Gray"
386411
BorderThickness="1" />
387412
</Grid>
388-
<Grid Grid.Row="8">
413+
<Grid Grid.Row="10">
389414
<Grid.RowDefinitions>
390415
<RowDefinition Height="30" />
391416
<RowDefinition Height="1*" />
@@ -464,7 +489,7 @@
464489
</ListBox.ItemTemplate>
465490
</ListBox>
466491
</Grid>
467-
<Grid Grid.Row="9">
492+
<Grid Grid.Row="11">
468493
<Grid.ColumnDefinitions>
469494
<ColumnDefinition Width="1*" />
470495
<ColumnDefinition Width="1" />

Background-Terminal/MainWindow.xaml.cs

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public partial class MainWindow : Window
4242
private ObservableCollection<string> _terminalData = new ObservableCollection<string>();
4343
public ObservableCollection<NewlineTrigger> NewlineTriggers { get; set; }
4444

45+
private string _processPath;
4546
private string _currentTrigger = null;
4647
private string _newlineString = Environment.NewLine;
4748

@@ -112,6 +113,7 @@ public MainWindow()
112113
_key1 = KeyInterop.KeyFromVirtualKey(_settings.Key1);
113114
_key2 = KeyInterop.KeyFromVirtualKey(_settings.Key2);
114115

116+
Process_TextBox.Text = _settings.ProcessPath;
115117
Key1_Button.Content = _key1.ToString();
116118
Key2_Button.Content = _key2.ToString();
117119
FontSize_TextBox.Text = _settings.FontSize.ToString();
@@ -170,9 +172,15 @@ private async Task<int> RunTerminalProcessAsync()
170172
{
171173
TaskCompletionSource<int> taskCompletionSource = new TaskCompletionSource<int>();
172174

175+
try
176+
{
177+
_process?.Kill();
178+
}
179+
catch (Exception e) { }
180+
173181
_process = new Process();
174182

175-
_process.StartInfo.FileName = "cmd.exe";
183+
_process.StartInfo.FileName = _settings.ProcessPath;
176184
_process.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
177185
_process.StartInfo.UseShellExecute = false;
178186
_process.StartInfo.CreateNoWindow = true;
@@ -186,17 +194,31 @@ private async Task<int> RunTerminalProcessAsync()
186194

187195
_process.Exited += new EventHandler((sender, args) =>
188196
{
189-
taskCompletionSource.SetResult(_process.ExitCode);
190-
_process.Dispose();
197+
Process process = (Process)sender;
198+
taskCompletionSource.SetResult(process.ExitCode);
199+
process.Dispose();
191200
});
192201

193-
_process.Start();
194-
_process.BeginOutputReadLine();
195-
_process.BeginErrorReadLine();
202+
try
203+
{
204+
_process.Start();
196205

197-
List<Process> children = GetProcessChildren();
198-
if (children.Count > 0)
199-
_cmdProcessId = children[0].Id;
206+
_process.BeginOutputReadLine();
207+
_process.BeginErrorReadLine();
208+
209+
List<Process> children = GetProcessChildren();
210+
if (children.Count > 0)
211+
_cmdProcessId = children[0].Id;
212+
}
213+
catch (Exception e)
214+
{
215+
Show();
216+
WindowState = WindowState.Normal;
217+
Topmost = true;
218+
219+
System.Windows.MessageBox.Show("There was an error starting the process. Check your Process input and Apply Changes to retry. Details: " + Environment.NewLine + Environment.NewLine + e.Message);
220+
taskCompletionSource.SetException(e);
221+
}
200222

201223
return await taskCompletionSource.Task;
202224
}
@@ -438,6 +460,14 @@ private void ApplyChangesButton_Click(object sender, RoutedEventArgs e)
438460

439461
_settings.NewlineTriggers = new List<NewlineTrigger>(NewlineTriggers);
440462

463+
if (!_settings.ProcessPath.Equals(Process_TextBox.Text))
464+
{
465+
_settings.ProcessPath = Process_TextBox.Text;
466+
467+
// Begin terminal process
468+
RunTerminalProcessAsync();
469+
}
470+
441471
ApplySettingsToTerminalWindow();
442472

443473
File.WriteAllText(_configPath, JsonConvert.SerializeObject(_settings));

0 commit comments

Comments
 (0)