feature: add hotkeys to move interactive rebase item up and down (#557)

This commit is contained in:
leo 2024-10-12 10:28:54 +08:00
parent adca61c538
commit b9a24ceb53
No known key found for this signature in database
10 changed files with 83 additions and 45 deletions

View file

@ -59,14 +59,14 @@
<!-- Body -->
<Border Grid.Row="2" Margin="8,0,8,8" BorderThickness="1" BorderBrush="{DynamicResource Brush.Border2}">
<Grid RowDefinitions="*,3,*">
<ListBox Grid.Row="0"
Background="{DynamicResource Brush.Contents}"
ItemsSource="{Binding Items}"
SelectionMode="Single"
SelectedItem="{Binding SelectedItem, Mode=OneWayToSource}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto"
KeyDown="OnItemsListBoxKeyDown">
<v:InteractiveRebaseListBox Grid.Row="0"
Focusable="True"
Background="{DynamicResource Brush.Contents}"
ItemsSource="{Binding Items}"
SelectionMode="Single"
SelectedItem="{Binding SelectedItem, Mode=OneWayToSource}"
ScrollViewer.HorizontalScrollBarVisibility="Disabled"
ScrollViewer.VerticalScrollBarVisibility="Auto">
<ListBox.Styles>
<Style Selector="ListBoxItem">
<Setter Property="Margin" Value="0"/>
@ -213,18 +213,18 @@
<TextBlock Grid.Column="6" Classes="primary" Text="{Binding Commit.CommitterTimeStr}" Margin="8,0"/>
<!-- MoveUp Button -->
<Button Grid.Column="7" Classes="icon_button" Click="OnMoveItemUp" ToolTip.Tip="{DynamicResource Text.InteractiveRebase.MoveUp}">
<Button Grid.Column="7" Classes="icon_button" Click="OnMoveItemUp" ToolTip.Tip="Alt+Up">
<Path Width="14" Height="14" Margin="0,4,0,0" Data="{StaticResource Icons.Up}"/>
</Button>
<!-- MoveDown Button -->
<Button Grid.Column="8" Classes="icon_button" Click="OnMoveItemDown" ToolTip.Tip="{DynamicResource Text.InteractiveRebase.MoveDown}">
<Button Grid.Column="8" Classes="icon_button" Click="OnMoveItemDown" ToolTip.Tip="Alt+Down">
<Path Width="14" Height="14" Margin="0,4,0,0" Data="{StaticResource Icons.Down}"/>
</Button>
</Grid>
</DataTemplate>
</ListBox.ItemTemplate>
</ListBox>
</v:InteractiveRebaseListBox>
<v:LoadingIcon Grid.Row="0" Width="48" Height="48" HorizontalAlignment="Center" VerticalAlignment="Center" IsVisible="{Binding IsLoading}"/>

View file

@ -1,9 +1,79 @@
using System;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
namespace SourceGit.Views
{
public class InteractiveRebaseListBox : ListBox
{
protected override Type StyleKeyOverride => typeof(ListBox);
/// <summary>
/// Prevent ListBox handle the arrow keys.
/// </summary>
/// <param name="e"></param>
protected override void OnKeyDown(KeyEventArgs e)
{
var vm = DataContext as ViewModels.InteractiveRebase;
if (vm == null)
return;
var item = vm.SelectedItem;
if (item == null)
{
base.OnKeyDown(e);
return;
}
if (e.Key == Key.P)
{
item.SetAction(Models.InteractiveRebaseAction.Pick);
e.Handled = true;
}
else if (e.Key == Key.E)
{
item.SetAction(Models.InteractiveRebaseAction.Edit);
e.Handled = true;
}
else if (e.Key == Key.R)
{
item.SetAction(Models.InteractiveRebaseAction.Reword);
e.Handled = true;
}
else if (e.Key == Key.S)
{
item.SetAction(Models.InteractiveRebaseAction.Squash);
e.Handled = true;
}
else if (e.Key == Key.F)
{
item.SetAction(Models.InteractiveRebaseAction.Fixup);
e.Handled = true;
}
else if (e.Key == Key.D)
{
item.SetAction(Models.InteractiveRebaseAction.Drop);
e.Handled = true;
}
else if (e.KeyModifiers == KeyModifiers.Alt && e.Key == Key.Up)
{
vm.MoveItemUp(item);
e.Handled = true;
}
else if (e.KeyModifiers == KeyModifiers.Alt && e.Key == Key.Down)
{
vm.MoveItemDown(item);
e.Handled = true;
}
else
{
base.OnKeyDown(e);
}
}
}
public partial class InteractiveRebase : ChromelessWindow
{
public InteractiveRebase()
@ -89,26 +159,6 @@ namespace SourceGit.Views
}
}
private void OnItemsListBoxKeyDown(object sender, KeyEventArgs e)
{
var item = (sender as ListBox)?.SelectedItem as ViewModels.InteractiveRebaseItem;
if (item == null)
return;
if (e.Key == Key.P)
item.SetAction(Models.InteractiveRebaseAction.Pick);
else if (e.Key == Key.E)
item.SetAction(Models.InteractiveRebaseAction.Edit);
else if (e.Key == Key.R)
item.SetAction(Models.InteractiveRebaseAction.Reword);
else if (e.Key == Key.S)
item.SetAction(Models.InteractiveRebaseAction.Squash);
else if (e.Key == Key.F)
item.SetAction(Models.InteractiveRebaseAction.Fixup);
else if (e.Key == Key.D)
item.SetAction(Models.InteractiveRebaseAction.Drop);
}
private async void StartJobs(object _1, RoutedEventArgs _2)
{
var vm = DataContext as ViewModels.InteractiveRebase;