diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml index 2cc908d9ad..2488816fed 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml +++ b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml @@ -46,6 +46,8 @@ Content="" FontSize="24" ToolTip="{x:Static props:Resources.Add_zone}" + DataContext="{Binding Path=Model, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}" + IsEnabled="{Binding IsZoneAddingAllowed}" Click="OnAddZone" /> diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs index a6959a8ecb..3ab05331e2 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/CanvasEditorWindow.xaml.cs @@ -10,18 +10,6 @@ namespace FancyZonesEditor { public partial class CanvasEditorWindow : EditorWindow { - // Default distance from the top and left borders to the zone. - private const int DefaultOffset = 100; - - // Next created zone will be by OffsetShift value below and to the right of the previous. - private const int OffsetShift = 50; - - // Zone size depends on the work area size multiplied by ZoneSizeMultiplier value. - private const double ZoneSizeMultiplier = 0.4; - - // Distance from the top and left borders to the zone. - private int _offset = DefaultOffset; - private CanvasLayoutModel _model; private CanvasLayoutModel _stashedModel; @@ -35,24 +23,17 @@ namespace FancyZonesEditor _stashedModel = (CanvasLayoutModel)_model.Clone(); } + public LayoutModel Model + { + get + { + return _model; + } + } + private void OnAddZone(object sender, RoutedEventArgs e) { - Rect workingArea = App.Overlay.WorkArea; - int offset = (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(_offset); - - if (offset + (int)(workingArea.Width * ZoneSizeMultiplier) < (int)workingArea.Width - && offset + (int)(workingArea.Height * ZoneSizeMultiplier) < (int)workingArea.Height) - { - _model.AddZone(new Int32Rect(offset, offset, (int)(workingArea.Width * ZoneSizeMultiplier), (int)(workingArea.Height * ZoneSizeMultiplier))); - } - else - { - _offset = DefaultOffset; - offset = (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(_offset); - _model.AddZone(new Int32Rect(offset, offset, (int)(workingArea.Width * ZoneSizeMultiplier), (int)(workingArea.Height * ZoneSizeMultiplier))); - } - - _offset += OffsetShift; + _model.AddZone(); } protected new void OnCancel(object sender, RoutedEventArgs e) diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs b/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs index 2326e21d0c..b4acdf6414 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs @@ -91,7 +91,7 @@ namespace FancyZonesEditor return; } - if (model.TemplateZoneCount < LayoutSettings.MaxZones) + if (model.IsZoneAddingAllowed) { model.TemplateZoneCount++; } diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs index 49cc928ac0..761ad48068 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/CanvasLayoutModel.cs @@ -14,12 +14,25 @@ namespace FancyZonesEditor.Models // Non-localizable strings public const string ModelTypeID = "canvas"; + // Default distance from the top and left borders to the zone. + private const int DefaultOffset = 100; + + // Next created zone will be by OffsetShift value below and to the right of the previous. + private const int OffsetShift = 50; + + // Zone size depends on the work area size multiplied by ZoneSizeMultiplier value. + private const double ZoneSizeMultiplier = 0.4; + + // Distance from the top and left borders to the zone. + private int _topLeft = DefaultOffset; + public Rect CanvasRect { get; private set; } public CanvasLayoutModel(string uuid, string name, LayoutType type, IList zones, int width, int height) : base(uuid, name, type) { Zones = zones; + TemplateZoneCount = Zones.Count; CanvasRect = new Rect(new Size(width, height)); } @@ -52,6 +65,7 @@ namespace FancyZonesEditor.Models public void RemoveZoneAt(int index) { Zones.RemoveAt(index); + TemplateZoneCount = Zones.Count; UpdateLayout(); } @@ -60,29 +74,56 @@ namespace FancyZonesEditor.Models public void AddZone(Int32Rect zone) { Zones.Add(zone); + TemplateZoneCount = Zones.Count; UpdateLayout(); } + public void AddZone() + { + AddNewZone(); + TemplateZoneCount = Zones.Count; + UpdateLayout(); + } + + private void AddNewZone() + { + if (Zones.Count == 0) + { + _topLeft = DefaultOffset; + } + + Rect workingArea = App.Overlay.WorkArea; + int topLeft = (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(_topLeft); + int width = (int)(workingArea.Width * ZoneSizeMultiplier); + int height = (int)(workingArea.Height * ZoneSizeMultiplier); + + if (topLeft + width >= (int)workingArea.Width || topLeft + height >= (int)workingArea.Height) + { + _topLeft = DefaultOffset; + topLeft = (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(_topLeft); + } + + Zones.Add(new Int32Rect(topLeft, topLeft, width, height)); + _topLeft += OffsetShift; + } + // InitTemplateZones // Creates zones based on template zones count public override void InitTemplateZones() { + if (Type == LayoutType.Custom || Type == LayoutType.Blank) + { + return; + } + Zones.Clear(); - - var workingArea = App.Overlay.WorkArea; - int topLeftCoordinate = (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(100); // TODO: replace magic numbers - int width = (int)(workingArea.Width * 0.4); - int height = (int)(workingArea.Height * 0.4); - Int32Rect focusZoneRect = new Int32Rect(topLeftCoordinate, topLeftCoordinate, width, height); - int focusRectXIncrement = (TemplateZoneCount <= 1) ? 0 : (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(50); // TODO: replace magic numbers - int focusRectYIncrement = (TemplateZoneCount <= 1) ? 0 : (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(50); // TODO: replace magic numbers - for (int i = 0; i < TemplateZoneCount; i++) { - Zones.Add(focusZoneRect); - focusZoneRect.X += focusRectXIncrement; - focusZoneRect.Y += focusRectYIncrement; + AddNewZone(); } + + TemplateZoneCount = Zones.Count; + UpdateLayout(); } private void UpdateLayout() diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs index ea66005b22..11981ee22c 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs @@ -173,12 +173,21 @@ namespace FancyZonesEditor.Models _zoneCount = value; InitTemplateZones(); FirePropertyChanged(nameof(TemplateZoneCount)); + FirePropertyChanged(nameof(IsZoneAddingAllowed)); } } } private int _zoneCount = LayoutSettings.DefaultZoneCount; + public bool IsZoneAddingAllowed + { + get + { + return TemplateZoneCount < LayoutSettings.MaxZones; + } + } + // implementation of INotifyPropertyChanged public event PropertyChangedEventHandler PropertyChanged;