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;
|
|
|
|
using System.Runtime.CompilerServices;
|
|
|
|
|
|
|
|
namespace FancyZonesEditor.Models
|
|
|
|
{
|
|
|
|
// Base LayoutModel
|
|
|
|
// Manages common properties and base persistence
|
|
|
|
public abstract class LayoutModel : INotifyPropertyChanged
|
|
|
|
{
|
|
|
|
protected LayoutModel()
|
|
|
|
{
|
|
|
|
_guid = Guid.NewGuid();
|
|
|
|
Type = LayoutType.Custom;
|
2021-03-25 15:44:55 +03:00
|
|
|
|
|
|
|
MainWindowSettingsModel.QuickKeys.PropertyChanged += QuickSwitchKeys_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-01-27 21:33:52 +03:00
|
|
|
protected LayoutModel(LayoutModel other)
|
|
|
|
{
|
|
|
|
_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;
|
|
|
|
|
|
|
|
MainWindowSettingsModel.QuickKeys.PropertyChanged += QuickSwitchKeys_PropertyChanged;
|
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; }
|
|
|
|
|
|
|
|
public Guid Guid
|
|
|
|
{
|
|
|
|
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;
|
|
|
|
|
2021-01-27 21:33:52 +03:00
|
|
|
public int SensitivityRadius
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _sensitivityRadius;
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
if (value != _sensitivityRadius)
|
|
|
|
{
|
|
|
|
_sensitivityRadius = value;
|
|
|
|
FirePropertyChanged(nameof(SensitivityRadius));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int _sensitivityRadius = LayoutSettings.DefaultSensitivityRadius;
|
|
|
|
|
2021-03-25 15:44:55 +03:00
|
|
|
public List<string> QuickKeysAvailable
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
List<string> result = new List<string>();
|
|
|
|
foreach (var pair in MainWindowSettingsModel.QuickKeys.SelectedKeys)
|
|
|
|
{
|
|
|
|
if (pair.Value == string.Empty || pair.Value == Uuid)
|
|
|
|
{
|
|
|
|
result.Add(pair.Key);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public string QuickKey
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return _quickKey == -1 ? Properties.Resources.Quick_Key_None : _quickKey.ToString();
|
|
|
|
}
|
|
|
|
|
|
|
|
set
|
|
|
|
{
|
|
|
|
string none = Properties.Resources.Quick_Key_None;
|
|
|
|
var intValue = value == none ? -1 : int.Parse(value);
|
|
|
|
if (intValue != _quickKey)
|
|
|
|
{
|
|
|
|
string prev = _quickKey == -1 ? none : _quickKey.ToString();
|
|
|
|
_quickKey = intValue;
|
|
|
|
|
|
|
|
if (intValue != -1)
|
|
|
|
{
|
|
|
|
MainWindowSettingsModel.QuickKeys.SelectKey(value, Uuid);
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
MainWindowSettingsModel.QuickKeys.FreeKey(prev);
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private int _zoneCount = LayoutSettings.DefaultZoneCount;
|
|
|
|
|
2021-01-29 15:10:37 +03:00
|
|
|
public bool IsZoneAddingAllowed
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
|
|
|
return TemplateZoneCount < LayoutSettings.MaxZones;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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)
|
|
|
|
{
|
|
|
|
MainWindowSettingsModel.QuickKeys.FreeKey(QuickKey);
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
2021-03-25 15:44:55 +03:00
|
|
|
|
|
|
|
private void QuickSwitchKeys_PropertyChanged(object sender, PropertyChangedEventArgs e)
|
|
|
|
{
|
|
|
|
foreach (var pair in MainWindowSettingsModel.QuickKeys.SelectedKeys)
|
|
|
|
{
|
|
|
|
if (pair.Value == Uuid)
|
|
|
|
{
|
|
|
|
QuickKey = pair.Key.ToString();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-08-17 10:00:56 -07:00
|
|
|
}
|
|
|
|
}
|