Skip to content

Commit 82ac122

Browse files
committed
refactor: use hotkey instead of custom OnKeyDown to re-order commits in Interactive Rebase window
Signed-off-by: leo <longshuang@msn.cn>
1 parent 81f622b commit 82ac122

File tree

2 files changed

+76
-37
lines changed

2 files changed

+76
-37
lines changed

src/Views/InteractiveRebase.axaml

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@
3838
</Grid>
3939

4040
<!-- Operation Information -->
41-
<Grid Grid.Row="1" Margin="8" ColumnDefinitions="*,Auto">
41+
<Grid Grid.Row="1" Margin="8" ColumnDefinitions="*,Auto,0,0">
4242
<StackPanel Grid.Column="0" Orientation="Horizontal" ClipToBounds="True">
4343
<TextBlock Text="{DynamicResource Text.InteractiveRebase.Target}" Foreground="{DynamicResource Brush.FG2}" FontWeight="Bold"/>
4444
<Path Width="14" Height="14" Margin="8,0,0,0" Data="{StaticResource Icons.Branch}"/>
@@ -61,6 +61,9 @@
6161

6262
<Path Width="14" Height="14" Data="{StaticResource Icons.Info}"/>
6363
</Border>
64+
65+
<Button Grid.Column="2" Width="0" Height="0" Click="OnMoveSelectedUp" HotKey="{OnPlatform Ctrl+Up, macOS=⌘+Up}"/>
66+
<Button Grid.Column="3" Width="0" Height="0" Click="OnMoveSelectedDown" HotKey="{OnPlatform Ctrl+Down, macOS=⌘+Down}"/>
6467
</Grid>
6568

6669
<!-- Body -->
@@ -122,7 +125,7 @@
122125
HorizontalAlignment="Center"/>
123126

124127
<!-- Action -->
125-
<Button Grid.Column="1" Opacity="1" Margin="4,0,0,0" Padding="8,2" Background="Transparent" Click="OnButtonActionClicked">
128+
<Button Grid.Column="1" Opacity="1" Margin="4,0,0,0" Padding="8,2" Background="Transparent" Click="OnShowActionsDropdownMenu">
126129
<StackPanel Orientation="Horizontal">
127130
<Ellipse Width="14" Height="14" Fill="{Binding Action, Converter={x:Static c:InteractiveRebaseActionConverters.ToIconBrush}}"/>
128131
<TextBlock Margin="8,0" Text="{Binding Action, Converter={x:Static c:InteractiveRebaseActionConverters.ToName}}"/>

src/Views/InteractiveRebase.axaml.cs

Lines changed: 71 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -76,40 +76,8 @@ protected override void OnKeyDown(KeyEventArgs e)
7676
}
7777
else if (e.KeyModifiers.HasFlag(KeyModifiers.Control))
7878
{
79-
if (e.Key == Key.Up)
80-
{
81-
var idx = 0;
82-
for (int i = 0; i < vm.Items.Count; i++)
83-
{
84-
if (items.Contains(vm.Items[i]))
85-
{
86-
idx = Math.Max(0, i - 1);
87-
break;
88-
}
89-
}
90-
91-
vm.Move(items, idx);
92-
SelectedItems = items;
93-
Focus();
94-
e.Handled = true;
95-
}
96-
else if (e.Key == Key.Down)
97-
{
98-
var idx = 0;
99-
for (int i = vm.Items.Count - 1; i >= 0; i--)
100-
{
101-
if (items.Contains(vm.Items[i]))
102-
{
103-
idx = Math.Min(vm.Items.Count, i + 2);
104-
break;
105-
}
106-
}
107-
108-
vm.Move(items, idx);
109-
SelectedItems = items;
110-
Focus();
111-
e.Handled = true;
112-
}
79+
if (e.Key == Key.Up || e.Key == Key.Down)
80+
return;
11381
}
11482

11583
if (!e.Handled)
@@ -265,7 +233,75 @@ private void OnRowDrop(object sender, DragEventArgs e)
265233
e.Handled = true;
266234
}
267235

268-
private void OnButtonActionClicked(object sender, RoutedEventArgs e)
236+
private void OnMoveSelectedUp(object sender, RoutedEventArgs e)
237+
{
238+
if (DataContext is not ViewModels.InteractiveRebase vm)
239+
return;
240+
241+
if (IRItemListBox.SelectedItems is not { Count: > 0 } selected)
242+
return;
243+
244+
var hashes = new HashSet<string>();
245+
var items = new List<ViewModels.InteractiveRebaseItem>();
246+
foreach (var item in selected)
247+
{
248+
if (item is ViewModels.InteractiveRebaseItem irItem)
249+
{
250+
hashes.Add(irItem.Commit.SHA);
251+
items.Add(irItem);
252+
}
253+
}
254+
255+
var idx = 0;
256+
for (int i = 0; i < vm.Items.Count; i++)
257+
{
258+
if (hashes.Contains(vm.Items[i].Commit.SHA))
259+
{
260+
idx = Math.Max(0, i - 1);
261+
break;
262+
}
263+
}
264+
265+
vm.Move(items, idx);
266+
IRItemListBox.SelectedItems = items;
267+
e.Handled = true;
268+
}
269+
270+
private void OnMoveSelectedDown(object sender, RoutedEventArgs e)
271+
{
272+
if (DataContext is not ViewModels.InteractiveRebase vm)
273+
return;
274+
275+
if (IRItemListBox.SelectedItems is not { Count: > 0 } selected)
276+
return;
277+
278+
var hashes = new HashSet<string>();
279+
var items = new List<ViewModels.InteractiveRebaseItem>();
280+
foreach (var item in selected)
281+
{
282+
if (item is ViewModels.InteractiveRebaseItem irItem)
283+
{
284+
hashes.Add(irItem.Commit.SHA);
285+
items.Add(irItem);
286+
}
287+
}
288+
289+
var idx = 0;
290+
for (int i = vm.Items.Count - 1; i >= 0; i--)
291+
{
292+
if (hashes.Contains(vm.Items[i].Commit.SHA))
293+
{
294+
idx = Math.Min(vm.Items.Count, i + 2);
295+
break;
296+
}
297+
}
298+
299+
vm.Move(items, idx);
300+
IRItemListBox.SelectedItems = items;
301+
e.Handled = true;
302+
}
303+
304+
private void OnShowActionsDropdownMenu(object sender, RoutedEventArgs e)
269305
{
270306
if (sender is not Button { DataContext: ViewModels.InteractiveRebaseItem item } button)
271307
return;

0 commit comments

Comments
 (0)