Skip to content

Commit

Permalink
Merge pull request gui-cs#3575 from tig/v2_3522-IDesignable
Browse files Browse the repository at this point in the history
Fixes gui-cs#3522. Adds `IDesignable`: Ability for Views to load design-time/demo data
  • Loading branch information
tig authored Jul 3, 2024
2 parents a6bd533 + 12562b1 commit 716ac31
Show file tree
Hide file tree
Showing 11 changed files with 279 additions and 281 deletions.
23 changes: 23 additions & 0 deletions Terminal.Gui/View/IDesignable.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
namespace Terminal.Gui;

/// <summary>
/// Interface declaring common functionality useful for designer implementations.
/// </summary>
public interface IDesignable
{
/// <summary>
/// Causes the View to enable design-time mode. This typically means that the view will load demo data and
/// be configured to allow for design-time manipulation.
/// </summary>
/// <param name="context">Optional arbitrary, View-specific, context.</param>
/// <typeparam name="TContext">A non-null type for <paramref name="context"/>.</typeparam>
/// <returns><see langword="true"/> if the view successfully loaded demo data.</returns>
public bool EnableForDesign<TContext> (ref readonly TContext context) where TContext : notnull => EnableForDesign ();

/// <summary>
/// Causes the View to enable design-time mode. This typically means that the view will load demo data and
/// be configured to allow for design-time manipulation.
/// </summary>
/// <returns><see langword="true"/> if the view successfully loaded demo data.</returns>
public bool EnableForDesign () => false;
}
10 changes: 9 additions & 1 deletion Terminal.Gui/Views/Button.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace Terminal.Gui;
/// invoked repeatedly while the button is pressed.
/// </para>
/// </remarks>
public class Button : View
public class Button : View, IDesignable
{
private readonly Rune _leftBracket;
private readonly Rune _leftDefault;
Expand Down Expand Up @@ -190,4 +190,12 @@ protected override void UpdateTextFormatterText ()
}
}
}

/// <inheritdoc />
public bool EnableForDesign ()
{
Title = "_Button";

return true;
}
}
12 changes: 11 additions & 1 deletion Terminal.Gui/Views/ComboBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,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, IDesignable
{
private readonly ComboListView _listview;
private readonly int _minimumHeight = 2;
Expand Down Expand Up @@ -994,4 +994,14 @@ private void SetInitialProperties (ComboBox container, bool hideDropdownListOnCl
AddCommand (Command.LineUp, () => _container.MoveUpList ());
}
}

/// <inheritdoc />
public bool EnableForDesign ()
{
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, IDesignable
{
private bool _allowsMarking;
private bool _allowsMultipleSelection = true;
Expand Down Expand Up @@ -921,6 +921,15 @@ public void ResumeSuspendCollectionChangedEvent ()
Source.SuspendCollectionChangedEvent = false;
}
}

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

return true;
}
}

/// <summary>
Expand Down
175 changes: 174 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, IDesignable
{
// Spaces before the Title
private static readonly int _leftPadding = 1;
Expand Down Expand Up @@ -1591,4 +1591,177 @@ private MenuBar GetMouseGrabViewInstance (View view)
}

#endregion Mouse Handling


/// <inheritdoc />
public bool EnableForDesign<TContext> (in TContext context) where TContext : notnull

Check warning on line 1597 in Terminal.Gui/Views/Menu/MenuBar.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Reference kind modifier of parameter 'in TContext context' doesn't match the corresponding parameter 'ref readonly TContext context' in overridden or implemented member.

Check warning on line 1597 in Terminal.Gui/Views/Menu/MenuBar.cs

View workflow job for this annotation

GitHub Actions / Build and Publish to Nuget.org

Reference kind modifier of parameter 'in TContext context' doesn't match the corresponding parameter 'ref readonly TContext context' in overridden or implemented member.

Check warning on line 1597 in Terminal.Gui/Views/Menu/MenuBar.cs

View workflow job for this annotation

GitHub Actions / build_and_test

Reference kind modifier of parameter 'in TContext context' doesn't match the corresponding parameter 'ref readonly TContext context' in overridden or implemented member.
{
if (context is not Func<string, bool> actionFn)
{
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, IDesignable
{
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 EnableForDesign ()
{
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, IDesignable
{
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 EnableForDesign ()
{
RadioLabels = new [] { "Option _1", "Option _2", "Option _3" };
return true;
}
}
Loading

0 comments on commit 716ac31

Please sign in to comment.