2019-12-12 12:13:31 -08:00
// 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.
2019-09-08 01:47:12 -07:00
using System.Windows ;
using System.Windows.Controls ;
using System.Windows.Input ;
2019-12-12 13:51:58 -08:00
using FancyZonesEditor.Models ;
2019-09-08 01:47:12 -07:00
namespace FancyZonesEditor
{
/// <summary>
2019-10-08 17:29:43 +02:00
/// Interaction logic for EditorOverlay.xaml
2019-09-08 01:47:12 -07:00
/// </summary>
public partial class EditorOverlay : Window
{
2019-12-12 14:34:25 -08:00
public static EditorOverlay Current { get ; set ; }
2019-12-12 15:07:52 -08:00
2019-12-12 15:00:24 -08:00
private readonly Settings _settings = ( ( App ) Application . Current ) . ZoneSettings ;
private LayoutPreview _layoutPreview ;
private UserControl _editor ;
2019-12-12 14:34:25 -08:00
2019-09-08 01:47:12 -07:00
public Int32Rect [ ] GetZoneRects ( )
{
if ( _editor ! = null )
{
2019-12-12 14:34:25 -08:00
if ( _editor is GridEditor gridEditor )
2019-09-08 01:47:12 -07:00
{
2020-01-22 20:15:35 +01:00
return ZoneRectsFromPanel ( gridEditor . PreviewPanel ) ;
2019-09-08 01:47:12 -07:00
}
else
{
2019-12-12 14:34:25 -08:00
// CanvasEditor
2020-01-22 20:15:35 +01:00
return ZoneRectsFromPanel ( ( ( CanvasEditor ) _editor ) . Preview ) ;
2019-09-08 01:47:12 -07:00
}
}
else
{
2020-01-22 20:15:35 +01:00
// One of the predefined zones (neither grid or canvas editor used).
return _layoutPreview . GetZoneRects ( ) ;
2019-09-08 01:47:12 -07:00
}
2020-01-22 20:15:35 +01:00
}
2019-09-08 01:47:12 -07:00
2020-01-22 20:15:35 +01:00
private Int32Rect [ ] ZoneRectsFromPanel ( Panel previewPanel )
{
// TODO: the ideal here is that the ArrangeRects logic is entirely inside the model, so we don't have to walk the UIElement children to get the rect info
int count = previewPanel . Children . Count ;
2019-09-08 01:47:12 -07:00
Int32Rect [ ] zones = new Int32Rect [ count ] ;
2020-01-22 20:15:35 +01:00
for ( int i = 0 ; i < count ; i + + )
2019-09-08 01:47:12 -07:00
{
2020-01-22 20:15:35 +01:00
FrameworkElement child = ( FrameworkElement ) previewPanel . Children [ i ] ;
2019-12-12 14:50:25 -08:00
Point topLeft = child . TransformToAncestor ( previewPanel ) . Transform ( default ) ;
2019-09-08 01:47:12 -07:00
zones [ i ] . X = ( int ) topLeft . X ;
zones [ i ] . Y = ( int ) topLeft . Y ;
zones [ i ] . Width = ( int ) child . ActualWidth ;
zones [ i ] . Height = ( int ) child . ActualHeight ;
}
return zones ;
}
public EditorOverlay ( )
{
InitializeComponent ( ) ;
2019-09-04 18:26:26 +02:00
Current = this ;
2019-09-08 01:47:12 -07:00
Left = _settings . WorkArea . Left ;
Top = _settings . WorkArea . Top ;
Width = _settings . WorkArea . Width ;
Height = _settings . WorkArea . Height ;
}
2019-12-12 13:59:58 -08:00
private void OnLoaded ( object sender , RoutedEventArgs e )
2019-09-08 01:47:12 -07:00
{
ShowLayoutPicker ( ) ;
}
public void ShowLayoutPicker ( )
{
DataContext = null ;
_editor = null ;
2019-12-12 14:50:25 -08:00
_layoutPreview = new LayoutPreview
{
IsActualSize = true ,
Opacity = 0.5 ,
} ;
2019-09-08 01:47:12 -07:00
Content = _layoutPreview ;
2019-12-12 14:50:25 -08:00
MainWindow window = new MainWindow
{
Owner = this ,
ShowActivated = true ,
Topmost = true ,
} ;
2019-09-08 01:47:12 -07:00
window . Show ( ) ;
2019-12-11 09:41:05 +01:00
// 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 ;
2019-09-08 01:47:12 -07:00
}
// These event handlers are used to track the current state of the Shift and Ctrl keys on the keyboard
// They reflect that current state into properties on the Settings object, which the Zone view will listen to in editing mode
protected override void OnPreviewKeyDown ( KeyEventArgs e )
{
_settings . IsShiftKeyPressed = Keyboard . Modifiers . HasFlag ( ModifierKeys . Shift ) ;
_settings . IsCtrlKeyPressed = Keyboard . Modifiers . HasFlag ( ModifierKeys . Control ) ;
base . OnPreviewKeyDown ( e ) ;
}
protected override void OnPreviewKeyUp ( KeyEventArgs e )
{
_settings . IsShiftKeyPressed = Keyboard . Modifiers . HasFlag ( ModifierKeys . Shift ) ;
_settings . IsCtrlKeyPressed = Keyboard . Modifiers . HasFlag ( ModifierKeys . Control ) ;
base . OnPreviewKeyUp ( e ) ;
}
public void Edit ( )
{
_layoutPreview = null ;
if ( DataContext is GridLayoutModel )
{
_editor = new GridEditor ( ) ;
}
else if ( DataContext is CanvasLayoutModel )
{
_editor = new CanvasEditor ( ) ;
}
2019-12-12 13:26:02 -08:00
2019-09-08 01:47:12 -07:00
Content = _editor ;
2019-09-04 18:26:26 +02:00
}
2019-09-08 01:47:12 -07:00
}
}