Free, open-source file converter. Convert 263 formats offline by drag & drop — images, PDF, Word, Excel, PowerPoint, HWP/HWPX, video, audio, e-books and archives. No upload, no Office needed.
C# audio-converteravaloniabatch-conversionconvertercsharpdocument-converterdocx-to-pdfdotnetdrag-and-dropfile-conversionfile-converterheic-converterhwphwpximage-converterlinuxmacospdf-convertervideo-converterwindows
Toolbar editor: the donut makes room where a dragged preset will land
KnifeLemon9cabe7804eec
diff --git a/src/Filee.App/Controls/DonutMenu.cs b/src/Filee.App/Controls/DonutMenu.csindex a16e6000..88ad36d4 100644--- a/src/Filee.App/Controls/DonutMenu.cs+++ b/src/Filee.App/Controls/DonutMenu.cs@@ -94,6 +94,16 @@ public sealed partial class DonutMenu : Controlprivate bool _externalCenterHot;private int _dimmedIndex = -1;private int _dropTargetIndex = -1;++ // Edit mode drop preview: the slices make room where a dragged preset would land and a translucent slice shows+ // it there. Every slice's centre angle and width is a spring, so the others slide aside instead of jumping.+ private Spring[] _mid = [];+ private Spring[] _span = [];+ private Spring _previewMid;+ private Spring _previewSpan;+ private int _previewSlot = -1;+ private int _previewRemoved = -1;+ private string? _previewLabel;private (int Index, Point Start, PointerPressedEventArgs Args)? _press;static DonutMenu()@@ -227,6 +237,82 @@ public sealed partial class DonutMenu : ControlInvalidateVisual();}+ /// <summary>+ /// Edit mode: opens a gap at <paramref name="slot"/> (the position in the list after the drop) and shows a+ /// translucent slice labelled <paramref name="label"/> there. <paramref name="removed"/> is the slice being moved,+ /// which leaves its place. -1 closes the gap.+ /// </summary>+ public void SetDropPreview(int slot, string? label = null, int removed = -1)+ {+ if (_previewSlot == slot && _previewRemoved == removed && _previewLabel == label)+ return;+ if (_previewSlot < 0 && slot >= 0)+ {+ // The new slice grows out of nothing where it appears.+ _previewMid = new Spring { Value = DonutGeometry.MidAngle(slot, SlotCount(removed)) };+ _previewSpan = default;+ }+ _previewSlot = slot;+ _previewRemoved = removed;+ _previewLabel = label;+ StartAnimation();+ }++ /// <summary>The slot under <paramref name="p"/> when the donut has <paramref name="slots"/> slices; -1 off the ring.</summary>+ public int SlotAt(Point p, int slots)+ {+ var hit = DonutGeometry.HitTest(Center, p, InnerRadius, OuterRadius, Math.Max(slots, 1), PopOut);+ return hit.Kind == DonutHitKind.Segment ? hit.Index : -1;+ }++ /// <summary>Slices while previewing: the items, minus the one being moved, plus the preview.</summary>+ private int SlotCount(int removed) => Count + 1 - (removed >= 0 ? 1 : 0);++ /// <summary>Where slice <paramref name="i"/> belongs: its usual place, or shifted to make room for the preview.</summary>+ private (double Mid, double Span) LayoutTarget(int i)+ {+ var n = Count;+ if (_previewSlot < 0)+ return (DonutGeometry.MidAngle(i, n), DonutGeometry.Span(n));+ if (i == _previewRemoved)+ return (i < _mid.Length ? _mid[i].Value : DonutGeometry.MidAngle(i, n), 0); // it becomes the preview+ var slot = _previewRemoved >= 0 && i > _previewRemoved ? i - 1 : i;+ if (slot >= _previewSlot)+ slot++;+ var m = SlotCount(_previewRemoved);+ return (DonutGeometry.MidAngle(slot, m), DonutGeometry.Span(m));+ }++ /// <summary>Moves the slices and the preview towards their places. Returns true while they are still moving.</summary>+ private bool StepLayout(double dt)+ {+ var active = false;+ for (var i = 0; i < _mid.Length; i++)+ {+ var (mid, span) = LayoutTarget(i);+ if (!Motion.Enabled)+ {+ _mid[i] = new Spring { Value = mid };+ _span[i] = new Spring { Value = span };+ continue;+ }+ active |= _mid[i].Step(mid, dt, 420, 36);+ active |= _span[i].Step(span, dt, 420, 36);+ }+ var m = SlotCount(_previewRemoved);+ var previewMid = _previewSlot >= 0 ? DonutGeometry.MidAngle(_previewSlot, m) : _previewMid.Value;+ var previewSpan = _previewSlot >= 0 ? DonutGeometry.Span(m) : 0;+ if (!Motion.Enabled)+ {+ _previewMid = new Spring { Value = previewMid };+ _previewSpan = new Spring { Value = previewSpan };+ return false;+ }+ active |= _previewMid.Step(previewMid, dt, 420, 36);+ active |= _previewSpan.Step(previewSpan, dt, 420, 36);+ return active;+ }+/// <summary>Hit-tests a point in this control's coordinates.</summary>public DonutHit HitTest(Point p){@@ -255,6 +341,12 @@ public sealed partial class DonutMenu : Control_externalHighlight = -1;_dimmedIndex = -1;_dropTargetIndex = -1;+ // A drop changes the items into exactly what the preview showed: start there, without a jump.+ _previewSlot = -1;+ _previewRemoved = -1;+ _previewSpan = default;+ _mid = Enumerable.Range(0, n).Select(i => new Spring { Value = DonutGeometry.MidAngle(i, n) }).ToArray();+ _span = Enumerable.Range(0, n).Select(_ => new Spring { Value = DonutGeometry.Span(n) }).ToArray();InvalidateVisual();}@@ -436,6 +528,7 @@ public sealed partial class DonutMenu : Control}Array.Fill(_open, _closing ? 0 : 1);_centerPulse = default;+ StepLayout(dt);if (_closing)FinishClose();active |= StepProgress(dt); // hold timers still run without animation@@ -446,6 +539,7 @@ public sealed partial class DonutMenu : Controlactive |= _hover[i].Step(i == ActiveIndex ? 1 : 0, dt);active |= _centerPulse.Step(0, dt, 260, 12);active |= StepProgress(dt);+ active |= StepLayout(dt);if (_openClock < double.MaxValue){@@ -524,8 +618,12 @@ public sealed partial class DonutMenu : Controlif (open <= 0.001)continue;- var hover = i < _hover.Length ? _hover[i].Value : 0;- var mid = DonutGeometry.MidAngle(i, n);+ var previewing = IsEditMode && i < _mid.Length;+ var hover = i < _hover.Length && _previewSlot < 0 ? _hover[i].Value : 0;+ var mid = previewing ? _mid[i].Value : DonutGeometry.MidAngle(i, n);+ var span = previewing ? _span[i].Value : DonutGeometry.Span(n);+ if (span < 0.01)+ continue;var offset = DonutGeometry.PointAt(new Point(0, 0), hover * PopOut, mid);// Open animation: scale up from the centre with a small twist.@@ -540,7 +638,7 @@ public sealed partial class DonutMenu : Controlusing (context.PushTransform(transform))using (context.PushOpacity(Math.Clamp(open, 0, 1) * (i == _dimmedIndex ? 0.3 : 1))){- var wedge = CreateWedge(center, r1, r2, i, n);+ var wedge = CreateWedge(center, r1, r2, mid, span);context.DrawGeometry(WithOpacity(SliceBrush, SliceOpacity * (item.IsEnabled ? 1 : 0.55)), BorderPen(), wedge);var emphasis = i == _dropTargetIndex ? 1.0 : hover;@@ -548,11 +646,14 @@ public sealed partial class DonutMenu : Controlcontext.DrawGeometry(WithOpacity(AccentBrush, 0.22 + 0.6 * Math.Clamp(emphasis, 0, 1)), null, wedge);var hot = (i == ActiveIndex || i == _dropTargetIndex) && item.IsEnabled;- DrawLabel(context, item, center, (r1 + r2) / 2, mid, DonutGeometry.Span(n) * (r1 + r2) / 2 - 10,+ DrawLabel(context, item, center, (r1 + r2) / 2, mid, span * (r1 + r2) / 2 - 10,r2 - r1 - 16, typeface, typefaceRegular, hot);}}+ if (IsEditMode && _previewSpan.Value > 0.01)+ DrawPreview(context, center, r1, r2, typeface, typefaceRegular);+DrawCenter(context, center, r1, typeface, typefaceRegular);if (IsEditMode && _hoverIndex >= 0 && _hoverIndex < n && _dimmedIndex < 0)@@ -661,15 +762,32 @@ public sealed partial class DonutMenu : Controlprivate static double Distance(Point a, Point b) => Math.Sqrt(Math.Pow(a.X - b.X, 2) + Math.Pow(a.Y - b.Y, 2));/// <summary>Wedge-shaped slice with a constant-width gap to its neighbours.</summary>- private static StreamGeometry CreateWedge(Point c, double r1, double r2, int index, int count)+ /// <summary>The translucent slice where a dragged preset would land, with its name.</summary>+ private void DrawPreview(DrawingContext context, Point center, double r1, double r2, Typeface bold, Typeface regular)+ {+ var mid = _previewMid.Value;+ var span = _previewSpan.Value;+ var wedge = CreateWedge(center, r1, r2, mid, span);+ var outline = new Pen(WithOpacity(AccentBrush, 0.9), 1.5, new DashStyle([4, 3], 0));+ context.DrawGeometry(WithOpacity(AccentBrush, 0.22), outline, wedge);+ if (_previewLabel is null || span < 0.25)+ return;+ using (context.PushOpacity(Math.Clamp((span - 0.25) / 0.3, 0, 1) * 0.9))+ DrawLabel(context, new DonutItem { Id = "", Label = _previewLabel }, center, (r1 + r2) / 2, mid,+ span * (r1 + r2) / 2 - 10, r2 - r1 - 16, bold, regular, hot: false);+ }++ private static StreamGeometry CreateWedge(Point c, double r1, double r2, int index, int count) =>+ count == 1 ? CreateRing(c, r1, r2) : CreateWedge(c, r1, r2, DonutGeometry.MidAngle(index, count), DonutGeometry.Span(count));++ /// <summary>A slice of <paramref name="span"/> radians centred on <paramref name="mid"/> (a ring when it is all of it).</summary>+ private static StreamGeometry CreateWedge(Point c, double r1, double r2, double mid, double span){- if (count == 1)+ if (span >= 2 * Math.PI - 0.001)return CreateRing(c, r1, r2);- var span = DonutGeometry.Span(count);- var mid = DonutGeometry.MidAngle(index, count);- var outerHalf = span / 2 - GapPixels / 2 / r2;- var innerHalf = span / 2 - GapPixels / 2 / r1;+ var outerHalf = Math.Max(0.001, span / 2 - GapPixels / 2 / r2);+ var innerHalf = Math.Max(0.001, span / 2 - GapPixels / 2 / r1);var geometry = new StreamGeometry();using var ctx = geometry.Open();diff --git a/src/Filee.App/Views/Pages/ToolbarPage.axaml.cs b/src/Filee.App/Views/Pages/ToolbarPage.axaml.csindex e7f5b45e..e303efab 100644--- a/src/Filee.App/Views/Pages/ToolbarPage.axaml.cs+++ b/src/Filee.App/Views/Pages/ToolbarPage.axaml.cs@@ -3,7 +3,8 @@// Sources: a palette chip (add) or a donut slice (reorder / remove).// Targets: a donut slice (insert at / move to that position), the empty ring or the hole (append),// or the palette box (remove a slice).-// A floating "ghost" chip follows the pointer; the donut highlights the slice that would receive the drop.+// A floating "ghost" chip follows the pointer, and the donut opens a gap where the drop would land, with a+// translucent slice of the dragged preset in it (DonutMenu.SetDropPreview).using Avalonia;using Avalonia.Controls;@@ -94,8 +95,9 @@ public partial class ToolbarPage : UserControlShowGhost(_drag.Label, p);var (sliceTarget, overPalette) = FindTarget(e);- Donut.SetDropTarget(sliceTarget);- PaletteBox.Classes.Set("over", overPalette && _drag.Source == Source.Slice);+ var removing = overPalette && _drag.Source == Source.Slice;+ Donut.SetDropPreview(removing ? -1 : sliceTarget, _drag.Label, _drag.Source == Source.Slice ? _drag.SliceIndex : -1);+ PaletteBox.Classes.Set("over", removing);}private void OnRootPointerReleased(object? sender, PointerReleasedEventArgs e)@@ -125,21 +127,19 @@ public partial class ToolbarPage : UserControl}/// <summary>- /// Where would a drop land? Returns the slice index (or SliceCount = append; -1 = not on the donut)- /// and whether the pointer is over the palette.+ /// Where would a drop land? Returns the position in the list after the drop (SliceCount = append; -1 = not on the+ /// donut) and whether the pointer is over the palette. The slots are those of the donut with the gap open (one+ /// more slice for a new preset), so the gap doesn't move away from under the pointer./// </summary>private (int SliceTarget, bool OverPalette) FindTarget(PointerEventArgs e){var overPalette = new Rect(PaletteBox.Bounds.Size).Contains(e.GetPosition(PaletteBox));- var hit = Donut.HitTest(e.GetPosition(Donut));+ var point = e.GetPosition(Donut);var count = Vm?.SliceCount ?? 0;- var target = hit.Kind switch- {- DonutHitKind.Segment or DonutHitKind.EditButton => hit.Index,- DonutHitKind.EmptyRing => 0,- DonutHitKind.Center when _drag?.Source == Source.Chip => count, // drop in the hole = append- _ => -1,- };+ var adding = _drag?.Source == Source.Chip;+ var target = Donut.HitTest(point).Kind == DonutHitKind.Center+ ? adding ? count : -1 // drop in the hole = append+ : Donut.SlotAt(point, adding ? count + 1 : count);return (target, overPalette);}@@ -156,6 +156,7 @@ public partial class ToolbarPage : UserControl_drag = null;Ghost.IsVisible = false;Donut.SetDropTarget(-1);+ Donut.SetDropPreview(-1);Donut.SetDimmedIndex(-1);PaletteBox.Classes.Set("over", false);}diff --git a/tests/Filee.App.Tests/RenderTests.cs b/tests/Filee.App.Tests/RenderTests.csindex 30239158..95e0a86b 100644--- a/tests/Filee.App.Tests/RenderTests.cs+++ b/tests/Filee.App.Tests/RenderTests.cs@@ -104,6 +104,47 @@ public class RenderTests}}+ [AvaloniaFact]+ public void Dragging_a_preset_opens_a_gap_with_a_preview_slice()+ {+ TestServices.EnsureInitialized("ko");+ var vm = new MainWindowViewModel(AppHost.Services, AppHost.Get<Filee.Core.Localization.ILocalizer>(), AppHost.Get<UpdateService>());+ vm.Navigate("toolbar");+ var window = new MainWindow { DataContext = vm, Width = 1080, Height = 820 };+ window.Show();+ Pump();+ var donut = window.GetVisualDescendants().OfType<DonutMenu>().Single(d => d.IsEditMode);+ var count = donut.Items!.Count;++ // A chip over the third slice: the slices from there on slide clockwise and a preview takes slot 2.+ donut.SetDropPreview(2, "TIFF 300dpi");+ for (var i = 0; i < 120; i++)+ donut.Advance(1 / 60.0);+ donut.InvalidateVisual();+ Pump();+ Assert.Equal(2, donut.SlotAt(DonutPoint(donut, 2, count + 1), count + 1));+ Save(window, "toolbar-drop-preview-ko.png");++ // Moving the first slice to slot 3: it leaves its place, the preview shows it at the new one.+ donut.SetDropPreview(3, donut.Items[0].Label, removed: 0);+ for (var i = 0; i < 120; i++)+ donut.Advance(1 / 60.0);+ donut.InvalidateVisual();+ Pump();+ Save(window, "toolbar-move-preview-ko.png");++ donut.SetDropPreview(-1);+ window.Close();+ }++ /// <summary>The middle of slot <paramref name="slot"/> of a donut with <paramref name="slots"/> slices.</summary>+ private static Avalonia.Point DonutPoint(DonutMenu donut, int slot, int slots)+ {+ var center = new Avalonia.Point(donut.Bounds.Width / 2, donut.Bounds.Height / 2);+ var radius = donut.OuterRadius * (1 + donut.HoleRatio) / 2;+ return DonutGeometry.PointAt(center, radius, DonutGeometry.MidAngle(slot, slots));+ }+[AvaloniaFact]public void Donut_toolbar_renders_for_images(){