[FZ Editor] Limit zones number for custom layouts. (#9364)

* disable add zone button

* unify zone adding
This commit is contained in:
Seraphima Zykova
2021-01-29 15:10:37 +03:00
committed by GitHub
parent b38da96933
commit 53c239f052
5 changed files with 74 additions and 41 deletions

View File

@@ -46,6 +46,8 @@
Content="" Content=""
FontSize="24" FontSize="24"
ToolTip="{x:Static props:Resources.Add_zone}" ToolTip="{x:Static props:Resources.Add_zone}"
DataContext="{Binding Path=Model, RelativeSource={RelativeSource Mode=FindAncestor, AncestorType=Window}}"
IsEnabled="{Binding IsZoneAddingAllowed}"
Click="OnAddZone" /> Click="OnAddZone" />
<Grid Margin="0,24,0,0"> <Grid Margin="0,24,0,0">

View File

@@ -10,18 +10,6 @@ namespace FancyZonesEditor
{ {
public partial class CanvasEditorWindow : EditorWindow 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 _model;
private CanvasLayoutModel _stashedModel; private CanvasLayoutModel _stashedModel;
@@ -35,24 +23,17 @@ namespace FancyZonesEditor
_stashedModel = (CanvasLayoutModel)_model.Clone(); _stashedModel = (CanvasLayoutModel)_model.Clone();
} }
public LayoutModel Model
{
get
{
return _model;
}
}
private void OnAddZone(object sender, RoutedEventArgs e) private void OnAddZone(object sender, RoutedEventArgs e)
{ {
Rect workingArea = App.Overlay.WorkArea; _model.AddZone();
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;
} }
protected new void OnCancel(object sender, RoutedEventArgs e) protected new void OnCancel(object sender, RoutedEventArgs e)

View File

@@ -91,7 +91,7 @@ namespace FancyZonesEditor
return; return;
} }
if (model.TemplateZoneCount < LayoutSettings.MaxZones) if (model.IsZoneAddingAllowed)
{ {
model.TemplateZoneCount++; model.TemplateZoneCount++;
} }

View File

@@ -14,12 +14,25 @@ namespace FancyZonesEditor.Models
// Non-localizable strings // Non-localizable strings
public const string ModelTypeID = "canvas"; 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 Rect CanvasRect { get; private set; }
public CanvasLayoutModel(string uuid, string name, LayoutType type, IList<Int32Rect> zones, int width, int height) public CanvasLayoutModel(string uuid, string name, LayoutType type, IList<Int32Rect> zones, int width, int height)
: base(uuid, name, type) : base(uuid, name, type)
{ {
Zones = zones; Zones = zones;
TemplateZoneCount = Zones.Count;
CanvasRect = new Rect(new Size(width, height)); CanvasRect = new Rect(new Size(width, height));
} }
@@ -52,6 +65,7 @@ namespace FancyZonesEditor.Models
public void RemoveZoneAt(int index) public void RemoveZoneAt(int index)
{ {
Zones.RemoveAt(index); Zones.RemoveAt(index);
TemplateZoneCount = Zones.Count;
UpdateLayout(); UpdateLayout();
} }
@@ -60,29 +74,56 @@ namespace FancyZonesEditor.Models
public void AddZone(Int32Rect zone) public void AddZone(Int32Rect zone)
{ {
Zones.Add(zone); Zones.Add(zone);
TemplateZoneCount = Zones.Count;
UpdateLayout(); 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 // InitTemplateZones
// Creates zones based on template zones count // Creates zones based on template zones count
public override void InitTemplateZones() public override void InitTemplateZones()
{ {
if (Type == LayoutType.Custom || Type == LayoutType.Blank)
{
return;
}
Zones.Clear(); 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++) for (int i = 0; i < TemplateZoneCount; i++)
{ {
Zones.Add(focusZoneRect); AddNewZone();
focusZoneRect.X += focusRectXIncrement;
focusZoneRect.Y += focusRectYIncrement;
} }
TemplateZoneCount = Zones.Count;
UpdateLayout();
} }
private void UpdateLayout() private void UpdateLayout()

View File

@@ -173,12 +173,21 @@ namespace FancyZonesEditor.Models
_zoneCount = value; _zoneCount = value;
InitTemplateZones(); InitTemplateZones();
FirePropertyChanged(nameof(TemplateZoneCount)); FirePropertyChanged(nameof(TemplateZoneCount));
FirePropertyChanged(nameof(IsZoneAddingAllowed));
} }
} }
} }
private int _zoneCount = LayoutSettings.DefaultZoneCount; private int _zoneCount = LayoutSettings.DefaultZoneCount;
public bool IsZoneAddingAllowed
{
get
{
return TemplateZoneCount < LayoutSettings.MaxZones;
}
}
// implementation of INotifyPropertyChanged // implementation of INotifyPropertyChanged
public event PropertyChangedEventHandler PropertyChanged; public event PropertyChangedEventHandler PropertyChanged;