Skip to content

Commit 42ccf41

Browse files
added option for changing open-in-terminal command
1 parent 2088beb commit 42ccf41

File tree

4 files changed

+74
-25
lines changed

4 files changed

+74
-25
lines changed

UnityHubNative.Net/ControlsExtensions.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,9 +230,9 @@ public static T OnSelectionChanged<T>(this T control, Action action) where T : S
230230
return control;
231231
}
232232

233-
public static T OnTextChanged<T>(this T control, Action action) where T : Control
233+
public static T OnTextChanged<T>(this T control, Action action) where T : TextBox
234234
{
235-
control.SizeChanged += (_, e) => action();
235+
control.TextChanged += (_, e) => action();
236236
return control;
237237
}
238238

UnityHubNative.Net/MainWindow.cs

Lines changed: 51 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
using Avalonia.Platform.Storage;
99
using FluentAvalonia.UI.Controls;
1010
using MsBox.Avalonia;
11+
using SkiaSharp;
1112

1213
namespace UnityHubNative.Net;
1314

@@ -27,7 +28,9 @@ class MainWindow : Window
2728

2829
static DockPanel s_transparentPanel;
2930
static Slider s_backgroundBlurIntensitySlider;
30-
static private Key s_lastKey;
31+
static TextBox s_openInTerminalFormatText;
32+
static Key s_lastKey;
33+
private static TabControl s_tabControl;
3134

3235
public MainWindow(object data)
3336
{
@@ -55,29 +58,32 @@ protected override void OnKeyDown(KeyEventArgs e)
5558

5659
s_lastKey = e.Key;
5760

58-
// focus on searchbar if typed a character
59-
if (e.KeyModifiers is KeyModifiers.None or KeyModifiers.Shift && (int)e.Key >= (int)Key.A && (int)e.Key <= (int)Key.Z)
61+
if (s_tabControl.SelectedIndex == 0)
6062
{
61-
if (!s_projectSearchBoxAutoComplete.IsKeyboardFocusWithin)
63+
// focus on searchbar if typed a character
64+
if (e.KeyModifiers is KeyModifiers.None or KeyModifiers.Shift && (int)e.Key >= (int)Key.A && (int)e.Key <= (int)Key.Z)
6265
{
63-
s_projectSearchBoxAutoComplete.Focus();
64-
s_projectSearchBoxAutoComplete.Text += e.KeyModifiers == KeyModifiers.Shift ? e.Key.ToString() : e.Key.ToString().ToLower();
65-
s_projectSearchBoxAutoComplete.CaretIndex = s_projectSearchBoxAutoComplete.Text.Length;
66-
e.Handled = true;
66+
if (!s_projectSearchBoxAutoComplete.IsKeyboardFocusWithin)
67+
{
68+
s_projectSearchBoxAutoComplete.Focus();
69+
s_projectSearchBoxAutoComplete.Text += e.KeyModifiers == KeyModifiers.Shift ? e.Key.ToString() : e.Key.ToString().ToLower();
70+
s_projectSearchBoxAutoComplete.CaretIndex = s_projectSearchBoxAutoComplete.Text.Length;
71+
e.Handled = true;
72+
}
73+
return;
6774
}
68-
return;
69-
}
7075

71-
// focus on the list of escaped
72-
if (e.Key == Key.Escape)
73-
{
74-
if (s_unityProjectsParent.SelectedItem != null)
76+
// focus on the list of escaped
77+
if (e.Key == Key.Escape)
7578
{
76-
s_unityProjectsParent.ContainerFromIndex(GetUnityProjectSelectedIndex())!.Focus();
77-
s_projectSearchBoxAutoComplete.Text = string.Empty;
78-
}
79+
if (s_unityProjectsParent.SelectedItem != null)
80+
{
81+
s_unityProjectsParent.ContainerFromIndex(GetUnityProjectSelectedIndex())!.Focus();
82+
s_projectSearchBoxAutoComplete.Text = string.Empty;
83+
}
7984

80-
return;
85+
return;
86+
}
8187
}
8288
}
8389

@@ -247,7 +253,7 @@ public static void MoveSelectedProjectDown()
247253
{
248254
}.AddChildren
249255
([
250-
new TabControl
256+
s_tabControl = new TabControl
251257
{
252258
TabStripPlacement = Dock.Top,
253259
}.AddItems
@@ -526,14 +532,39 @@ public static void MoveSelectedProjectDown()
526532
VerticalAlignment = VerticalAlignment.Center,
527533
}.OnCheckChanged(OnCloseAfterOpenProjectCheckboxChanged).SetDock(Dock.Right)
528534
])
529-
}.SetTooltip("If checked, the app will close after opening a project")
535+
}.SetTooltip("If checked, the app will close after opening a project"),
536+
new SettingsExpanderItem
537+
{
538+
Content = new DockPanel
539+
{
540+
}.AddChildren
541+
([
542+
new TextBlock
543+
{
544+
Text = "Format to open project in Terminal",
545+
VerticalAlignment = VerticalAlignment.Center,
546+
Margin = new(0, 0, 10, 0),
547+
}.SetDock(Dock.Left),
548+
s_openInTerminalFormatText = new TextBox
549+
{
550+
Text = UnityHubNativeNetApp.Config.openInTerminalFormat,
551+
VerticalAlignment = VerticalAlignment.Center,
552+
}.OnTextChanged(OnOpenInTerminalFormatChanged).SetDock(Dock.Right)
553+
])
554+
}.SetTooltip("Defines the process format of when opening a project in terminal. {path} will be replaced by the project path"),
530555
])
531556
])
532557
}
533558
])
534559
])
535560
]);
536561

562+
static void OnOpenInTerminalFormatChanged()
563+
{
564+
UnityHubNativeNetApp.Config.openInTerminalFormat = s_openInTerminalFormatText.Text;
565+
UnityHubNativeNetApp.SaveConfig(UnityHubNativeNetApp.Config);
566+
}
567+
537568
static void OnExtendToTitlebarCheckChanged()
538569
{
539570
UnityHubNativeNetApp.Config.extendToTitlebar = !UnityHubNativeNetApp.Config.extendToTitlebar;

UnityHubNative.Net/OsUtils.cs

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,14 +13,29 @@ public static void OpenExplorer(string dir)
1313
});
1414
}
1515

16-
public static void OpenInTerminal(string path)
16+
public static void OpenInTerminal(string command)
1717
{
1818
#if Windows
19+
var (filename, args) = ExtractCommand(UnityHubNativeNetApp.Config.openInTerminalFormat.Replace("{path}", command, StringComparison.InvariantCultureIgnoreCase));
1920
Process.Start(new ProcessStartInfo
2021
{
21-
FileName = "cmd.exe",
22-
Arguments = $"/K cd /d \"{path}\""
22+
FileName = filename,
23+
Arguments = args,
24+
UseShellExecute = true
2325
});
2426
#endif
2527
}
28+
29+
static (string fileName, string args) ExtractCommand(string command)
30+
{
31+
bool insideQuote = false;
32+
for (int i = 0; i < command.Length; i++)
33+
{
34+
if (command[i] == '\"' && command.Length > 0 && command[i - 1] != '\\')
35+
insideQuote = !insideQuote;
36+
else if (command[i] == ' ')
37+
return (command[..i], command.Length > i ? command[(i + 1)..] : string.Empty);
38+
}
39+
return (command, string.Empty);
40+
}
2641
}

UnityHubNative.Net/UnityHubNativeNetApp.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public static AppConfig LoadConfig()
5454
blurIntensity = txt.Length >= 3 && float.TryParse(txt[2], out var acrylicAmount) ? acrylicAmount : 0.2f,
5555
closeAfterProjectOpen = txt.Length >= 4 && txt[3] == "true",
5656
extendToTitlebar = txt.Length >= 5 && txt[4] == "true",
57+
openInTerminalFormat = txt.Length >= 6 ? txt[5] : "cmd.exe /K cd /d \"{path}\"",
5758
};
5859
}
5960
catch (Exception ex)
@@ -72,6 +73,7 @@ public static void SaveConfig(AppConfig config)
7273
config.blurIntensity.ToString(),
7374
config.closeAfterProjectOpen ? "true" : "false",
7475
config.extendToTitlebar ? "true" : "false",
76+
config.openInTerminalFormat
7577
]);
7678
}
7779

@@ -82,5 +84,6 @@ public struct AppConfig
8284
public float blurIntensity;
8385
public bool extendToTitlebar;
8486
public bool closeAfterProjectOpen;
87+
public string openInTerminalFormat;
8588
}
8689
}

0 commit comments

Comments
 (0)