Skip to content

Commit

Permalink
Merge pull request gui-cs#3613 from BDisp/v2_3350_scenario-refactoring
Browse files Browse the repository at this point in the history
Fixes gui-cs#3350. Scenario is complex, confusing, and causes errors.
  • Loading branch information
tig authored Jul 14, 2024
2 parents f1fe7e7 + fb20fea commit 3e598e7
Show file tree
Hide file tree
Showing 65 changed files with 494 additions and 429 deletions.
84 changes: 4 additions & 80 deletions UICatalog/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,10 @@ public class Scenario : IDisposable
/// <returns></returns>
public string GetName () { return ScenarioMetadata.GetName (GetType ()); }

/// <summary>Helper to get the <see cref="Application.QuitKey"/> and the <see cref="Scenario"/> Name (defined in <see cref="ScenarioMetadata"/>)</summary>
/// <returns></returns>
public string GetQuitKeyAndName () { return $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"; }

/// <summary>
/// Returns a list of all <see cref="Scenario"/> instanaces defined in the project, sorted by
/// <see cref="ScenarioMetadata.Name"/>.
Expand Down Expand Up @@ -132,90 +136,12 @@ public static ObservableCollection<Scenario> GetScenarios ()
/// </remarks>
public virtual void Main ()
{
Init ();
Setup ();
Run ();
}

/// <summary>
/// Helper that calls <see cref="Application.Init"/> and creates the default <see cref="Terminal.Gui.Window"/>
/// implementation with a frame and label
/// showing the name of the <see cref="Scenario"/> and logic to exit back to the Scenario picker UI. Override
/// <see cref="Init"/> to provide any <see cref="Terminal.Gui.Toplevel"/> behavior needed.
/// </summary>
/// <remarks>
/// <para>
/// The base implementation calls <see cref="Application.Init"/> and creates a <see cref="Window"/> for
/// <see cref="Win"/> and adds it to <see cref="Application.Top"/>.
/// </para>
/// <para>
/// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Init"/> before
/// creating any views or calling other Terminal.Gui APIs.
/// </para>
/// </remarks>
[ObsoleteAttribute ("This method is obsolete and will be removed in v2. Use Main instead.", false)]
public virtual void Init ()
{
Application.Init ();

ConfigurationManager.Themes.Theme = Theme;
ConfigurationManager.Apply ();

Top = new ();

Win = new ()
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
X = 0,
Y = 0,
Width = Dim.Fill (),
Height = Dim.Fill (),
ColorScheme = Colors.ColorSchemes [TopLevelColorScheme]
};
Top.Add (Win);
}

/// <summary>
/// Runs the <see cref="Scenario"/>. Override to start the <see cref="Scenario"/> using a <see cref="Toplevel"/>
/// different than `Top`.
/// </summary>
/// <remarks>
/// Overrides that do not call the base.<see cref="Run"/>, must call <see cref="Application.Shutdown"/> before
/// returning.
/// </remarks>
[ObsoleteAttribute ("This method is obsolete and will be removed in v2. Use Main instead.", false)]
public virtual void Run ()
{
// Must explicitly call Application.Shutdown method to shutdown.
Application.Run (Top);
Top.Dispose ();
Application.Shutdown ();
}

/// <summary>Override this to implement the <see cref="Scenario"/> setup logic (create controls, etc...).</summary>
/// <remarks>This is typically the best place to put scenario logic code.</remarks>
[ObsoleteAttribute ("This method is obsolete and will be removed in v2. Use Main instead.", false)]
public virtual void Setup () { }

/// <summary>
/// The Toplevel for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/>.
/// </summary>

//[ObsoleteAttribute ("This property is obsolete and will be removed in v2. Use Main instead.", false)]
public Toplevel Top { get; set; }

/// <summary>Gets the Scenario Name + Description with the Description padded based on the longest known Scenario name.</summary>
/// <returns></returns>
public override string ToString () { return $"{GetName ().PadRight (_maxScenarioNameLen)}{GetDescription ()}"; }

/// <summary>
/// The Window for the <see cref="Scenario"/>. This should be set to <see cref="Terminal.Gui.Application.Top"/> in
/// most cases.
/// </summary>

//[ObsoleteAttribute ("This property is obsolete and will be removed in v2. Use Main instead.", false)]
public Window Win { get; set; }

#region IDispose

public void Dispose ()
Expand All @@ -231,8 +157,6 @@ protected virtual void Dispose (bool disposing)
{
if (disposing)
{
Top?.Dispose ();
Win?.Dispose ();
}

_disposedValue = true;
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/ASCIICustomButton.cs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ public class ScrollViewTestWindow : Window

public ScrollViewTestWindow ()
{
Title = "ScrollViewTestWindow";
Title = $"{Application.QuitKey} to Quit - Scenario: ScrollViewTestWindow";

Label titleLabel = null;

Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/Adornments.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ public override void Main ()

Window app = new ()
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
Title = GetQuitKeyAndName ()
};

var editor = new AdornmentsEditor
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/AllViewsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ public override void Main ()

var app = new Window
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
Title = GetQuitKeyAndName (),
ColorScheme = Colors.ColorSchemes ["TopLevel"]
};

Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/AnimationScenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public override void Main ()

var win = new Window
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
Title = GetQuitKeyAndName (),
X = 0,
Y = 0,
Width = Dim.Fill (),
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/Bars.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public override void Main ()
// QuitKey and it only sticks if changed after init
private void App_Loaded (object sender, EventArgs e)
{
Application.Top.Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}";
Application.Top.Title = GetQuitKeyAndName ();

ObservableCollection<string> eventSource = new ();
ListView eventLog = new ListView ()
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/BasicColors.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public override void Main ()

Window app = new ()
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
Title = GetQuitKeyAndName (),
};

var vx = 30;
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/Buttons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ public override void Main ()

Window main = new ()
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}"
Title = GetQuitKeyAndName ()
};

// Add a label & text field so we can demo IsDefault
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/ChineseUI.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ public override void Main ()

var win = new Window
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
Title = GetQuitKeyAndName (),
X = 0,
Y = 0,
Width = Dim.Fill (),
Expand Down
28 changes: 19 additions & 9 deletions UICatalog/Scenarios/ClassExplorer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,15 @@ public class ClassExplorer : Scenario
private TextView _textView;
private TreeView<object> _treeView;

public override void Setup ()
public override void Main ()
{
Win.Title = GetName ();
Win.Y = 1; // menu
Win.Height = Dim.Fill (1); // status bar
Application.Init ();
var top = new Toplevel ();

var win = new Window
{
Title = GetName ()
};

var menu = new MenuBar
{
Expand Down Expand Up @@ -55,15 +59,15 @@ public override void Setup ()
)
]
};
Top.Add (menu);
top.Add (menu);

_treeView = new TreeView<object> { X = 0, Y = 1, Width = Dim.Percent (50), Height = Dim.Fill () };

var lblSearch = new Label { Text = "Search" };
var tfSearch = new TextField { Width = 20, X = Pos.Right (lblSearch) };

Win.Add (lblSearch);
Win.Add (tfSearch);
win.Add (lblSearch);
win.Add (tfSearch);

TreeViewTextFilter<object> filter = new (_treeView);
_treeView.Filter = filter;
Expand All @@ -83,11 +87,17 @@ public override void Setup ()
_treeView.TreeBuilder = new DelegateTreeBuilder<object> (ChildGetter, CanExpand);
_treeView.SelectionChanged += TreeView_SelectionChanged;

Win.Add (_treeView);
win.Add (_treeView);

_textView = new TextView { X = Pos.Right (_treeView), Y = 0, Width = Dim.Fill (), Height = Dim.Fill () };

Win.Add (_textView);
win.Add (_textView);

top.Add (win);

Application.Run (top);
top.Dispose ();
Application.Shutdown ();
}

private bool CanExpand (object arg) { return arg is Assembly || arg is Type || arg is ShowForType; }
Expand Down
20 changes: 8 additions & 12 deletions UICatalog/Scenarios/Clipping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,24 +8,16 @@ namespace UICatalog.Scenarios;
[ScenarioCategory ("Scrolling")]
public class Clipping : Scenario
{
public override void Init ()
public override void Main ()
{
Application.Init ();
Top = new ();
Top.ColorScheme = Colors.ColorSchemes ["Base"];
}
var win = new Window { Title = GetQuitKeyAndName () };

public override void Setup ()
{
//Win.X = 1;
//Win.Y = 2;
//Win.Width = Dim.Fill () - 4;
//Win.Height = Dim.Fill () - 2;
var label = new Label
{
X = 0, Y = 0, Text = "ScrollView (new Rectangle (3, 3, 50, 20)) with a 200, 100 GetContentSize ()..."
};
Top.Add (label);
win.Add (label);

var scrollView = new ScrollView { X = 3, Y = 3, Width = 50, Height = 20 };
scrollView.ColorScheme = Colors.ColorSchemes ["Menu"];
Expand Down Expand Up @@ -84,6 +76,10 @@ public override void Setup ()

scrollView.Add (embedded1);

Top.Add (scrollView);
win.Add (scrollView);

Application.Run (win);
win.Dispose ();
Application.Shutdown ();
}
}
25 changes: 13 additions & 12 deletions UICatalog/Scenarios/CollectionNavigatorTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -74,19 +74,16 @@ public class CollectionNavigatorTester : Scenario
"quitter"
}.ToList ());

private Toplevel top;
private ListView _listView;
private TreeView _treeView;

// Don't create a Window, just return the top-level view
public override void Init ()
public override void Main ()
{
Application.Init ();
Top = new ();
Top.ColorScheme = Colors.ColorSchemes ["Base"];
}
top = new Toplevel { ColorScheme = Colors.ColorSchemes ["Base"] };

public override void Setup ()
{
var allowMarking = new MenuItem ("Allow _Marking", "", null)
{
CheckType = MenuItemCheckStyle.Checked, Checked = false
Expand Down Expand Up @@ -128,14 +125,18 @@ public override void Setup ()
]
};

Top.Add (menu);
top.Add (menu);

_items = new (_items.OrderBy (i => i, StringComparer.OrdinalIgnoreCase));

CreateListView ();
var vsep = new LineView (Orientation.Vertical) { X = Pos.Right (_listView), Y = 1, Height = Dim.Fill () };
Top.Add (vsep);
top.Add (vsep);
CreateTreeView ();

Application.Run (top);
top.Dispose ();
Application.Shutdown ();
}

private void CreateListView ()
Expand All @@ -149,7 +150,7 @@ private void CreateListView ()
Width = Dim.Percent (50),
Height = 1
};
Top.Add (label);
top.Add (label);

_listView = new ListView
{
Expand All @@ -160,7 +161,7 @@ private void CreateListView ()
AllowsMarking = false,
AllowsMultipleSelection = false
};
Top.Add (_listView);
top.Add (_listView);

_listView.SetSource (_items);

Expand All @@ -178,14 +179,14 @@ private void CreateTreeView ()
Width = Dim.Percent (50),
Height = 1
};
Top.Add (label);
top.Add (label);

_treeView = new TreeView
{
X = Pos.Right (_listView) + 1, Y = Pos.Bottom (label), Width = Dim.Fill (), Height = Dim.Fill ()
};
_treeView.Style.HighlightModelTextOnly = true;
Top.Add (_treeView);
top.Add (_treeView);

var root = new TreeNode ("IsLetterOrDigit examples");

Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/ColorPicker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public override void Main ()

Window app = new ()
{
Title = $"{Application.QuitKey} to Quit - Scenario: {GetName ()}",
Title = GetQuitKeyAndName (),
};

// Foreground ColorPicker.
Expand Down
Loading

0 comments on commit 3e598e7

Please sign in to comment.