Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
WindowSizeModel.cs
1using System;
2using System.Threading.Tasks;
3using System.Windows;
5
7{
11 [Singleton]
13 {
14 private readonly PersistedProperty<WindowState> state;
15 private readonly PersistedProperty<double> width;
16 private readonly PersistedProperty<double> height;
17 private readonly PersistedProperty<double> left;
18 private readonly PersistedProperty<double> top;
19 private readonly PersistedProperty<long> tabIndex;
20 private bool skipNextPosition = false;
21
26 : this(WindowState.Normal, 0, 0, 0, 0, 0)
27 {
28 }
29
38 public WindowSizeModel(WindowState State, double Left, double Top, double Width, double Height, int TabIndex)
39 : base()
40 {
41 this.Add(this.width = new PersistedProperty<double>("MainWindow", nameof(this.Width), true, Width, this));
42 this.Add(this.height = new PersistedProperty<double>("MainWindow", nameof(this.Height), true, Height, this));
43 this.Add(this.left = new PersistedProperty<double>("MainWindow", nameof(this.Left), true, Left, this));
44 this.Add(this.top = new PersistedProperty<double>("MainWindow", nameof(this.Top), true, Top, this));
45 this.Add(this.state = new PersistedProperty<WindowState>("MainWindow", nameof(this.State), true, State, this));
46 this.Add(this.tabIndex = new PersistedProperty<long>("MainWindow", nameof(this.TabIndex), true, TabIndex, this));
47 }
48
52 public double Width
53 {
54 get => this.width.Value;
55 set => this.width.Value = value;
56 }
57
61 public double Height
62 {
63 get => this.height.Value;
64 set => this.height.Value = value;
65 }
66
70 public double Left
71 {
72 get => this.left.Value;
73 set => this.left.Value = value;
74 }
75
79 public double Top
80 {
81 get => this.top.Value;
82 set => this.top.Value = value;
83 }
84
88 public WindowState State
89 {
90 get => this.state.Value;
91 set => this.state.Value = value;
92 }
93
97 public int TabIndex
98 {
99 get => (int)this.tabIndex.Value;
100 set => this.tabIndex.Value = value;
101 }
102
106 public override Task Start()
107 {
109 {
110 this.skipNextPosition = double.IsNaN(MainWindow.currentInstance.Left);
111
112 MainWindow.currentInstance.WindowState = this.State;
113
114 if (this.Left > -25600)
115 MainWindow.currentInstance.Left = this.Left;
116
117 if (this.Top > -25600)
118 MainWindow.currentInstance.Top = this.Top;
119
120 MainWindow.currentInstance.Width = this.Width;
121 MainWindow.currentInstance.Height = this.Height;
122 MainWindow.currentInstance.TabControl.SelectedIndex = this.TabIndex;
123 MainWindow.currentInstance.Visibility = Visibility.Visible;
124
125 MainWindow.currentInstance.SizeChanged += WindowSizeChanged;
126 MainWindow.currentInstance.LocationChanged += WindowLocationChanged;
127 MainWindow.currentInstance.StateChanged += WindowStateChanged;
128 MainWindow.currentInstance.TabControl.SelectionChanged += TabIndexChanged;
129
130 return Task.CompletedTask;
131 });
132
133 return base.Start();
134 }
135
139 public override Task Stop()
140 {
141 MainWindow.currentInstance.SizeChanged -= WindowSizeChanged;
142 MainWindow.currentInstance.LocationChanged -= WindowLocationChanged;
143 MainWindow.currentInstance.StateChanged -= WindowStateChanged;
144 MainWindow.currentInstance.TabControl.SelectionChanged -= TabIndexChanged;
145
146 return base.Stop();
147 }
148
149 private void WindowSizeChanged(object sender, SizeChangedEventArgs e)
150 {
151 if (MainWindow.currentInstance.WindowState == WindowState.Normal)
152 {
153 if (e.WidthChanged && e.NewSize.Width > 0)
154 this.Width = e.NewSize.Width;
155
156 if (e.HeightChanged && e.NewSize.Height > 0)
157 this.Height = e.NewSize.Height;
158 }
159 }
160
161 private void WindowLocationChanged(object sender, EventArgs e)
162 {
163 if (MainWindow.currentInstance.WindowState == WindowState.Normal)
164 {
165 if (this.skipNextPosition)
166 {
167 if (MainWindow.currentInstance.Left != this.Left)
168 MainWindow.currentInstance.Left = this.Left;
169
170 if (MainWindow.currentInstance.Top != this.Top)
171 MainWindow.currentInstance.Top = this.Top;
172
173 this.skipNextPosition = false;
174 }
175 else
176 {
177 if (MainWindow.currentInstance.Left > -25600)
178 this.Left = MainWindow.currentInstance.Left;
179
180 if (MainWindow.currentInstance.Top > -25600)
181 this.Top = MainWindow.currentInstance.Top;
182 }
183 }
184 }
185
186 private void WindowStateChanged(object sender, EventArgs e)
187 {
188 this.State = MainWindow.currentInstance.WindowState;
189 }
190
191 private void TabIndexChanged(object sender, System.Windows.Controls.SelectionChangedEventArgs e)
192 {
193 this.TabIndex = MainWindow.currentInstance.TabControl.SelectedIndex;
194 }
195 }
196}