CPF/CPF.Toolkit/Controls/MdiWindow.cs

478 lines
27 KiB
C#
Raw Normal View History

2023-11-23 23:46:40 +08:00
using CPF;
using CPF.Animation;
using CPF.Charts;
using CPF.Controls;
using CPF.Drawing;
2023-11-24 14:26:26 +08:00
using CPF.Input;
2023-11-23 23:46:40 +08:00
using CPF.Platform;
using CPF.Shapes;
using CPF.Styling;
using CPF.Svg;
2023-11-29 17:07:17 +08:00
using CPF.Toolkit.Dialogs;
2023-11-23 23:46:40 +08:00
using System;
using System.Collections.Generic;
2023-11-24 14:26:26 +08:00
using System.Diagnostics;
2023-11-23 23:46:40 +08:00
using System.Linq;
2023-11-29 17:07:17 +08:00
using System.Reflection;
2023-11-23 23:46:40 +08:00
using System.Security.Cryptography;
using System.Text;
namespace CPF.Toolkit.Controls
{
2023-11-24 14:26:26 +08:00
public class MdiWindow : Control
2023-11-23 23:46:40 +08:00
{
2023-11-29 17:07:17 +08:00
public MdiWindow()
{
this.Init();
}
2023-11-23 23:46:40 +08:00
public WindowState WindowState { get => GetValue<WindowState>(); set => SetValue(value); }
public UIElement Content { get => GetValue<UIElement>(); set => SetValue(value); }
2023-11-29 11:00:06 +08:00
[PropertyMetadata("Title")]
public string Title { get => GetValue<string>(); set => SetValue(value); }
2023-11-24 14:26:26 +08:00
[PropertyMetadata(true)]
public bool MaximizeBox { get { return GetValue<bool>(); } set { SetValue(value); } }
2023-11-29 11:00:06 +08:00
2023-11-24 14:26:26 +08:00
[PropertyMetadata(true)]
public bool MinimizeBox { get { return GetValue<bool>(); } set { SetValue(value); } }
2023-11-29 11:00:06 +08:00
2023-11-24 20:01:57 +08:00
[PropertyMetadata(true)]
public bool CloseBox { get { return GetValue<bool>(); } set { SetValue(value); } }
2023-11-29 11:00:06 +08:00
2023-11-24 20:01:57 +08:00
[UIPropertyMetadata((byte)5, UIPropertyOptions.AffectsMeasure)]
public byte ShadowBlur { get { return GetValue<byte>(); } set { SetValue(value); } }
2023-11-24 14:26:26 +08:00
2023-11-29 17:07:17 +08:00
[PropertyMetadata(true)]
public bool CanResize
{
get => GetValue<bool>();
set
{
SetValue(value);
if (!value) this.MaximizeBox = false;
}
}
2023-11-28 17:31:16 +08:00
public event EventHandler<ClosingEventArgs> Closing;
2023-11-29 17:07:17 +08:00
void Init()
2023-11-23 23:46:40 +08:00
{
2023-11-29 17:07:17 +08:00
this.Focusable = true;
var borderColor = (ViewFill)"152,180,208";
var lostFocusBorderColor = (ViewFill)"214,227,241";
var dragEnabled = new BindingDescribe(this, nameof(WindowState), BindingMode.OneWay, x =>
{
if (this.CanResize)
{
return ((WindowState)x) == WindowState.Normal;
}
return false;
});
2023-11-29 11:00:06 +08:00
this.Size = new SizeField(500, 500);
2023-11-24 20:01:57 +08:00
this.Background = null;
2023-11-24 14:26:26 +08:00
this.MarginLeft = 0;
this.MarginTop = 0;
2023-11-24 17:31:24 +08:00
this.ClipToBounds = true;
2023-11-24 20:01:57 +08:00
this.Children.Add(new Border
2023-11-24 14:26:26 +08:00
{
Size = SizeField.Fill,
2023-11-29 17:07:17 +08:00
Background = "white",
2023-11-24 20:01:57 +08:00
BorderType = BorderType.BorderStroke,
ShadowBlur = ShadowBlur,
ShadowColor = Color.FromRgba(0, 0, 0, 150),
Child = new Decorator
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
Size = SizeField.Fill,
ClipToBounds = true,
Child = new Grid
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
Size = SizeField.Fill,
ColumnDefinitions =
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
new ColumnDefinition{ Width = "auto" },
new ColumnDefinition{ },
new ColumnDefinition{ Width = "auto" },
},
RowDefinitions =
2023-11-24 17:31:24 +08:00
{
2023-11-24 20:01:57 +08:00
new RowDefinition{ Height = "auto" },
new RowDefinition{ Height = 30 },
new RowDefinition{ },
new RowDefinition{ Height = "auto" },
},
Children =
{
new Thumb
2023-11-24 17:31:24 +08:00
{
2023-11-24 20:01:57 +08:00
Name = "top",
Size = "100%,5",
Cursor = Cursors.SizeNorthSouth,
2023-11-29 17:07:17 +08:00
Attacheds = { { Grid.ColumnIndex,1 } },
[nameof(IsEnabled)] = dragEnabled,
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) =>
2023-11-24 17:31:24 +08:00
{
2023-11-29 17:07:17 +08:00
var args = e as DragDeltaEventArgs;
if (this.Height.Value - args.VerticalChange > 0)
2023-11-24 17:31:24 +08:00
{
2023-11-29 17:07:17 +08:00
this.MarginTop += args.VerticalChange;
this.Height -= args.VerticalChange;
2023-11-24 17:31:24 +08:00
}
2023-11-29 17:07:17 +08:00
}),
2023-11-24 20:01:57 +08:00
},
new Thumb
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
Name = "left",
Size = "5,100%",
Cursor = Cursors.SizeWestEast,
2023-11-29 17:07:17 +08:00
Attacheds = { { Grid.RowIndex,1 },{ Grid.RowSpan,2 } },
[nameof(IsEnabled)] = dragEnabled,
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) =>
2023-11-24 14:26:26 +08:00
{
2023-11-29 17:07:17 +08:00
var args = e as DragDeltaEventArgs;
if (this.Width.Value - args.HorizontalChange > 0)
2023-11-24 14:26:26 +08:00
{
2023-11-29 17:07:17 +08:00
this.MarginLeft += args.HorizontalChange;
this.Width -= args.HorizontalChange;
}
}),
},
new Thumb
{
Name = "left_top",
Size = SizeField.Fill,
Cursor = Cursors.TopLeftCorner,
[nameof(IsEnabled)] = dragEnabled,
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) =>
{
var args = e as DragDeltaEventArgs;
if (this.Width.Value - args.HorizontalChange > 0 && this.Height.Value - args.VerticalChange > 0)
{
this.MarginLeft += args.HorizontalChange;
this.MarginTop += args.VerticalChange;
this.Width -= args.HorizontalChange;
this.Height -= args.VerticalChange;
2023-11-24 14:26:26 +08:00
}
2023-11-29 17:07:17 +08:00
}),
2023-11-24 20:01:57 +08:00
},
new Thumb
{
Name = "right",
Size = "5,100%",
Cursor = Cursors.SizeWestEast,
MarginRight = 0,
2023-11-29 17:07:17 +08:00
Attacheds = { { Grid.ColumnIndex,2 },{ Grid.RowIndex,1 },{ Grid.RowSpan,2 } },
[nameof(IsEnabled)] = dragEnabled,
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) => this.Width += (e as DragDeltaEventArgs).HorizontalChange),
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
},
new Thumb
{
Name = "right_top",
Size = SizeField.Fill,
Cursor = Cursors.TopRightCorner,
MarginRight = 0,
Attacheds = { { Grid.ColumnIndex,2 } },
[nameof(IsEnabled)] = dragEnabled,
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) =>
{
var args = e as DragDeltaEventArgs;
if (this.Width.Value - args.HorizontalChange > 0 && this.Height.Value - args.VerticalChange > 0)
{
this.MarginTop += args.VerticalChange;
this.Width += args.HorizontalChange;
this.Height -= args.VerticalChange;
}
}),
2023-11-24 20:01:57 +08:00
},
new Thumb
{
Name = "bottom",
Size = "100%,5",
Cursor = Cursors.SizeNorthSouth,
2023-11-29 17:07:17 +08:00
Attacheds = { { Grid.RowIndex,3 },{ Grid.ColumnIndex,1 } },
[nameof(IsEnabled)] = dragEnabled,
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) => this.Height += (e as DragDeltaEventArgs).VerticalChange),
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
},
new Thumb
{
Name = "left_bottom",
Size = SizeField.Fill,
Cursor = Cursors.BottomLeftCorner,
Attacheds = { { Grid.RowIndex,3 } },
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
[nameof(IsEnabled)] = dragEnabled,
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) =>
{
var args = e as DragDeltaEventArgs;
if (this.Width.Value - args.HorizontalChange > 0 && this.Height.Value + args.VerticalChange > 0)
{
this.MarginLeft += args.HorizontalChange;
this.Width -= args.HorizontalChange;
this.Height += args.VerticalChange;
}
}),
},
new Thumb
{
Name = "right_bottom",
Size = SizeField.Fill,
Cursor = Cursors.BottomRightCorner,
Attacheds = { { Grid.RowIndex,3 },{ Grid.ColumnIndex,2 } },
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor),
[nameof(IsEnabled)] = dragEnabled,
[nameof(Thumb.DragDelta)] = new CommandDescribe((s,e) =>
{
var args = e as DragDeltaEventArgs;
if (this.Height.Value + args.VerticalChange > 0 && this.Width.Value + args.HorizontalChange > 0)
{
this.Width += args.HorizontalChange;
this.Height += args.VerticalChange;
}
}),
2023-11-24 20:01:57 +08:00
},
new Thumb
{
Name = "caption",
Attacheds = { { Grid.RowIndex,1 },{ Grid.ColumnIndex,1 } },
Size = SizeField.Fill,
Child = new Panel
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
Size = SizeField.Fill,
2023-11-24 14:26:26 +08:00
Children =
{
2023-11-24 20:01:57 +08:00
new StackPanel
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
Orientation = Orientation.Horizontal,
MarginLeft = 0,
Children =
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
new TextBlock
{
[nameof(TextBlock.Text)] = new BindingDescribe(this,nameof(this.Title),BindingMode.OneWay),
FontSize = 14,
MarginLeft = 10,
},
2023-11-24 14:26:26 +08:00
}
},
2023-11-24 20:01:57 +08:00
new StackPanel
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
Orientation = Orientation.Horizontal,
MarginRight = 0,
2023-11-24 14:26:26 +08:00
Height = "100%",
Children =
{
new SystemButton
{
2023-11-24 20:01:57 +08:00
Name = "min",
2023-11-24 14:26:26 +08:00
Size = new SizeField(30,"100%"),
2023-11-24 20:01:57 +08:00
Content = new Line
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
MarginLeft = "auto",
StartPoint = new Point(1,13),
EndPoint = new Point(14,13),
2023-11-24 14:26:26 +08:00
StrokeStyle = "2",
2023-11-24 20:01:57 +08:00
IsAntiAlias = true,
StrokeFill = "black"
2023-11-24 14:26:26 +08:00
},
2023-11-29 17:07:17 +08:00
[nameof(Visibility)] = new BindingDescribe(this,nameof(MinimizeBox),BindingMode.OneWay,x=>(bool)x?Visibility.Visible: Visibility.Collapsed),
[nameof(Button.Click)] = new CommandDescribe((s,e) => this.WindowState = WindowState.Minimized)
2023-11-24 20:01:57 +08:00
},
new Panel
{
Height = "100%",
2023-11-24 22:58:59 +08:00
[nameof(Visibility)] = new BindingDescribe(this,nameof(MaximizeBox),BindingMode.OneWay,a => (bool)a ? Visibility.Visible : Visibility.Collapsed),
2023-11-24 20:01:57 +08:00
Children =
{
new SystemButton
{
Name = "max",
Size = new SizeField(30,"100%"),
Content = new Rectangle
{
Size = new SizeField(14,12),
MarginTop = 5,
StrokeStyle = "2",
},
2023-11-29 17:07:17 +08:00
[nameof(Button.Click)] = new CommandDescribe((s, e) => this.WindowState = WindowState.Maximized),
[nameof(Visibility)] =
new BindingDescribe(
this,
nameof(WindowState),
BindingMode.OneWay,
x => ((WindowState)x).Or(WindowState.Maximized,WindowState.FullScreen) ? Visibility.Collapsed : Visibility.Visible),
2023-11-24 20:01:57 +08:00
},
new SystemButton
{
Name = "nor",
Visibility = Visibility.Collapsed,
Size = new SizeField(30,"100%"),
Content = new Panel
{
Size = SizeField.Fill,
Children =
{
new Rectangle
{
MarginTop = 10,
MarginLeft =8,
Size = new SizeField(11,8),
StrokeStyle = "1.5",
},
new Polyline
{
MarginTop =5,
MarginLeft = 12,
Points =
{
new Point(0,3),
new Point(0,0),
new Point(9,0),
new Point(9,7),
new Point(6,7)
},
StrokeStyle = "2"
}
}
},
2023-11-29 17:07:17 +08:00
[nameof(Button.Click)] = new CommandDescribe((s, e) => this.WindowState = WindowState.Normal),
[nameof(Visibility)] =
new BindingDescribe(
this,
nameof(WindowState),
BindingMode.OneWay,
x => ((WindowState)x).Or(WindowState.Normal,WindowState.Minimized)? Visibility.Collapsed : Visibility.Visible)
2023-11-24 20:01:57 +08:00
}
2023-11-24 14:26:26 +08:00
}
},
new SystemButton
{
2023-11-24 20:01:57 +08:00
Name = "close",
2023-11-24 14:26:26 +08:00
Size = new SizeField(30,"100%"),
Content = new Panel
{
Size = SizeField.Fill,
Children =
{
2023-11-24 20:01:57 +08:00
new Line
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
MarginTop=4,
MarginLeft=8,
StartPoint = new Point(1, 1),
EndPoint = new Point(14, 13),
StrokeStyle = "2",
IsAntiAlias=true,
2023-11-24 14:26:26 +08:00
},
2023-11-24 20:01:57 +08:00
new Line
2023-11-24 14:26:26 +08:00
{
2023-11-24 20:01:57 +08:00
MarginTop=4,
MarginLeft=8,
StartPoint = new Point(14, 1),
EndPoint = new Point(1, 13),
StrokeStyle = "2",
IsAntiAlias=true,
2023-11-24 14:26:26 +08:00
}
}
},
2023-11-29 17:07:17 +08:00
[nameof(Button.Click)] = new CommandDescribe((ss,ee) => this.Close()),
[nameof(Visibility)] = new BindingDescribe(this,nameof(this.CloseBox),BindingMode.OneWay,x=>(bool)x?Visibility.Visible: Visibility.Collapsed)
2023-11-24 14:26:26 +08:00
}
}
},
2023-11-24 20:01:57 +08:00
},
},
2023-11-29 17:07:17 +08:00
[nameof(Thumb.DragDelta)] = new CommandDescribe((ss,ee)=>
2023-11-24 20:01:57 +08:00
{
2023-11-29 17:07:17 +08:00
if (this.WindowState.Or(WindowState.Normal))
2023-11-24 20:01:57 +08:00
{
2023-11-29 17:07:17 +08:00
var arge = ee as DragDeltaEventArgs;
this.MarginLeft += arge.HorizontalChange;
this.MarginTop += arge.VerticalChange;
}
}),
[nameof(DoubleClick)] = new CommandDescribe((ss,ee) => this.Delay(TimeSpan.FromMilliseconds(150),()=>
{
if(!this.MaximizeBox) return;
if (this.WindowState.Or(WindowState.Maximized,WindowState.Minimized))
2023-11-24 20:01:57 +08:00
{
2023-11-29 17:07:17 +08:00
this.WindowState = WindowState.Normal;
2023-11-24 14:26:26 +08:00
}
2023-11-29 17:07:17 +08:00
else if (this.WindowState == WindowState.Normal)
{
this.WindowState = WindowState.Maximized;
}
})),
[nameof(Background)] = new BindingDescribe(this,nameof(IsFocused),BindingMode.OneWay ,x => ((bool)x) ? borderColor : lostFocusBorderColor)
2023-11-24 14:26:26 +08:00
},
2023-11-24 20:01:57 +08:00
new Decorator
2023-11-24 17:31:24 +08:00
{
2023-11-24 20:01:57 +08:00
Attacheds = { { Grid.RowIndex,2 } ,{ Grid.ColumnIndex,1 } },
Size = SizeField.Fill,
2023-11-29 17:07:17 +08:00
[nameof(Decorator.Child)] = new BindingDescribe(this,nameof(Content))
2023-11-24 17:31:24 +08:00
},
2023-11-24 20:01:57 +08:00
}
},
},
2023-11-29 17:07:17 +08:00
[nameof(ShadowBlur)] = new BindingDescribe(this, nameof(WindowState), BindingMode.OneWay, x => ((WindowState)x).Or(WindowState.Maximized, WindowState.FullScreen) ? 0 : ShadowBlur),
[nameof(ShadowBlur)] = new BindingDescribe(this, nameof(IsFocused), BindingMode.OneWay, x => ((bool)x) ? ShadowBlur : 0),
[nameof(BorderStroke)] = new BindingDescribe(this, nameof(IsFocused), BindingMode.OneWay, x => ((bool)x) ? "0" : "1"),
[nameof(BorderFill)] = new BindingDescribe(this, nameof(IsFocused), BindingMode.OneWay, x => ((bool)x) ? null : "0,0,0,100"),
2023-11-24 14:26:26 +08:00
});
2023-11-29 17:07:17 +08:00
}
2023-11-24 20:01:57 +08:00
2023-11-29 17:07:17 +08:00
protected override void OnPropertyChanged(string propertyName, object oldValue, object newValue, PropertyMetadataAttribute propertyMetadata)
{
if (propertyName == nameof(DataContext) && newValue != null)
{
if (newValue is IClosable closable)
{
closable.Closable -= Closable_Closable;
closable.Closable += Closable_Closable;
}
if (newValue is ILoading loading)
{
loading.CreateLoading(this);
}
}
else if (propertyName == nameof(Content) && newValue != null)
{
this.Content.Margin = "0";
this.Content.ClipToBounds = true;
}
base.OnPropertyChanged(propertyName, oldValue, newValue, propertyMetadata);
}
private void Closable_Closable(object sender, ClosingEventArgs e)
{
this.DoClose(sender, e);
}
public void Close()
{
if (this.Closing != null)
{
this.DoClose(this, new ClosingEventArgs());
}
else
{
this.Dispose();
}
2023-11-23 23:46:40 +08:00
}
2023-11-29 17:07:17 +08:00
void DoClose(object sender, ClosingEventArgs e)
2023-11-28 17:31:16 +08:00
{
2023-11-29 17:07:17 +08:00
if (this.DataContext is IClosable closable)
{
closable.OnClosable(sender, e);
}
this.Closing.Invoke(sender, e);
2023-11-28 17:31:16 +08:00
}
2023-11-23 23:46:40 +08:00
}
}