[FancyZones Editor] Quick layout switch hotkeys (#10437)

Co-authored-by: Ivan Stošić <ivan100sic@gmail.com>
This commit is contained in:
Seraphima Zykova
2021-03-25 15:44:55 +03:00
committed by GitHub
parent 7ba03ed24f
commit 13c4c188fa
34 changed files with 986 additions and 376 deletions

View File

@@ -3,6 +3,7 @@
// See the LICENSE file in the project root for more information.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Runtime.CompilerServices;
@@ -16,6 +17,8 @@ namespace FancyZonesEditor.Models
{
_guid = Guid.NewGuid();
Type = LayoutType.Custom;
MainWindowSettingsModel.QuickKeys.PropertyChanged += QuickSwitchKeys_PropertyChanged;
}
protected LayoutModel(string name)
@@ -48,6 +51,9 @@ namespace FancyZonesEditor.Models
_isApplied = other._isApplied;
_sensitivityRadius = other._sensitivityRadius;
_zoneCount = other._zoneCount;
_quickKey = other._quickKey;
MainWindowSettingsModel.QuickKeys.PropertyChanged += QuickSwitchKeys_PropertyChanged;
}
// Name - the display name for this layout model - is also used as the key in the registry
@@ -158,6 +164,55 @@ namespace FancyZonesEditor.Models
private int _sensitivityRadius = LayoutSettings.DefaultSensitivityRadius;
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;
// TemplateZoneCount - number of zones selected in the picker window for template layouts
public int TemplateZoneCount
{
@@ -200,6 +255,11 @@ namespace FancyZonesEditor.Models
// Removes this Layout from the registry and the loaded CustomModels list
public void Delete()
{
if (_quickKey != -1)
{
MainWindowSettingsModel.QuickKeys.FreeKey(QuickKey);
}
var customModels = MainWindowSettingsModel.CustomModels;
int i = customModels.IndexOf(this);
if (i != -1)
@@ -241,5 +301,17 @@ namespace FancyZonesEditor.Models
{
PersistData();
}
private void QuickSwitchKeys_PropertyChanged(object sender, PropertyChangedEventArgs e)
{
foreach (var pair in MainWindowSettingsModel.QuickKeys.SelectedKeys)
{
if (pair.Value == Uuid)
{
QuickKey = pair.Key.ToString();
break;
}
}
}
}
}

View File

@@ -6,7 +6,7 @@ namespace FancyZonesEditor.Models
{
public enum LayoutType
{
Blank = -1,
Blank = 0,
Focus,
Columns,
Rows,

View File

@@ -23,13 +23,6 @@ namespace FancyZonesEditor
VirtualDesktopId,
}
private readonly CanvasLayoutModel _blankModel;
private readonly CanvasLayoutModel _focusModel;
private readonly GridLayoutModel _rowsModel;
private readonly GridLayoutModel _columnsModel;
private readonly GridLayoutModel _gridModel;
private readonly GridLayoutModel _priorityGridModel;
// Non-localizable strings
public static readonly string RegistryPath = "SOFTWARE\\SuperFancyZones";
public static readonly string FullRegistryPath = "HKEY_CURRENT_USER\\" + RegistryPath;
@@ -53,40 +46,40 @@ namespace FancyZonesEditor
public MainWindowSettingsModel()
{
// Initialize default layout models: Blank, Focus, Columns, Rows, Grid, and PriorityGrid
_blankModel = new CanvasLayoutModel(Properties.Resources.Template_Layout_Blank, LayoutType.Blank)
var blankModel = new CanvasLayoutModel(Properties.Resources.Template_Layout_Blank, LayoutType.Blank)
{
TemplateZoneCount = 0,
SensitivityRadius = 0,
};
DefaultModels.Add(_blankModel);
DefaultModels.Insert((int)LayoutType.Blank, blankModel);
_focusModel = new CanvasLayoutModel(Properties.Resources.Template_Layout_Focus, LayoutType.Focus);
_focusModel.InitTemplateZones();
DefaultModels.Add(_focusModel);
var focusModel = new CanvasLayoutModel(Properties.Resources.Template_Layout_Focus, LayoutType.Focus);
focusModel.InitTemplateZones();
DefaultModels.Insert((int)LayoutType.Focus, focusModel);
_columnsModel = new GridLayoutModel(Properties.Resources.Template_Layout_Columns, LayoutType.Columns)
var columnsModel = new GridLayoutModel(Properties.Resources.Template_Layout_Columns, LayoutType.Columns)
{
Rows = 1,
RowPercents = new List<int>(1) { GridLayoutModel.GridMultiplier },
};
_columnsModel.InitTemplateZones();
DefaultModels.Add(_columnsModel);
columnsModel.InitTemplateZones();
DefaultModels.Insert((int)LayoutType.Columns, columnsModel);
_rowsModel = new GridLayoutModel(Properties.Resources.Template_Layout_Rows, LayoutType.Rows)
var rowsModel = new GridLayoutModel(Properties.Resources.Template_Layout_Rows, LayoutType.Rows)
{
Columns = 1,
ColumnPercents = new List<int>(1) { GridLayoutModel.GridMultiplier },
};
_rowsModel.InitTemplateZones();
DefaultModels.Add(_rowsModel);
rowsModel.InitTemplateZones();
DefaultModels.Insert((int)LayoutType.Rows, rowsModel);
_gridModel = new GridLayoutModel(Properties.Resources.Template_Layout_Grid, LayoutType.Grid);
_gridModel.InitTemplateZones();
DefaultModels.Add(_gridModel);
var gridModel = new GridLayoutModel(Properties.Resources.Template_Layout_Grid, LayoutType.Grid);
gridModel.InitTemplateZones();
DefaultModels.Insert((int)LayoutType.Grid, gridModel);
_priorityGridModel = new GridLayoutModel(Properties.Resources.Template_Layout_Priority_Grid, LayoutType.PriorityGrid);
_priorityGridModel.InitTemplateZones();
DefaultModels.Add(_priorityGridModel);
var priorityGridModel = new GridLayoutModel(Properties.Resources.Template_Layout_Priority_Grid, LayoutType.PriorityGrid);
priorityGridModel.InitTemplateZones();
DefaultModels.Insert((int)LayoutType.PriorityGrid, priorityGridModel);
}
// IsShiftKeyPressed - is the shift key currently being held down
@@ -133,7 +126,7 @@ namespace FancyZonesEditor
{
get
{
return _blankModel;
return DefaultModels[(int)LayoutType.Blank];
}
}
@@ -149,6 +142,8 @@ namespace FancyZonesEditor
private static ObservableCollection<LayoutModel> _customModels = new ObservableCollection<LayoutModel>();
public static QuickKeysModel QuickKeys { get; } = new QuickKeysModel();
public LayoutModel SelectedModel
{
get
@@ -202,7 +197,7 @@ namespace FancyZonesEditor
{
foreach (LayoutModel model in CustomModels)
{
if ("{" + model.Guid.ToString().ToUpperInvariant() + "}" == currentApplied.ZonesetUuid.ToUpperInvariant())
if (model.Uuid == currentApplied.ZonesetUuid.ToUpperInvariant())
{
// found match
foundModel = model;
@@ -234,7 +229,7 @@ namespace FancyZonesEditor
if (foundModel == null)
{
foundModel = _priorityGridModel;
foundModel = DefaultModels[(int)LayoutType.PriorityGrid];
}
SetSelectedModel(foundModel);
@@ -255,6 +250,7 @@ namespace FancyZonesEditor
SelectedModel.IsSelected = model.IsSelected;
SelectedModel.IsApplied = model.IsApplied;
SelectedModel.Name = model.Name;
SelectedModel.QuickKey = model.QuickKey;
if (model is GridLayoutModel grid)
{

View File

@@ -0,0 +1,87 @@
// 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.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Runtime.CompilerServices;
namespace FancyZonesEditor.Models
{
public class QuickKeysModel : INotifyPropertyChanged
{
public SortedDictionary<string, string> SelectedKeys { get; } = new SortedDictionary<string, string>()
{
{ Properties.Resources.Quick_Key_None, string.Empty },
{ "0", string.Empty },
{ "1", string.Empty },
{ "2", string.Empty },
{ "3", string.Empty },
{ "4", string.Empty },
{ "5", string.Empty },
{ "6", string.Empty },
{ "7", string.Empty },
{ "8", string.Empty },
{ "9", string.Empty },
};
public QuickKeysModel()
{
}
public event PropertyChangedEventHandler PropertyChanged;
public void FreeKey(string key)
{
if (SelectedKeys.ContainsKey(key))
{
SelectedKeys[key] = string.Empty;
FirePropertyChanged();
}
}
public bool SelectKey(string key, string uuid)
{
if (!SelectedKeys.ContainsKey(key))
{
return false;
}
if (SelectedKeys[key] == uuid)
{
return true;
}
// clean previous value
foreach (var pair in SelectedKeys)
{
if (pair.Value == uuid)
{
SelectedKeys[pair.Key] = string.Empty;
break;
}
}
SelectedKeys[key] = uuid;
FirePropertyChanged();
return true;
}
public void CleanUp()
{
var keys = SelectedKeys.Keys.ToList();
foreach (var key in keys)
{
SelectedKeys[key] = string.Empty;
}
FirePropertyChanged();
}
protected virtual void FirePropertyChanged([CallerMemberName] string propertyName = null)
{
PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName));
}
}
}