Skip to content

armgit / Filee

Public Imported from GitHub
0

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.

Donut groups: a ring never overlaps itself and keeps its labels readable

SeoJCalive34aa0e758562
diff --git a/src/Filee.App/Controls/DonutGeometry.cs b/src/Filee.App/Controls/DonutGeometry.cs
index 7d3ef46c..58de8a75 100644
--- a/src/Filee.App/Controls/DonutGeometry.cs
+++ b/src/Filee.App/Controls/DonutGeometry.cs
@@ -64,12 +64,24 @@ public static class DonutGeometry
/// <summary>How far a group's fan reaches into each neighbouring slice, as a share of a slice.</summary>
public const double SubOverhang = 0.25;
+ /// <summary>Narrowest a slice of a group's ring gets (along its middle), so its label still fits.</summary>
+ public const double MinSubWidth = 44;
+
/// <summary>
/// Width of one slice of a group's outer ring: the fan covers the group's slice and a quarter of each neighbour
- /// (<see cref="SubOverhang"/>), split evenly, so it stays close to the slice it belongs to.
+ /// (<see cref="SubOverhang"/>), split evenly, so it stays close to the slice it belongs to. On a crowded donut it
+ /// widens to keep each slice <see cref="MinSubWidth"/> wide at <paramref name="midRadius"/>; it never goes round
+ /// more than once (a donut of one slice), where slices would lie on top of each other.
/// </summary>
- public static double SubSpan(double parentSpan, int count) =>
- count <= 0 ? 0 : parentSpan * (1 + 2 * SubOverhang) / count;
+ public static double SubSpan(double parentSpan, int count, double midRadius = 0)
+ {
+ if (count <= 0)
+ return 0;
+ var span = parentSpan * (1 + 2 * SubOverhang) / count;
+ if (midRadius > 0)
+ span = Math.Max(span, MinSubWidth / midRadius);
+ return Math.Min(span, 2 * Math.PI / count);
+ }
/// <summary>Centre angle of slice <paramref name="sub"/> of a fan of <paramref name="count"/>, centred on <paramref name="parentMid"/>.</summary>
public static double SubMidAngle(double parentMid, int sub, int count, double subSpan) =>
diff --git a/src/Filee.App/Controls/DonutMenu.cs b/src/Filee.App/Controls/DonutMenu.cs
index e68c171b..436eb4dc 100644
--- a/src/Filee.App/Controls/DonutMenu.cs
+++ b/src/Filee.App/Controls/DonutMenu.cs
@@ -382,7 +382,7 @@ public sealed partial class DonutMenu : Control
{
var (inner, outer) = SubRadii;
var parentMid = DonutGeometry.MidAngle(_expanded, Count);
- var subSpan = DonutGeometry.SubSpan(DonutGeometry.Span(Count), children.Count);
+ var subSpan = SubSpanOf(children.Count);
var sub = DonutGeometry.SubIndexAt(Center, p, inner, outer, parentMid, children.Count, subSpan);
if (sub >= 0)
return new DonutHit(DonutHitKind.SubSegment, _expanded, sub);
@@ -412,6 +412,18 @@ public sealed partial class DonutMenu : Control
private (double Inner, double Outer) SubRadii => (OuterRadius + PopOut + SubGap, OuterRadius + PopOut + SubGap + SubBand);
+ /// <summary>Width of each slice of a ring of <paramref name="count"/>.</summary>
+ private double SubSpanOf(int count) =>
+ DonutGeometry.SubSpan(DonutGeometry.Span(Count), count, (SubRadii.Inner + SubRadii.Outer) / 2);
+
+ /// <summary>The middle of slice <paramref name="sub"/> of group <paramref name="group"/>'s ring (tests).</summary>
+ internal Point SubSliceCenter(int group, int sub)
+ {
+ var count = Items![group].Children!.Count;
+ var mid = DonutGeometry.SubMidAngle(DonutGeometry.MidAngle(group, Count), sub, count, SubSpanOf(count));
+ return DonutGeometry.PointAt(Center, (SubRadii.Inner + SubRadii.Outer) / 2, mid);
+ }
+
/// <summary>
/// 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
@@ -1035,9 +1047,9 @@ public sealed partial class DonutMenu : Control
var group = _shownExpanded;
var children = Items![group].Children!;
var parentMid = DonutGeometry.MidAngle(group, Count);
- var subSpan = DonutGeometry.SubSpan(DonutGeometry.Span(Count), children.Count);
+ var subSpan = SubSpanOf(children.Count);
var (inner, outer) = SubRadii;
- var t = Math.Max(0, _expand.Value);
+ var t =Math.Max(0, _expand.Value);
var opening = _open.Length == 0 ? 1 : Math.Clamp(_open.Average(), 0, 1);
// Grows outward from the group's slice: scaled about the point where it meets the donut.
var anchor = DonutGeometry.PointAt(center, inner, parentMid);
diff --git a/tests/Filee.App.Tests/DonutGeometryTests.cs b/tests/Filee.App.Tests/DonutGeometryTests.cs
index 840c61cd..ce52a0a9 100644
--- a/tests/Filee.App.Tests/DonutGeometryTests.cs
+++ b/tests/Filee.App.Tests/DonutGeometryTests.cs
@@ -52,6 +52,18 @@ public class DonutGeometryTests
Assert.Equal(DonutGeometry.Span(9) * 1.5, subSpan * 3, 6);
}
+ [Fact]
+ public void A_groups_ring_never_overlaps_itself_and_keeps_room_for_labels()
+ {
+ // A donut of just the group: 1.5 turns would put slices on top of each other; it goes round once at most.
+ Assert.Equal(2 * Math.PI, DonutGeometry.SubSpan(DonutGeometry.Span(1), 3, 190) * 3, 6);
+ Assert.Equal(2 * Math.PI, DonutGeometry.SubSpan(DonutGeometry.Span(1), 2) * 2, 6);
+ // A crowded donut (12 slices, a group of 6): each ring slice stays wide enough for its label.
+ Assert.Equal(DonutGeometry.MinSubWidth, DonutGeometry.SubSpan(DonutGeometry.Span(12), 6, 190) * 190, 6);
+ // Roomy enough already: unchanged.
+ Assert.Equal(DonutGeometry.Span(4) * 1.5 / 3, DonutGeometry.SubSpan(DonutGeometry.Span(4), 3, 190), 6);
+ }
+
[Fact]
public void Hole_ring_and_outside_are_classified()
{
diff --git a/tests/Filee.App.Tests/DonutGroupTests.cs b/tests/Filee.App.Tests/DonutGroupTests.cs
index 29dbde5e..a259fb20 100644
--- a/tests/Filee.App.Tests/DonutGroupTests.cs
+++ b/tests/Filee.App.Tests/DonutGroupTests.cs
@@ -16,20 +16,18 @@ public class DonutGroupTests
{
private const int Group = 2;
+ private static DonutItem Item(string label, bool enabled = true) => new() { Id = label, Label = label, IsEnabled = enabled };
+
+ private static DonutItem GroupOf(params DonutItem[] children) => new() { Id = "group", Label = "More", Children = children };
+
/// <summary>PNG, JPG, a group of three (WEBP / PDF / TIFF) and BMP.</summary>
- private static (DonutMenu Donut, Window Window) Open()
+ private static (DonutMenu Donut, Window Window) Open() =>
+ Open(Item("PNG"), Item("JPG"), GroupOf(Item("WEBP"), Item("PDF"), Item("TIFF")), Item("BMP"));
+
+ private static (DonutMenu Donut, Window Window) Open(params DonutItem[] items)
{
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 donut = new DonutMenu { Items = items };
var window = new Window { Content = donut, SizeToContent = SizeToContent.WidthAndHeight };
window.Show();
Dispatcher.UIThread.RunJobs();
@@ -43,12 +41,7 @@ public class DonutGroupTests
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 Point OnRing(DonutMenu donut, int sub) => donut.SubSliceCenter(Group, sub);
private static bool IsOpen(DonutMenu donut) => donut.HitTest(OnRing(donut, 1)).Kind == DonutHitKind.SubSegment;
@@ -85,6 +78,19 @@ public class DonutGroupTests
window.Close();
}
+ [AvaloniaFact]
+ public void A_donut_of_just_a_group_fans_out_slices_that_dont_overlap()
+ {
+ var (donut, window) = Open(GroupOf(Item("WEBP"), Item("PDF"), Item("TIFF")));
+ var at = DonutGeometry.PointAt(Center(donut), donut.OuterRadius * 0.6, 0);
+ donut.SetExternalHit(donut.HitTest(at), at);
+ donut.ExpandPendingGroup();
+ // Each slice is hit where it is drawn: around the donut once, not one and a half times over itself.
+ for (var sub = 0; sub < 3; sub++)
+ Assert.Equal(new DonutHit(DonutHitKind.SubSegment, 0, sub), donut.HitTest(donut.SubSliceCenter(0, sub)));
+ window.Close();
+ }
+
[AvaloniaFact]
public void The_keyboard_moves_opens_converts_and_goes_back()
{
diff --git a/tests/Filee.App.Tests/RenderTests.cs b/tests/Filee.App.Tests/RenderTests.cs
index 4e9de2cb..27887d9b 100644
--- a/tests/Filee.App.Tests/RenderTests.cs
+++ b/tests/Filee.App.Tests/RenderTests.cs
@@ -377,9 +377,8 @@ public class RenderTests
Pump();
var n = vm.Items.Count;
- var mid = DonutGeometry.SubMidAngle(DonutGeometry.MidAngle(index, n), 1, 3, DonutGeometry.SubSpan(DonutGeometry.Span(n), 3));
var center = new Avalonia.Point(donut.Bounds.Width / 2, donut.Bounds.Height / 2);
- var onRing = DonutGeometry.PointAt(center, vm.OuterRadius + 7 + DonutMenu.SubRingSpace / 2 + 3, mid);
+ var onRing = donut.SubSliceCenter(index, 1);
Assert.Equal(new DonutHit(DonutHitKind.SubSegment, index, 1), donut.HitTest(onRing));
// Heading out to the fan's edge across a neighbour's outer half keeps the group; its inner half doesn't.
var neighbour = DonutGeometry.MidAngle(index, n) - DonutGeometry.Span(n) * 0.6;