Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes #3522. Adds IDesignable: Ability for Views to load design-time/demo data #3568

Closed
wants to merge 16 commits into from
14 changes: 14 additions & 0 deletions Terminal.Gui/View/ISupportsDesignMode.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
namespace Terminal.Gui;

/// <summary>
/// Indicates that the view supports design mode.
/// </summary>
public interface ISupportsDesignMode
tig marked this conversation as resolved.
Show resolved Hide resolved
{
/// <summary>
/// Call this to tell the View to load "demo data"
/// </summary>
/// <param name="ctx">Optional arbitrary context.</param>
/// <returns><see langword="true"/> if the view succesfully loaded demo data.</returns>
public bool LoadDemoData (object ctx = null);
}
10 changes: 9 additions & 1 deletion Terminal.Gui/Views/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ namespace Terminal.Gui;
/// invoked repeatedly while the button is pressed.
/// </para>
/// </remarks>
public class Button : View
public class Button : View, ISupportsDesignMode
{
private readonly Rune _leftBracket;
private readonly Rune _leftDefault;
Expand Down Expand Up @@ -177,4 +177,12 @@ protected override void UpdateTextFormatterText ()
}
}
}

/// <inheritdoc />
public bool LoadDemoData (object ctx = null)
tig marked this conversation as resolved.
Show resolved Hide resolved
{
Title = "_Button";

return true;
}
}
16 changes: 13 additions & 3 deletions Terminal.Gui/Views/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
namespace Terminal.Gui;

/// <summary>Provides a drop-down list of items the user can select from.</summary>
public class ComboBox : View
public class ComboBox : View, ISupportsDesignMode
{
private readonly ComboListView _listview;
private readonly int _minimumHeight = 2;
Expand Down Expand Up @@ -243,7 +243,7 @@ public virtual bool Expand ()
public event EventHandler Expanded;

/// <inheritdoc/>
protected internal override bool OnMouseEvent (MouseEvent me)
protected internal override bool OnMouseEvent (MouseEvent me)
{
if (me.Position.X == Viewport.Right - 1
&& me.Position.Y == Viewport.Top
Expand Down Expand Up @@ -813,7 +813,7 @@ public bool HideDropdownListOnClick
set => _hideDropdownListOnClick = WantContinuousButtonPressed = value;
}

protected internal override bool OnMouseEvent (MouseEvent me)
protected internal override bool OnMouseEvent (MouseEvent me)
{
var res = false;
bool isMousePositionValid = IsMousePositionValid (me);
Expand Down Expand Up @@ -983,4 +983,14 @@ private void SetInitialProperties (ComboBox container, bool hideDropdownListOnCl
AddCommand (Command.LineUp, () => _container.MoveUpList ());
}
}

/// <inheritdoc />
public bool LoadDemoData (object ctx = null)
{
var source = new ObservableCollection<string> (["Combo Item 1", "Combo Item two", "Combo Item Quattro", "Last Combo Item"]);
SetSource (source);
Height = Dim.Auto (DimAutoStyle.Content, minimumContentDim: source.Count + 1);

return true;
}
}
11 changes: 10 additions & 1 deletion Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ void Render (
/// first item that starts with what the user types will be selected.
/// </para>
/// </remarks>
public class ListView : View
public class ListView : View, ISupportsDesignMode
{
private bool _allowsMarking;
private bool _allowsMultipleSelection = true;
Expand Down Expand Up @@ -921,6 +921,15 @@ public void ResumeSuspendCollectionChangedEvent ()
Source.SuspendCollectionChangedEvent = false;
}
}

/// <inheritdoc />
public bool LoadDemoData (object ctx = null)
{
var source = new ListWrapper<string> (["List Item 1", "List Item two", "List Item Quattro", "Last List Item"]);
Source = source;

return true;
}
}

/// <summary>
Expand Down
177 changes: 176 additions & 1 deletion Terminal.Gui/Views/Menu/MenuBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ namespace Terminal.Gui;
/// duplicates a shortcut (e.g. _File and Alt-F), the hot key wins.
/// </para>
/// </remarks>
public class MenuBar : View
public class MenuBar : View, ISupportsDesignMode
{
// Spaces before the Title
private static readonly int _leftPadding = 1;
Expand Down Expand Up @@ -1591,4 +1591,179 @@ private MenuBar GetMouseGrabViewInstance (View view)
}

#endregion Mouse Handling


/// <inheritdoc />
public bool LoadDemoData (object ctx = null)
{
Func<string, bool> actionFn = (Func<string, bool>)ctx;

if (actionFn is null)
{
actionFn = (s) => true;
}

Menus =
[
new MenuBarItem (
"_File",
new MenuItem []
{
new (
"_New",
"",
() => actionFn ("New"),
null,
null,
KeyCode.CtrlMask | KeyCode.N
),
new (
"_Open",
"",
() => actionFn ("Open"),
null,
null,
KeyCode.CtrlMask | KeyCode.O
),
new (
"_Save",
"",
() => actionFn ("Save"),
null,
null,
KeyCode.CtrlMask | KeyCode.S
),
null,

// Don't use Application.Quit so we can disambiguate between quitting and closing the toplevel
new (
"_Quit",
"",
() => actionFn ("Quit"),
null,
null,
KeyCode.CtrlMask | KeyCode.Q
)
}
),
new MenuBarItem (
"_Edit",
new MenuItem []
{
new (
"_Copy",
"",
() => actionFn ("Copy"),
null,
null,
KeyCode.CtrlMask | KeyCode.C
),
new (
"C_ut",
"",
() => actionFn ("Cut"),
null,
null,
KeyCode.CtrlMask | KeyCode.X
),
new (
"_Paste",
"",
() => actionFn ("Paste"),
null,
null,
KeyCode.CtrlMask | KeyCode.V
),
new MenuBarItem (
"_Find and Replace",
new MenuItem []
{
new (
"F_ind",
"",
() => actionFn ("Find"),
null,
null,
KeyCode.CtrlMask | KeyCode.F
),
new (
"_Replace",
"",
() => actionFn ("Replace"),
null,
null,
KeyCode.CtrlMask | KeyCode.H
),
new MenuBarItem (
"_3rd Level",
new MenuItem []
{
new (
"_1st",
"",
() => actionFn (
"1"
),
null,
null,
KeyCode.F1
),
new (
"_2nd",
"",
() => actionFn (
"2"
),
null,
null,
KeyCode.F2
)
}
),
new MenuBarItem (
"_4th Level",
new MenuItem []
{
new (
"_5th",
"",
() => actionFn (
"5"
),
null,
null,
KeyCode.CtrlMask
| KeyCode.D5
),
new (
"_6th",
"",
() => actionFn (
"6"
),
null,
null,
KeyCode.CtrlMask
| KeyCode.D6
)
}
)
}
),
new (
"_Select All",
"",
() => actionFn ("Select All"),
null,
null,
KeyCode.CtrlMask
| KeyCode.ShiftMask
| KeyCode.S
)
}
),
new MenuBarItem ("_About", "Top-Level", () => actionFn ("About"))
];
return true;
}
}
11 changes: 10 additions & 1 deletion Terminal.Gui/Views/ProgressBar.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public enum ProgressBarFormat
/// <see cref="Pulse"/> method is called. Call <see cref="Pulse"/> repeatedly as progress is made.
/// </para>
/// </remarks>
public class ProgressBar : View
public class ProgressBar : View, ISupportsDesignMode
{
private int [] _activityPos;
private bool _bidirectionalMarquee = true;
Expand Down Expand Up @@ -277,4 +277,13 @@ private void SetInitialProperties ()
_fraction = 0;
Initialized += ProgressBar_Initialized;
}

/// <inheritdoc />
public bool LoadDemoData (object ctx = null)
{
Width = Dim.Fill ();
Height = Dim.Auto (DimAutoStyle.Text, minimumContentDim: 1);
Fraction = 0.75f;
return true;
}
}
35 changes: 8 additions & 27 deletions Terminal.Gui/Views/RadioGroup.cs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
namespace Terminal.Gui;

/// <summary>Displays a group of labels each with a selected indicator. Only one of those can be selected at a given time.</summary>
public class RadioGroup : View
public class RadioGroup : View, ISupportsDesignMode
{
private int _cursor;
private List<(int pos, int length)> _horizontal;
Expand Down Expand Up @@ -229,32 +229,6 @@ public string [] RadioLabels
}
}

/// <inheritdoc/>
public override string Text
{
get
{
if (_radioLabels.Count == 0)
{
return string.Empty;
}

// Return labels as a CSV string
return string.Join (",", _radioLabels);
}
set
{
if (string.IsNullOrEmpty (value))
{
RadioLabels = [];
}
else
{
RadioLabels = value.Split (',').Select (x => x.Trim ()).ToArray ();
}
}
}

/// <summary>The currently selected item from the list of radio labels</summary>
/// <value>The selected.</value>
public int SelectedItem
Expand Down Expand Up @@ -487,4 +461,11 @@ private void SetContentSize ()
break;
}
}

/// <inheritdoc />
public bool LoadDemoData (object ctx = null)
{
RadioLabels = new [] { "Option _1", "Option _2", "Option _3" };
return true;
}
}
Loading
Loading