Skip to content

Commit d4055f8

Browse files
committed
replace local path with remote path when lose focus to filePathTextBox
1 parent a6e1d20 commit d4055f8

3 files changed

Lines changed: 86 additions & 1 deletion

File tree

GitContentSearch.UI/ViewModels/MainWindowViewModel.cs

Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,56 @@ public MainWindowViewModel(IStorageProvider storageProvider, SettingsService set
3232
[NotifyCanExecuteChangedFor(nameof(StartSearchCommand))]
3333
private string filePath = string.Empty;
3434

35+
[RelayCommand]
36+
private async Task HandleFilePathLostFocusAsync()
37+
{
38+
if (string.IsNullOrWhiteSpace(FilePath) || string.IsNullOrWhiteSpace(WorkingDirectory))
39+
return;
40+
41+
var processWrapper = new ProcessWrapper();
42+
var absolutePath = Path.IsPathRooted(FilePath)
43+
? FilePath
44+
: Path.GetFullPath(Path.Combine(WorkingDirectory, FilePath));
45+
46+
var result = await Task.Run(() =>
47+
processWrapper.Start("rev-parse --show-toplevel", Path.GetDirectoryName(absolutePath), null));
48+
49+
if (result.ExitCode == 0)
50+
{
51+
var gitRoot = result.StandardOutput.Trim().Replace('/', '\\'); // Normalize to Windows path
52+
53+
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
54+
{
55+
LogOutput.Add($"Git Root: {gitRoot}");
56+
LogOutput.Add($"File Path: {absolutePath}");
57+
58+
WorkingDirectory = gitRoot;
59+
60+
// Convert paths to the same format and case for comparison
61+
if (absolutePath.StartsWith(gitRoot, StringComparison.OrdinalIgnoreCase))
62+
{
63+
var relativePath = absolutePath[gitRoot.Length..].TrimStart('\\', '/');
64+
FilePath = relativePath.Replace('\\', '/');
65+
LogOutput.Add($"Relative Path: {FilePath}");
66+
}
67+
else
68+
{
69+
LogOutput.Add("Warning: File path does not start with git root path.");
70+
FilePath = string.Empty;
71+
}
72+
});
73+
}
74+
else
75+
{
76+
await Avalonia.Threading.Dispatcher.UIThread.InvokeAsync(() =>
77+
{
78+
LogOutput.Add("Warning: Selected file is not in a Git repository. Please select a file within a Git repository.");
79+
FilePath = string.Empty;
80+
WorkingDirectory = string.Empty;
81+
});
82+
}
83+
}
84+
3585
[ObservableProperty]
3686
[NotifyCanExecuteChangedFor(nameof(StartSearchCommand))]
3787
private string searchString = string.Empty;

GitContentSearch.UI/Views/MainWindow.axaml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,10 @@
2020
<!-- File Path -->
2121
<Grid Grid.Row="0" ColumnDefinitions="Auto,*,Auto" Margin="0,8">
2222
<TextBlock Grid.Column="0" Text="File Path" VerticalAlignment="Center" Width="140" FontWeight="Medium"/>
23-
<TextBox Grid.Column="1" Text="{Binding FilePath}" Margin="8,0"/>
23+
<TextBox Grid.Column="1"
24+
Text="{Binding FilePath}"
25+
Margin="8,0"
26+
AttachedToVisualTree="OnFilePathTextBoxAttached"/>
2427
<Button Grid.Column="2"
2528
Classes="Secondary"
2629
Content="Browse"
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,43 @@
1+
using Avalonia;
12
using Avalonia.Controls;
3+
using Avalonia.Input;
4+
using Avalonia.Interactivity;
5+
using Avalonia.VisualTree;
6+
using GitContentSearch.UI.ViewModels;
27

38
namespace GitContentSearch.UI.Views;
49

510
public partial class MainWindow : Window
611
{
12+
private TextBox? _filePathTextBox;
13+
714
public MainWindow()
815
{
916
InitializeComponent();
1017
}
18+
19+
private void OnFilePathTextBoxAttached(object? sender, VisualTreeAttachmentEventArgs e)
20+
{
21+
_filePathTextBox = sender as TextBox;
22+
if (_filePathTextBox != null)
23+
{
24+
_filePathTextBox.LostFocus += OnFilePathLostFocus;
25+
}
26+
}
27+
28+
private void OnFilePathLostFocus(object? sender, RoutedEventArgs e)
29+
{
30+
if (DataContext is MainWindowViewModel viewModel)
31+
{
32+
// Only process if focus is still within our window
33+
var focusManager = TopLevel.GetTopLevel(this)?.FocusManager;
34+
var currentFocus = focusManager?.GetFocusedElement();
35+
36+
// Check if the newly focused element is still within our window
37+
if (currentFocus != null && currentFocus is Visual visual && visual.GetVisualRoot() == this.GetVisualRoot())
38+
{
39+
viewModel.HandleFilePathLostFocusCommand.Execute(null);
40+
}
41+
}
42+
}
1143
}

0 commit comments

Comments
 (0)