Skip to content

Commit

Permalink
Align zone dimensions from layout preview with those from grid editor (
Browse files Browse the repository at this point in the history
  • Loading branch information
vldmr11080 authored and udit3333 committed Feb 20, 2020
1 parent 848f581 commit dec8e5a
Show file tree
Hide file tree
Showing 4 changed files with 131 additions and 44 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,40 +22,40 @@ 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)
{
// 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
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 RenderActualScalePreview(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)settings.WorkArea.Width;
int height = (int)settings.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 RenderSmallScalePreview(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)
{
RenderActualScalePreview(grid);
}
else
{
RenderSmallScalePreview(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

0 comments on commit dec8e5a

Please sign in to comment.