11using System ;
2+ using System . Collections . Generic ;
23
34using Avalonia ;
45using Avalonia . Controls ;
@@ -14,60 +15,67 @@ public class InteractiveRebaseListBox : ListBox
1415 {
1516 protected override Type StyleKeyOverride => typeof ( ListBox ) ;
1617
17- /// <summary>
18- /// Prevent ListBox handle the arrow keys.
19- /// </summary>
20- /// <param name="e"></param>
2118 protected override void OnKeyDown ( KeyEventArgs e )
2219 {
23- if ( DataContext is not ViewModels . InteractiveRebase vm )
20+ if ( DataContext is not ViewModels . InteractiveRebase vm || SelectedItems == null )
2421 return ;
2522
26- var item = vm . SelectedItem ;
27- if ( item == null )
23+ var items = new List < ViewModels . InteractiveRebaseItem > ( ) ;
24+ foreach ( var item in SelectedItems )
25+ {
26+ if ( item is ViewModels . InteractiveRebaseItem rebaseItem )
27+ items . Add ( rebaseItem ) ;
28+ }
29+
30+ if ( items . Count == 0 )
2831 {
2932 base . OnKeyDown ( e ) ;
3033 return ;
3134 }
3235
3336 if ( e . Key == Key . P )
3437 {
35- vm . ChangeAction ( item , Models . InteractiveRebaseAction . Pick ) ;
38+ vm . ChangeAction ( items , Models . InteractiveRebaseAction . Pick ) ;
3639 MoveSelection ( NavigationDirection . Next ) ;
3740 e . Handled = true ;
3841 }
3942 else if ( e . Key == Key . E )
4043 {
41- vm . ChangeAction ( item , Models . InteractiveRebaseAction . Edit ) ;
44+ vm . ChangeAction ( items , Models . InteractiveRebaseAction . Edit ) ;
4245 MoveSelection ( NavigationDirection . Next ) ;
4346 e . Handled = true ;
4447 }
4548 else if ( e . Key == Key . R )
4649 {
47- vm . ChangeAction ( item , Models . InteractiveRebaseAction . Reword ) ;
48- MoveSelection ( NavigationDirection . Next ) ;
50+ vm . ChangeAction ( items , Models . InteractiveRebaseAction . Reword ) ;
51+ if ( items . Count == 1 )
52+ this . FindAncestorOfType < InteractiveRebase > ( ) ? . OpenCommitMessageEditor ( items [ 0 ] ) ;
53+ else
54+ MoveSelection ( NavigationDirection . Next ) ;
55+
4956 e . Handled = true ;
5057 }
5158 else if ( e . Key == Key . S )
5259 {
53- vm . ChangeAction ( item , Models . InteractiveRebaseAction . Squash ) ;
60+ vm . ChangeAction ( items , Models . InteractiveRebaseAction . Squash ) ;
5461 MoveSelection ( NavigationDirection . Next ) ;
5562 e . Handled = true ;
5663 }
5764 else if ( e . Key == Key . F )
5865 {
59- vm . ChangeAction ( item , Models . InteractiveRebaseAction . Fixup ) ;
66+ vm . ChangeAction ( items , Models . InteractiveRebaseAction . Fixup ) ;
6067 MoveSelection ( NavigationDirection . Next ) ;
6168 e . Handled = true ;
6269 }
6370 else if ( e . Key == Key . D )
6471 {
65- vm . ChangeAction ( item , Models . InteractiveRebaseAction . Drop ) ;
72+ vm . ChangeAction ( items , Models . InteractiveRebaseAction . Drop ) ;
6673 MoveSelection ( NavigationDirection . Next ) ;
6774 e . Handled = true ;
6875 }
69- else if ( e . KeyModifiers . HasFlag ( OperatingSystem . IsMacOS ( ) ? KeyModifiers . Meta : KeyModifiers . Control ) )
76+ else if ( e . KeyModifiers . HasFlag ( OperatingSystem . IsMacOS ( ) ? KeyModifiers . Meta : KeyModifiers . Control ) && items . Count == 1 )
7077 {
78+ var item = items [ 0 ] ;
7179 if ( e . Key == Key . Up )
7280 {
7381 vm . MoveItemUp ( item ) ;
@@ -93,6 +101,13 @@ public InteractiveRebase()
93101 InitializeComponent ( ) ;
94102 }
95103
104+ public void OpenCommitMessageEditor ( ViewModels . InteractiveRebaseItem item )
105+ {
106+ var dialog = new CommitMessageEditor ( ) ;
107+ dialog . AsBuiltin ( item . FullMessage , msg => item . FullMessage = msg ) ;
108+ dialog . ShowDialog ( this ) ;
109+ }
110+
96111 protected override void OnLoaded ( RoutedEventArgs e )
97112 {
98113 base . OnLoaded ( e ) ;
@@ -108,19 +123,25 @@ private void CloseWindow(object _1, RoutedEventArgs _2)
108123
109124 private void OnRowsSelectionChanged ( object sender , SelectionChangedEventArgs e )
110125 {
111- if ( ! _firstSelectionChangedHandled &&
112- sender is InteractiveRebaseListBox list &&
113- list . SelectedItem is ViewModels . InteractiveRebaseItem item )
114- {
126+ if ( DataContext is not ViewModels . InteractiveRebase vm || sender is not InteractiveRebaseListBox listBox )
127+ return ;
128+
129+ var isFirstTimeHere = ! _firstSelectionChangedHandled ;
130+ if ( isFirstTimeHere )
115131 _firstSelectionChangedHandled = true ;
116132
117- if ( item . Action == Models . InteractiveRebaseAction . Reword )
118- {
119- var dialog = new CommitMessageEditor ( ) ;
120- dialog . AsBuiltin ( item . FullMessage , msg => item . FullMessage = msg ) ;
121- dialog . ShowDialog ( this ) ;
122- }
133+ var selected = listBox . SelectedItems ?? new List < object > ( ) ;
134+ var items = new List < ViewModels . InteractiveRebaseItem > ( ) ;
135+ foreach ( var item in selected )
136+ {
137+ if ( item is ViewModels . InteractiveRebaseItem rebaseItem )
138+ items . Add ( rebaseItem ) ;
123139 }
140+
141+ vm . SelectCommits ( items ) ;
142+
143+ if ( items . Count == 1 && isFirstTimeHere && items [ 0 ] . Action == Models . InteractiveRebaseAction . Reword )
144+ OpenCommitMessageEditor ( items [ 0 ] ) ;
124145 }
125146
126147 private void OnSetupRowHeaderDragDrop ( object sender , RoutedEventArgs e )
@@ -201,11 +222,7 @@ private void OnButtonActionClicked(object sender, RoutedEventArgs e)
201222 private void OnOpenCommitMessageEditor ( object sender , RoutedEventArgs e )
202223 {
203224 if ( sender is Button { DataContext : ViewModels . InteractiveRebaseItem item } )
204- {
205- var dialog = new CommitMessageEditor ( ) ;
206- dialog . AsBuiltin ( item . FullMessage , msg => item . FullMessage = msg ) ;
207- dialog . ShowDialog ( this ) ;
208- }
225+ OpenCommitMessageEditor ( item ) ;
209226
210227 e . Handled = true ;
211228 }
@@ -256,7 +273,12 @@ private void CreateActionMenuItem(MenuFlyout flyout, ViewModels.InteractiveRebas
256273 menuItem . Click += ( _ , e ) =>
257274 {
258275 if ( DataContext is ViewModels . InteractiveRebase vm )
259- vm . ChangeAction ( item , action ) ;
276+ {
277+ vm . ChangeAction ( [ item ] , action ) ;
278+
279+ if ( action == Models . InteractiveRebaseAction . Reword )
280+ OpenCommitMessageEditor ( item ) ;
281+ }
260282
261283 e . Handled = true ;
262284 } ;
0 commit comments