forked from changbowen/DesktopNote
-
Notifications
You must be signed in to change notification settings - Fork 0
/
RoundedWindow.cs
342 lines (300 loc) · 13.1 KB
/
RoundedWindow.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
//using System;
//using System.Collections.Generic;
//using System.Linq;
//using System.Text;
using System.Threading.Tasks;
using System;
using System.Globalization;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.ComponentModel;
namespace DesktopNote
{
public class RectConverter : IMultiValueConverter
{
public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture)
{
//parameter is the clipped width to preserve
var offset = 0d; var thickness = 20d;
if (parameter is string paraStr) {
var paraAry = paraStr.Split(',', ' ');
if (paraAry.Length > 0) offset = double.Parse(paraAry[0]);
if (paraAry.Length > 1) thickness = double.Parse(paraAry[1]);
}
var width = (double)values[0];
var height = (double)values[1];
return new Rect(new Point(-offset, -offset), new Point(width + offset - thickness, height + offset - thickness));//20是两边空隙总和
}
public object[] ConvertBack(object value, Type[] targetTypes, object parameter, CultureInfo culture)
{
throw new NotImplementedException();
}
}
public class RoundedWindow : Window
{
public enum CloseBehaviors { Close, Hide, FadeOutAndHide, FadeOutAndClose }
#region Properties
/// <summary>
/// Get or set the background of the rounded rectangle.
/// </summary>
public new Brush Background
{
get { return (Brush)GetValue(BackgroundProperty); }
set { SetValue(BackgroundProperty, value); }
}
public static readonly new DependencyProperty BackgroundProperty =
DependencyProperty.Register("Background", typeof(Brush), typeof(RoundedWindow), new PropertyMetadata(new SolidColorBrush(Color.FromRgb(255, 247, 197))));
public bool ButtonCloseVisible
{
get { return (bool)GetValue(ButtonCloseVisibleProperty); }
set { SetValue(ButtonCloseVisibleProperty, value); }
}
public static readonly DependencyProperty ButtonCloseVisibleProperty =
DependencyProperty.Register("ButtonCloseVisible", typeof(bool), typeof(RoundedWindow), new PropertyMetadata(true));
public bool ButtonMaxVisible
{
get { return (bool)GetValue(ButtonMaxVisibleProperty); }
set { SetValue(ButtonMaxVisibleProperty, value); }
}
public static readonly DependencyProperty ButtonMaxVisibleProperty =
DependencyProperty.Register("ButtonMaxVisible", typeof(bool), typeof(RoundedWindow), new PropertyMetadata(false));
public bool ButtonMinVisible
{
get { return (bool)GetValue(ButtonMinVisibleProperty); }
set { SetValue(ButtonMinVisibleProperty, value); }
}
public static readonly DependencyProperty ButtonMinVisibleProperty =
DependencyProperty.Register("ButtonMinVisible", typeof(bool), typeof(RoundedWindow), new PropertyMetadata(false));
public CloseBehaviors CloseBehavior
{
get { return (CloseBehaviors)GetValue(CloseBehaviorProperty); }
set { SetValue(CloseBehaviorProperty, value); }
}
public static readonly DependencyProperty CloseBehaviorProperty =
DependencyProperty.Register("CloseBehavior", typeof(CloseBehaviors), typeof(RoundedWindow), new PropertyMetadata(CloseBehaviors.FadeOutAndClose));
/// <summary>
/// If set to false, keyboard focus will be set to the owner when the window gets focus.
/// </summary>
public new bool Focusable
{
get { return (bool)GetValue(FocusableProperty); }
set { SetValue(FocusableProperty, value); }
}
public static readonly new DependencyProperty FocusableProperty =
DependencyProperty.Register("Focusable", typeof(bool), typeof(RoundedWindow), new PropertyMetadata(true));
/// <summary>
/// This maps to the RenderTransformOrigin property of BackgroundGrid.
/// </summary>
public Point RenderTransformOrigin_BG
{
get { return (Point)GetValue(RenderTransformOrigin_BGProperty); }
set { SetValue(RenderTransformOrigin_BGProperty, value); }
}
public static readonly DependencyProperty RenderTransformOrigin_BGProperty =
DependencyProperty.Register("RenderTransformOrigin_BG", typeof(Point), typeof(RoundedWindow), new PropertyMetadata(new Point(0d, 0d)));
public double CornerRadius
{
get { return (double)GetValue(CornerRadiusProperty); }
set { SetValue(CornerRadiusProperty, value); }
}
public static readonly DependencyProperty CornerRadiusProperty =
DependencyProperty.Register("CornerRadius", typeof(double), typeof(RoundedWindow), new PropertyMetadata(8d));
#endregion
static RoundedWindow()
{
DefaultStyleKeyProperty.OverrideMetadata(typeof(RoundedWindow), new FrameworkPropertyMetadata(typeof(RoundedWindow)));
}
public static readonly RoutedEvent FadingInEvent = EventManager.RegisterRoutedEvent("FadingIn", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RoundedWindow));
public event RoutedEventHandler FadingIn
{
add
{
AddHandler(FadingInEvent, value);
}
remove
{
RemoveHandler(FadingInEvent, value);
}
}
public static readonly RoutedEvent FadingOutEvent = EventManager.RegisterRoutedEvent("FadingOut", RoutingStrategy.Bubble, typeof(RoutedEventHandler), typeof(RoundedWindow));
public event RoutedEventHandler FadingOut
{
add
{
AddHandler(FadingOutEvent, value);
}
remove
{
RemoveHandler(FadingOutEvent, value);
}
}
public RoundedWindow()
{
AllowsTransparency = true;
WindowStyle = WindowStyle.None;
base.Background = null;
}
/// <summary>
/// This is a grid on which the scaling animations are applied.
/// </summary>
internal Grid BackgroundGrid { get; set; }
public override void OnApplyTemplate()
{
base.OnApplyTemplate();
BackgroundGrid = (Grid)GetTemplateChild("Grid_Main");
Button minimizeButton = GetTemplateChild("minimizeButton") as Button;
if (minimizeButton != null) minimizeButton.Click += MinimizeClick;
Button restoreButton = GetTemplateChild("restoreButton") as Button;
if (restoreButton != null) restoreButton.Click += RestoreClick;
Button closeButton = GetTemplateChild("closeButton") as Button;
if (closeButton != null) closeButton.Click += CloseClick;
if (!Focusable && Owner != null)
{
PreviewGotKeyboardFocus += (object sender, KeyboardFocusChangedEventArgs e) =>
{
e.Handled = true;
var o = sender as Window;
if (o != null && o != this) o.Focus();
};
}
}
#region Click Events
protected void MinimizeClick(object sender, RoutedEventArgs e)
{
WindowState = WindowState.Minimized;
}
protected void RestoreClick(object sender, RoutedEventArgs e)
{
WindowState = (WindowState == WindowState.Normal) ? WindowState.Maximized : WindowState.Normal;
}
protected void CloseClick(object sender, RoutedEventArgs e)
{
//make sure the scale animation ends at the top right corner when close is clicked.
RenderTransformOrigin_BG = new Point(1d, 0d);
Close();
}
#endregion
protected override void OnClosing(CancelEventArgs e)
{
base.OnClosing(e);
switch (CloseBehavior)
{
case CloseBehaviors.Close:
break;
case CloseBehaviors.FadeOutAndClose:
e.Cancel = true;
FadeOut(true);
break;
case CloseBehaviors.FadeOutAndHide:
e.Cancel = true;
FadeOut();
break;
case CloseBehaviors.Hide:
e.Cancel = true;
Hide();
break;
}
}
/// <summary>
/// Show the window without fading animation.
/// </summary>
public new void Show()
{
base.Show();
//ensure the contents are visible when not using fading animation.
BackgroundGrid.RenderTransform = new ScaleTransform(1d, 1d);
Opacity = 1;
}
/// <summary>
/// Fade in the window at mouse position. Or the specified Left and Top position.
/// If the window is already faded in, move the window to the new position. In this case the FadingInEvent will not be raised.
/// </summary>
public void FadeIn(double left = double.NaN, double top = double.NaN)
{
if (!IsLoaded) base.Show();//how to call something similar to initializecomponent?
//compute mouse position or set to existing values
Point newpos, realpos;
double currscrnW = App.CurrScrnRect.Right;
double currscrnH = App.CurrScrnRect.Bottom;
if (left.Equals(double.NaN) || top.Equals(double.NaN))//nan==nan returns false.
{
//get the physical pixel-based position.
newpos = PointToScreen(Mouse.GetPosition(this));
//convert to the actual position considering the DPI settings etc.
realpos = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice.Transform(newpos);
//make sure the window is displayed inside the screens.
double originX, originY;
if (currscrnW - realpos.X > ActualWidth)
originX = 0d;
else
{
originX = 1d;
realpos.X -= ActualWidth;
}
if (currscrnH - realpos.Y > ActualHeight)
originY = 0d;
else
{
originY = 1d;
realpos.Y -= ActualHeight;
}
RenderTransformOrigin_BG = new Point(originX, originY);
}
else
{
//make sure the window is displayed inside the screens.
if (left < 0d) left = 0d;
if (top < 0d) top = 0d;
if (left + ActualWidth > currscrnW)
left = currscrnW - ActualWidth;
if (top + ActualHeight > currscrnH)
top = currscrnH - ActualHeight;
realpos = new Point(left, top);
}
if (Opacity == 0)
{
Left = realpos.X;
Top = realpos.Y;
base.Show();
RaiseEvent(new RoutedEventArgs(FadingInEvent));
}
else
{
//move window to the new cursor location
var easefunc = new CubicEase() { EasingMode = EasingMode.EaseInOut };
var anim_move_x = new DoubleAnimation(realpos.X, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.Stop) { EasingFunction = easefunc };
var anim_move_y = new DoubleAnimation(realpos.Y, new Duration(new TimeSpan(0, 0, 0, 0, 300)), FillBehavior.Stop) { EasingFunction = easefunc };
BeginAnimation(LeftProperty, anim_move_x);
BeginAnimation(TopProperty, anim_move_y);
}
}
/// <summary>
/// Fade out the window. This ignores the CloseBehavior setting.
/// </summary>
/// <param name="closeafterfade">Set to true to close the window after. Otherwise it is only hidden.</param>
public async Task FadeOut(bool closeafterfade = false)
{
if (IsLoaded)//without this it will crash at the below line.
{
RaiseEvent(new RoutedEventArgs(FadingOutEvent));
//need to be longer than the fading animation otherwise the window will flash when Show() is called.
await Task.Run(() => System.Threading.Thread.Sleep(250));
if (closeafterfade)
{
CloseBehavior = CloseBehaviors.Close;
Close();
}
else Hide();
}
}
protected override void OnMouseDown(MouseButtonEventArgs e)
{
base.OnMouseDown(e);
if (e.ChangedButton == MouseButton.Left && e.ButtonState == MouseButtonState.Pressed) DragMove();
}
}
}