Skip to content

Commit 9e9f495

Browse files
committed
ux: show feedback while dragging repository tabs
Show a title/icon preview, dim the dragged tab, and highlight the destination with an insertion marker matching the existing reorder behavior. Clear feedback when leaving the tab bar, cancelling, dropping, or detaching. Match the tab's Border style key and keep its transparent background in a default style so the drag feedback styles take effect. Constrain preview titles to the available width and ellipsize long names. Clip the preview content to keep it inside the tab-sized border. Keep native drag-and-drop and the current Launcher.MoveTab behavior.
1 parent dc52ae3 commit 9e9f495

2 files changed

Lines changed: 186 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
}

0 commit comments

Comments
 (0)