Skip to content

Commit fad197d

Browse files
committed
ux: show feedback while dragging repository tabs
Show a pointer-following title/icon preview, dim the dragged tab, and highlight the destination with an insertion marker matching the existing reorder behavior. Clear feedback on leave, cancellation, drop and detach. Match the tab Border style key and constrain preview titles to the available width with ellipsis and clipping. Include isolated rendering and lifecycle checks for long and short fixed-width titles, welcome tabs and variable-width tabs.
1 parent 93d34c7 commit fad197d

6 files changed

Lines changed: 420 additions & 4 deletions

File tree

‎src/Views/LauncherTabBar.axaml‎

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,11 @@
99
mc:Ignorable="d" d:DesignWidth="800" d:DesignHeight="450"
1010
x:Class="SourceGit.Views.LauncherTabBar"
1111
x:DataType="vm:Launcher"
12-
x:Name="ThisControl">
12+
x:Name="ThisControl"
13+
DragDrop.AllowDrop="True"
14+
DragDrop.DragEnter="DragOverTabs"
15+
DragDrop.DragOver="DragOverTabs"
16+
DragDrop.DragLeave="DragLeaveTabs">
1317
<Grid ColumnDefinitions="Auto,*,Auto">
1418
<RepeatButton Grid.Column="0"
1519
Classes="icon_button"
@@ -36,6 +40,16 @@
3640
SelectionChanged="OnTabsSelectionChanged"
3741
LayoutUpdated="OnTabsLayoutUpdated">
3842
<ListBox.Styles>
43+
<Style Selector="Border.launcher_tab">
44+
<Setter Property="Background" Value="Transparent"/>
45+
</Style>
46+
<Style Selector="Border.tab_drag_source">
47+
<Setter Property="Opacity" Value="0.45"/>
48+
</Style>
49+
<Style Selector="Border.tab_drop_target">
50+
<Setter Property="Background" Value="{DynamicResource Brush.AccentHovered}"/>
51+
<Setter Property="CornerRadius" Value="4"/>
52+
</Style>
3953
<Style Selector="ListBoxItem">
4054
<Setter Property="Padding" Value="0"/>
4155
</Style>
@@ -61,9 +75,9 @@
6175

6276
<ListBox.ItemTemplate>
6377
<DataTemplate DataType="vm:LauncherPage">
64-
<v:LauncherTabSizeBox UseFixedWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseFixedTabWidth}" Height="30"
78+
<v:LauncherTabSizeBox Classes="launcher_tab"
79+
UseFixedWidth="{Binding Source={x:Static vm:Preferences.Instance}, Path=UseFixedTabWidth}" Height="30"
6580
Padding="0,2,0,0"
66-
Background="Transparent"
6781
PointerPressed="OnPointerPressedTab"
6882
PointerMoved="OnPointerMovedOverTab"
6983
PointerReleased="OnPointerReleasedTab"

‎src/Views/LauncherTabBar.axaml.cs‎

Lines changed: 169 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
using System;
22
using System.IO;
3+
using System.Linq;
34

45
using Avalonia;
56
using Avalonia.Controls;
7+
using Avalonia.Controls.Primitives;
68
using Avalonia.Input;
79
using Avalonia.Interactivity;
10+
using Avalonia.Layout;
811
using Avalonia.Media;
912
using Avalonia.Styling;
13+
using Avalonia.VisualTree;
1014

1115
namespace SourceGit.Views
1216
{
@@ -254,11 +258,167 @@ private async void OnPointerMovedOverTab(object sender, PointerEventArgs e)
254258

255259
var data = new DataTransfer();
256260
data.Add(DataTransferItem.Create(_dndMainTabFormat, page.Node.Id));
257-
await DragDrop.DoDragDropAsync(_pressedTabEvent, data, DragDropEffects.Move);
261+
try
262+
{
263+
BeginDragFeedback(border, page);
264+
await DragDrop.DoDragDropAsync(_pressedTabEvent, data, DragDropEffects.Move);
265+
}
266+
finally
267+
{
268+
ClearDragFeedback();
269+
_pressedTabEvent = null;
270+
_startDragTab = false;
271+
}
258272
}
259273
e.Handled = true;
260274
}
261275

276+
private void BeginDragFeedback(Border source, ViewModels.LauncherPage page)
277+
{
278+
_draggedPage = page;
279+
_dragSource = source;
280+
_dragLayer = AdornerLayer.GetAdornerLayer(this);
281+
if (_dragLayer != null)
282+
{
283+
var size = source.Bounds.Size;
284+
var text = source.GetVisualDescendants().OfType<TextBlock>().FirstOrDefault(t => t.IsEffectivelyVisible);
285+
var icon = page.Node.IsRepository
286+
? (Control)new Bookmark { Value = page.Node.Bookmark }
287+
: new Avalonia.Controls.Shapes.Path
288+
{
289+
Data = this.FindResource("Icons.Repositories") as Geometry,
290+
Fill = this.FindResource("Brush.FG1") as IBrush
291+
};
292+
icon.Width = 12;
293+
icon.Height = 12;
294+
icon.Margin = new Thickness(0, 0, 8, 0);
295+
var content = new Grid
296+
{
297+
ColumnDefinitions = new ColumnDefinitions("Auto,*"),
298+
Margin = new Thickness(12, 0),
299+
VerticalAlignment = VerticalAlignment.Center
300+
};
301+
content.Children.Add(icon);
302+
var title = new TextBlock
303+
{
304+
Text = page.Node.IsRepository ? page.Node.Name : App.Text("PageTabBar.Welcome.Title"),
305+
FontFamily = text?.FontFamily ?? FontFamily.Default,
306+
FontSize = text?.FontSize ?? 12,
307+
Foreground = this.FindResource("Brush.FG1") as IBrush,
308+
TextTrimming = TextTrimming.CharacterEllipsis,
309+
VerticalAlignment = VerticalAlignment.Center
310+
};
311+
Grid.SetColumn(title, 1);
312+
content.Children.Add(title);
313+
_dragPreview = new Border
314+
{
315+
Width = size.Width,
316+
Height = size.Height,
317+
Background = this.FindResource("Brush.ToolBar") as IBrush,
318+
BorderBrush = this.FindResource("Brush.Border0") as IBrush,
319+
BorderThickness = new Thickness(1),
320+
CornerRadius = new CornerRadius(4),
321+
Opacity = 0.85,
322+
IsVisible = false,
323+
ClipToBounds = true,
324+
Child = content
325+
};
326+
_dropIndicator = new Border
327+
{
328+
Width = 3,
329+
Height = Math.Max(0, size.Height - 4),
330+
Background = this.FindResource("Brush.Accent") as IBrush,
331+
IsVisible = false
332+
};
333+
_dragOverlay = new Canvas { IsHitTestVisible = false };
334+
_dragOverlay.Children.Add(_dropIndicator);
335+
_dragOverlay.Children.Add(_dragPreview);
336+
AdornerLayer.SetAdornedElement(_dragOverlay, this);
337+
AdornerLayer.SetIsClipEnabled(_dragOverlay, false);
338+
_dragLayer.Children.Add(_dragOverlay);
339+
}
340+
source.Classes.Add("tab_drag_source");
341+
}
342+
343+
private void DragOverTabs(object sender, DragEventArgs e)
344+
{
345+
if (_draggedPage == null || DataContext is not ViewModels.Launcher launcher)
346+
return;
347+
348+
var point = e.GetPosition(this);
349+
if (_dragPreview != null)
350+
{
351+
Canvas.SetLeft(_dragPreview, point.X + 12);
352+
Canvas.SetTop(_dragPreview, point.Y + 12);
353+
_dragPreview.IsVisible = true;
354+
}
355+
356+
ClearDropTarget();
357+
e.DragEffects = DragDropEffects.None;
358+
var fromIndex = launcher.Pages.IndexOf(_draggedPage);
359+
if (fromIndex < 0 || !new Rect(LauncherTabsScroller.Bounds.Size).Contains(e.GetPosition(LauncherTabsScroller)))
360+
return;
361+
362+
for (var i = 0; i < LauncherTabsList.ItemCount; i++)
363+
{
364+
var container = LauncherTabsList.ContainerFromIndex(i);
365+
var tab = container?.GetVisualDescendants().OfType<LauncherTabSizeBox>().FirstOrDefault();
366+
if (tab == null || i == fromIndex)
367+
continue;
368+
369+
var position = e.GetPosition(tab);
370+
if (!new Rect(tab.Bounds.Size).Contains(position))
371+
continue;
372+
373+
_dropTarget = tab;
374+
tab.Classes.Add("tab_drop_target");
375+
e.DragEffects = DragDropEffects.Move;
376+
if (_dropIndicator != null && tab.TranslatePoint(new Point(i > fromIndex ? tab.Bounds.Width : 0, 2), this) is { } edge)
377+
{
378+
Canvas.SetLeft(_dropIndicator, edge.X - 1.5);
379+
Canvas.SetTop(_dropIndicator, edge.Y);
380+
_dropIndicator.IsVisible = true;
381+
}
382+
break;
383+
}
384+
e.Handled = true;
385+
}
386+
387+
private void DragLeaveTabs(object sender, DragEventArgs e)
388+
{
389+
ClearDropTarget();
390+
if (_dragPreview != null)
391+
_dragPreview.IsVisible = false;
392+
}
393+
394+
private void ClearDropTarget()
395+
{
396+
_dropTarget?.Classes.Remove("tab_drop_target");
397+
_dropTarget = null;
398+
if (_dropIndicator != null)
399+
_dropIndicator.IsVisible = false;
400+
}
401+
402+
private void ClearDragFeedback()
403+
{
404+
ClearDropTarget();
405+
_dragSource?.Classes.Remove("tab_drag_source");
406+
if (_dragOverlay != null)
407+
_dragLayer?.Children.Remove(_dragOverlay);
408+
_draggedPage = null;
409+
_dragSource = null;
410+
_dragOverlay = null;
411+
_dragPreview = null;
412+
_dropIndicator = null;
413+
_dragLayer = null;
414+
}
415+
416+
protected override void OnDetachedFromVisualTree(VisualTreeAttachmentEventArgs e)
417+
{
418+
ClearDragFeedback();
419+
base.OnDetachedFromVisualTree(e);
420+
}
421+
262422
private void DropTab(object sender, DragEventArgs e)
263423
{
264424
if (e.DataTransfer.TryGetValue(_dndMainTabFormat) is not { Length: > 0 } id)
@@ -286,6 +446,7 @@ private void DropTab(object sender, DragEventArgs e)
286446
if (target == to)
287447
return;
288448

449+
ClearDragFeedback();
289450
launcher.MoveTab(target, to);
290451

291452
_pressedTabEvent = null;
@@ -427,6 +588,13 @@ private void OnCloseTab(object sender, RoutedEventArgs e)
427588
private readonly Vector _scrollStep = new(64, 0);
428589
private PointerPressedEventArgs _pressedTabEvent = null;
429590
private bool _startDragTab = false;
591+
private ViewModels.LauncherPage _draggedPage;
592+
private Border _dragSource;
593+
private LauncherTabSizeBox _dropTarget;
594+
private AdornerLayer _dragLayer;
595+
private Canvas _dragOverlay;
596+
private Border _dragPreview;
597+
private Border _dropIndicator;
430598
private readonly DataFormat<string> _dndMainTabFormat = DataFormat.CreateStringApplicationFormat("sourcegit-dnd-main-tab");
431599
}
432600
}

‎tests/TabDragFeedback/Program.cs‎

Lines changed: 123 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,123 @@
1+
using System.Reflection;
2+
using Avalonia;
3+
using Avalonia.Controls;
4+
using Avalonia.Controls.Primitives;
5+
using Avalonia.Headless;
6+
using Avalonia.Input;
7+
using Avalonia.Media;
8+
using Avalonia.Media.Imaging;
9+
using Avalonia.Styling;
10+
using Avalonia.Threading;
11+
using Avalonia.VisualTree;
12+
using SourceGit.Views;
13+
14+
// Linux.GetXdgDirectory only honors XDG roots that already exist.
15+
// Create both app directories before setup to prevent fallback or legacy migration.
16+
var outputRoot = Path.GetFullPath(Environment.GetEnvironmentVariable("TEST_OUTPUT") ?? throw new Exception("Run this fixture using tests/TabDragFeedback/run.py"));
17+
var configRoot = Path.Combine(outputRoot, "config");
18+
var cacheRoot = Path.Combine(outputRoot, "cache");
19+
Directory.CreateDirectory(Path.Combine(configRoot, "SourceGit"));
20+
Directory.CreateDirectory(Path.Combine(cacheRoot, "SourceGit"));
21+
Environment.SetEnvironmentVariable("XDG_CONFIG_HOME", configRoot);
22+
Environment.SetEnvironmentVariable("XDG_CACHE_HOME", cacheRoot);
23+
Environment.SetEnvironmentVariable("APPIMAGE", null);
24+
25+
Console.WriteLine("stage: directories");
26+
SourceGit.Native.OS.SetupBasicDirectories();
27+
if (SourceGit.Native.OS.BasicDirectories.ConfigDir != Path.Combine(configRoot, "SourceGit") || SourceGit.Native.OS.BasicDirectories.CacheDir != Path.Combine(cacheRoot, "SourceGit")) throw new Exception("Test directories are not isolated");
28+
Console.WriteLine("stage: setup");
29+
SourceGit.App.BuildAvaloniaApp().UseHeadless(new AvaloniaHeadlessPlatformOptions { UseHeadlessDrawing = false }).SetupWithoutStarting();
30+
Console.WriteLine("stage: launcher");
31+
var vm = new SourceGit.ViewModels.Launcher(null);
32+
vm.AddNewTab();
33+
vm.AddNewTab();
34+
if (vm.Pages.Count != 3) throw new Exception("Expected exactly three isolated test pages");
35+
var testCase = args.FirstOrDefault() ?? "long";
36+
Console.WriteLine("case: " + testCase);
37+
SourceGit.ViewModels.Preferences.Instance.UseFixedTabWidth = testCase != "variable";
38+
if (testCase != "welcome")
39+
{
40+
vm.Pages[0].Node.IsRepository = true;
41+
vm.Pages[0].Node.Name = testCase == "short"
42+
? "SourceGit"
43+
: "sourcegit-repository-with-a-very-long-name-for-preview-validation";
44+
}
45+
var bar = new LauncherTabBar { DataContext = vm };
46+
var window = new Window { Width = 850, Height = 180, Content = bar };
47+
Console.WriteLine("stage: show");
48+
window.Show();
49+
Flush();
50+
var list = bar.FindControl<ListBox>("LauncherTabsList");
51+
var tabs = Enumerable.Range(0, vm.Pages.Count).Select(i => list.ContainerFromIndex(i).GetVisualDescendants().OfType<LauncherTabSizeBox>().First()).ToArray();
52+
var layer = AdornerLayer.GetAdornerLayer(bar);
53+
var initialAdorners = layer.Children.Count;
54+
var first = vm.Pages[0];
55+
var order = vm.Pages.Select(p => p.Node.Id).ToArray();
56+
var format = Field<DataFormat<string>>("_dndMainTabFormat");
57+
var data = new DataTransfer();
58+
data.Add(DataTransferItem.Create(format, first.Node.Id));
59+
60+
Console.WriteLine("stage: begin feedback");
61+
Call("BeginDragFeedback", tabs[0], first);
62+
Flush();
63+
Check(tabs[0].Classes.Contains("tab_drag_source"), "source is dimmed");
64+
Check(Math.Abs(tabs[0].Opacity - 0.45) < 0.01, "source dimming is rendered");
65+
Check(layer.Children.Count == initialAdorners + 1, "preview adorner attached");
66+
var over = Drag(DragDrop.DragOverEvent, tabs[1]);
67+
Call("DragOverTabs", bar, over);
68+
Flush();
69+
var previewForReview = Field<Border>("_dragPreview");
70+
var titleForReview = previewForReview.GetVisualDescendants().OfType<TextBlock>().Single();
71+
Console.WriteLine($"REVIEW preview width={previewForReview.Bounds.Width}, title width={titleForReview.Bounds.Width}, title desired width={titleForReview.DesiredSize.Width}, preview clips={previewForReview.ClipToBounds}");
72+
var titlePosition = titleForReview.TranslatePoint(new Point(0, 0), previewForReview)!.Value;
73+
Check(titlePosition.X >= 0 && titlePosition.X + titleForReview.Bounds.Width <= previewForReview.Bounds.Width + 0.01, "title stays within preview width");
74+
Check(titleForReview.TextLayout.TextLines.Any(line => line.HasCollapsed) == (testCase == "long"), "long fixed-width title has ellipsis and fitting titles stay complete");
75+
var bookmark = previewForReview.GetVisualDescendants().OfType<Bookmark>().FirstOrDefault();
76+
var welcomeIcon = previewForReview.GetVisualDescendants().OfType<Avalonia.Controls.Shapes.Path>().FirstOrDefault();
77+
Check(testCase == "welcome" ? welcomeIcon != null : bookmark != null, "preview retains the correct repository or welcome icon");
78+
Check(over.DragEffects == DragDropEffects.Move, "valid target accepts move");
79+
Check(tabs[1].Classes.Contains("tab_drop_target"), "target highlighted");
80+
Check(tabs[1].Background is ISolidColorBrush targetBrush &&
81+
bar.FindResource("Brush.AccentHovered") is ISolidColorBrush expectedBrush &&
82+
targetBrush.Color == expectedBrush.Color, "destination highlight is rendered");
83+
Check(Field<Border>("_dragPreview").IsVisible && Field<Border>("_dropIndicator").IsVisible, "preview and insertion marker visible");
84+
var expectedEdge = tabs[1].TranslatePoint(new Point(tabs[1].Bounds.Width, 2), bar)!.Value;
85+
Check(Math.Abs(Canvas.GetLeft(Field<Border>("_dropIndicator")) - expectedEdge.X + 1.5) < 0.01, "rightward move marker matches insertion edge");
86+
var screenshot = new RenderTargetBitmap(new PixelSize(850, 180));
87+
screenshot.Render(window);
88+
screenshot.Save(Path.Combine(Environment.GetEnvironmentVariable("TEST_OUTPUT")!, "feedback.png"));
89+
screenshot.Dispose();
90+
91+
Call("DragLeaveTabs", bar, new DragEventArgs(DragDrop.DragLeaveEvent, data, bar, new Point(0, 0), KeyModifiers.None));
92+
Check(!Field<Border>("_dragPreview").IsVisible && !tabs[1].Classes.Contains("tab_drop_target"), "leave clears feedback");
93+
Call("DragOverTabs", bar, Drag(DragDrop.DragOverEvent, tabs[2]));
94+
Check(tabs[2].Classes.Contains("tab_drop_target"), "enter another target restores feedback");
95+
Call("ClearDragFeedback");
96+
Check(order.SequenceEqual(vm.Pages.Select(p => p.Node.Id)), "cancel leaves order unchanged");
97+
Check(layer.Children.Count == initialAdorners && Field<Border>("_dragPreview") == null, "cancel removes preview");
98+
Check(Math.Abs(tabs[0].Opacity - 1) < 0.01 &&
99+
tabs[2].Background is ISolidColorBrush clearedTargetBrush &&
100+
clearedTargetBrush.Color == Colors.Transparent, "cancel restores rendered tab styles");
101+
102+
Console.WriteLine("stage: begin feedback");
103+
Call("BeginDragFeedback", tabs[0], first);
104+
Call("DragOverTabs", bar, Drag(DragDrop.DragOverEvent, tabs[1]));
105+
Call("DropTab", tabs[1], Drag(DragDrop.DropEvent, tabs[1]));
106+
Check(vm.Pages[1] == first && vm.Pages[0].Node.Id == order[1], "drop preserves existing reorder behavior");
107+
Check(layer.Children.Count == initialAdorners && !tabs[0].Classes.Contains("tab_drag_source") && !tabs[1].Classes.Contains("tab_drop_target"), "drop clears all feedback");
108+
Flush();
109+
var source = list.ContainerFromIndex(1).GetVisualDescendants().OfType<LauncherTabSizeBox>().First();
110+
var target = list.ContainerFromIndex(0).GetVisualDescendants().OfType<LauncherTabSizeBox>().First();
111+
Call("BeginDragFeedback", source, first);
112+
Call("DragOverTabs", bar, Drag(DragDrop.DragOverEvent, target));
113+
expectedEdge = target.TranslatePoint(new Point(0, 2), bar)!.Value;
114+
Check(Math.Abs(Canvas.GetLeft(Field<Border>("_dropIndicator")) - expectedEdge.X + 1.5) < 0.01, "leftward move marker matches insertion edge");
115+
window.Close();
116+
Check(Field<Border>("_dragPreview") == null, "detach clears preview");
117+
Console.WriteLine("PASS all feedback lifecycle and reorder checks");
118+
119+
void Flush() { Dispatcher.UIThread.RunJobs(); window.UpdateLayout(); Dispatcher.UIThread.RunJobs(); }
120+
T Field<T>(string name) => (T)typeof(LauncherTabBar).GetField(name, BindingFlags.Instance | BindingFlags.NonPublic)!.GetValue(bar)!;
121+
void Call(string name, params object[] args) => typeof(LauncherTabBar).GetMethod(name, BindingFlags.Instance | BindingFlags.NonPublic)!.Invoke(bar, args);
122+
void Check(bool condition, string message) { if (!condition) throw new Exception(message); Console.WriteLine("PASS " + message); }
123+
DragEventArgs Drag(Avalonia.Interactivity.RoutedEvent<DragEventArgs> type, LauncherTabSizeBox target) => new(type, data, target, new Point(target.Bounds.Width / 2, target.Bounds.Height / 2), KeyModifiers.None);

0 commit comments

Comments
 (0)