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.

fix(images): fit the hide window's toolbar by measuring, in two steps

KnifeLemon7e3d4c884b99
diff --git a/src/Filee.App/Views/HideWindow.axaml b/src/Filee.App/Views/HideWindow.axaml
index f46641ed..bc5430a2 100644
--- a/src/Filee.App/Views/HideWindow.axaml
+++ b/src/Filee.App/Views/HideWindow.axaml
@@ -163,11 +163,12 @@
<Setter Property="Opacity" Value="1" />
</Style>
- <!-- A narrow window (or long names): the toolbar's buttons show their icons only, their names in the tooltips -->
- <Style Selector="Window.compact TextBlock.named, Window.compact Border.named">
+ <!-- A narrow window (or long names, or wide fonts): the toolbar gives up names in two steps (HideWindow.FitToolbar),
+ first those of Clear all and Skip and the keycaps, then those of Pixelate and Blur; the tooltips keep them -->
+ <Style Selector="Window.tight TextBlock.minor, Window.tight Border.minor, Window.compact TextBlock.major">
<Setter Property="IsVisible" Value="False" />
</Style>
- <Style Selector="Window.compact Button.tool:not(.round)">
+ <Style Selector="Window.tight Button.tool:not(.round)">
<Setter Property="Padding" Value="10,8" />
</Style>
<Style Selector="Slider#StrengthSlider">
@@ -302,7 +303,7 @@
ToolTip.Tip="{DynamicResource hide.pixelate_hint}" AutomationProperties.Name="{DynamicResource hide.pixelate}">
<StackPanel Orientation="Horizontal" Spacing="7">
<PathIcon Width="13" Height="13" Data="{StaticResource Icon.Pixelate}" Foreground="{DynamicResource AccentBrush}" />
- <TextBlock Classes="named" Text="{DynamicResource hide.pixelate}" FontSize="13" VerticalAlignment="Center" />
+ <TextBlock Classes="major" Text="{DynamicResource hide.pixelate}" FontSize="13" VerticalAlignment="Center" />
</StackPanel>
</Button>
<Button x:Name="BlurButton" Classes="tool" Classes.open="{Binding IsBlur}" Padding="10,8,12,8"
@@ -310,7 +311,7 @@
ToolTip.Tip="{DynamicResource hide.blur_hint}" AutomationProperties.Name="{DynamicResource hide.blur}">
<StackPanel Orientation="Horizontal" Spacing="7">
<PathIcon Width="13" Height="13" Data="{StaticResource Hide.Blur}" Foreground="{DynamicResource AccentBrush}" />
- <TextBlock Classes="named" Text="{DynamicResource hide.blur}" FontSize="13" VerticalAlignment="Center" />
+ <TextBlock Classes="major" Text="{DynamicResource hide.blur}" FontSize="13" VerticalAlignment="Center" />
</StackPanel>
</Button>
@@ -343,14 +344,14 @@
ToolTip.Tip="{DynamicResource hide.clear}" AutomationProperties.Name="{DynamicResource hide.clear}">
<StackPanel Orientation="Horizontal" Spacing="7">
<PathIcon Width="13" Height="13" Data="{StaticResource Icon.Refresh}" Foreground="{DynamicResource TextSecondaryBrush}" />
- <TextBlock Classes="named" Text="{DynamicResource hide.clear}" FontSize="13" VerticalAlignment="Center" />
- <Border Classes="key named"><TextBlock Text="R" /></Border>
+ <TextBlock Classes="minor" Text="{DynamicResource hide.clear}" FontSize="13" VerticalAlignment="Center" />
+ <Border Classes="key minor"><TextBlock Text="R" /></Border>
</StackPanel>
</Button>
<Button Classes="tool" Command="{Binding SkipCommand}" IsVisible="{Binding HasMultipleFiles}"
ToolTip.Tip="{DynamicResource crop.skip}" AutomationProperties.Name="{DynamicResource crop.skip}">
<StackPanel Orientation="Horizontal" Spacing="5">
- <TextBlock Classes="named" Text="{DynamicResource crop.skip}" FontSize="13" VerticalAlignment="Center" />
+ <TextBlock Classes="minor" Text="{DynamicResource crop.skip}" FontSize="13" VerticalAlignment="Center" />
<PathIcon Width="11" Height="11" Data="{StaticResource Icon.ChevronRight}" Foreground="{DynamicResource TextSecondaryBrush}" />
</StackPanel>
</Button>
@@ -359,7 +360,7 @@
AutomationProperties.Name="{Binding SaveText}">
<StackPanel Orientation="Horizontal" Spacing="9">
<TextBlock Text="{Binding SaveText}" FontSize="13" VerticalAlignment="Center" />
- <Border Classes="key onaccent named"><TextBlock Text="Enter" /></Border>
+ <Border Classes="key onaccent minor"><TextBlock Text="Enter" /></Border>
</StackPanel>
</Button>
</StackPanel>
diff --git a/src/Filee.App/Views/HideWindow.axaml.cs b/src/Filee.App/Views/HideWindow.axaml.cs
index c3eda35d..b625c7d3 100644
--- a/src/Filee.App/Views/HideWindow.axaml.cs
+++ b/src/Filee.App/Views/HideWindow.axaml.cs
@@ -3,7 +3,9 @@ using Avalonia;
using Avalonia.Controls;
using Avalonia.Input;
using Avalonia.Interactivity;
+using Avalonia.Layout;
using Avalonia.Threading;
+using Avalonia.VisualTree;
using Filee.App.Controls;
using Filee.App.Services;
using Filee.App.ViewModels;
@@ -45,41 +47,61 @@ public partial class HideWindow : Window
Closed += (_, _) => Motion.Changed -= OnMotionChanged;
}
- /// <summary>The toolbar's width with its buttons' names, measured when it last had to give them up.</summary>
- private double _namedWidth;
+ /// <summary>
+ /// How much of the toolbar's names it gives up, in turn, until it fits the window: none; <c>tight</c> drops the names
+ /// of Clear all and Skip and the keycaps (the tooltips and the line of keys below keep them); <c>compact</c> also
+ /// the names of Pixelate and Blur, with a shorter strength slider.
+ /// </summary>
+ internal static readonly string?[] ToolbarLevels = [null, "tight", "compact"];
+
+ /// <summary>The level in use (see <see cref="ToolbarLevels"/>).</summary>
+ internal string? ToolbarLevel { get; private set; }
+
+ /// <summary>Room for the toolbar: the stage less the pill's margin.</summary>
+ internal double ToolbarRoom => Stage.Bounds.Width - 32;
/// <summary>
- /// Shows the toolbar's buttons with their icons only (<c>compact</c>) when their names don't fit the window, and with
- /// the names again once there is room.
+ /// The width the toolbar asks for at <paramref name="level"/>, measured with the fonts in use (they differ from one
+ /// system to another, so no width is assumed).
/// </summary>
- private void FitToolbar()
+ internal double ToolbarWidthAt(string? level)
+ {
+ SetToolbarLevel(level);
+ // Measured afresh all the way down: a button keeps the size it measured before its name was hidden or shown.
+ foreach (var part in ToolbarContent.GetVisualDescendants().OfType<Layoutable>())
+ part.InvalidateMeasure();
+ ToolbarContent.InvalidateMeasure();
+ ToolbarContent.Measure(Size.Infinity);
+ return ToolbarContent.DesiredSize.Width + Toolbar.Padding.Left + Toolbar.Padding.Right + Toolbar.BorderThickness.Left
+ + Toolbar.BorderThickness.Right;
+ }
+
+ /// <summary>The first level, from all the names to icons only, at which the toolbar fits the window.</summary>
+ internal void FitToolbar()
{
- var available = Stage.Bounds.Width - 32; // the pill's margin
- if (available <= 0)
+ var room = ToolbarRoom;
+ if (room <= 0)
return;
- if (!Classes.Contains("compact"))
+ foreach (var level in ToolbarLevels)
{
- ToolbarContent.Measure(Size.Infinity);
- var wanted = ToolbarContent.DesiredSize.Width + Toolbar.Padding.Left + Toolbar.Padding.Right + 2;
- if (wanted > available)
+ if (level == ToolbarLevels[^1] || ToolbarWidthAt(level) <= room)
{
- _namedWidth = wanted;
- Classes.Set("compact", true);
+ SetToolbarLevel(level);
+ return;
}
}
- else if (available >= _namedWidth)
- {
- Classes.Set("compact", false);
- }
}
- /// <summary>The names changed (language, "Hide & Next" → "Hide & Save", Skip shown): measured again with them.</summary>
- private void RefitToolbar()
+ private void SetToolbarLevel(string? level)
{
- Classes.Set("compact", false);
- Dispatcher.UIThread.Post(FitToolbar, DispatcherPriority.Render);
+ ToolbarLevel = level;
+ foreach (var each in ToolbarLevels.OfType<string>())
+ Classes.Set(each, each == level || (each == "tight" && level == "compact"));
}
+ /// <summary>The names changed (language, "Hide & Next" → "Hide & Save", Skip shown): fitted again once they show.</summary>
+ private void RefitToolbar() => Dispatcher.UIThread.Post(FitToolbar, DispatcherPriority.Render);
+
internal HideCanvas HideCanvas => Canvas;
/// <summary>
diff --git a/tests/Filee.App.Tests/HideWindowTests.cs b/tests/Filee.App.Tests/HideWindowTests.cs
index f251e5f3..5cc3dcce 100644
--- a/tests/Filee.App.Tests/HideWindowTests.cs
+++ b/tests/Filee.App.Tests/HideWindowTests.cs
@@ -315,16 +315,45 @@ public class HideWindowTests
var bounds = new Rect(a, b).Normalize();
Assert.True(bounds.Left >= 12 && bounds.Right <= window.Bounds.Width - 12, $"{button.Name}: {bounds} outside {window.Bounds}");
}
- Assert.Contains("compact", window.Classes); // the names don't fit next to each other here
+ AssertFittedLevel(window);
+ Assert.NotNull(window.ToolbarLevel); // two files' names never fit 680 px, in any font
Save(window, $"hide-narrow-{suffix}.png");
- // Wide again: the names come back.
+ // The window as it opens: as many names as fit there with this system's fonts.
window.Width = 1040;
Settle(window);
+ AssertFittedLevel(window);
+
+ // Wide: every name is back.
+ window.Width = 1900;
+ Settle(window);
+ AssertFittedLevel(window);
+ Assert.Null(window.ToolbarLevel);
+ Assert.DoesNotContain("tight", window.Classes);
Assert.DoesNotContain("compact", window.Classes);
});
}
+ /// <summary>
+ /// The toolbar gave up no more names than it had to: every level before the one in use is wider than the room, and the
+ /// one in use fits (the last fits by giving up all it can). Measured, not assumed: fonts differ between systems.
+ /// </summary>
+ private static void AssertFittedLevel(HideWindow window)
+ {
+ var level = window.ToolbarLevel;
+ var room = window.ToolbarRoom;
+ var at = Array.IndexOf(HideWindow.ToolbarLevels, level);
+ var widths = HideWindow.ToolbarLevels.Select(window.ToolbarWidthAt).ToList();
+ window.FitToolbar();
+ Assert.Equal(level, window.ToolbarLevel);
+ var detail = $"room {room:0}, widths {string.Join(" / ", widths.Select(w => w.ToString("0")))}, level {level ?? "none"}";
+ for (var i = 0; i < at; i++)
+ Assert.True(widths[i] > room, detail);
+ Assert.True(at == HideWindow.ToolbarLevels.Length - 1 || widths[at] <= room, detail);
+ Assert.Equal(level is "tight" or "compact", window.Classes.Contains("tight"));
+ Assert.Equal(level == "compact", window.Classes.Contains("compact"));
+ }
+
// ───────────────────────── Helpers ─────────────────────────
private static void AssertClose(Rect expected, Rect actual)