2020-08-17 10:00:56 -07:00
|
|
|
// Copyright (c) Microsoft Corporation
|
|
|
|
// The Microsoft Corporation licenses this file to you under the MIT license.
|
|
|
|
// See the LICENSE file in the project root for more information.
|
|
|
|
|
|
|
|
using System;
|
2021-03-25 15:44:55 +03:00
|
|
|
using System.Collections.Generic;
|
2020-08-17 10:00:56 -07:00
|
|
|
using System.ComponentModel;
|
2022-02-22 20:52:34 +01:00
|
|
|
using System.Globalization;
|
2020-08-17 10:00:56 -07:00
|
|
|
using System.Runtime.CompilerServices;
|
2024-03-22 13:10:10 +01:00
|
|
|
using FancyZonesEditorCommon.Data;
|
2020-08-17 10:00:56 -07:00
|
|
|
|
|
|
|
namespace FancyZonesEditor.Models
|
|
|
|
{
|
|
|
|
// Base LayoutModel
|
|
|
|
// Manages common properties and base persistence
|
|
|
|
public abstract class LayoutModel : INotifyPropertyChanged
|
|
|
|
{
|
|
|
|
protected LayoutModel()
|
|
|
|
{
|
|
|
|
_guid = Guid.NewGuid();
|
|
|
|
Type = LayoutType.Custom;
|
2022-10-25 17:55:36 +02:00
|
|
|
|
|
|
|
MainWindowSettingsModel.DefaultLayouts.PropertyChanged += DefaultLayouts_PropertyChanged;
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
protected LayoutModel(string name)
|
|
|
|
: this()
|
|
|
|
{
|
|
|
|
Name = name;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected LayoutModel(string uuid, string name, LayoutType type)
|
|
|
|
: this()
|
|
|
|
{
|
|
|
|
_guid = Guid.Parse(uuid);
|
|
|
|
Name = name;
|
|
|
|
Type = type;
|
|
|
|
}
|
|
|
|
|
|
|
|
protected LayoutModel(string name, LayoutType type)
|
|
|
|
: this(name)
|
|
|
|
{
|
|
|
|
_guid = Guid.NewGuid();
|
|
|
|
Type = type;
|
|
|
|
}
|
|
|
|
|
2021-08-25 14:26:55 +03:00
|
|
|
protected LayoutModel(LayoutModel other)
|
2021-01-27 21:33:52 +03:00
|
|
|
{
|
|
|
|
_guid = other._guid;
|
|
|
|
_name = other._name;
|
|
|
|
Type = other.Type;
|
|
|
|
_isSelected = other._isSelected;
|
|
|
|
_isApplied = other._isApplied;
|
|
|
|
_sensitivityRadius = other._sensitivityRadius;
|
|
|
|
_zoneCount = other._zoneCount;
|
2021-03-25 15:44:55 +03:00
|
|
|
_quickKey = other._quickKey;
|
2021-01-27 21:33:52 +03:00
|
|
|
}
|
|
|
|
|
2020-08-17 10:00:56 -07:00
|
|
|
// Name - the display name for this layout model - is also used as the key in the registry
|
|
|
|
public string Name
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _name;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_name != value)
|
|
|
|
{
|
|
|
|
_name = value;
|
2021-01-27 21:33:52 +03:00
|
|
|
FirePropertyChanged(nameof(Name));
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private string _name;
|
|
|
|
|
|
|
|
public LayoutType Type { get; set; }
|
|
|
|
|
2022-02-22 20:52:34 +01:00
|
|
|
#pragma warning disable CA1720 // Identifier contains type name (Not worth the effort to change this now.)
|
2020-08-17 10:00:56 -07:00
|
|
|
public Guid Guid
|
2022-02-22 20:52:34 +01:00
|
|
|
#pragma warning restore CA1720 // Identifier contains type name
|
2020-08-17 10:00:56 -07:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _guid;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private Guid _guid;
|
|
|
|
|
2020-11-17 11:38:19 +03:00
|
|
|
public string Uuid
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2020-11-18 12:04:24 -08:00
|
|
|
return "{" + Guid.ToString().ToUpperInvariant() + "}";
|
2020-11-17 11:38:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:33:52 +03:00
|
|
|
public bool IsCustom
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return Type == LayoutType.Custom;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 10:00:56 -07:00
|
|
|
// IsSelected (not-persisted) - tracks whether or not this LayoutModel is selected in the picker
|
|
|
|
// TODO: once we switch to a picker per monitor, we need to move this state to the view
|
|
|
|
public bool IsSelected
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isSelected;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_isSelected != value)
|
|
|
|
{
|
|
|
|
_isSelected = value;
|
2021-01-27 21:33:52 +03:00
|
|
|
FirePropertyChanged(nameof(IsSelected));
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool _isSelected;
|
|
|
|
|
2021-01-27 21:33:52 +03:00
|
|
|
// IsApplied (not-persisted) - tracks whether or not this LayoutModel is applied in the picker
|
2020-11-17 11:38:19 +03:00
|
|
|
public bool IsApplied
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _isApplied;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (_isApplied != value)
|
|
|
|
{
|
|
|
|
_isApplied = value;
|
2021-01-27 21:33:52 +03:00
|
|
|
FirePropertyChanged(nameof(IsApplied));
|
2020-11-17 11:38:19 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private bool _isApplied;
|
|
|
|
|
2022-10-25 17:55:36 +02:00
|
|
|
public bool IsHorizontalDefault
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2023-08-15 13:25:54 +03:00
|
|
|
return MainWindowSettingsModel.DefaultLayouts.Layouts[(int)MonitorConfigurationType.Horizontal].Uuid == this.Uuid;
|
2022-10-25 17:55:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanBeSetAsHorizontalDefault
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2023-08-15 13:25:54 +03:00
|
|
|
return MainWindowSettingsModel.DefaultLayouts.Layouts[(int)MonitorConfigurationType.Horizontal].Uuid != this.Uuid;
|
2022-10-25 17:55:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool IsVerticalDefault
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2023-10-18 04:14:16 -06:00
|
|
|
return MainWindowSettingsModel.DefaultLayouts.Layouts[MonitorConfigurationType.Vertical].Uuid == this.Uuid;
|
2022-10-25 17:55:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool CanBeSetAsVerticalDefault
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2023-10-18 04:14:16 -06:00
|
|
|
return MainWindowSettingsModel.DefaultLayouts.Layouts[MonitorConfigurationType.Vertical].Uuid != this.Uuid;
|
2022-10-25 17:55:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-27 21:33:52 +03:00
|
|
|
public int SensitivityRadius
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _sensitivityRadius;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value != _sensitivityRadius)
|
|
|
|
{
|
|
|
|
_sensitivityRadius = value;
|
|
|
|
FirePropertyChanged(nameof(SensitivityRadius));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 13:10:10 +01:00
|
|
|
private int _sensitivityRadius = LayoutDefaultSettings.DefaultSensitivityRadius;
|
2021-01-27 21:33:52 +03:00
|
|
|
|
2022-08-23 22:41:22 +03:00
|
|
|
public int SensitivityRadiusMinimum
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int SensitivityRadiusMaximum
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 1000;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:44:55 +03:00
|
|
|
public List<string> QuickKeysAvailable
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
List<string> result = new List<string>();
|
2022-01-17 11:50:24 +03:00
|
|
|
foreach (var pair in MainWindowSettingsModel.LayoutHotkeys.SelectedKeys)
|
2021-03-25 15:44:55 +03:00
|
|
|
{
|
2021-05-14 09:54:34 -07:00
|
|
|
if (string.IsNullOrEmpty(pair.Value) || pair.Value == Uuid)
|
2021-03-25 15:44:55 +03:00
|
|
|
{
|
|
|
|
result.Add(pair.Key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string QuickKey
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2022-02-22 20:52:34 +01:00
|
|
|
return _quickKey == -1 ? Properties.Resources.Quick_Key_None : _quickKey.ToString(CultureInfo.CurrentCulture);
|
2021-03-25 15:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
2022-02-22 20:52:34 +01:00
|
|
|
var intValue = -1;
|
2021-03-25 15:44:55 +03:00
|
|
|
string none = Properties.Resources.Quick_Key_None;
|
2022-02-22 20:52:34 +01:00
|
|
|
|
|
|
|
if (value != none && int.TryParse(value, out var parsedInt))
|
|
|
|
{
|
|
|
|
intValue = parsedInt;
|
|
|
|
}
|
|
|
|
|
2021-03-25 15:44:55 +03:00
|
|
|
if (intValue != _quickKey)
|
|
|
|
{
|
2022-02-22 20:52:34 +01:00
|
|
|
string prev = _quickKey == -1 ? none : _quickKey.ToString(CultureInfo.CurrentCulture);
|
2021-03-25 15:44:55 +03:00
|
|
|
_quickKey = intValue;
|
|
|
|
|
|
|
|
if (intValue != -1)
|
|
|
|
{
|
2022-01-17 11:50:24 +03:00
|
|
|
MainWindowSettingsModel.LayoutHotkeys.SelectKey(value, Uuid);
|
2021-03-25 15:44:55 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2022-01-17 11:50:24 +03:00
|
|
|
MainWindowSettingsModel.LayoutHotkeys.FreeKey(prev);
|
2021-03-25 15:44:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
FirePropertyChanged(nameof(QuickKey));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int _quickKey = -1;
|
|
|
|
|
2021-01-27 21:33:52 +03:00
|
|
|
// TemplateZoneCount - number of zones selected in the picker window for template layouts
|
|
|
|
public int TemplateZoneCount
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _zoneCount;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value != _zoneCount)
|
|
|
|
{
|
|
|
|
_zoneCount = value;
|
|
|
|
InitTemplateZones();
|
|
|
|
FirePropertyChanged(nameof(TemplateZoneCount));
|
2021-01-29 15:10:37 +03:00
|
|
|
FirePropertyChanged(nameof(IsZoneAddingAllowed));
|
2021-01-27 21:33:52 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-25 22:09:25 +02:00
|
|
|
public int TemplateZoneCountMinimum
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public int TemplateZoneCountMaximum
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return 128;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-03-22 13:10:10 +01:00
|
|
|
private int _zoneCount = LayoutDefaultSettings.DefaultZoneCount;
|
2021-01-27 21:33:52 +03:00
|
|
|
|
2021-01-29 15:10:37 +03:00
|
|
|
public bool IsZoneAddingAllowed
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2024-03-22 13:10:10 +01:00
|
|
|
return TemplateZoneCount < LayoutDefaultSettings.MaxZones;
|
2021-01-29 15:10:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-17 10:00:56 -07:00
|
|
|
// implementation of INotifyPropertyChanged
|
|
|
|
public event PropertyChangedEventHandler PropertyChanged;
|
|
|
|
|
|
|
|
// FirePropertyChanged -- wrapper that calls INPC.PropertyChanged
|
|
|
|
protected virtual void FirePropertyChanged([CallerMemberName] string propertyName = null)
|
|
|
|
{
|
|
|
|
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes this Layout from the registry and the loaded CustomModels list
|
|
|
|
public void Delete()
|
|
|
|
{
|
2021-03-25 15:44:55 +03:00
|
|
|
if (_quickKey != -1)
|
|
|
|
{
|
2022-01-17 11:50:24 +03:00
|
|
|
MainWindowSettingsModel.LayoutHotkeys.FreeKey(QuickKey);
|
2021-03-25 15:44:55 +03:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:33:52 +03:00
|
|
|
var customModels = MainWindowSettingsModel.CustomModels;
|
|
|
|
int i = customModels.IndexOf(this);
|
2020-08-17 10:00:56 -07:00
|
|
|
if (i != -1)
|
|
|
|
{
|
2021-01-27 21:33:52 +03:00
|
|
|
customModels.RemoveAt(i);
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-08-29 22:24:30 +03:00
|
|
|
public void RestoreTo(LayoutModel layout)
|
|
|
|
{
|
|
|
|
layout.SensitivityRadius = SensitivityRadius;
|
|
|
|
layout.TemplateZoneCount = TemplateZoneCount;
|
|
|
|
}
|
|
|
|
|
2020-11-17 11:38:19 +03:00
|
|
|
// Adds new custom Layout
|
|
|
|
public void AddCustomLayout(LayoutModel model)
|
2020-08-17 10:00:56 -07:00
|
|
|
{
|
2020-11-17 11:38:19 +03:00
|
|
|
bool updated = false;
|
2021-01-27 21:33:52 +03:00
|
|
|
var customModels = MainWindowSettingsModel.CustomModels;
|
|
|
|
for (int i = 0; i < customModels.Count && !updated; i++)
|
2020-11-17 11:38:19 +03:00
|
|
|
{
|
2021-01-27 21:33:52 +03:00
|
|
|
if (customModels[i].Uuid == model.Uuid)
|
2020-11-17 11:38:19 +03:00
|
|
|
{
|
2021-01-27 21:33:52 +03:00
|
|
|
customModels[i] = model;
|
2020-11-17 11:38:19 +03:00
|
|
|
updated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!updated)
|
|
|
|
{
|
2021-01-27 21:33:52 +03:00
|
|
|
customModels.Add(model);
|
2020-11-17 11:38:19 +03:00
|
|
|
}
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
|
|
|
|
2021-01-27 21:33:52 +03:00
|
|
|
// InitTemplateZones
|
|
|
|
// Creates zones based on template zones count
|
|
|
|
public abstract void InitTemplateZones();
|
2020-08-17 10:00:56 -07:00
|
|
|
|
|
|
|
// Callbacks that the base LayoutModel makes to derived types
|
|
|
|
protected abstract void PersistData();
|
|
|
|
|
|
|
|
public abstract LayoutModel Clone();
|
|
|
|
|
|
|
|
public void Persist()
|
|
|
|
{
|
|
|
|
PersistData();
|
2023-11-21 16:03:12 +01:00
|
|
|
FirePropertyChanged(nameof(PersistData));
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
2021-03-25 15:44:55 +03:00
|
|
|
|
2022-01-17 11:50:24 +03:00
|
|
|
public void LayoutHotkeys_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
2021-03-25 15:44:55 +03:00
|
|
|
{
|
2022-01-17 11:50:24 +03:00
|
|
|
foreach (var pair in MainWindowSettingsModel.LayoutHotkeys.SelectedKeys)
|
2021-03-25 15:44:55 +03:00
|
|
|
{
|
|
|
|
if (pair.Value == Uuid)
|
|
|
|
{
|
|
|
|
QuickKey = pair.Key.ToString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-10-25 17:55:36 +02:00
|
|
|
|
|
|
|
public void DefaultLayouts_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
{
|
|
|
|
FirePropertyChanged(nameof(IsHorizontalDefault));
|
|
|
|
FirePropertyChanged(nameof(IsVerticalDefault));
|
|
|
|
FirePropertyChanged(nameof(CanBeSetAsHorizontalDefault));
|
|
|
|
FirePropertyChanged(nameof(CanBeSetAsVerticalDefault));
|
|
|
|
}
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
|
|
|
}
|