-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWindow.xaml.cs
121 lines (101 loc) · 4.14 KB
/
MainWindow.xaml.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace Rule
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window, INotifyPropertyChanged
{
#region Property Change Notification
public event PropertyChangedEventHandler PropertyChanged;
private void OnPropertyChanged( string propertyName )
{
var handler = PropertyChanged;
if( handler != null ) handler( this, new PropertyChangedEventArgs( propertyName ) );
}
#endregion // Property Change Notification
private Orientation _orientation = Orientation.Horizontal;
private int _mousePosition;
public Orientation Orientation
{
get { return _orientation; }
set { if( value != _orientation ) { _orientation = value; OnPropertyChanged( "Orientation" ); } }
}
public int MousePosition
{
get { return _mousePosition; }
set { _mousePosition = value; OnPropertyChanged( "MousePosition" ); }
}
public MainWindow()
{
InitializeComponent();
needle.PropertyChanged += ( s, e ) => { if( e.PropertyName.Equals( "MousePosition" ) ) this.MousePosition = needle.MousePosition; };
var props = Properties.Settings.Default;
var rect = props.WindowRect;
if( rect.Size.Width > 0 && rect.Size.Height > 0 )
{
this.Top = rect.Top;
this.Left = rect.Left;
this.Width = rect.Width;
this.Height = rect.Height;
}
Orientation = props.Orientation;
this.DataContext = this;
this.Closing += ( s, e ) =>
{
props.WindowRect = new Rect( this.Left, this.Top, this.Width, this.Height );
props.Orientation = Orientation;
props.Save();
};
}
private void Grid_MouseDown( object sender, MouseButtonEventArgs e )
{
if( e.LeftButton == MouseButtonState.Pressed ) this.DragMove();
}
private void ToggleOrientation( object sender, ExecutedRoutedEventArgs e )
{
Orientation = Orientation == Orientation.Horizontal ? Orientation.Vertical : Orientation.Horizontal;
FlipWindowDimensions();
}
private void FlipWindowDimensions()
{
var tmp = this.Width;
this.Width = this.Height;
this.Height = tmp;
var adj = ( Math.Max( this.Width, this.Height ) / 2 - Math.Min( this.Width, this.Height ) / 2 ) *
(Orientation == Orientation.Vertical ? 1 : -1 );
this.Top -= adj;
this.Left += adj;
if( this.Top + this.Height > SystemParameters.VirtualScreenTop + SystemParameters.VirtualScreenHeight )
this.Top = SystemParameters.VirtualScreenTop + SystemParameters.VirtualScreenHeight - this.Height;
if( this.Left + this.Width > SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth )
this.Left = SystemParameters.VirtualScreenLeft + SystemParameters.VirtualScreenWidth - this.Width;
this.Top = Math.Max( this.Top, SystemParameters.VirtualScreenTop );
this.Left = Math.Max( this.Left, SystemParameters.VirtualScreenLeft );
}
private void Quit( object sender, ExecutedRoutedEventArgs e )
{
this.Close();
}
private void Grid_PreviewMouseMove( object sender, MouseEventArgs e )
{
var pos = e.GetPosition( this );
tooltip.HorizontalOffset = pos.X;
tooltip.VerticalOffset = pos.Y - 24;
}
}
}