From b0594233914bf0ea4e70eb9fc593a9d39f15c235 Mon Sep 17 00:00:00 2001 From: leo Date: Mon, 5 Aug 2024 22:10:43 +0800 Subject: [PATCH] feature: enable using drag-drop to re-order commits (#319) --- src/Resources/Icons.axaml | 1 + src/Views/InteractiveRebase.axaml | 14 +++++++- src/Views/InteractiveRebase.axaml.cs | 49 ++++++++++++++++++++++++++++ 3 files changed, 63 insertions(+), 1 deletion(-) diff --git a/src/Resources/Icons.axaml b/src/Resources/Icons.axaml index 5e79e402..1657ead3 100644 --- a/src/Resources/Icons.axaml +++ b/src/Resources/Icons.axaml @@ -68,6 +68,7 @@ M0 4 0 20 16 20 0 4M4 0 20 0 20 16 4 0z M192 192m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM192 512m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM192 832m-64 0a64 64 0 1 0 128 0 64 64 0 1 0-128 0ZM864 160H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 480H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32zM864 800H352c-17.7 0-32 14.3-32 32s14.3 32 32 32h512c17.7 0 32-14.3 32-32s-14.3-32-32-32z M824 645V307c0-56-46-102-102-102h-102V102l-154 154 154 154V307h102v338c-46 20-82 67-82 123 0 72 61 133 133 133 72 0 133-61 133-133 0-56-36-102-82-123zm-51 195c-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72s-31 72-72 72zM384 256c0-72-61-133-133-133-72 0-133 61-133 133 0 56 36 102 82 123v266C154 666 118 712 118 768c0 72 61 133 133 133 72 0 133-61 133-133 0-56-36-102-82-123V379C348 358 384 312 384 256zM323 768c0 41-31 72-72 72-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72zM251 328c-41 0-72-31-72-72s31-72 72-72c41 0 72 31 72 72s-31 72-72 72z + M299 811 299 725 384 725 384 811 299 811M469 811 469 725 555 725 555 811 469 811M640 811 640 725 725 725 725 811 640 811M299 640 299 555 384 555 384 640 299 640M469 640 469 555 555 555 555 640 469 640M640 640 640 555 725 555 725 640 640 640M299 469 299 384 384 384 384 469 299 469M469 469 469 384 555 384 555 469 469 469M640 469 640 384 725 384 725 469 640 469M299 299 299 213 384 213 384 299 299 299M469 299 469 213 555 213 555 299 469 299M640 299 640 213 725 213 725 299 640 299Z M683 409v204L1024 308 683 0v191c-413 0-427 526-427 526c117-229 203-307 427-307zm85 492H102V327h153s38-63 114-122H51c-28 0-51 27-51 61v697c0 34 23 61 51 61h768c28 0 51-27 51-61V614l-102 100v187z M544 85c49 0 90 37 95 85h75a96 96 0 0196 89L811 267a32 32 0 01-28 32L779 299a32 32 0 01-32-28L747 267a32 32 0 00-28-32L715 235h-91a96 96 0 01-80 42H395c-33 0-62-17-80-42L224 235a32 32 0 00-32 28L192 267v576c0 16 12 30 28 32l4 0h128a32 32 0 0132 28l0 4a32 32 0 01-32 32h-128a96 96 0 01-96-89L128 843V267a96 96 0 0189-96L224 171h75a96 96 0 0195-85h150zm256 256a96 96 0 0196 89l0 7v405a96 96 0 01-89 96L800 939h-277a96 96 0 01-96-89L427 843v-405a96 96 0 0189-96L523 341h277zm-256-192H395a32 32 0 000 64h150a32 32 0 100-64z m186 532 287 0 0 287c0 11 9 20 20 20s20-9 20-20l0-287 287 0c11 0 20-9 20-20s-9-20-20-20l-287 0 0-287c0-11-9-20-20-20s-20 9-20 20l0 287-287 0c-11 0-20 9-20 20s9 20 20 20z diff --git a/src/Views/InteractiveRebase.axaml b/src/Views/InteractiveRebase.axaml index 553d99f1..c2bd7741 100644 --- a/src/Views/InteractiveRebase.axaml +++ b/src/Views/InteractiveRebase.axaml @@ -73,7 +73,6 @@ CanUserReorderColumns="False" CanUserResizeColumns="False" CanUserSortColumns="False" - DragDrop.AllowDrop="True" IsReadOnly="True" HeadersVisibility="None" Focusable="False" @@ -82,6 +81,19 @@ VerticalScrollBarVisibility="Auto" KeyDown="OnDataGridKeyDown"> + + + + + + + + + + diff --git a/src/Views/InteractiveRebase.axaml.cs b/src/Views/InteractiveRebase.axaml.cs index 287a0be1..453f1a6c 100644 --- a/src/Views/InteractiveRebase.axaml.cs +++ b/src/Views/InteractiveRebase.axaml.cs @@ -21,6 +21,55 @@ namespace SourceGit.Views Close(); } + private void OnSetupRowHeaderDragDrop(object sender, RoutedEventArgs e) + { + if (sender is Border border) + { + DragDrop.SetAllowDrop(border, true); + border.AddHandler(DragDrop.DragOverEvent, OnRowHeaderDragOver); + } + } + + private void OnRowHeaderPointerPressed(object sender, PointerPressedEventArgs e) + { + if (sender is Border border && border.DataContext is ViewModels.InteractiveRebaseItem item) + { + var data = new DataObject(); + data.Set("InteractiveRebaseItem", item); + DragDrop.DoDragDrop(e, data, DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Link); + } + } + + private void OnRowHeaderDragOver(object sender, DragEventArgs e) + { + if (DataContext is ViewModels.InteractiveRebase vm && + e.Data.Get("InteractiveRebaseItem") is ViewModels.InteractiveRebaseItem src && + sender is Border { DataContext: ViewModels.InteractiveRebaseItem dst } border && + src != dst) + { + e.DragEffects = DragDropEffects.Move | DragDropEffects.Copy | DragDropEffects.Link; + + var p = e.GetPosition(border); + if (p.Y > border.Bounds.Height * 0.33 && p.Y < border.Bounds.Height * 0.67) + { + var srcIdx = vm.Items.IndexOf(src); + var dstIdx = vm.Items.IndexOf(dst); + if (srcIdx < dstIdx) + { + for (var i = srcIdx; i < dstIdx; i++) + vm.MoveItemDown(src); + } + else + { + for (var i = srcIdx; i > dstIdx; i--) + vm.MoveItemUp(src); + } + } + + e.Handled = true; + } + } + private void OnMoveItemUp(object sender, RoutedEventArgs e) { if (sender is Control control && DataContext is ViewModels.InteractiveRebase vm)