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
Donut groups: open only when meant, keyboard, on screen near edges
SeoJCaliveb76fa5ea4317
diff --git a/src/Filee.App/Controls/DonutMenu.cs b/src/Filee.App/Controls/DonutMenu.csindex 50e4157c..759b6548 100644--- a/src/Filee.App/Controls/DonutMenu.cs+++ b/src/Filee.App/Controls/DonutMenu.cs@@ -136,6 +136,16 @@ public sealed partial class DonutMenu : Controlprivate int _subHover = -1;private Spring _expand;+ // A group slice the pointer is on but hasn't opened yet (-1 none), and how far from the centre it came in.+ private int _pendingGroup = -1;+ private double _pendingDistance;++ /// <summary>How long the pointer rests on a group slice before it opens: passing over on the way elsewhere doesn't.</summary>+ internal static readonly TimeSpan ExpandDelay = TimeSpan.FromMilliseconds(160);++ /// <summary>Moving this far outward on a group slice opens it at once (the user is heading for its ring).</summary>+ private const double OutwardPixels = 10;+static DonutMenu(){AffectsMeasure<DonutMenu>(OuterRadiusProperty, ReserveSubRingProperty);@@ -403,36 +413,132 @@ public sealed partial class DonutMenu : Controlprivate (double Inner, double Outer) SubRadii => (OuterRadius + PopOut + SubGap, OuterRadius + PopOut + SubGap + SubBand);/// <summary>- /// Fans a group out when its slice is reached and keeps it open while the pointer is on its ring or crossing the- /// gap to it; another slice or the hole folds it away.+ /// Fans a group out once the pointer means it: after resting on its slice for <see cref="ExpandDelay"/>, or at once+ /// when it moves outward on it (<paramref name="at"/>, when known). Keeps it open while the pointer is on its ring or+ /// crossing the gap to it; another slice or the hole folds it away./// </summary>- private void TrackGroups(DonutHit hit)+ private void TrackGroups(DonutHit hit, Point? at){if (IsEditMode)return;var sub = -1;switch (hit.Kind){- case DonutHitKind.Segment:- _expanded = hit.Index >= 0 && hit.Index < Count && Items![hit.Index].IsGroup ? hit.Index : -1;+ case DonutHitKind.Segment when hit.Index == _expanded:+ break;+ case DonutHitKind.Segment when hit.Index >= 0 && hit.Index < Count && Items![hit.Index].IsGroup:+ _expanded = -1;+ var distance = at is { } p ? Distance(p, Center) : 0;+ if (_pendingGroup != hit.Index)+ {+ _pendingGroup = hit.Index;+ _pendingDistance = distance;+ var group = hit.Index;+ DispatcherTimer.RunOnce(() =>+ {+ if (_pendingGroup == group)+ ExpandPendingGroup();+ }, ExpandDelay);+ }+ else if (at is not null && distance - _pendingDistance >= OutwardPixels)+ {+ ExpandPendingGroup();+ }break;case DonutHitKind.SubSegment:sub = hit.Sub;break;case DonutHitKind.Outside:+ _pendingGroup = -1; // left the slice before it opened: it stays closedbreak;default:- _expanded = -1;+ _expanded = _pendingGroup = -1;break;}- if (_expanded >= 0)- _shownExpanded = _expanded;- if (_subHover != sub)+ SetSub(sub);+ StartAnimation();+ }++ /// <summary>Opens the group the pointer rests on (the timer of <see cref="TrackGroups"/>; tests call it directly).</summary>+ internal void ExpandPendingGroup()+ {+ if (_pendingGroup < 0 || _pendingGroup >= Count)+ return;+ Expand(_pendingGroup);+ }++ private void Expand(int group)+ {+ _expanded = _shownExpanded = group;+ _pendingGroup = -1;+ ActiveIndexChanged?.Invoke(this, ActiveIndex);+ StartAnimation();+ }++ private void SetSub(int sub)+ {+ if (_subHover == sub)+ return;+ _subHover = sub;+ ActiveIndexChanged?.Invoke(this, ActiveIndex);+ StartAnimation();+ }++ /// <summary>+ /// Keyboard (click mode): arrows and Tab move around the donut, Enter converts or opens a group; in a group's ring+ /// the arrows move between its slices, Enter converts and Esc / Backspace go back to the donut.+ /// Returns false for keys left to the window (Esc on the donut closes it).+ /// </summary>+ public bool HandleKey(Key key, KeyModifiers modifiers = KeyModifiers.None)+ {+ if (IsEditMode || _progressMode || Count == 0)+ return false;+ var step = key switch+ {+ Key.Right or Key.Down => 1,+ Key.Left or Key.Up => -1,+ Key.Tab => modifiers.HasFlag(KeyModifiers.Shift) ? -1 : 1,+ _ => 0,+ };++ if (ExpandedChildren is { } children && _subHover >= 0)+ {+ switch (key)+ {+ case Key.Enter or Key.Space:+ if (children[_subHover].IsEnabled)+ SubSegmentInvoked?.Invoke(this, (_expanded, _subHover));+ return true;+ case Key.Escape or Key.Back:+ CollapseGroup();+ return true;+ }+ if (step == 0)+ return false;+ SetSub((_subHover + step + children.Count) % children.Count);+ return true;+ }++ var current = ActiveIndex;+ switch (key){- _subHover = sub;- ActiveIndexChanged?.Invoke(this, ActiveIndex);+ case Key.Enter or Key.Space when current >= 0:+ if (Items![current].IsGroup)+ {+ Expand(current);+ SetSub(0);+ }+ else if (Items[current].IsEnabled)+ {+ SegmentInvoked?.Invoke(this, current);+ }+ return true;}- StartAnimation();+ if (step == 0)+ return false;+ CollapseGroup();+ SetHover(current < 0 ? (step > 0 ? 0 : Count - 1) : (current + step + Count) % Count);+ return true;}/// <summary>True when dropping or clicking at <paramref name="hit"/> converts: a slice that isn't a group, or a slice of a group's ring.</summary>@@ -446,15 +552,18 @@ public sealed partial class DonutMenu : Control/// <summary>Folds an open group away (the drag left the window, or files were dropped).</summary>public void CollapseGroup(){- _expanded = -1;- _subHover = -1;+ _expanded = _pendingGroup = -1;+ SetSub(-1);StartAnimation();}- /// <summary>Highlights what a drag is over from outside: a slice, a slice of a group's ring, or the hole.</summary>- public void SetExternalHit(DonutHit hit)+ /// <summary>+ /// Highlights what a drag is over from outside: a slice, a slice of a group's ring, or the hole.+ /// <paramref name="at"/> (in this control) lets a group open at once when the drag heads outward on it.+ /// </summary>+ public void SetExternalHit(DonutHit hit, Point? at = null){- TrackGroups(hit);+ TrackGroups(hit, at);SetExternalHighlight(hit.Kind is DonutHitKind.Segment or DonutHitKind.SubSegment ? hit.Index : -1,hit.Kind == DonutHitKind.Center);}@@ -474,7 +583,7 @@ public sealed partial class DonutMenu : Control_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();- _expanded = _shownExpanded = _subHover = -1;+ _expanded = _shownExpanded = _subHover = _pendingGroup = -1;_expand = default;InvalidateMeasure();InvalidateVisual();@@ -500,7 +609,7 @@ public sealed partial class DonutMenu : Control}var hit = HitTest(p);- TrackGroups(hit);+ TrackGroups(hit, p);SetHover(hit.Kind switch{DonutHitKind.Segment or DonutHitKind.EditButton or DonutHitKind.SubSegment => hit.Index,diff --git a/src/Filee.App/Services/RadialController.cs b/src/Filee.App/Services/RadialController.csindex 279ad386..2c70ed83 100644--- a/src/Filee.App/Services/RadialController.cs+++ b/src/Filee.App/Services/RadialController.cs@@ -276,6 +276,21 @@ public sealed class RadialController(_window.Activate();}+ /// <summary>+ /// Top-left corner of a <paramref name="sidePx"/> square window centred on <paramref name="cursor"/>, moved into+ /// <paramref name="area"/> when that would cross its edge. With group slices the window holds the ring they fan out+ /// (<see cref="DonutMenu.SideFor"/>), so near a screen edge the donut moves in far enough for every ring to fit.+ /// </summary>+ internal static PixelPoint WindowPosition(PixelPoint cursor, PixelRect? area, int sidePx)+ {+ var position = new PixelPoint(cursor.X - sidePx / 2, cursor.Y - sidePx / 2);+ if (area is not { } a)+ return position;+ return new PixelPoint(+ Math.Clamp(position.X, a.X, Math.Max(a.X, a.Right - sidePx)),+ Math.Clamp(position.Y, a.Y, Math.Max(a.Y, a.Bottom - sidePx)));+ }+private void PlaceAndShow(RadialWindow window, int x, int y, bool activate){var cursor = new PixelPoint(x, y);@@ -285,14 +300,7 @@ public sealed class RadialController(// Window size follows the donut size (SizeToContent); compute it up front to centre on the cursor.var side = DonutMenu.SideFor(viewModel.OuterRadius, viewModel.ReserveSubRing);var sidePx = (int)Math.Ceiling(side * scaling);- var position = new PixelPoint(x - sidePx / 2, y - sidePx / 2);- if (screen is not null)- {- var area = screen.WorkingArea;- position = new PixelPoint(- Math.Clamp(position.X, area.X, Math.Max(area.X, area.Right - sidePx)),- Math.Clamp(position.Y, area.Y, Math.Max(area.Y, area.Bottom - sidePx)));- }+ var position = WindowPosition(cursor, screen?.WorkingArea, sidePx);Motion.Refresh();window.Position = position;diff --git a/src/Filee.App/Views/RadialWindow.axaml.cs b/src/Filee.App/Views/RadialWindow.axaml.csindex 07fc9331..2ce59d51 100644--- a/src/Filee.App/Views/RadialWindow.axaml.cs+++ b/src/Filee.App/Views/RadialWindow.axaml.cs@@ -55,11 +55,12 @@ public partial class RadialWindow : Windowprivate void OnDragOver(object? sender, DragEventArgs e){- var hit = Donut.HitTest(e.GetPosition(Donut));+ var at = e.GetPosition(Donut);+ var hit = Donut.HitTest(at);_dragOverEvents++;_lastDragHit = hit;var files = e.DataTransfer.Contains(DataFormat.File);- Donut.SetExternalHit(hit);+ Donut.SetExternalHit(hit, at);// Always COPY. Reporting MOVE would make Explorer delete the source files after the drop.e.DragEffects = files && Donut.Converts(hit) ? DragDropEffects.Copy : DragDropEffects.None;@@ -88,6 +89,11 @@ public partial class RadialWindow : Windowprotected override void OnKeyDown(KeyEventArgs e){base.OnKeyDown(e);+ if (Donut.HandleKey(e.Key, e.KeyModifiers))+ {+ e.Handled = true;+ return;+ }if (e.Key == Key.Escape){EscapePressed?.Invoke();diff --git a/tests/Filee.App.Tests/DonutGroupTests.cs b/tests/Filee.App.Tests/DonutGroupTests.csnew file mode 100644index 00000000..29dbde5e--- /dev/null+++ b/tests/Filee.App.Tests/DonutGroupTests.cs@@ -0,0 +1,169 @@+// Group slices: a group opens only when the pointer means it, works from the keyboard and opens without animation+// when animations are off.++using Avalonia;+using Avalonia.Controls;+using Avalonia.Headless.XUnit;+using Avalonia.Input;+using Avalonia.Threading;+using Filee.App.Controls;+using Filee.App.Services;++namespace Filee.App.Tests;++[Collection("Motion")]+public class DonutGroupTests+{+ private const int Group = 2;++ /// <summary>PNG, JPG, a group of three (WEBP / PDF / TIFF) and BMP.</summary>+ private static (DonutMenu Donut, Window Window) Open()+ {+ TestServices.EnsureInitialized("en");+ static DonutItem Item(string label) => new() { Id = label, Label = label };+ var donut = new DonutMenu+ {+ Items =+ [+ Item("PNG"), Item("JPG"),+ new DonutItem { Id = "group", Label = "More", Children = [Item("WEBP"), Item("PDF"), Item("TIFF")] },+ Item("BMP"),+ ],+ };+ var window = new Window { Content = donut, SizeToContent = SizeToContent.WidthAndHeight };+ window.Show();+ Dispatcher.UIThread.RunJobs();+ return (donut, window);+ }++ private static Point Center(DonutMenu donut) => new(donut.Bounds.Width / 2, donut.Bounds.Height / 2);++ /// <summary>A point on the group's slice at <paramref name="radius"/> from the centre.</summary>+ private static Point OnGroup(DonutMenu donut, double radius) =>+ DonutGeometry.PointAt(Center(donut), radius, DonutGeometry.MidAngle(Group, donut.Items!.Count));++ /// <summary>The middle of the group's ring slice <paramref name="sub"/>.</summary>+ private static Point OnRing(DonutMenu donut, int sub)+ {+ var n = donut.Items!.Count;+ var mid = DonutGeometry.SubMidAngle(DonutGeometry.MidAngle(Group, n), sub, 3, DonutGeometry.SubSpan(DonutGeometry.Span(n), 3));+ return DonutGeometry.PointAt(Center(donut), donut.OuterRadius + 7 + DonutMenu.SubRingSpace / 2 + 3, mid);+ }++ private static bool IsOpen(DonutMenu donut) => donut.HitTest(OnRing(donut, 1)).Kind == DonutHitKind.SubSegment;++ [AvaloniaFact]+ public void Passing_over_a_group_on_the_way_elsewhere_does_not_open_it()+ {+ var (donut, window) = Open();+ var at = OnGroup(donut, donut.OuterRadius * 0.7);+ donut.SetExternalHit(donut.HitTest(at), at);+ Assert.False(IsOpen(donut)); // not at once++ var bmp = DonutGeometry.PointAt(Center(donut), donut.OuterRadius * 0.7, DonutGeometry.MidAngle(3, 4));+ donut.SetExternalHit(donut.HitTest(bmp), bmp);+ donut.ExpandPendingGroup(); // the delay runs out after the pointer moved on+ Assert.False(IsOpen(donut));+ window.Close();+ }++ [AvaloniaFact]+ public void Resting_on_a_group_or_heading_outward_opens_it()+ {+ var (donut, window) = Open();+ var inner = OnGroup(donut, donut.OuterRadius * 0.6);+ donut.SetExternalHit(donut.HitTest(inner), inner);+ donut.ExpandPendingGroup(); // rested for the delay+ Assert.True(IsOpen(donut));++ donut.CollapseGroup();+ donut.SetExternalHit(donut.HitTest(inner), inner);+ var further = OnGroup(donut, donut.OuterRadius * 0.6 + 12);+ donut.SetExternalHit(donut.HitTest(further), further); // moving outward: no waiting+ Assert.True(IsOpen(donut));+ Assert.Equal(new DonutHit(DonutHitKind.SubSegment, Group, 2), donut.HitTest(OnRing(donut, 2)));+ window.Close();+ }++ [AvaloniaFact]+ public void The_keyboard_moves_opens_converts_and_goes_back()+ {+ var (donut, window) = Open();+ var invoked = new List<int>();+ var subInvoked = new List<(int, int)>();+ donut.SegmentInvoked += (_, i) => invoked.Add(i);+ donut.SubSegmentInvoked += (_, hit) => subInvoked.Add(hit);++ Assert.True(donut.HandleKey(Key.Right)); // nothing active: the first slice+ Assert.Equal(0, donut.ActiveIndex);+ donut.HandleKey(Key.Left); // wraps around+ Assert.Equal(3, donut.ActiveIndex);+ donut.HandleKey(Key.Enter);+ Assert.Equal([3], invoked);++ donut.HandleKey(Key.Up); // to the group+ Assert.Equal(Group, donut.ActiveIndex);+ donut.HandleKey(Key.Enter); // opens it on its first slice+ Assert.True(IsOpen(donut));+ Assert.Equal(0, donut.ActiveSubIndex);+ donut.HandleKey(Key.Right);+ donut.HandleKey(Key.Enter);+ Assert.Equal([(Group, 1)], subInvoked);+ Assert.Equal([3], invoked); // the group itself converts nothing++ Assert.True(donut.HandleKey(Key.Escape)); // back to the donut, not closing it+ Assert.False(IsOpen(donut));+ Assert.Equal(Group, donut.ActiveIndex);+ Assert.False(donut.HandleKey(Key.Escape)); // now Esc is the window's: it closes the donut+ window.Close();+ }++ [AvaloniaFact]+ public void With_animations_off_a_group_opens_at_once()+ {+ var (donut, window) = Open();+ try+ {+ Motion.Configure(true, () => false); // "Reduce motion" on+ var inner = OnGroup(donut, donut.OuterRadius * 0.6);+ donut.SetExternalHit(donut.HitTest(inner), inner);+ donut.ExpandPendingGroup();+ Assert.False(donut.Advance(0)); // nothing left to animate: drawn fully open on the first frame+ Assert.True(IsOpen(donut));+ }+ finally+ {+ Motion.Configure(false, () => false);+ window.Close();+ }+ }++ [Theory]+ [InlineData(5, 5)] // top-left corner+ [InlineData(1915, 1075)] // bottom-right corner+ [InlineData(960, 3)] // top edge+ [InlineData(2500, 540)] // second monitor's cursor reported on this one: still pulled in+ public void Near_a_screen_edge_the_rings_of_groups_stay_on_screen(int x, int y)+ {+ var area = new PixelRect(0, 0, 1920, 1080);+ const double radius = 150;+ var side = (int)Math.Ceiling(DonutMenu.SideFor(radius, subRing: true));++ var position = RadialController.WindowPosition(new PixelPoint(x, y), area, side);++ // The ring a group fans out (labels included) is inside the window, and the window inside the screen.+ var center = new PixelPoint(position.X + side / 2, position.Y + side / 2);+ var ringReach = radius + 7 + DonutMenu.SubRingSpace;+ Assert.True(ringReach <= side / 2.0);+ Assert.True(center.X - ringReach >= area.X && center.X + ringReach <= area.Right, $"x {center.X}");+ Assert.True(center.Y - ringReach >= area.Y && center.Y + ringReach <= area.Bottom, $"y {center.Y}");+ }++ [Fact]+ public void Away_from_the_edges_the_donut_is_centred_on_the_cursor()+ {+ var side = (int)Math.Ceiling(DonutMenu.SideFor(150, subRing: true));+ var position = RadialController.WindowPosition(new PixelPoint(960, 540), new PixelRect(0, 0, 1920, 1080), side);+ Assert.Equal(new PixelPoint(960 - side / 2, 540 - side / 2), position);+ }+}diff --git a/tests/Filee.App.Tests/RenderTests.cs b/tests/Filee.App.Tests/RenderTests.csindex 23c1f85d..4e9de2cb 100644--- a/tests/Filee.App.Tests/RenderTests.cs+++ b/tests/Filee.App.Tests/RenderTests.cs@@ -371,6 +371,7 @@ public class RenderTestswindow.Show();var donut = window.DonutControl;donut.SetExternalHit(new DonutHit(DonutHitKind.Segment, index));+ donut.ExpandPendingGroup(); // as after resting on itdonut.SetExternalHit(new DonutHit(DonutHitKind.SubSegment, index, 1));vm.ShowHint(index, donut.ActiveSubIndex, centerHot: false);Pump();