From 3836aaa9d10774a7663f4daedd2475c292610e61 Mon Sep 17 00:00:00 2001 From: Bret Anderson Date: Sun, 8 Sep 2019 14:54:44 -0700 Subject: [PATCH] Almost working. Doesn't apply on my small monitor running at 150% scaling. --- .../FancyZonesEditor/Models/LayoutModel.cs | 25 ++---- .../FancyZonesEditor/Models/Settings.cs | 87 ++++++++++++++----- src/modules/fancyzones/lib/FancyZones.cpp | 10 +-- 3 files changed, 76 insertions(+), 46 deletions(-) diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs index 8448677b84..ddc1f94bef 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/LayoutModel.cs @@ -186,14 +186,12 @@ namespace FancyZonesEditor.Models // Scale all the zones to the DPI and then pack them up to be marshalled. int zoneCount = zones.Length; var zoneArray = new int[zoneCount * 4]; - var graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero); - float dpi = graphics.DpiX / 96; for (int i = 0; i < zones.Length; i++) { - var left = (int)(zones[i].X * dpi); - var top = (int)(zones[i].Y * dpi); - var right = left + (int)(zones[i].Width * dpi); - var bottom = top + (int)(zones[i].Height * dpi); + var left = (int)(zones[i].X * Settings.Dpi); + var top = (int)(zones[i].Y * Settings.Dpi); + var right = left + (int)(zones[i].Width * Settings.Dpi); + var bottom = top + (int)(zones[i].Height * Settings.Dpi); var index = i * 4; zoneArray[index] = left; @@ -202,19 +200,8 @@ namespace FancyZonesEditor.Models zoneArray[index+3] = bottom; } - string[] args = Environment.GetCommandLineArgs(); - if (args.Length > 1) - { - string uniqueId = args[1]; - uint monitor = 0; - if (args.Length > 3) - { - monitor = uint.Parse(args[4]); - } - - var persistZoneSet = Marshal.GetDelegateForFunctionPointer(pfn); - persistZoneSet(uniqueId, monitor, _id, zoneCount, zoneArray); - } + var persistZoneSet = Marshal.GetDelegateForFunctionPointer(pfn); + persistZoneSet(Settings.UniqueKey, Settings.Monitor, _id, zoneCount, zoneArray); } private static readonly string c_registryPath = Settings.RegistryPath + "\\Layouts"; diff --git a/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs b/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs index 6445554e66..6449772831 100644 --- a/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs +++ b/src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs @@ -23,21 +23,7 @@ namespace FancyZonesEditor { public Settings() { - _workArea = System.Windows.SystemParameters.WorkArea; - string[] args = Environment.GetCommandLineArgs(); - if (args.Length > 2) - { - var foregroundWindow = uint.Parse(args[3]); - var screen = System.Windows.Forms.Screen.FromHandle(new IntPtr(foregroundWindow)); - - var graphics = System.Drawing.Graphics.FromHwnd(IntPtr.Zero); - float dpi = graphics.DpiX / 96; - _workArea = new Rect( - screen.WorkingArea.X / dpi, - screen.WorkingArea.Y / dpi, - screen.WorkingArea.Width / dpi, - screen.WorkingArea.Height / dpi); - } + ParseCommandLineArgs(); // Initialize the five default layout models: Focus, Columns, Rows, Grid, and PriorityGrid _defaultModels = new List(5); @@ -62,9 +48,9 @@ namespace FancyZonesEditor _blankCustomModel = new CanvasLayoutModel("Create new custom", c_blankCustomModelId, (int)_workArea.Width, (int)_workArea.Height); - _zoneCount = (int)Registry.GetValue(FullRegistryPath, "ZoneCount", 3); - _spacing = (int)Registry.GetValue(FullRegistryPath, "Spacing", 16); - _showSpacing = (int)Registry.GetValue(FullRegistryPath, "ShowSpacing", 1) == 1; + _zoneCount = (int)Registry.GetValue(_uniqueRegistryPath, "ZoneCount", 3); + _spacing = (int)Registry.GetValue(_uniqueRegistryPath, "Spacing", 16); + _showSpacing = (int)Registry.GetValue(_uniqueRegistryPath, "ShowSpacing", 1) == 1; UpdateLayoutModels(); } @@ -78,7 +64,7 @@ namespace FancyZonesEditor if (_zoneCount != value) { _zoneCount = value; - Registry.SetValue(FullRegistryPath, "ZoneCount", _zoneCount, RegistryValueKind.DWord); + Registry.SetValue(_uniqueRegistryPath, "ZoneCount", _zoneCount, RegistryValueKind.DWord); UpdateLayoutModels(); FirePropertyChanged("ZoneCount"); } @@ -95,7 +81,7 @@ namespace FancyZonesEditor if (_spacing != value) { _spacing = value; - Registry.SetValue(FullRegistryPath, "Spacing", _spacing, RegistryValueKind.DWord); + Registry.SetValue(_uniqueRegistryPath, "Spacing", _spacing, RegistryValueKind.DWord); FirePropertyChanged("Spacing"); } } @@ -111,7 +97,7 @@ namespace FancyZonesEditor if (_showSpacing != value) { _showSpacing = value; - Registry.SetValue(FullRegistryPath, "ShowSpacing", _showSpacing, RegistryValueKind.DWord); + Registry.SetValue(_uniqueRegistryPath, "ShowSpacing", _showSpacing, RegistryValueKind.DWord); FirePropertyChanged("ShowSpacing"); } } @@ -154,6 +140,25 @@ namespace FancyZonesEditor } private Rect _workArea; + public static uint Monitor + { + get { return _monitor; } + } + private static uint _monitor; + + public static String UniqueKey + { + get { return _uniqueKey; } + } + private static String _uniqueKey; + private String _uniqueRegistryPath; + + public static float Dpi + { + get { return _dpi; } + } + private static float _dpi; + // UpdateLayoutModels // Update the five default layouts based on the new ZoneCount private void UpdateLayoutModels() @@ -251,7 +256,45 @@ namespace FancyZonesEditor _priorityGridModel.ColumnPercents = _gridModel.ColumnPercents; _priorityGridModel.CellChildMap = _gridModel.CellChildMap; } - } + } + + private void ParseCommandLineArgs() + { + _workArea = System.Windows.SystemParameters.WorkArea; + _monitor = 0; + _uniqueKey = ""; + _dpi = 1; + + string[] args = Environment.GetCommandLineArgs(); + if (args.Length == 5) + { + // 1 = unique key for per-monitor settings + // 2 = layoutid used to generate current layout + // 3 = handle to foreground window (used to figure out which monitor to show on) + // 4 = handle to monitor (passed back to engine to persist data) + + _uniqueKey = args[1]; + _uniqueRegistryPath = FullRegistryPath + "\\" + _uniqueKey; + + var foregroundWindow = new IntPtr(uint.Parse(args[3])); + var screen = System.Windows.Forms.Screen.FromHandle(foregroundWindow); + + var graphics = System.Drawing.Graphics.FromHwnd(foregroundWindow); + _dpi = graphics.DpiX / 96; + _workArea = new Rect( + screen.WorkingArea.X / _dpi, + screen.WorkingArea.Y / _dpi, + screen.WorkingArea.Width / _dpi, + screen.WorkingArea.Height / _dpi); + + uint monitor = 0; + if (uint.TryParse(args[4], out monitor)) + { + _monitor = monitor; + } + } + } + public IList DefaultModels { get { return _defaultModels; } } public ObservableCollection CustomModels diff --git a/src/modules/fancyzones/lib/FancyZones.cpp b/src/modules/fancyzones/lib/FancyZones.cpp index 372eeb5718..6587694cdc 100644 --- a/src/modules/fancyzones/lib/FancyZones.cpp +++ b/src/modules/fancyzones/lib/FancyZones.cpp @@ -237,7 +237,7 @@ void FancyZones::ToggleEditor() noexcept m_terminateEditorEvent.reset(CreateEvent(nullptr, true, false, nullptr)); } - const HWND foregroundWindow = GetForegroundWindow(); + const HWND foregroundWindow = GetForegroundWindow(); if (const HMONITOR monitor = MonitorFromWindow(foregroundWindow, MONITOR_DEFAULTTOPRIMARY)) { std::shared_lock readLock(m_lock); @@ -245,10 +245,10 @@ void FancyZones::ToggleEditor() noexcept if (iter != m_zoneWindowMap.end()) { const std::wstring params = - iter->second->UniqueId() + L" " + - std::to_wstring(iter->second->ActiveZoneSet()->LayoutId()) + L" " + - std::to_wstring(reinterpret_cast(foregroundWindow)) + L" " + - std::to_wstring(reinterpret_cast(monitor)); + iter->second->UniqueId() + L" " + + std::to_wstring(iter->second->ActiveZoneSet()->LayoutId()) + L" " + + std::to_wstring(reinterpret_cast(foregroundWindow)) + L" " + + std::to_wstring(reinterpret_cast(monitor)); SHELLEXECUTEINFO sei{ sizeof(sei) }; sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };