Skip to content

Portable Tool for an Ideal Windows Setup

upd flowpanel responsive logic

Greedeks923afa90e52c
diff --git a/.Source/GTweak/Assets/UserControls/FlowPanel.cs b/.Source/GTweak/Assets/UserControls/FlowPanel.cs
index 267ed834..787e8e21 100644
--- a/.Source/GTweak/Assets/UserControls/FlowPanel.cs
+++ b/.Source/GTweak/Assets/UserControls/FlowPanel.cs
@@ -154,78 +154,104 @@ namespace GTweak.Assets.UserControls
}
}
+ private double GetResponsiveAlignmentOffset(double outerWidth, double contentWidth)
+ {
+ double extra = Math.Max(0, outerWidth - contentWidth);
+
+ return ContentAlignment switch
+ {
+ HorizontalAlignment.Center => extra / 2.0,
+ HorizontalAlignment.Right => extra,
+ _ => 0
+ };
+ }
+
protected override Size MeasureOverride(Size availableSize)
{
try
{
UpdateVisibleCache();
-
int count = _visibleCache.Count;
if (count == 0)
{
return new Size(0, 0);
}
- Size constraint = EqualizeItemSize ? new Size(ItemWidth, double.PositiveInfinity) : new Size(availableSize.Width, double.PositiveInfinity);
+ double aw = availableSize.Width;
+ double ah = availableSize.Height;
+
+ Size constraint = EqualizeItemSize ? new Size(ItemWidth, double.PositiveInfinity) : new Size(aw, double.PositiveInfinity);
double totalItemWidth = 0;
- double totalItemHeight = 0;
double maxItemHeight = 0;
+ double totalVerticalHeight = 0;
double maxItemWidth = 0;
for (int i = 0; i < count; i++)
{
- UIElement child = _visibleCache[i];
+ var child = _visibleCache[i];
child?.Measure(constraint);
double cw = GetChildWidth(child);
double ch = GetChildHeight(child);
totalItemWidth += cw;
- totalItemHeight += ch;
+ totalVerticalHeight += ch;
+ if (ch > maxItemHeight)
+ {
+ maxItemHeight = ch;
+ }
if (cw > maxItemWidth)
{
maxItemWidth = cw;
}
+ }
- if (ch > maxItemHeight)
+ if (ResponsiveMode)
+ {
+ if (_cachedLayout != null) { ReleaseGroups(_cachedLayout); _cachedLayout = null; }
+
+ double smallOffset = 30.0;
+ double horizontalRequired = totalItemWidth;
+ if (count > 1)
{
- maxItemHeight = ch;
+ horizontalRequired += smallOffset;
+ if (count > 2)
+ {
+ horizontalRequired += (count - 2) * HorizontalSpacing;
+ }
}
- }
-
- double horizontalRequired = totalItemWidth + (count > 1 ? (count - 1) * HorizontalSpacing : 0);
- double verticalRequired = totalItemHeight + (count > 1 ? (count - 1) * VerticalSpacing : 0);
- double aw = double.IsInfinity(availableSize.Width) ? horizontalRequired : availableSize.Width;
- double ah = availableSize.Height;
+ double verticalRequired = totalVerticalHeight + (count > 1 ? (count - 1) * VerticalSpacing : 0);
- if (ResponsiveMode)
- {
- if (horizontalRequired <= aw)
+ Size resultSize;
+ if (horizontalRequired <= aw || double.IsInfinity(aw))
{
- double returnWidth = double.IsInfinity(availableSize.Width) ? horizontalRequired : aw;
- return new Size(returnWidth, maxItemHeight);
+ resultSize = new Size(double.IsInfinity(aw) ? horizontalRequired : aw, maxItemHeight);
+ }
+ else
+ {
+ resultSize = new Size(double.IsInfinity(aw) ? maxItemWidth : aw, verticalRequired);
}
- return new Size(maxItemWidth, verticalRequired);
+ _cachedMeasureWidth = resultSize.Width;
+ return resultSize;
}
if (_cachedLayout != null)
{
- ReleaseGroups(_cachedLayout);
- _cachedLayout = null;
+ ReleaseGroups(_cachedLayout); _cachedLayout = null;
}
_cachedLayout = CreateOptimalColumns(aw, ah, _visibleCache);
double neededHeight = CalculateLayoutHeight(_cachedLayout);
- double neededWidth = double.IsInfinity(availableSize.Width) ? CalculateTotalWidth(_cachedLayout) : aw;
+ double finalWidth = double.IsInfinity(aw) ? CalculateTotalWidth(_cachedLayout) : aw;
- _cachedMeasureWidth = neededWidth;
+ _cachedMeasureWidth = finalWidth;
- return new Size(neededWidth, neededHeight);
+ return new Size(finalWidth, neededHeight);
}
catch (Exception ex)
{
@@ -234,77 +260,65 @@ namespace GTweak.Assets.UserControls
}
}
- private double GetResponsiveAlignmentOffset(double outerWidth, double contentWidth)
- {
- double extra = Math.Max(0, outerWidth - contentWidth);
-
- return ContentAlignment switch
- {
- HorizontalAlignment.Center => extra / 2.0,
- HorizontalAlignment.Right => extra,
- _ => 0
- };
- }
-
private void ArrangeResponsive(Size finalSize)
{
int count = _visibleCache.Count;
- if (count != 0)
+ if (count == 0)
{
- double totalItemWidth = 0;
- double maxItemWidth = 0;
+ return;
+ }
- for (int i = 0; i < count; i++)
+ double smallOffset = 30.0;
+ double totalWidth = 0;
+ for (int i = 0; i < count; i++)
+ {
+ totalWidth += GetChildWidth(_visibleCache[i]);
+ }
+
+ double horizontalRequired = totalWidth;
+ if (count > 1)
+ {
+ horizontalRequired += smallOffset;
+ if (count > 2)
{
- double w = GetChildWidth(_visibleCache[i]);
- totalItemWidth += w;
- if (w > maxItemWidth)
- {
- maxItemWidth = w;
- }
+ horizontalRequired += (count - 2) * HorizontalSpacing;
}
+ }
- double rowSpacing = count > 1 ? HorizontalSpacing * (count - 1) : 0;
- double rowWidth = totalItemWidth + rowSpacing;
-
- if (rowWidth <= finalSize.Width)
+ if (horizontalRequired <= finalSize.Width || double.IsInfinity(finalSize.Width))
+ {
+ double x = 0;
+ for (int i = 0; i < count; i++)
{
- double x = GetResponsiveAlignmentOffset(finalSize.Width, rowWidth);
+ UIElement child = _visibleCache[i];
+ double w = GetChildWidth(child);
+ double h = GetChildHeight(child);
+ double y = Math.Max(0, (finalSize.Height - h) / 2.0);
- for (int i = 0; i < count; i++)
+ if (i == count - 1 && count > 1)
{
- UIElement child = _visibleCache[i];
- double w = GetChildWidth(child);
- double h = GetChildHeight(child);
-
- if (i == count - 1)
- {
- child.Arrange(new Rect(finalSize.Width - w, 0, w, h));
- }
- else
- {
- child.Arrange(new Rect(x, 0, w, h));
- x += w + HorizontalSpacing;
- }
+ child.Arrange(new Rect(finalSize.Width - w, y, w, h));
}
- }
- else
- {
- double y = 0;
-
- for (int i = 0; i < count; i++)
+ else
{
- UIElement child = _visibleCache[i];
- double w = GetChildWidth(child);
- double h = GetChildHeight(child);
-
- double x = GetResponsiveAlignmentOffset(finalSize.Width, w);
child.Arrange(new Rect(x, y, w, h));
-
- y += h + VerticalSpacing;
+ x += w + (i == 0 ? smallOffset : HorizontalSpacing);
}
}
}
+ else
+ {
+ double y = 0;
+ for (int i = 0; i < count; i++)
+ {
+ UIElement child = _visibleCache[i];
+ double w = GetChildWidth(child);
+ double h = GetChildHeight(child);
+ double xOffset = GetResponsiveAlignmentOffset(finalSize.Width, w);
+ child.Arrange(new Rect(xOffset, y, w, h));
+ y += h + VerticalSpacing;
+ }
+ }
}
protected override Size ArrangeOverride(Size finalSize)
diff --git a/.Source/GTweak/View/ToolsetView.xaml b/.Source/GTweak/View/ToolsetView.xaml
index 22a66a09..b68d550c 100644
--- a/.Source/GTweak/View/ToolsetView.xaml
+++ b/.Source/GTweak/View/ToolsetView.xaml
@@ -47,7 +47,7 @@
<ItemsControl ItemsSource="{Binding Tools}" VirtualizingPanel.IsVirtualizing="True">
<ItemsControl.ItemsPanel>
<ItemsPanelTemplate>
- <uc:FlowPanel Margin="20,10,5,20" UseMasonryLayout="True" ItemWidth="100" HorizontalSpacing="5" VerticalSpacing="5" ContentAlignment="Stretch" HorizontalAlignment="Left" VerticalAlignment="Top"/>
+ <uc:FlowPanel Margin="10,10,10,20" HorizontalSpacing="5" VerticalSpacing="10" ContentAlignment="Stretch" HorizontalAlignment="Stretch" VerticalAlignment="Top"/>
</ItemsPanelTemplate>
</ItemsControl.ItemsPanel>