Skip to content

Commit

Permalink
Migrate FancyZones data persisting from Registry to JSON file (micros…
Browse files Browse the repository at this point in the history
…oft#1194)

* Migrate FancyZones data persisting from Registry to JSON file

* Address PR comment: Remove redundant check

* Addres PR comment: Remove unused Dpi and add CmdArgs enum

* Address PR comment: Make methods const and inline

* Address PR comments: Expose GenerateUniqueId function and use const ref instead of passing wstring by value

* Address PR comment: Use lamdba as callback

* Address PR comment: Move GenerateUniqueId to ZoneWindowUtils namespace

* Address PR comment: Use regular comparison instead of std::wstring::compare

* Address PR comment: Use std::wstring_view for tmp file paths

* Address PR comment: Use scoped lock when accessing member data

* Address PR comment: Remove typedefs to increase code readability

* Address PR comment: removed nullptr checks with corresponding tests

* Address PR comment: Move ZoneSet object instead of copying

* Address PR comment: Make FancyZonesData instance const where possible

* Remove unnecessary gutter variable during calculating zone coordinates

* Remove uneeded subclass

* Avoid unnecessary copying and reserve space for vector if possible

* Save FancyZones data after exiting editor

* App zone history (microsoft#18)

* added window and zone set ids to app zone history

* Rename JSON file

* Remove AppZoneHistory migration

* Move parsing of ZoneWindow independent temp files outside of it

* Unit tests update (microsoft#19)

* check device existence in map
* updated ZoneSet tests
* updated JsonHelpers tests

* Use single zone count information

* Remove uneeded tests

* Remove one more test

* Remove uneeded line

* Address PR comments - Missing whitespace

* Update zoneset data for new virtual desktops (microsoft#21)

* update active zone set with actual data

* Introduce Blank zone set (used to indicate that no layout applied yet). Move parsing completely outside of ZoneWindow.

* Fix unit tests to match modifications in implementation

* Fix applying layouts on startup (second monitor)

Co-authored-by: vldmr11080 <[email protected]>
Co-authored-by: Seraphima <[email protected]>
  • Loading branch information
3 people authored and udit3333 committed Feb 20, 2020
1 parent 4da2759 commit 88a757f
Show file tree
Hide file tree
Showing 41 changed files with 8,475 additions and 1,884 deletions.
2 changes: 2 additions & 0 deletions src/common/settings_helpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@

namespace PTSettingsHelper {

std::wstring get_module_save_folder_location(std::wstring_view powertoy_name);

void save_module_settings(std::wstring_view powertoy_name, json::JsonObject& settings);
json::JsonObject load_module_settings(std::wstring_view powertoy_name);
void save_general_settings(const json::JsonObject& settings);
Expand Down
3 changes: 0 additions & 3 deletions src/modules/fancyzones/dll/FancyZonesModule.vcxproj
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalDependencies>gdiplus.lib;dwmapi.lib;shlwapi.lib;uxtheme.lib;shcore.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>fancyzones.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
Expand All @@ -96,7 +95,6 @@
<GenerateDebugInformation>true</GenerateDebugInformation>
<OutputFile>$(OutDir)$(TargetName)$(TargetExt)</OutputFile>
<AdditionalDependencies>gdiplus.lib;dwmapi.lib;shlwapi.lib;uxtheme.lib;shcore.lib;%(AdditionalDependencies)</AdditionalDependencies>
<ModuleDefinitionFile>fancyzones.def</ModuleDefinitionFile>
</Link>
</ItemDefinitionGroup>
<ItemGroup>
Expand All @@ -121,7 +119,6 @@
</ProjectReference>
</ItemGroup>
<ItemGroup>
<None Include="fancyzones.def" />
<None Include="packages.config" />
</ItemGroup>
<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
</ItemGroup>
<ItemGroup>
<None Include="packages.config" />
<None Include="fancyzones.def" />
</ItemGroup>
<ItemGroup>
<ResourceCompile Include="..\lib\fancyzones.rc" />
Expand Down
76 changes: 4 additions & 72 deletions src/modules/fancyzones/dll/dllmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
#include <interface/lowlevel_keyboard_event_data.h>
#include <interface/win_hook_event_data.h>
#include <lib/ZoneSet.h>
#include <lib/RegistryHelpers.h>

#include <lib/resource.h>
#include <lib/trace.h>
Expand Down Expand Up @@ -33,76 +32,6 @@ BOOL APIENTRY DllMain(HMODULE hModule, DWORD ul_reason_for_call, LPVOID lpReser
return TRUE;
}

// 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.
UUID id{GUID_NULL};
if (wil::unique_hkey key{ RegistryHelpers::OpenKey(resolutionKey) })
{
ZoneSetPersistedData data{};
DWORD dataSize = sizeof(data);
wchar_t value[256]{};
DWORD valueLength = ARRAYSIZE(value);
DWORD i = 0;
while (RegEnumValueW(key.get(), i++, value, &valueLength, nullptr, nullptr, reinterpret_cast<BYTE*>(&data), &dataSize) == ERROR_SUCCESS)
{
if (data.LayoutId == layoutId)
{
if (data.ZoneCount == zoneCount)
{
CLSIDFromString(value, &id);
break;
}
}
valueLength = ARRAYSIZE(value);
dataSize = sizeof(data);
}
}

if (id == GUID_NULL)
{
// No existing layout found so let's create a new one.
UuidCreate(&id);
}

if (id != GUID_NULL)
{
winrt::com_ptr<IZoneSet> zoneSet = MakeZoneSet(
ZoneSetConfig(
id,
layoutId,
MonitorFromPoint({}, MONITOR_DEFAULTTOPRIMARY),
resolutionKey));

for (int i = 0; i < zoneCount; i++)
{
const int baseIndex = i * 4;
const int left = zones[baseIndex];
const int top = zones[baseIndex+1];
const int right = zones[baseIndex+2];
const int bottom = zones[baseIndex+3];
zoneSet->AddZone(MakeZone({ left, top, right, bottom }));
}
zoneSet->Save();

wil::unique_cotaskmem_string zoneSetId;
if (SUCCEEDED_LOG(StringFromCLSID(id, &zoneSetId)))
{
RegistryHelpers::SetString(activeKey, L"ActiveZoneSetId", zoneSetId.get());
}

return S_OK;
}
return E_FAIL;
}

class FancyZonesModule : public PowertoyModuleIface
{
public:
Expand Down Expand Up @@ -147,7 +76,7 @@ class FancyZonesModule : public PowertoyModuleIface
if (!m_app)
{
Trace::FancyZones::EnableFancyZones(true);
m_app = MakeFancyZones(reinterpret_cast<HINSTANCE>(&__ImageBase), m_settings.get());
m_app = MakeFancyZones(reinterpret_cast<HINSTANCE>(&__ImageBase), m_settings);
if (m_app)
{
m_app->Run();
Expand Down Expand Up @@ -200,12 +129,15 @@ class FancyZonesModule : public PowertoyModuleIface
{
app_name = GET_RESOURCE_STRING(IDS_FANCYZONES);
m_settings = MakeFancyZonesSettings(reinterpret_cast<HINSTANCE>(&__ImageBase), FancyZonesModule::get_name());
JSONHelpers::FancyZonesDataInstance().LoadFancyZonesData();
}

private:
void Disable(bool const traceEvent)
{
if (m_app) {
const auto& fancyZonesData = JSONHelpers::FancyZonesDataInstance();
fancyZonesData.SaveFancyZonesData();
if (traceEvent)
{
Trace::FancyZones::EnableFancyZones(false);
Expand Down
4 changes: 0 additions & 4 deletions src/modules/fancyzones/dll/fancyzones.def

This file was deleted.

34 changes: 12 additions & 22 deletions src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,46 +15,36 @@ public partial class App : Application
{
public Settings ZoneSettings { get; }

private ushort _idInitial = 0;

public App()
{
ZoneSettings = new Settings();
}

private void OnStartup(object sender, StartupEventArgs e)
{
if (e.Args.Length > 1)
LayoutModel foundModel = null;

foreach (LayoutModel model in ZoneSettings.DefaultModels)
{
ushort.TryParse(e.Args[1], out _idInitial);
if (model.Type == Settings.ActiveZoneSetLayoutType)
{
// found match
foundModel = model;
break;
}
}

LayoutModel foundModel = null;

if (_idInitial != 0)
if (foundModel == null)
{
foreach (LayoutModel model in ZoneSettings.DefaultModels)
foreach (LayoutModel model in Settings.CustomModels)
{
if (model.Id == _idInitial)
if ("{" + model.Guid.ToString().ToUpper() + "}" == Settings.ActiveZoneSetUUid.ToUpper())
{
// found match
foundModel = model;
break;
}
}

if (foundModel == null)
{
foreach (LayoutModel model in ZoneSettings.CustomModels)
{
if (model.Id == _idInitial)
{
// found match
foundModel = model;
break;
}
}
}
}

if (foundModel == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,11 @@ public partial class EditorOverlay : Window

private readonly Settings _settings = ((App)Application.Current).ZoneSettings;
private LayoutPreview _layoutPreview;

private UserControl _editor;

private static MainWindow _mainWindow = new MainWindow();

public Int32Rect[] GetZoneRects()
{
if (_editor != null)
Expand Down Expand Up @@ -79,27 +82,23 @@ private void OnLoaded(object sender, RoutedEventArgs e)

public void ShowLayoutPicker()
{
DataContext = null;

_editor = null;
_layoutPreview = new LayoutPreview
{
IsActualSize = true,
Opacity = 0.5,
};

Content = _layoutPreview;

MainWindow window = new MainWindow
{
Owner = this,
ShowActivated = true,
Topmost = true,
};
window.Show();
_mainWindow.Owner = this;
_mainWindow.ShowActivated = true;
_mainWindow.Topmost = true;
_mainWindow.Show();

// window is set to topmost to make sure it shows on top of PowerToys settings page
// we can reset topmost flag now
window.Topmost = false;
_mainWindow.Topmost = false;
}

// These event handlers are used to track the current state of the Shift and Ctrl keys on the keyboard
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
<Reference Include="System.Configuration" />
<Reference Include="System.Data" />
<Reference Include="System.Drawing" />
<Reference Include="System.Runtime.Serialization" />
<Reference Include="System.Windows.Forms" />
<Reference Include="System.Xml" />
<Reference Include="Microsoft.CSharp" />
Expand Down Expand Up @@ -194,6 +195,9 @@
<PackageReference Include="MahApps.Metro">
<Version>2.0.0-alpha0455</Version>
</PackageReference>
<PackageReference Include="System.Text.Json">
<Version>4.6.0</Version>
</PackageReference>
<PackageReference Include="StyleCop.Analyzers">
<Version>1.1.118</Version>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
Expand All @@ -208,4 +212,4 @@
<Resource Include="images\Merge.png" />
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
</Project>
18 changes: 7 additions & 11 deletions src/modules/fancyzones/editor/FancyZonesEditor/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ public partial class MainWindow : MetroWindow
public const int MaxZones = 40;
private readonly Settings _settings = ((App)Application.Current).ZoneSettings;
private static readonly string _defaultNamePrefix = "Custom Layout ";
private bool _editing = false;

public int WrapPanelItemSize { get; set; } = 262;

Expand Down Expand Up @@ -67,7 +66,7 @@ private void NewCustomLayoutButton_Click(object sender, RoutedEventArgs e)
{
WindowLayout window = new WindowLayout();
window.Show();
Close();
Hide();
}

private void LayoutItem_Click(object sender, MouseButtonEventArgs e)
Expand Down Expand Up @@ -95,12 +94,11 @@ private void EditLayout_Click(object sender, RoutedEventArgs e)
}

model.IsSelected = false;
_editing = true;
Close();
Hide();

bool isPredefinedLayout = Settings.IsPredefinedLayout(model);

if (!_settings.CustomModels.Contains(model) || isPredefinedLayout)
if (!Settings.CustomModels.Contains(model) || isPredefinedLayout)
{
if (isPredefinedLayout)
{
Expand All @@ -110,7 +108,7 @@ private void EditLayout_Click(object sender, RoutedEventArgs e)
}

int maxCustomIndex = 0;
foreach (LayoutModel customModel in _settings.CustomModels)
foreach (LayoutModel customModel in Settings.CustomModels)
{
string name = customModel.Name;
if (name.StartsWith(_defaultNamePrefix))
Expand Down Expand Up @@ -165,10 +163,8 @@ private void Apply_Click(object sender, RoutedEventArgs e)

private void OnClosing(object sender, EventArgs e)
{
if (!_editing)
{
EditorOverlay.Current.Close();
}
LayoutModel.SerializeDeletedCustomZoneSets();
EditorOverlay.Current.Close();
}

private void OnInitialized(object sender, EventArgs e)
Expand All @@ -178,7 +174,7 @@ private void OnInitialized(object sender, EventArgs e)

private void SetSelectedItem()
{
foreach (LayoutModel model in _settings.CustomModels)
foreach (LayoutModel model in Settings.CustomModels)
{
if (model.IsSelected)
{
Expand Down
Loading

0 comments on commit 88a757f

Please sign in to comment.