Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 17 additions & 11 deletions UICatalog/Scenario.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,14 +73,11 @@ public class Scenario : IDisposable {
/// 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>
public virtual void Init(Toplevel top, ColorScheme colorScheme)
public virtual void Init (Toplevel top, ColorScheme colorScheme)
{
Application.Init ();

Top = top;
if (Top == null) {
Top = Application.Top;
}
Top = top != null ? top : Application.Top;

Win = new Window ($"CTRL-Q to Close - Scenario: {GetName ()}") {
X = 0,
Expand Down Expand Up @@ -177,7 +174,14 @@ public static List<string> GetCategories (Type t) => System.Attribute.GetCustomA
/// <returns>list of category names</returns>
public List<string> GetCategories () => ScenarioCategory.GetCategories (this.GetType ());

public override string ToString () => $"{GetName (),-30}{GetDescription ()}";
private static int _maxScenarioNameLen = 30;

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

/// <summary>
/// Override this to implement the <see cref="Scenario"/> setup logic (create controls, etc...).
Expand Down Expand Up @@ -232,12 +236,14 @@ internal static List<string> GetAllCategories ()
/// Returns an instance of each <see cref="Scenario"/> defined in the project.
/// https://stackoverflow.com/questions/5411694/get-all-inherited-classes-of-an-abstract-class
/// </summary>
public static List<Type> GetDerivedClasses<T> ()
public static List<Scenario> GetScenarios ()
{
List<Type> objects = new List<Type> ();
foreach (Type type in typeof (T).Assembly.GetTypes ()
.Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf (typeof (T)))) {
objects.Add (type);
List<Scenario> objects = new List<Scenario> ();
foreach (Type type in typeof (Scenario).Assembly.ExportedTypes
.Where (myType => myType.IsClass && !myType.IsAbstract && myType.IsSubclassOf (typeof (Scenario)))) {
var scenario = (Scenario)Activator.CreateInstance (type);
objects.Add (scenario);
_maxScenarioNameLen = Math.Max (_maxScenarioNameLen, scenario.GetName ().Length + 1);
}
return objects;
}
Expand Down
5 changes: 1 addition & 4 deletions UICatalog/Scenarios/AllViewsTester.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,7 @@ public override void Init (Toplevel top, ColorScheme colorScheme)
{
Application.Init ();

Top = top;
if (Top == null) {
Top = Application.Top;
}
Top = top != null ? top : Application.Top;

//Win = new Window ($"CTRL-Q to Close - Scenario: {GetName ()}") {
// X = 0,
Expand Down
1 change: 0 additions & 1 deletion UICatalog/Scenarios/BordersComparisons.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ namespace UICatalog.Scenarios {
public class BordersComparisons : Scenario {
public override void Init (Toplevel top, ColorScheme colorScheme)
{
top.Dispose ();
Application.Init ();

top = Application.Top;
Expand Down
12 changes: 1 addition & 11 deletions UICatalog/Scenarios/Clipping.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,9 @@ public override void Init (Toplevel top, ColorScheme colorScheme)
{
Application.Init ();

Top = top;
if (Top == null) {
Top = Application.Top;
}
Top = top != null ? top : Application.Top;

Top.ColorScheme = Colors.Base;
//Win = new TopLevel($"CTRL-Q to Close - Scenario: {GetName ()}") {
// X = 0,
// Y = 0,
// Width = Dim.Fill (),
// Height = Dim.Fill ()
//};
//Top.Add (Win);
}

public override void Setup ()
Expand Down
5 changes: 1 addition & 4 deletions UICatalog/Scenarios/Editor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,10 +35,7 @@ public class Editor : Scenario {
public override void Init (Toplevel top, ColorScheme colorScheme)
{
Application.Init ();
Top = top;
if (Top == null) {
Top = Application.Top;
}
Top = top != null ? top : Application.Top;

Win = new Window (_fileName ?? "Untitled") {
X = 0,
Expand Down
2 changes: 1 addition & 1 deletion UICatalog/Scenarios/Keys.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public override bool ProcessColdKey (KeyEvent keyEvent)
public override void Init (Toplevel top, ColorScheme colorScheme)
{
Application.Init ();
Top = top;
Top = top != null ? top : Application.Top;

Win = new TestWindow ($"CTRL-Q to Close - Scenario: {GetName ()}") {
X = 0,
Expand Down
21 changes: 12 additions & 9 deletions UICatalog/Scenarios/ListViewWithSelection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Nodes;
using Terminal.Gui;
using Attribute = Terminal.Gui.Attribute;

Expand All @@ -16,11 +17,13 @@ public class ListViewWithSelection : Scenario {
public CheckBox _allowMultipleCB;
public ListView _listView;

public List<Type> _scenarios = Scenario.GetDerivedClasses<Scenario>().OrderBy (t => Scenario.ScenarioMetadata.GetName (t)).ToList ();
public List<Scenario> _scenarios;

public override void Setup ()
{
_customRenderCB = new CheckBox ("Render with columns") {
_scenarios = Scenario.GetScenarios ().OrderBy (s => s.GetName ()).ToList ();

_customRenderCB = new CheckBox ("Use custom rendering") {
X = 0,
Y = 0,
Height = 1,
Expand Down Expand Up @@ -137,11 +140,11 @@ private void AllowMultipleCB_Toggled (bool prev)
// This is basically the same implementation used by the UICatalog main window
internal class ScenarioListDataSource : IListDataSource {
int _nameColumnWidth = 30;
private List<Type> scenarios;
private List<Scenario> scenarios;
BitArray marks;
int count, len;

public List<Type> Scenarios {
public List<Scenario> Scenarios {
get => scenarios;
set {
if (value != null) {
Expand All @@ -163,14 +166,14 @@ public bool IsMarked (int item)

public int Length => len;

public ScenarioListDataSource (List<Type> itemList) => Scenarios = itemList;
public ScenarioListDataSource (List<Scenario> itemList) => Scenarios = itemList;

public void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width, int start = 0)
{
container.Move (col, line);
// Equivalent to an interpolated string like $"{Scenarios[item].Name, -widtestname}"; if such a thing were possible
var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [item]));
RenderUstr (driver, $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [item])}", col, line, width, start);
var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenarios [item].GetName ());
RenderUstr (driver, $"{s} ({Scenarios [item].GetDescription ()})", col, line, width, start);
}

public void SetMark (int item, bool value)
Expand All @@ -187,8 +190,8 @@ int GetMaxLengthItem ()

int maxLength = 0;
for (int i = 0; i < scenarios.Count; i++) {
var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenario.ScenarioMetadata.GetName (Scenarios [i]));
var sc = $"{s} {Scenario.ScenarioMetadata.GetDescription (Scenarios [i])}";
var s = String.Format (String.Format ("{{0,{0}}}", -_nameColumnWidth), Scenarios [i].GetName ());
var sc = $"{s} {Scenarios [i].GetDescription ()}";
var l = sc.Length;
if (l > maxLength) {
maxLength = l;
Expand Down
6 changes: 1 addition & 5 deletions UICatalog/Scenarios/Notepad.cs
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,7 @@ public class Notepad : Scenario {
public override void Init (Toplevel top, ColorScheme colorScheme)
{
Application.Init ();

Top = top;
if (Top == null) {
Top = Application.Top;
}
Top = top != null ? top : Application.Top;
Top.ColorScheme = Colors.Base;
}

Expand Down
5 changes: 1 addition & 4 deletions UICatalog/Scenarios/WindowsAndFrameViews.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,7 @@ public override void Init (Toplevel top, ColorScheme colorScheme)
{
Application.Init ();

Top = top;
if (Top == null) {
Top = Application.Top;
}
Top = top != null ? top : Application.Top;
}

public override void RequestStop ()
Expand Down
Loading