Skip to content

Commit

Permalink
Almost working. Doesn't apply on my small monitor running at 150% sca…
Browse files Browse the repository at this point in the history
…ling.
  • Loading branch information
Bret Anderson committed Sep 8, 2019
1 parent e562b29 commit 3836aaa
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -186,14 +186,12 @@ public void Apply(System.Windows.Int32Rect[] zones)
// 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;
Expand All @@ -202,19 +200,8 @@ public void Apply(System.Windows.Int32Rect[] zones)
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<Native.PersistZoneSet>(pfn);
persistZoneSet(uniqueId, monitor, _id, zoneCount, zoneArray);
}
var persistZoneSet = Marshal.GetDelegateForFunctionPointer<Native.PersistZoneSet>(pfn);
persistZoneSet(Settings.UniqueKey, Settings.Monitor, _id, zoneCount, zoneArray);
}

private static readonly string c_registryPath = Settings.RegistryPath + "\\Layouts";
Expand Down
87 changes: 65 additions & 22 deletions src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,7 @@ public class Settings : INotifyPropertyChanged
{
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<LayoutModel>(5);
Expand All @@ -62,9 +48,9 @@ public Settings()

_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();
}
Expand All @@ -78,7 +64,7 @@ public int ZoneCount
if (_zoneCount != value)
{
_zoneCount = value;
Registry.SetValue(FullRegistryPath, "ZoneCount", _zoneCount, RegistryValueKind.DWord);
Registry.SetValue(_uniqueRegistryPath, "ZoneCount", _zoneCount, RegistryValueKind.DWord);
UpdateLayoutModels();
FirePropertyChanged("ZoneCount");
}
Expand All @@ -95,7 +81,7 @@ public int Spacing
if (_spacing != value)
{
_spacing = value;
Registry.SetValue(FullRegistryPath, "Spacing", _spacing, RegistryValueKind.DWord);
Registry.SetValue(_uniqueRegistryPath, "Spacing", _spacing, RegistryValueKind.DWord);
FirePropertyChanged("Spacing");
}
}
Expand All @@ -111,7 +97,7 @@ public bool ShowSpacing
if (_showSpacing != value)
{
_showSpacing = value;
Registry.SetValue(FullRegistryPath, "ShowSpacing", _showSpacing, RegistryValueKind.DWord);
Registry.SetValue(_uniqueRegistryPath, "ShowSpacing", _showSpacing, RegistryValueKind.DWord);
FirePropertyChanged("ShowSpacing");
}
}
Expand Down Expand Up @@ -154,6 +140,25 @@ public Rect WorkArea
}
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()
Expand Down Expand Up @@ -251,7 +256,45 @@ private void UpdateLayoutModels()
_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<LayoutModel> DefaultModels { get { return _defaultModels; } }
public ObservableCollection<LayoutModel> CustomModels
Expand Down
10 changes: 5 additions & 5 deletions src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,18 +237,18 @@ 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);
auto iter = m_zoneWindowMap.find(monitor);
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<UINT_PTR>(foregroundWindow)) + L" " +
std::to_wstring(reinterpret_cast<UINT_PTR>(monitor));
iter->second->UniqueId() + L" " +
std::to_wstring(iter->second->ActiveZoneSet()->LayoutId()) + L" " +
std::to_wstring(reinterpret_cast<UINT_PTR>(foregroundWindow)) + L" " +
std::to_wstring(reinterpret_cast<UINT_PTR>(monitor));

SHELLEXECUTEINFO sei{ sizeof(sei) };
sei.fMask = { SEE_MASK_NOCLOSEPROCESS | SEE_MASK_FLAG_NO_UI };
Expand Down

0 comments on commit 3836aaa

Please sign in to comment.