static int QueryFull(bool useErrorColors, int width, int height, ustring title, ustring message, params ustring[] buttons) { const int defaultWidth = 50; int textWidth = TextFormatter.MaxWidth(message, width == 0 ? defaultWidth : width); int textHeight = TextFormatter.MaxLines(message, textWidth); // message.Count (ustring.Make ('\n')) + 1; int msgboxHeight = Math.Max(1, textHeight) + 3; // textHeight + (top + top padding + buttons + bottom) // Create button array for Dialog int count = 0; List <Button> buttonList = new List <Button>(); foreach (var s in buttons) { var b = new Button(s); if (count == 0) { b.IsDefault = true; } buttonList.Add(b); count++; } // Create Dialog (retain backwards compat by supporting specifying height/width) Dialog d; if (width == 0 & height == 0) { d = new Dialog(title, buttonList.ToArray()); d.Height = msgboxHeight; } else { d = new Dialog(title, Math.Max(width, textWidth) + 4, height, buttonList.ToArray()); } if (useErrorColors) { d.ColorScheme = Colors.Error; } if (message != null) { var l = new Label(textWidth > width ? 0 : (width - 4 - textWidth) / 2, 1, message); l.LayoutStyle = LayoutStyle.Computed; l.TextAlignment = TextAlignment.Centered; l.X = Pos.Center(); l.Y = Pos.Center(); l.Width = Dim.Fill(2); l.Height = Dim.Fill(1); d.Add(l); } // Dynamically size Width int msgboxWidth = Math.Max(defaultWidth, Math.Max(title.RuneCount + 8, Math.Max(textWidth + 4, d.GetButtonsWidth()) + 8)); // textWidth + (left + padding + padding + right) d.Width = msgboxWidth; // Setup actions int clicked = -1; for (int n = 0; n < buttonList.Count; n++) { int buttonId = n; buttonList[n].Clicked += () => { clicked = buttonId; Application.RequestStop(); }; } // Rin the modal; do not shutdown the mainloop driver when done Application.Run(d); return(clicked); }
public void Initialized_Event_Comparing_With_Added_Event() { Application.Init(new FakeDriver(), new FakeMainLoop(() => FakeConsole.ReadKey(true))); var t = new Toplevel() { Id = "0", }; var w = new Window() { Id = "t", Width = Dim.Fill(), Height = Dim.Fill() }; var v1 = new View() { Id = "v1", Width = Dim.Fill(), Height = Dim.Fill() }; var v2 = new View() { Id = "v2", Width = Dim.Fill(), Height = Dim.Fill() }; var sv1 = new View() { Id = "sv1", Width = Dim.Fill(), Height = Dim.Fill() }; int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0; w.Added += (e) => { Assert.Equal(e.Frame.Width, w.Frame.Width); Assert.Equal(e.Frame.Height, w.Frame.Height); }; v1.Added += (e) => { Assert.Equal(e.Frame.Width, v1.Frame.Width); Assert.Equal(e.Frame.Height, v1.Frame.Height); }; v2.Added += (e) => { Assert.Equal(e.Frame.Width, v2.Frame.Width); Assert.Equal(e.Frame.Height, v2.Frame.Height); }; sv1.Added += (e) => { Assert.Equal(e.Frame.Width, sv1.Frame.Width); Assert.Equal(e.Frame.Height, sv1.Frame.Height); }; t.Initialized += (s, e) => { tc++; Assert.Equal(1, tc); Assert.Equal(1, wc); Assert.Equal(1, v1c); Assert.Equal(1, v2c); Assert.Equal(1, sv1c); Assert.True(t.CanFocus); Assert.True(w.CanFocus); Assert.False(v1.CanFocus); Assert.False(v2.CanFocus); Assert.False(sv1.CanFocus); Application.Refresh(); }; w.Initialized += (s, e) => { wc++; Assert.Equal(t.Frame.Width, w.Frame.Width); Assert.Equal(t.Frame.Height, w.Frame.Height); }; v1.Initialized += (s, e) => { v1c++; Assert.Equal(t.Frame.Width, v1.Frame.Width); Assert.Equal(t.Frame.Height, v1.Frame.Height); }; v2.Initialized += (s, e) => { v2c++; Assert.Equal(t.Frame.Width, v2.Frame.Width); Assert.Equal(t.Frame.Height, v2.Frame.Height); }; sv1.Initialized += (s, e) => { sv1c++; Assert.Equal(t.Frame.Width, sv1.Frame.Width); Assert.Equal(t.Frame.Height, sv1.Frame.Height); Assert.False(sv1.CanFocus); Assert.Throws <InvalidOperationException> (() => sv1.CanFocus = true); Assert.False(sv1.CanFocus); }; v1.Add(sv1); w.Add(v1, v2); t.Add(w); Application.Iteration = () => { Application.Refresh(); t.Running = false; }; Application.Run(t); Application.Shutdown(); Assert.Equal(1, tc); Assert.Equal(1, wc); Assert.Equal(1, v1c); Assert.Equal(1, v2c); Assert.Equal(1, sv1c); Assert.True(t.CanFocus); Assert.True(w.CanFocus); Assert.False(v1.CanFocus); Assert.False(v2.CanFocus); Assert.False(sv1.CanFocus); v1.CanFocus = true; Assert.False(sv1.CanFocus); // False because sv1 was disposed and it isn't a subview of v1. }
public void Initialized_Event_Will_Be_Invoked_When_Added_Dynamically() { Application.Init(new FakeDriver(), new FakeMainLoop(() => FakeConsole.ReadKey(true))); var t = new Toplevel() { Id = "0", }; var w = new Window() { Id = "t", Width = Dim.Fill(), Height = Dim.Fill() }; var v1 = new View() { Id = "v1", Width = Dim.Fill(), Height = Dim.Fill() }; var v2 = new View() { Id = "v2", Width = Dim.Fill(), Height = Dim.Fill() }; int tc = 0, wc = 0, v1c = 0, v2c = 0, sv1c = 0; t.Initialized += (s, e) => { tc++; Assert.Equal(1, tc); Assert.Equal(1, wc); Assert.Equal(1, v1c); Assert.Equal(1, v2c); Assert.Equal(0, sv1c); // Added after t in the Application.Iteration. Assert.True(t.CanFocus); Assert.True(w.CanFocus); Assert.False(v1.CanFocus); Assert.False(v2.CanFocus); Application.Refresh(); }; w.Initialized += (s, e) => { wc++; Assert.Equal(t.Frame.Width, w.Frame.Width); Assert.Equal(t.Frame.Height, w.Frame.Height); }; v1.Initialized += (s, e) => { v1c++; Assert.Equal(t.Frame.Width, v1.Frame.Width); Assert.Equal(t.Frame.Height, v1.Frame.Height); }; v2.Initialized += (s, e) => { v2c++; Assert.Equal(t.Frame.Width, v2.Frame.Width); Assert.Equal(t.Frame.Height, v2.Frame.Height); }; w.Add(v1, v2); t.Add(w); Application.Iteration = () => { var sv1 = new View() { Id = "sv1", Width = Dim.Fill(), Height = Dim.Fill() }; sv1.Initialized += (s, e) => { sv1c++; Assert.NotEqual(t.Frame.Width, sv1.Frame.Width); Assert.NotEqual(t.Frame.Height, sv1.Frame.Height); Assert.False(sv1.CanFocus); Assert.Throws <InvalidOperationException> (() => sv1.CanFocus = true); Assert.False(sv1.CanFocus); }; v1.Add(sv1); Application.Refresh(); t.Running = false; }; Application.Run(t); Application.Shutdown(); Assert.Equal(1, tc); Assert.Equal(1, wc); Assert.Equal(1, v1c); Assert.Equal(1, v2c); Assert.Equal(1, sv1c); Assert.True(t.CanFocus); Assert.True(w.CanFocus); Assert.False(v1.CanFocus); Assert.False(v2.CanFocus); }
public void LeftTopBottomRight_Win_ShouldNotThrow() { // Setup Fake driver (Window win, Button button) setup() { Application.Init(new FakeDriver(), new NetMainLoop(() => FakeConsole.ReadKey(true))); Application.Iteration = () => { Application.RequestStop(); }; var win = new Window("window") { X = 0, Y = 0, Width = Dim.Fill(), Height = Dim.Fill(), }; Application.Top.Add(win); var button = new Button("button") { X = Pos.Center(), }; win.Add(button); return(win, button); } Application.RunState rs; void cleanup(Application.RunState rs) { // Cleanup Application.End(rs); } // Test cases: var app = setup(); app.button.Y = Pos.Left(app.win); rs = Application.Begin(Application.Top); Application.Run(); cleanup(rs); app = setup(); app.button.Y = Pos.X(app.win); rs = Application.Begin(Application.Top); Application.Run(); cleanup(rs); app = setup(); app.button.Y = Pos.Top(app.win); rs = Application.Begin(Application.Top); Application.Run(); cleanup(rs); app = setup(); app.button.Y = Pos.Y(app.win); rs = Application.Begin(Application.Top); Application.Run(); cleanup(rs); app = setup(); app.button.Y = Pos.Bottom(app.win); rs = Application.Begin(Application.Top); Application.Run(); cleanup(rs); app = setup(); app.button.Y = Pos.Right(app.win); rs = Application.Begin(Application.Top); Application.Run(); cleanup(rs); }
public void New_Initializes() { // Parameterless var r = new View(); Assert.NotNull(r); Assert.Equal(LayoutStyle.Computed, r.LayoutStyle); Assert.Equal("View()({X=0,Y=0,Width=0,Height=0})", r.ToString()); Assert.False(r.CanFocus); Assert.False(r.HasFocus); Assert.Equal(new Rect(0, 0, 0, 0), r.Bounds); Assert.Equal(new Rect(0, 0, 0, 0), r.Frame); Assert.Null(r.Focused); Assert.Null(r.ColorScheme); Assert.Equal(Dim.Sized(0), r.Width); Assert.Equal(Dim.Sized(0), r.Height); // BUGBUG: Pos needs eqality implemented //Assert.Equal (Pos.At (0), r.X); //Assert.Equal (Pos.At (0), r.Y); Assert.False(r.IsCurrentTop); Assert.Empty(r.Id); Assert.Empty(r.Subviews); Assert.False(r.WantContinuousButtonPressed); Assert.False(r.WantMousePositionReports); Assert.Null(r.SuperView); Assert.Null(r.MostFocused); // Empty Rect r = new View(Rect.Empty); Assert.NotNull(r); Assert.Equal(LayoutStyle.Absolute, r.LayoutStyle); Assert.Equal("View()({X=0,Y=0,Width=0,Height=0})", r.ToString()); Assert.False(r.CanFocus); Assert.False(r.HasFocus); Assert.Equal(new Rect(0, 0, 0, 0), r.Bounds); Assert.Equal(new Rect(0, 0, 0, 0), r.Frame); Assert.Null(r.Focused); Assert.Null(r.ColorScheme); Assert.Null(r.Height); Assert.Null(r.Height); Assert.Null(r.X); Assert.Null(r.Y); Assert.False(r.IsCurrentTop); Assert.Empty(r.Id); Assert.Empty(r.Subviews); Assert.False(r.WantContinuousButtonPressed); Assert.False(r.WantMousePositionReports); Assert.Null(r.SuperView); Assert.Null(r.MostFocused); // Rect with values r = new View(new Rect(1, 2, 3, 4)); Assert.NotNull(r); Assert.Equal(LayoutStyle.Absolute, r.LayoutStyle); Assert.Equal("View()({X=1,Y=2,Width=3,Height=4})", r.ToString()); Assert.False(r.CanFocus); Assert.False(r.HasFocus); Assert.Equal(new Rect(0, 0, 3, 4), r.Bounds); Assert.Equal(new Rect(1, 2, 3, 4), r.Frame); Assert.Null(r.Focused); Assert.Null(r.ColorScheme); Assert.Null(r.Height); Assert.Null(r.Height); Assert.Null(r.X); Assert.Null(r.Y); Assert.False(r.IsCurrentTop); Assert.Empty(r.Id); Assert.Empty(r.Subviews); Assert.False(r.WantContinuousButtonPressed); Assert.False(r.WantMousePositionReports); Assert.Null(r.SuperView); Assert.Null(r.MostFocused); }
public DimCombine(bool add, Dim left, Dim right) { this.left = left; this.right = right; this.add = add; }
/// <summary> /// Initializes a new instance of the <see cref="Toplevel"/> class with <see cref="LayoutStyle.Computed"/> layout, defaulting to full screen. /// </summary> public Toplevel() : base() { Initialize(); Width = Dim.Fill(); Height = Dim.Fill(); }
void Initialize(Rect frame) { contentView = new View(frame); vertical = new ScrollBarView(1, 0, isVertical: true) { X = Pos.AnchorEnd(1), Y = 0, Width = 1, Height = Dim.Fill(showHorizontalScrollIndicator ? 1 : 0) }; vertical.ChangedPosition += delegate { ContentOffset = new Point(ContentOffset.X, vertical.Position); }; vertical.Host = this; horizontal = new ScrollBarView(1, 0, isVertical: false) { X = 0, Y = Pos.AnchorEnd(1), Width = Dim.Fill(showVerticalScrollIndicator ? 1 : 0), Height = 1 }; horizontal.ChangedPosition += delegate { ContentOffset = new Point(horizontal.Position, ContentOffset.Y); }; horizontal.Host = this; vertical.OtherScrollBarView = horizontal; horizontal.OtherScrollBarView = vertical; base.Add(contentView); CanFocus = true; MouseEnter += View_MouseEnter; MouseLeave += View_MouseLeave; contentView.MouseEnter += View_MouseEnter; contentView.MouseLeave += View_MouseLeave; // Things this view knows how to do AddCommand(Command.ScrollUp, () => ScrollUp(1)); AddCommand(Command.ScrollDown, () => ScrollDown(1)); AddCommand(Command.ScrollLeft, () => ScrollLeft(1)); AddCommand(Command.ScrollRight, () => ScrollRight(1)); AddCommand(Command.PageUp, () => ScrollUp(Bounds.Height)); AddCommand(Command.PageDown, () => ScrollDown(Bounds.Height)); AddCommand(Command.PageLeft, () => ScrollLeft(Bounds.Width)); AddCommand(Command.PageRight, () => ScrollRight(Bounds.Width)); AddCommand(Command.TopHome, () => ScrollUp(contentSize.Height)); AddCommand(Command.BottomEnd, () => ScrollDown(contentSize.Height)); AddCommand(Command.LeftHome, () => ScrollLeft(contentSize.Width)); AddCommand(Command.RightEnd, () => ScrollRight(contentSize.Width)); // Default keybindings for this view AddKeyBinding(Key.CursorUp, Command.ScrollUp); AddKeyBinding(Key.CursorDown, Command.ScrollDown); AddKeyBinding(Key.CursorLeft, Command.ScrollLeft); AddKeyBinding(Key.CursorRight, Command.ScrollRight); AddKeyBinding(Key.PageUp, Command.PageUp); AddKeyBinding((Key)'v' | Key.AltMask, Command.PageUp); AddKeyBinding(Key.PageDown, Command.PageDown); AddKeyBinding(Key.V | Key.CtrlMask, Command.PageDown); AddKeyBinding(Key.PageUp | Key.CtrlMask, Command.PageLeft); AddKeyBinding(Key.PageDown | Key.CtrlMask, Command.PageRight); AddKeyBinding(Key.Home, Command.TopHome); AddKeyBinding(Key.End, Command.BottomEnd); AddKeyBinding(Key.Home | Key.CtrlMask, Command.LeftHome); AddKeyBinding(Key.End | Key.CtrlMask, Command.RightEnd); }
public void Only_DimAbsolute_And_DimFactor_As_A_Different_Procedure_For_Assigning_Value_To_Width_Or_Height() { // Testing with the Button because it properly handles the Dim class. Application.Init(new FakeDriver(), new NetMainLoop(() => FakeConsole.ReadKey(true))); var t = Application.Top; var w = new Window("w") { Width = 100, Height = 100 }; var f1 = new FrameView("f1") { X = 0, Y = 0, Width = Dim.Percent(50), Height = 5 }; var f2 = new FrameView("f2") { X = Pos.Right(f1), Y = 0, Width = Dim.Fill(), Height = 5 }; var v1 = new Button("v1") { X = Pos.X(f1) + 2, Y = Pos.Bottom(f1) + 2, Width = Dim.Width(f1) - 2, Height = Dim.Fill() - 2 }; var v2 = new Button("v2") { X = Pos.X(f2) + 2, Y = Pos.Bottom(f2) + 2, Width = Dim.Width(f2) - 2, Height = Dim.Fill() - 2 }; var v3 = new Button("v3") { Width = Dim.Percent(10), Height = Dim.Percent(10) }; var v4 = new Button("v4") { Width = Dim.Sized(50), Height = Dim.Sized(50) }; var v5 = new Button("v5") { Width = Dim.Width(v1) - Dim.Width(v3), Height = Dim.Height(v1) - Dim.Height(v3) }; var v6 = new Button("v6") { X = Pos.X(f2), Y = Pos.Bottom(f2) + 2, Width = Dim.Percent(20, true), Height = Dim.Percent(20, true) }; w.Add(f1, f2, v1, v2, v3, v4, v5, v6); t.Add(w); t.Ready += () => { Assert.Equal("Dim.Absolute(100)", w.Width.ToString()); Assert.Equal("Dim.Absolute(100)", w.Height.ToString()); Assert.Equal(100, w.Frame.Width); Assert.Equal(100, w.Frame.Height); Assert.Equal("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString()); Assert.Equal("Dim.Absolute(5)", f1.Height.ToString()); Assert.Equal(49, f1.Frame.Width); // 50-1=49 Assert.Equal(5, f1.Frame.Height); Assert.Equal("Dim.Fill(margin=0)", f2.Width.ToString()); Assert.Equal("Dim.Absolute(5)", f2.Height.ToString()); Assert.Equal(49, f2.Frame.Width); // 50-1=49 Assert.Equal(5, f2.Frame.Height); Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v1.Width.ToString()); Assert.Equal("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v1.Height.ToString()); Assert.Equal(47, v1.Frame.Width); // 49-2=47 Assert.Equal(89, v1.Frame.Height); // 98-5-2-2=89 Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=49,Y=0,Width=49,Height=5}))-Dim.Absolute(2))", v2.Width.ToString()); Assert.Equal("Dim.Combine(Dim.Fill(margin=0)-Dim.Absolute(2))", v2.Height.ToString()); Assert.Equal(47, v2.Frame.Width); // 49-2=47 Assert.Equal(89, v2.Frame.Height); // 98-5-2-2=89 Assert.Equal("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString()); Assert.Equal("Dim.Factor(factor=0.1, remaining=False)", v3.Height.ToString()); Assert.Equal(9, v3.Frame.Width); // 98*10%=9 Assert.Equal(9, v3.Frame.Height); // 98*10%=9 Assert.Equal("Dim.Absolute(50)", v4.Width.ToString()); Assert.Equal("Dim.Absolute(50)", v4.Height.ToString()); Assert.Equal(50, v4.Frame.Width); Assert.Equal(50, v4.Frame.Height); Assert.Equal("Dim.Combine(DimView(side=Width, target=Button()({X=2,Y=7,Width=47,Height=89}))-DimView(side=Width, target=Button()({X=0,Y=0,Width=9,Height=9})))", v5.Width.ToString()); Assert.Equal("Dim.Combine(DimView(side=Height, target=Button()({X=2,Y=7,Width=47,Height=89}))-DimView(side=Height, target=Button()({X=0,Y=0,Width=9,Height=9})))", v5.Height.ToString()); Assert.Equal(38, v5.Frame.Width); // 47-9=38 Assert.Equal(80, v5.Frame.Height); // 89-9=80 Assert.Equal("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString()); Assert.Equal("Dim.Factor(factor=0.2, remaining=True)", v6.Height.ToString()); Assert.Equal(9, v6.Frame.Width); // 47*20%=9 Assert.Equal(18, v6.Frame.Height); // 89*20%=18 w.Width = 200; w.Height = 200; t.LayoutSubviews(); Assert.Equal("Dim.Absolute(200)", w.Width.ToString()); Assert.Equal("Dim.Absolute(200)", w.Height.ToString()); Assert.Equal(200, w.Frame.Width); Assert.Equal(200, w.Frame.Height); f1.Text = "Frame1"; Assert.Equal("Dim.Factor(factor=0.5, remaining=False)", f1.Width.ToString()); Assert.Equal("Dim.Absolute(5)", f1.Height.ToString()); Assert.Equal(99, f1.Frame.Width); // 100-1=99 Assert.Equal(5, f1.Frame.Height); f2.Text = "Frame2"; Assert.Equal("Dim.Fill(margin=0)", f2.Width.ToString()); Assert.Equal("Dim.Absolute(5)", f2.Height.ToString()); Assert.Equal(99, f2.Frame.Width); // 100-1=99 Assert.Equal(5, f2.Frame.Height); v1.Text = "Button1"; Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=0,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v1.Width.ToString()); Assert.Equal("Dim.Absolute(1)", v1.Height.ToString()); Assert.Equal(97, v1.Frame.Width); // 99-2=97 Assert.Equal(1, v1.Frame.Height); // 1 because is Dim.DimAbsolute v2.Text = "Button2"; Assert.Equal("Dim.Combine(DimView(side=Width, target=FrameView()({X=99,Y=0,Width=99,Height=5}))-Dim.Absolute(2))", v2.Width.ToString()); Assert.Equal("Dim.Absolute(1)", v2.Height.ToString()); Assert.Equal(97, v2.Frame.Width); // 99-2=97 Assert.Equal(1, v2.Frame.Height); // 1 because is Dim.DimAbsolute v3.Text = "Button3"; Assert.Equal("Dim.Factor(factor=0.1, remaining=False)", v3.Width.ToString()); Assert.Equal("Dim.Absolute(1)", v3.Height.ToString()); Assert.Equal(19, v3.Frame.Width); // 198*10%=19 * Percent is related to the super-view if it isn't null otherwise the view width Assert.Equal(1, v3.Frame.Height); // 1 because is Dim.DimAbsolute v4.Text = "Button4"; Assert.Equal("Dim.Absolute(11)", v4.Width.ToString()); Assert.Equal("Dim.Absolute(1)", v4.Height.ToString()); Assert.Equal(11, v4.Frame.Width); // 11 is the text length and because is Dim.DimAbsolute Assert.Equal(1, v4.Frame.Height); // 1 because is Dim.DimAbsolute v5.Text = "Button5"; Assert.Equal("Dim.Combine(DimView(side=Width, target=Button()({X=2,Y=7,Width=97,Height=1}))-DimView(side=Width, target=Button()({X=0,Y=0,Width=19,Height=1})))", v5.Width.ToString()); Assert.Equal("Dim.Absolute(1)", v5.Height.ToString()); Assert.Equal(78, v5.Frame.Width); // 97-19=78 Assert.Equal(1, v5.Frame.Height); // 1 because is Dim.DimAbsolute v6.Text = "Button6"; Assert.Equal("Dim.Factor(factor=0.2, remaining=True)", v6.Width.ToString()); Assert.Equal("Dim.Absolute(1)", v6.Height.ToString()); Assert.Equal(19, v6.Frame.Width); // 99*20%=19 Assert.Equal(1, v6.Frame.Height); // 1 because is Dim.DimAbsolute }; Application.Iteration += () => Application.RequestStop(); Application.Run(); Application.Shutdown(); }
public void New_Works() { var dim = new Dim(); Assert.Equal("Terminal.Gui.Dim", dim.ToString()); }