Skip to content

Commit 3380cb2

Browse files
authoredSep 27, 2020
Merge pull request #810 from BDisp/pos-dim-validation
Fixes #731. Pos and Dim validation.
2 parents 0aabf41 + 0aa4739 commit 3380cb2

File tree

4 files changed

+205
-1
lines changed

4 files changed

+205
-1
lines changed
 

‎Terminal.Gui/Core/View.cs

+29
Original file line numberDiff line numberDiff line change
@@ -434,6 +434,10 @@ public Rect Bounds {
434434
public Pos X {
435435
get => x;
436436
set {
437+
if (!ValidatePosDim (x, value)) {
438+
throw new ArgumentException ();
439+
}
440+
437441
x = value;
438442
SetNeedsLayout ();
439443
SetNeedsDisplay (frame);
@@ -450,6 +454,10 @@ public Pos X {
450454
public Pos Y {
451455
get => y;
452456
set {
457+
if (!ValidatePosDim (y, value)) {
458+
throw new ArgumentException ();
459+
}
460+
453461
y = value;
454462
SetNeedsLayout ();
455463
SetNeedsDisplay (frame);
@@ -468,6 +476,10 @@ public Pos Y {
468476
public Dim Width {
469477
get => width;
470478
set {
479+
if (!ValidatePosDim (width, value)) {
480+
throw new ArgumentException ();
481+
}
482+
471483
width = value;
472484
SetNeedsLayout ();
473485
SetNeedsDisplay (frame);
@@ -482,12 +494,29 @@ public Dim Width {
482494
public Dim Height {
483495
get => height;
484496
set {
497+
if (!ValidatePosDim (height, value)) {
498+
throw new ArgumentException ();
499+
}
500+
485501
height = value;
486502
SetNeedsLayout ();
487503
SetNeedsDisplay (frame);
488504
}
489505
}
490506

507+
bool ValidatePosDim (object oldvalue, object newValue)
508+
{
509+
if (!IsInitialized || layoutStyle == LayoutStyle.Absolute || oldvalue == null || oldvalue.GetType () == newValue.GetType () || this is Toplevel) {
510+
return true;
511+
}
512+
if (layoutStyle == LayoutStyle.Computed) {
513+
if (oldvalue.GetType () != newValue.GetType () && !(newValue is Pos.PosAbsolute || newValue is Dim.DimAbsolute)) {
514+
return true;
515+
}
516+
}
517+
return false;
518+
}
519+
491520
/// <summary>
492521
/// Returns the container for this view, or null if this view has not been added to a container.
493522
/// </summary>

‎Terminal.Gui/Views/StatusBar.cs

+6-1
Original file line numberDiff line numberDiff line change
@@ -88,13 +88,18 @@ public StatusBar (StatusItem [] items) : base ()
8888
CanFocus = false;
8989
ColorScheme = Colors.Menu;
9090
X = 0;
91-
Y = SuperView != null ? SuperView.Frame.Height - 1 : Pos.AnchorEnd (1);
9291
Width = Dim.Fill ();
9392
Height = 1;
9493

94+
Initialized += StatusBar_Initialized;
9595
Application.Resized += Application_Resized ();
9696
}
9797

98+
private void StatusBar_Initialized (object sender, EventArgs e)
99+
{
100+
Y = SuperView.Frame.Height - 1;
101+
}
102+
98103
private Action<Application.ResizedEventArgs> Application_Resized ()
99104
{
100105
return delegate {

‎UnitTests/DimTests.cs

+85
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,91 @@ public void Percent_ThrowsOnIvalid ()
233233
Assert.Throws<ArgumentException> (() => dim = Dim.Percent (1000001));
234234
}
235235

236+
[Fact]
237+
public void Dim_Validation_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type ()
238+
{
239+
Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
240+
241+
var t = Application.Top;
242+
243+
var w = new Window ("w") {
244+
Width = Dim.Fill (0),
245+
Height = Dim.Sized (10)
246+
};
247+
var v = new View ("v") {
248+
Width = Dim.Width (w) - 2,
249+
Height = Dim.Percent (10)
250+
};
251+
252+
w.Add (v);
253+
t.Add (w);
254+
255+
t.Ready += () => {
256+
Assert.Equal (2, w.Width = 2);
257+
Assert.Equal (2, w.Height = 2);
258+
Assert.Throws<ArgumentException> (() => v.Width = 2);
259+
Assert.Throws<ArgumentException> (() => v.Height = 2);
260+
};
261+
262+
Application.Iteration += () => Application.RequestStop ();
263+
264+
Application.Run ();
265+
Application.Shutdown ();
266+
}
267+
268+
[Fact]
269+
public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Null ()
270+
{
271+
Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
272+
273+
var t = Application.Top;
274+
275+
var w = new Window (new Rect (1, 2, 4, 5), "w");
276+
t.Add (w);
277+
278+
t.Ready += () => {
279+
Assert.Equal (3, w.Width = 3);
280+
Assert.Equal (4, w.Height = 4);
281+
};
282+
283+
Application.Iteration += () => Application.RequestStop ();
284+
285+
Application.Run ();
286+
Application.Shutdown ();
287+
}
288+
289+
[Fact]
290+
public void Dim_Validation_Do_Not_Throws_If_NewValue_Is_DimAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
291+
{
292+
Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
293+
294+
var t = Application.Top;
295+
296+
var w = new Window ("w") {
297+
Width = Dim.Fill (0),
298+
Height = Dim.Sized (10)
299+
};
300+
var v = new View ("v") {
301+
Width = Dim.Width (w) - 2,
302+
Height = Dim.Percent (10)
303+
};
304+
305+
w.Add (v);
306+
t.Add (w);
307+
308+
t.Ready += () => {
309+
v.LayoutStyle = LayoutStyle.Absolute;
310+
Assert.Equal (2, v.Width = 2);
311+
Assert.Equal (2, v.Height = 2);
312+
};
313+
314+
Application.Iteration += () => Application.RequestStop ();
315+
316+
Application.Run ();
317+
Application.Shutdown ();
318+
}
319+
320+
236321
// TODO: Test operators
237322
}
238323
}

‎UnitTests/PosTests.cs

+85
Original file line numberDiff line numberDiff line change
@@ -371,6 +371,91 @@ public void Percent_ThrowsOnIvalid ()
371371
Assert.Throws<ArgumentException> (() => pos = Pos.Percent (1000001));
372372
}
373373

374+
[Fact]
375+
public void Pos_Validation_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type ()
376+
{
377+
Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
378+
379+
var t = Application.Top;
380+
381+
var w = new Window ("w") {
382+
X = Pos.Left (t) + 2,
383+
Y = Pos.At (2)
384+
};
385+
var v = new View ("v") {
386+
X = Pos.Center (),
387+
Y = Pos.Percent (10)
388+
};
389+
390+
w.Add (v);
391+
t.Add (w);
392+
393+
t.Ready += () => {
394+
Assert.Equal (2, w.X = 2);
395+
Assert.Equal (2, w.Y = 2);
396+
Assert.Throws<ArgumentException> (() => v.X = 2);
397+
Assert.Throws<ArgumentException> (() => v.Y = 2);
398+
};
399+
400+
Application.Iteration += () => Application.RequestStop ();
401+
402+
Application.Run ();
403+
Application.Shutdown ();
404+
}
405+
406+
[Fact]
407+
public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Null ()
408+
{
409+
Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
410+
411+
var t = Application.Top;
412+
413+
var w = new Window (new Rect (1, 2, 4, 5), "w");
414+
t.Add (w);
415+
416+
t.Ready += () => {
417+
Assert.Equal (2, w.X = 2);
418+
Assert.Equal (2, w.Y = 2);
419+
};
420+
421+
Application.Iteration += () => Application.RequestStop ();
422+
423+
Application.Run ();
424+
Application.Shutdown ();
425+
426+
}
427+
428+
[Fact]
429+
public void Pos_Validation_Do_Not_Throws_If_NewValue_Is_PosAbsolute_And_OldValue_Is_Another_Type_After_Sets_To_LayoutStyle_Absolute ()
430+
{
431+
Application.Init (new FakeDriver (), new NetMainLoop (() => FakeConsole.ReadKey (true)));
432+
433+
var t = Application.Top;
434+
435+
var w = new Window ("w") {
436+
X = Pos.Left (t) + 2,
437+
Y = Pos.At (2)
438+
};
439+
var v = new View ("v") {
440+
X = Pos.Center (),
441+
Y = Pos.Percent (10)
442+
};
443+
444+
w.Add (v);
445+
t.Add (w);
446+
447+
t.Ready += () => {
448+
v.LayoutStyle = LayoutStyle.Absolute;
449+
Assert.Equal (2, v.X = 2);
450+
Assert.Equal (2, v.Y = 2);
451+
};
452+
453+
Application.Iteration += () => Application.RequestStop ();
454+
455+
Application.Run ();
456+
Application.Shutdown ();
457+
}
458+
374459
// TODO: Test PosCombine
375460

376461

0 commit comments

Comments
 (0)
Please sign in to comment.