Skip to content

Commit

Permalink
Everything is working.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheMrJukes committed Sep 15, 2019
1 parent d4c8c84 commit c0cdf7a
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 29 deletions.
18 changes: 4 additions & 14 deletions src/modules/fancyzones/dll/dllmain.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "pch.h"
#include <common/settings_objects.h>
#include <common/dpi_aware.h>
#include <interface/powertoy_module_interface.h>
#include <interface/lowlevel_keyboard_event_data.h>
#include <interface/win_hook_event_data.h>
Expand Down Expand Up @@ -27,29 +28,18 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReser
return TRUE;
}

// TODO: multimon support, need to pass the HMONITOR from the editor to here instead
// of using MonitorFromPoint
// This function is exported and called from FancyZonesEditor.exe to save a layout from the editor.
STDAPI PersistZoneSet(
PCWSTR activeKey, // Registry key holding ActiveZoneSet
PCWSTR resolutionKey, // Registry key to persist ZoneSet to
HMONITOR monitor,
WORD layoutId, // LayoutModel Id
int zoneCount, // Number of zones in zones
int zones[]) // Array of zones serialized in left/top/right/bottom chunks
{
// See if we have already persisted this layout we can update.
std::wstringstream stream;
MONITORINFOEX mi;
mi.cbSize = sizeof(mi);
if (GetMonitorInfo(monitor, &mi))
{
stream << (mi.rcMonitor.right - mi.rcMonitor.left) << "_";
stream << (mi.rcMonitor.bottom - mi.rcMonitor.top);
}

std::wstring resolutionKey(stream.str());
UUID id{GUID_NULL};
if (wil::unique_hkey key{ RegistryHelpers::OpenKey(resolutionKey.c_str()) })
if (wil::unique_hkey key{ RegistryHelpers::OpenKey(resolutionKey) })
{
ZoneSetPersistedData data{};
DWORD dataSize = sizeof(data);
Expand Down Expand Up @@ -84,7 +74,7 @@ STDAPI PersistZoneSet(
id,
layoutId,
reinterpret_cast<HMONITOR>(monitor),
resolutionKey.c_str(),
resolutionKey,
ZoneSetLayout::Custom,
0, 0, 0));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ internal static class Native

internal delegate int PersistZoneSet(
[MarshalAs(UnmanagedType.LPWStr)] string activeKey,
[MarshalAs(UnmanagedType.LPWStr)] string resolutionKey,
uint monitor,
ushort layoutId,
int zoneCount,
Expand Down Expand Up @@ -201,7 +202,7 @@ public void Apply(System.Windows.Int32Rect[] zones)
}

var persistZoneSet = Marshal.GetDelegateForFunctionPointer<Native.PersistZoneSet>(pfn);
persistZoneSet(Settings.UniqueKey, Settings.Monitor, _id, zoneCount, zoneArray);
persistZoneSet(Settings.UniqueKey, Settings.WorkAreaKey, Settings.Monitor, _id, zoneCount, zoneArray);
}

private static readonly string c_registryPath = Settings.RegistryPath + "\\Layouts";
Expand Down
33 changes: 20 additions & 13 deletions src/modules/fancyzones/editor/FancyZonesEditor/Models/Settings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,12 @@ public static String UniqueKey
private static String _uniqueKey;
private String _uniqueRegistryPath;

public static String WorkAreaKey
{
get { return _workAreaKey; }
}
private static String _workAreaKey;

public static float Dpi
{
get { return _dpi; }
Expand Down Expand Up @@ -267,26 +273,27 @@ private void ParseCommandLineArgs()
_dpi = 1;

string[] args = Environment.GetCommandLineArgs();
if (args.Length == 6)
if (args.Length == 7)
{
// 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)
// 5 = monitor DPI (float)
// 2 = layoutid used to generate current layout (used to pick the default layout to show)
// 3 = handle to monitor (passed back to engine to persist data)
// 4 = X_Y_Width_Height (where EditorOverlay shows up)
// 5 = resolution key (passed back to engine to persist data)
// 6 = monitor DPI (float)

_uniqueKey = args[1];
_uniqueRegistryPath += "\\" + _uniqueKey;

var foregroundWindow = new IntPtr(uint.Parse(args[3]));
var screen = System.Windows.Forms.Screen.FromHandle(foregroundWindow);
var parsedLocation = args[4].Split('_');
var x = int.Parse(parsedLocation[0]);
var y = int.Parse(parsedLocation[1]);
var width = int.Parse(parsedLocation[2]);
var height = int.Parse(parsedLocation[3]);

_dpi = float.Parse(args[5]);
_workArea = new Rect(
screen.WorkingArea.X / _dpi,
screen.WorkingArea.Y / _dpi,
screen.WorkingArea.Width / _dpi,
screen.WorkingArea.Height / _dpi);
_workAreaKey = args[5];
_dpi = float.Parse(args[6]);
_workArea = new Rect(x, y, width, height);

uint monitor = 0;
if (uint.TryParse(args[4], out monitor))
Expand Down
14 changes: 13 additions & 1 deletion src/modules/fancyzones/lib/FancyZones.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,11 +249,23 @@ void FancyZones::ToggleEditor() noexcept
UINT dpi_y = 96;
DPIAware::GetScreenDPIForWindow(foregroundWindow, dpi_x, dpi_y);

MONITORINFOEX mi;
mi.cbSize = sizeof(mi);
GetMonitorInfo(monitor, &mi);

// Location that the editor should occupy, scaled by DPI
std::wstring editorLocation =
std::to_wstring(MulDiv(mi.rcWork.left, 96, dpi_x)) + L"_" +
std::to_wstring(MulDiv(mi.rcWork.top, 96, dpi_y)) + L"_" +
std::to_wstring(MulDiv(mi.rcWork.right - mi.rcWork.left, 96, dpi_x)) + L"_" +
std::to_wstring(MulDiv(mi.rcWork.bottom - mi.rcWork.top, 96, dpi_y));

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)) + L" " +
editorLocation + L" " +
iter->second->WorkAreaKey() + L" " +
std::to_wstring(static_cast<float>(dpi_x) / 96.0f);

SHELLEXECUTEINFO sei{ sizeof(sei) };
Expand Down
1 change: 1 addition & 0 deletions src/modules/fancyzones/lib/ZoneWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ struct ZoneWindow : public winrt::implements<ZoneWindow, IZoneWindow>
IFACEMETHODIMP_(void) CycleActiveZoneSet(DWORD vkCode) noexcept;
IFACEMETHODIMP_(std::wstring) DeviceId() noexcept { return { m_deviceId.get() }; }
IFACEMETHODIMP_(std::wstring) UniqueId() noexcept { return { m_uniqueId }; }
IFACEMETHODIMP_(std::wstring) WorkAreaKey() noexcept { return { m_workArea }; }
IFACEMETHODIMP_(void) SaveWindowProcessToZoneIndex(HWND window) noexcept;
IFACEMETHODIMP_(IZoneSet*) ActiveZoneSet() noexcept { return m_activeZoneSet.get(); }

Expand Down
1 change: 1 addition & 0 deletions src/modules/fancyzones/lib/ZoneWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ interface __declspec(uuid("{7F017528-8110-4FB3-BE41-F472969C2560}")) IZoneWindow
IFACEMETHOD_(void, SaveWindowProcessToZoneIndex)(HWND window) = 0;
IFACEMETHOD_(std::wstring, DeviceId)() = 0;
IFACEMETHOD_(std::wstring, UniqueId)() = 0;
IFACEMETHOD_(std::wstring, WorkAreaKey)() = 0;
IFACEMETHOD_(IZoneSet*, ActiveZoneSet)() = 0;
};

Expand Down

0 comments on commit c0cdf7a

Please sign in to comment.