Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Align zone dimensions from layout preview with those from grid editor #1115

Merged
merged 4 commits into from
Jan 22, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -23,39 +23,39 @@ public partial class EditorOverlay : Window
public Int32Rect[] GetZoneRects()
{
// TODO: the ideal here is that the ArrangeRects logic is entirely inside the model, so we don't have to walk the UIElement children to get the rect info
Panel previewPanel;
if (_editor != null)
{
if (_editor is GridEditor gridEditor)
{
previewPanel = gridEditor.PreviewPanel;
return ZoneRectsFromPanel(gridEditor.PreviewPanel);
}
else
{
// CanvasEditor
previewPanel = ((CanvasEditor)_editor).Preview;
return ZoneRectsFromPanel(((CanvasEditor)_editor).Preview);
}
}
else
{
previewPanel = _layoutPreview.PreviewPanel;
// One of the predefined zones (neither grid or canvas editor used).
return _layoutPreview.GetZoneRects();
}
}

var count = previewPanel.Children.Count;
private Int32Rect[] ZoneRectsFromPanel(Panel previewPanel)
{
int count = previewPanel.Children.Count;
Int32Rect[] zones = new Int32Rect[count];

int i = 0;
foreach (FrameworkElement child in previewPanel.Children)
for (int i = 0; i < count; i++)
{
FrameworkElement child = (FrameworkElement)previewPanel.Children[i];
Point topLeft = child.TransformToAncestor(previewPanel).Transform(default);

var right = topLeft.X + child.ActualWidth;
var bottom = topLeft.Y + child.ActualHeight;
zones[i].X = (int)topLeft.X;
zones[i].Y = (int)topLeft.Y;
zones[i].Width = (int)child.ActualWidth;
zones[i].Height = (int)child.ActualHeight;
i++;
}

return zones;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -558,23 +558,22 @@ private void ArrangeGridRects(Size arrangeSize)

Settings settings = ((App)Application.Current).ZoneSettings;

int spacing, gutter;
spacing = gutter = settings.ShowSpacing ? settings.Spacing : 0;
int spacing = settings.ShowSpacing ? settings.Spacing : 0;

int cols = model.Columns;
int rows = model.Rows;

double totalWidth = arrangeSize.Width - (gutter * 2) - (spacing * (cols - 1));
double totalHeight = arrangeSize.Height - (gutter * 2) - (spacing * (rows - 1));
double totalWidth = arrangeSize.Width - (spacing * (cols + 1));
double totalHeight = arrangeSize.Height - (spacing * (rows + 1));

double top = gutter;
double top = spacing;
for (int row = 0; row < rows; row++)
{
double cellHeight = _rowInfo[row].Recalculate(top, totalHeight);
top += cellHeight + spacing;
}

double left = gutter;
double left = spacing;
for (int col = 0; col < cols; col++)
{
double cellWidth = _colInfo[col].Recalculate(left, totalWidth);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
mc:Ignorable="d"
Background="LightGray"
BorderThickness="1"
BorderBrush="SlateGray"
BorderBrush="DarkGray"
Opacity="0.5"
d:DesignHeight="450" d:DesignWidth="800">
<Grid x:Name="Frame" Visibility="Collapsed">
Expand Down
140 changes: 114 additions & 26 deletions src/modules/fancyzones/editor/FancyZonesEditor/LayoutPreview.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.

using System.Collections.Generic;
using System.Linq;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
Expand All @@ -19,6 +20,7 @@ public partial class LayoutPreview : UserControl
public static readonly DependencyProperty IsActualSizeProperty = DependencyProperty.Register("IsActualSize", typeof(bool), typeof(LayoutPreview), new PropertyMetadata(false));

private LayoutModel _model;
private List<Int32Rect> _zones = new List<Int32Rect>();

public LayoutPreview()
{
Expand Down Expand Up @@ -47,26 +49,16 @@ private void ZoneSettings_PropertyChanged(object sender, System.ComponentModel.P
}
else if ((e.PropertyName == "ShowSpacing") || (e.PropertyName == "Spacing"))
{
if (IsActualSize)
{
Settings settings = ((App)Application.Current).ZoneSettings;
Body.Margin = new Thickness(settings.ShowSpacing ? settings.Spacing / 2 : 0);
}
else
{
Body.Margin = new Thickness(0);
}

if (_model is GridLayoutModel)
{
RenderPreview();
}
}
}

public Panel PreviewPanel
public Int32Rect[] GetZoneRects()
{
get { return Body; }
return _zones.ToArray();
}

private void OnLoaded(object sender, RoutedEventArgs e)
Expand All @@ -84,23 +76,110 @@ private void RenderPreview()
}

Body.Children.Clear();
Body.RowDefinitions.Clear();
Body.ColumnDefinitions.Clear();

_zones.Clear();

if (_model is GridLayoutModel gridModel)
{
RenderGridPreview(gridModel);
}
else
else if (_model is CanvasLayoutModel canvasModel)
{
RenderCanvasPreview(canvasModel);
}
}

private void RenderActualScalePriview(GridLayoutModel grid)
{
int rows = grid.Rows;
int cols = grid.Columns;

RowColInfo[] rowInfo = (from percent in grid.RowPercents
select new RowColInfo(percent)).ToArray();

RowColInfo[] colInfo = (from percent in grid.ColumnPercents
select new RowColInfo(percent)).ToArray();

Settings settings = ((App)Application.Current).ZoneSettings;

int spacing = settings.ShowSpacing ? settings.Spacing : 0;

int width = (int)SystemParameters.WorkArea.Width;
int height = (int)SystemParameters.WorkArea.Height;

double totalWidth = width - (spacing * (cols + 1));
double totalHeight = height - (spacing * (rows + 1));

double top = spacing;
for (int row = 0; row < rows; row++)
{
double cellHeight = rowInfo[row].Recalculate(top, totalHeight);
top += cellHeight + spacing;
}

double left = spacing;
for (int col = 0; col < cols; col++)
{
double cellWidth = colInfo[col].Recalculate(left, totalWidth);
left += cellWidth + spacing;
}

Viewbox viewbox = new Viewbox
{
Stretch = Stretch.Uniform,
};
Body.Children.Add(viewbox);
Canvas frame = new Canvas
{
Width = width,
Height = height,
};
viewbox.Child = frame;

for (int row = 0; row < rows; row++)
{
if (_model is CanvasLayoutModel canvasModel)
for (int col = 0; col < cols; col++)
{
RenderCanvasPreview(canvasModel);
int childIndex = grid.CellChildMap[row, col];
if (((row == 0) || (grid.CellChildMap[row - 1, col] != childIndex)) &&
((col == 0) || (grid.CellChildMap[row, col - 1] != childIndex)))
{
// this is not a continuation of a span
Rectangle rect = new Rectangle();
left = colInfo[col].Start;
top = rowInfo[row].Start;
Canvas.SetTop(rect, top);
Canvas.SetLeft(rect, left);

int maxRow = row;
while (((maxRow + 1) < rows) && (grid.CellChildMap[maxRow + 1, col] == childIndex))
{
maxRow++;
}

int maxCol = col;
while (((maxCol + 1) < cols) && (grid.CellChildMap[row, maxCol + 1] == childIndex))
{
maxCol++;
}

rect.Width = colInfo[maxCol].End - left;
rect.Height = rowInfo[maxRow].End - top;
rect.StrokeThickness = 1;
rect.Stroke = Brushes.DarkGray;
rect.Fill = Brushes.LightGray;
frame.Children.Add(rect);
_zones.Add(new Int32Rect(
(int)left, (int)top, (int)rect.Width, (int)rect.Height));
}
}
}
}

private void RenderGridPreview(GridLayoutModel grid)
private void RenderSmallScalePriview(GridLayoutModel grid)
{
Body.RowDefinitions.Clear();
foreach (int percent in grid.RowPercents)
{
RowDefinition def = new RowDefinition
Expand All @@ -110,7 +189,6 @@ private void RenderGridPreview(GridLayoutModel grid)
Body.RowDefinitions.Add(def);
}

Body.ColumnDefinitions.Clear();
foreach (int percent in grid.ColumnPercents)
{
ColumnDefinition def = new ColumnDefinition
Expand All @@ -121,8 +199,7 @@ private void RenderGridPreview(GridLayoutModel grid)
}

Settings settings = ((App)Application.Current).ZoneSettings;
int divisor = IsActualSize ? 2 : 20;
Thickness margin = new Thickness(settings.ShowSpacing ? settings.Spacing / divisor : 0);
Thickness margin = new Thickness(settings.ShowSpacing ? settings.Spacing / 20 : 0);

List<int> visited = new List<int>();

Expand Down Expand Up @@ -167,20 +244,31 @@ private void RenderGridPreview(GridLayoutModel grid)
}
}

private void RenderCanvasPreview(CanvasLayoutModel canvas)
private void RenderGridPreview(GridLayoutModel grid)
{
Body.RowDefinitions.Clear();
Body.ColumnDefinitions.Clear();
if (IsActualSize)
{
RenderActualScalePriview(grid);
}
else
{
RenderSmallScalePriview(grid);
}
}

private void RenderCanvasPreview(CanvasLayoutModel canvas)
{
Viewbox viewbox = new Viewbox
{
Stretch = Stretch.Uniform,
};
Body.Children.Add(viewbox);
Canvas frame = new Canvas();
Canvas frame = new Canvas
{
Width = canvas.ReferenceWidth,
Height = canvas.ReferenceHeight,
};
viewbox.Child = frame;
frame.Width = canvas.ReferenceWidth;
frame.Height = canvas.ReferenceHeight;
foreach (Int32Rect zone in canvas.Zones)
{
Rectangle rect = new Rectangle();
Expand Down