Skip to content

Commit

Permalink
[FancyZones] Editor multi monitor support (#6562)
Browse files Browse the repository at this point in the history
Co-authored-by: Enrico Giordani <enrico.giordani@gmail.com>
Co-authored-by: Enrico Giordani <enricogior@users.noreply.github.com>
  • Loading branch information
3 people committed Nov 17, 2020
1 parent 687fc2e commit b8e5ccf
Show file tree
Hide file tree
Showing 88 changed files with 4,867 additions and 1,483 deletions.
1 change: 1 addition & 0 deletions .github/actions/spell-check/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,7 @@ NDEBUG
ndp
neq
NESW
netcore
netcoreapp
netframework
netfx
Expand Down
4 changes: 2 additions & 2 deletions src/modules/fancyzones/editor/FancyZonesEditor.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ Global
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{5CCC8468-DEC8-4D36-99D4-5C891BEBD481}.Debug|Any CPU.ActiveCfg = Debug|x64
{5CCC8468-DEC8-4D36-99D4-5C891BEBD481}.Debug|Any CPU.Build.0 = Debug|x64
{5CCC8468-DEC8-4D36-99D4-5C891BEBD481}.Release|Any CPU.ActiveCfg = Debug|x64
{5CCC8468-DEC8-4D36-99D4-5C891BEBD481}.Release|Any CPU.Build.0 = Debug|x64
{5CCC8468-DEC8-4D36-99D4-5C891BEBD481}.Release|Any CPU.ActiveCfg = Release|x64
{5CCC8468-DEC8-4D36-99D4-5C891BEBD481}.Release|Any CPU.Build.0 = Release|x64
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
Expand Down
3 changes: 3 additions & 0 deletions src/modules/fancyzones/editor/FancyZonesEditor/App.config
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,7 @@
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
<runtime>
<AppContextSwitchOverrides value = "Switch.System.Windows.DoNotScaleForDpiChanges=false"/>
</runtime>
</configuration>
99 changes: 62 additions & 37 deletions src/modules/fancyzones/editor/FancyZonesEditor/App.xaml.cs
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
// Copyright (c) Microsoft Corporation
// 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;
using System.Collections.Generic;
using System.Diagnostics;
using System.Globalization;
using System.IO;
using System.IO.Abstractions;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using FancyZonesEditor.Models;
using FancyZonesEditor.Utils;
using ManagedCommon;

namespace FancyZonesEditor
Expand All @@ -24,6 +23,7 @@ public partial class App : Application
{
// Non-localizable strings
private const string CrashReportLogFile = "FZEditorCrashLog.txt";
private const string ErrorReportLogFile = "FZEditorErrorLog.txt";
private const string PowerToysIssuesURL = "https://aka.ms/powerToysReportBug";

private const string CrashReportExceptionTag = "Exception";
Expand All @@ -44,57 +44,76 @@ public partial class App : Application

private readonly IFileSystem _fileSystem = new FileSystem();

public Settings ZoneSettings { get; }
public MainWindowSettingsModel MainWindowSettings { get; }

public static FancyZonesEditorIO FancyZonesEditorIO { get; private set; }

public static Overlay Overlay { get; private set; }

public static int PowerToysPID { get; set; }

public static bool DebugMode
{
get
{
return _debugMode;
}
}

private static bool _debugMode = false;

[Conditional("DEBUG")]
private void DebugModeCheck()
{
_debugMode = true;
}

public App()
{
ZoneSettings = new Settings();
DebugModeCheck();
FancyZonesEditorIO = new FancyZonesEditorIO();
Overlay = new Overlay();
MainWindowSettings = new MainWindowSettingsModel();
}

private void OnStartup(object sender, StartupEventArgs e)
{
AppDomain.CurrentDomain.UnhandledException += OnUnhandledException;

RunnerHelper.WaitForPowerToysRunner(Settings.PowerToysPID, () =>
RunnerHelper.WaitForPowerToysRunner(PowerToysPID, () =>
{
Environment.Exit(0);
});

LayoutModel foundModel = null;
FancyZonesEditorIO.ParseCommandLineArguments();
FancyZonesEditorIO.ParseDeviceInfoData();

foreach (LayoutModel model in ZoneSettings.DefaultModels)
{
if (model.Type == Settings.ActiveZoneSetLayoutType)
{
// found match
foundModel = model;
break;
}
}
MainWindowSettingsModel settings = ((App)Current).MainWindowSettings;
settings.UpdateSelectedLayoutModel();

if (foundModel == null)
{
foreach (LayoutModel model in Settings.CustomModels)
{
if ("{" + model.Guid.ToString().ToUpper() + "}" == Settings.ActiveZoneSetUUid.ToUpper())
{
// found match
foundModel = model;
break;
}
}
}
Overlay.Show();
}

if (foundModel == null)
public static void ShowExceptionMessageBox(string message, Exception exception = null)
{
string fullMessage = FancyZonesEditor.Properties.Resources.Error_Report + PowerToysIssuesURL + " \n" + message;
if (exception != null)
{
foundModel = ZoneSettings.DefaultModels[0];
fullMessage += ": " + exception.Message;
}

foundModel.IsSelected = true;
MessageBox.Show(fullMessage, FancyZonesEditor.Properties.Resources.Error_Exception_Message_Box_Title);
}

EditorOverlay overlay = new EditorOverlay();
overlay.Show();
overlay.DataContext = foundModel;
public static void ShowExceptionReportMessageBox(string reportData)
{
var fileStream = File.OpenWrite(ErrorReportLogFile);
var sw = new StreamWriter(fileStream);
sw.Write(reportData);
sw.Flush();
fileStream.Close();

ShowReportMessageBox(fileStream.Name);
}

private void OnUnhandledException(object sender, UnhandledExceptionEventArgs args)
Expand All @@ -103,16 +122,22 @@ private void OnUnhandledException(object sender, UnhandledExceptionEventArgs arg
var sw = new StreamWriter(fileStream);
sw.Write(FormatException((Exception)args.ExceptionObject));
fileStream.Close();

ShowReportMessageBox(fileStream.Name);
}

private static void ShowReportMessageBox(string fileName)
{
MessageBox.Show(
FancyZonesEditor.Properties.Resources.Crash_Report_Message_Box_Text_Part1 +
Path.GetFullPath(fileStream.Name) +
Path.GetFullPath(fileName) +
"\n" +
FancyZonesEditor.Properties.Resources.Crash_Report_Message_Box_Text_Part2 +
PowerToysIssuesURL,
FancyZonesEditor.Properties.Resources.Fancy_Zones_Editor_App_Title);
}

private string FormatException(Exception ex)
private static string FormatException(Exception ex)
{
var sb = new StringBuilder();
sb.AppendLine();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
mc:Ignorable="d"
d:DesignHeight="300" d:DesignWidth="300">
<Grid>
<Canvas x:Name="Preview"/>
<Grid x:Name="Body">
<Viewbox Stretch="Uniform">
<Canvas x:Name="Preview"/>
</Viewbox>
</Grid>
</UserControl>
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
// Copyright (c) Microsoft Corporation
// 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.Windows;
using System.Windows.Controls;
using FancyZonesEditor.Models;
using FancyZonesEditor.Utils;

namespace FancyZonesEditor
{
Expand Down Expand Up @@ -46,6 +47,10 @@ private void OnModelChanged(object sender, System.ComponentModel.PropertyChanged

private void UpdateZoneRects()
{
var workArea = App.Overlay.WorkArea;
Preview.Width = workArea.Width;
Preview.Height = workArea.Height;

UIElementCollection previewChildren = Preview.Children;
int previewChildrenCount = previewChildren.Count;
while (previewChildrenCount < _model.Zones.Count)
Expand All @@ -54,6 +59,7 @@ private void UpdateZoneRects()
{
Model = _model,
};

Preview.Children.Add(zone);
previewChildrenCount++;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
SizeToContent="Height"
Background="White"
ResizeMode="NoResize"
WindowStartupLocation="CenterScreen"
WindowStartupLocation="CenterOwner"
Closed="OnClosed">


Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// Copyright (c) Microsoft Corporation
// 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.

Expand All @@ -19,24 +19,28 @@ public CanvasEditorWindow()

KeyUp += CanvasEditorWindow_KeyUp;

_model = EditorOverlay.Current.DataContext as CanvasLayoutModel;
_model = App.Overlay.CurrentDataContext as CanvasLayoutModel;
_stashedModel = (CanvasLayoutModel)_model.Clone();
}

private void OnAddZone(object sender, RoutedEventArgs e)
{
if (_offset + (int)(Settings.WorkArea.Width * 0.4) < (int)Settings.WorkArea.Width
&& _offset + (int)(Settings.WorkArea.Height * 0.4) < (int)Settings.WorkArea.Height)
Rect workingArea = App.Overlay.WorkArea;
int offset = (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(_offset);

if (offset + (int)(workingArea.Width * 0.4) < (int)workingArea.Width
&& offset + (int)(workingArea.Height * 0.4) < (int)workingArea.Height)
{
_model.AddZone(new Int32Rect(_offset, _offset, (int)(Settings.WorkArea.Width * 0.4), (int)(Settings.WorkArea.Height * 0.4)));
_model.AddZone(new Int32Rect(offset, offset, (int)(workingArea.Width * 0.4), (int)(workingArea.Height * 0.4)));
}
else
{
_offset = 100;
_model.AddZone(new Int32Rect(_offset, _offset, (int)(Settings.WorkArea.Width * 0.4), (int)(Settings.WorkArea.Height * 0.4)));
offset = (int)App.Overlay.ScaleCoordinateWithCurrentMonitorDpi(_offset);
_model.AddZone(new Int32Rect(offset, offset, (int)(workingArea.Width * 0.4), (int)(workingArea.Height * 0.4)));
}

_offset += 50;
_offset += 50; // TODO: replace hardcoded numbers
}

protected new void OnCancel(object sender, RoutedEventArgs e)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Windows.Controls;
using System.Windows.Input;
using FancyZonesEditor.Models;
using FancyZonesEditor.Utils;

namespace FancyZonesEditor
{
Expand Down Expand Up @@ -84,7 +85,7 @@ public SnappyHelperBase(IList<Int32Rect> zones, int zoneIndex, bool isX, ResizeM
}
}

foreach (Rect singleMonitor in Settings.UsedWorkAreas)
foreach (Rect singleMonitor in App.Overlay.WorkAreas)
{
int monitorPositionLow = (int)(isX ? singleMonitor.Left : singleMonitor.Top);
int monitorPositionHigh = (int)(isX ? singleMonitor.Right : singleMonitor.Bottom);
Expand Down Expand Up @@ -213,8 +214,9 @@ public override void Move(int delta)

private SnappyHelperBase NewDefaultSnappyHelper(bool isX, ResizeMode mode)
{
int screenAxisOrigin = (int)(isX ? Settings.WorkArea.Left : Settings.WorkArea.Top);
int screenAxisSize = (int)(isX ? Settings.WorkArea.Width : Settings.WorkArea.Height);
Rect workingArea = App.Overlay.WorkArea;
int screenAxisOrigin = (int)(isX ? workingArea.Left : workingArea.Top);
int screenAxisSize = (int)(isX ? workingArea.Width : workingArea.Height);
return new SnappyHelperMagnetic(Model.Zones, ZoneIndex, isX, mode, screenAxisOrigin, screenAxisSize);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public class ModelToVisibilityConverter : IValueConverter
{
public object Convert(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
{
return Settings.IsPredefinedLayout((LayoutModel)value) ? Visibility.Collapsed : Visibility.Visible;
return MainWindowSettingsModel.IsPredefinedLayout((LayoutModel)value) ? Visibility.Collapsed : Visibility.Visible;
}

public object ConvertBack(object value, Type targetType, object parameter, System.Globalization.CultureInfo culture)
Expand Down

0 comments on commit b8e5ccf

Please sign in to comment.