Skip to content

Commit b09b6d1

Browse files
authored
fix: copy folder path when a folder is selected (#2614)
Pressing Ctrl+C on a folder row in the unstaged/staged change tree copied the first file path under the folder (or silently did nothing when the folder contained multiple files). Now the Ctrl+C handler checks the real selection in the tree container first and copies the folder path, and multi-selection copies every path line by line. Ctrl+Shift+C keeps copying absolute paths.
1 parent 9961e36 commit b09b6d1

1 file changed

Lines changed: 47 additions & 14 deletions

File tree

src/Views/WorkingCopy.axaml.cs

Lines changed: 47 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.IO;
4+
using System.Text;
45

56
using Avalonia.Controls;
67
using Avalonia.Input;
@@ -117,15 +118,31 @@ private async void OnUnstagedKeyDown(object _, KeyEventArgs e)
117118
Native.OS.OpenWithDefaultEditor(fullpath);
118119
e.Handled = true;
119120
}
120-
else if (e.Key is Key.C && e.KeyModifiers.HasFlag(cmdKey) && vm.SelectedUnstaged is { Count: 1 })
121+
else if (e.Key is Key.C && e.KeyModifiers.HasFlag(cmdKey))
121122
{
122-
var change = vm.SelectedUnstaged[0];
123-
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
124-
await this.CopyTextAsync(Native.OS.GetAbsPath(vm.Repository.FullPath, change.Path));
125-
else
126-
await this.CopyTextAsync(change.Path);
123+
var builder = new StringBuilder();
124+
var copyAbsPath = e.KeyModifiers.HasFlag(KeyModifiers.Shift);
125+
var container = UnstagedChangesView.FindDescendantOfType<ChangeCollectionContainer>();
126+
if (container is { SelectedItems.Count: 1, SelectedItem: ViewModels.ChangeTreeNode { IsFolder: true } node })
127+
{
128+
builder.Append(copyAbsPath ? Native.OS.GetAbsPath(vm.Repository.FullPath, node.FullPath) : node.FullPath);
129+
}
130+
else if (vm.SelectedUnstaged is { Count: 1 })
131+
{
132+
var change = vm.SelectedUnstaged[0];
133+
builder.Append(copyAbsPath ? Native.OS.GetAbsPath(vm.Repository.FullPath, change.Path) : change.Path);
134+
}
135+
else if (vm.SelectedUnstaged is { Count: > 0 })
136+
{
137+
foreach (var c in vm.SelectedUnstaged)
138+
builder.AppendLine(copyAbsPath ? Native.OS.GetAbsPath(vm.Repository.FullPath, c.Path) : c.Path);
139+
}
127140

128-
e.Handled = true;
141+
if (builder.Length > 0)
142+
{
143+
await this.CopyTextAsync(builder.ToString());
144+
e.Handled = true;
145+
}
129146
}
130147
else if (e.Key is Key.F && e.KeyModifiers == cmdKey)
131148
{
@@ -156,15 +173,31 @@ private async void OnStagedKeyDown(object _, KeyEventArgs e)
156173
Native.OS.OpenWithDefaultEditor(fullpath);
157174
e.Handled = true;
158175
}
159-
else if (e.Key is Key.C && e.KeyModifiers.HasFlag(cmdKey) && vm.SelectedStaged is { Count: 1 })
176+
else if (e.Key is Key.C && e.KeyModifiers.HasFlag(cmdKey))
160177
{
161-
var change = vm.SelectedStaged[0];
162-
if (e.KeyModifiers.HasFlag(KeyModifiers.Shift))
163-
await this.CopyTextAsync(Native.OS.GetAbsPath(vm.Repository.FullPath, change.Path));
164-
else
165-
await this.CopyTextAsync(change.Path);
178+
var builder = new StringBuilder();
179+
var copyAbsPath = e.KeyModifiers.HasFlag(KeyModifiers.Shift);
180+
var container = StagedChangesView.FindDescendantOfType<ChangeCollectionContainer>();
181+
if (container is { SelectedItems.Count: 1, SelectedItem: ViewModels.ChangeTreeNode { IsFolder: true } node })
182+
{
183+
builder.Append(copyAbsPath ? Native.OS.GetAbsPath(vm.Repository.FullPath, node.FullPath) : node.FullPath);
184+
}
185+
else if (vm.SelectedStaged is { Count: 1 })
186+
{
187+
var change = vm.SelectedStaged[0];
188+
builder.Append(copyAbsPath ? Native.OS.GetAbsPath(vm.Repository.FullPath, change.Path) : change.Path);
189+
}
190+
else if (vm.SelectedStaged is { Count: > 0 })
191+
{
192+
foreach (var c in vm.SelectedStaged)
193+
builder.AppendLine(copyAbsPath ? Native.OS.GetAbsPath(vm.Repository.FullPath, c.Path) : c.Path);
194+
}
166195

167-
e.Handled = true;
196+
if (builder.Length > 0)
197+
{
198+
await this.CopyTextAsync(builder.ToString());
199+
e.Handled = true;
200+
}
168201
}
169202
else if (e.Key is Key.F && e.KeyModifiers == cmdKey)
170203
{

0 commit comments

Comments
 (0)