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

Fixed Frame != Bounds; changed UICat Scenarios list to use tableview! #164

Merged
merged 1 commit into from
Apr 27, 2023
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
21 changes: 13 additions & 8 deletions Terminal.Gui/Views/TableView/TableView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -230,8 +230,9 @@ public TableView () : base ()
///<inheritdoc/>
public override void Redraw (Rect bounds)
{
base.Redraw (bounds);

Move (0, 0);
var frame = Frame;

scrollRightPoint = null;
scrollLeftPoint = null;
Expand Down Expand Up @@ -272,7 +273,7 @@ public override void Redraw (Rect bounds)
int headerLinesConsumed = line;

//render the cells
for (; line < frame.Height; line++) {
for (; line < Bounds.Height; line++) {

ClearLine (line, bounds.Width);

Expand Down Expand Up @@ -1160,25 +1161,29 @@ public override bool MouseEvent (MouseEvent me)
return true;
}

// TODO: Revert this (or not) once #2578 is solved
var boundsX = me.X - GetFramesThickness ().Left;
var boundsY = me.Y - GetFramesThickness ().Top;

if (me.Flags.HasFlag (MouseFlags.Button1Clicked)) {

if (scrollLeftPoint != null
&& scrollLeftPoint.Value.X == me.X
&& scrollLeftPoint.Value.Y == me.Y) {
&& scrollLeftPoint.Value.X == boundsX
&& scrollLeftPoint.Value.Y == boundsY) {
ColumnOffset--;
EnsureValidScrollOffsets ();
SetNeedsDisplay ();
}

if (scrollRightPoint != null
&& scrollRightPoint.Value.X == me.X
&& scrollRightPoint.Value.Y == me.Y) {
&& scrollRightPoint.Value.X == boundsX
&& scrollRightPoint.Value.Y == boundsY) {
ColumnOffset++;
EnsureValidScrollOffsets ();
SetNeedsDisplay ();
}

var hit = ScreenToCell (me.X, me.Y);
var hit = ScreenToCell (boundsX, boundsY);
if (hit != null) {

if (MultiSelect && HasControlOrAlt (me)) {
Expand All @@ -1193,7 +1198,7 @@ public override bool MouseEvent (MouseEvent me)

// Double clicking a cell activates
if (me.Flags == MouseFlags.Button1DoubleClicked) {
var hit = ScreenToCell (me.X, me.Y);
var hit = ScreenToCell (boundsX, boundsY);
if (hit != null) {
OnCellActivated (new CellActivatedEventArgs (Table, hit.Value.X, hit.Value.Y));
}
Expand Down
78 changes: 44 additions & 34 deletions UICatalog/UICatalog.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,10 @@
using System.Threading;
using static Terminal.Gui.ConfigurationManager;
using System.Text.Json.Serialization;
using static Terminal.Gui.TableView;

#nullable enable

#nullable enable
/// <summary>
/// UI Catalog is a comprehensive sample library for Terminal.Gui. It provides a simple UI for adding to the catalog of scenarios.
/// </summary>
Expand Down Expand Up @@ -253,8 +254,8 @@ public class UICatalogTopLevel : Toplevel {
public MenuItem? miIsMouseDisabled;
public MenuItem? miEnableConsoleScrolling;

public ListView CategoryListView;
public ListView ScenarioListView;
public ListView CategoryList;
public TableView ScenarioList;

public StatusItem Capslock;
public StatusItem Numlock;
Expand Down Expand Up @@ -323,7 +324,7 @@ public UICatalogTopLevel ()
//ContentPane.SetSplitterPos (0, 25);
//ContentPane.ShortcutAction = () => ContentPane.SetFocus ();

CategoryListView = new ListView (_categories) {
CategoryList = new ListView (_categories) {
X = 0,
Y = 1,
Width = Dim.Percent (30),
Expand All @@ -334,37 +335,40 @@ public UICatalogTopLevel ()
BorderStyle = LineStyle.Single,
SuperViewRendersLineCanvas = true
};
CategoryListView.OpenSelectedItem += (s, a) => {
ScenarioListView!.SetFocus ();
CategoryList.OpenSelectedItem += (s, a) => {
ScenarioList!.SetFocus ();
};
CategoryListView.SelectedItemChanged += CategoryListView_SelectedChanged;

//ContentPane.Tiles.ElementAt (0).Title = "Categories";
//ContentPane.Tiles.ElementAt (0).MinSize = 2;
//ContentPane.Tiles.ElementAt (0).ContentView.Add (CategoryListView);
CategoryList.SelectedItemChanged += CategoryView_SelectedChanged;

ScenarioListView = new ListView () {
X = Pos.Right (CategoryListView) - 1,
ScenarioList = new TableView () {
X = Pos.Right (CategoryList) - 1,
Y = 1,
Width = Dim.Fill (0),
Height = Dim.Fill (1),
AllowsMarking = false,
//AllowsMarking = false,
CanFocus = true,
Title = "Scenarios",
BorderStyle = LineStyle.Single,
SuperViewRendersLineCanvas = true
};
ScenarioList.FullRowSelect = true;
//ScenarioList.Style.ShowHeaders = false;
ScenarioList.Style.ShowHorizontalHeaderOverline = false;
//ScenarioList.Style.ShowHorizontalHeaderUnderline = false;
ScenarioList.Style.ShowHorizontalBottomline = false;
ScenarioList.Style.ShowVerticalCellLines = false;
ScenarioList.Style.ShowVerticalHeaderLines = false;

ScenarioListView.OpenSelectedItem += ScenarioListView_OpenSelectedItem;
var longestName = _scenarios!.Max (s => s.GetName ().Length);
ScenarioList.Style.ColumnStyles.Add (0, new ColumnStyle () { MaxWidth = longestName, MinWidth = longestName, MinAcceptableWidth = longestName });
ScenarioList.Style.ColumnStyles.Add (1, new ColumnStyle () { MaxWidth = 1 });

//ContentPane.Tiles.ElementAt (1).Title = "Scenarios";
//ContentPane.Tiles.ElementAt (1).ContentView.Add (ScenarioListView);
//ContentPane.Tiles.ElementAt (1).MinSize = 2;
ScenarioList.CellActivated += ScenarioView_OpenSelectedItem;

KeyDown += KeyDownHandler;
//Add (ContentPane);
Add (CategoryListView);
Add (ScenarioListView);

Add (CategoryList);
Add (ScenarioList);

Add (MenuBar);
Add (StatusBar);
Expand All @@ -373,8 +377,10 @@ public UICatalogTopLevel ()
Unloaded += UnloadedHandler;

// Restore previous selections
CategoryListView.SelectedItem = _cachedCategoryIndex;
ScenarioListView.SelectedItem = _cachedScenarioIndex;
CategoryList.SelectedItem = _cachedCategoryIndex;
CategoryList.EnsureSelectedItemVisible ();
ScenarioList.SelectedRow = _cachedScenarioIndex;
ScenarioList.EnsureSelectedCellIsVisible ();

ConfigurationManager.Applied += ConfigAppliedHandler;
}
Expand All @@ -393,15 +399,15 @@ void LoadedHandler (object? sender, EventArgs? args)
_isFirstRunning = false;
}
if (!_isFirstRunning) {
ScenarioListView.SetFocus ();
ScenarioList.SetFocus ();
}

StatusBar.VisibleChanged += (s, e) => {
UICatalogApp.ShowStatusBar = StatusBar.Visible;

var height = (StatusBar.Visible ? 1 : 0);
CategoryListView.Height = Dim.Fill (height);
ScenarioListView.Height = Dim.Fill (height);
CategoryList.Height = Dim.Fill (height);
ScenarioList.Height = Dim.Fill (height);
// ContentPane.Height = Dim.Fill (height);
LayoutSubviews ();
SetSubViewNeedsDisplay ();
Expand All @@ -425,16 +431,16 @@ void ConfigAppliedHandler (object? sender, ConfigurationManagerEventArgs? a)
/// Launches the selected scenario, setting the global _selectedScenario
/// </summary>
/// <param name="e"></param>
void ScenarioListView_OpenSelectedItem (object? sender, EventArgs? e)
void ScenarioView_OpenSelectedItem (object? sender, EventArgs? e)
{
if (_selectedScenario is null) {
// Save selected item state
_cachedCategoryIndex = CategoryListView.SelectedItem;
_cachedScenarioIndex = ScenarioListView.SelectedItem;
// Create new instance of scenario (even though Scenarios contains instances)
var sourceList = ScenarioListView.Source.ToList ();
_cachedCategoryIndex = CategoryList.SelectedItem;
_cachedScenarioIndex = ScenarioList.SelectedRow;

_selectedScenario = (Scenario)Activator.CreateInstance (ScenarioListView.Source.ToList () [ScenarioListView.SelectedItem]!.GetType ())!;
// Create new instance of scenario (even though Scenarios contains instances)
string selectedScenarioName = (string)ScenarioList.Table [ScenarioList.SelectedRow, 0];
_selectedScenario = (Scenario)Activator.CreateInstance (_scenarios!.FirstOrDefault (s => s.GetName () == selectedScenarioName)!.GetType ())!;

// Tell the main app to stop
Application.RequestStop ();
Expand Down Expand Up @@ -729,18 +735,22 @@ void KeyDownHandler (object? sender, KeyEventEventArgs? a)
}
}

void CategoryListView_SelectedChanged (object? sender, ListViewItemEventArgs? e)
void CategoryView_SelectedChanged (object? sender, ListViewItemEventArgs? e)
{
var item = _categories! [e!.Item];
List<Scenario> newlist;
if (e.Item == 0) {
// First category is "All"
newlist = _scenarios!;
newlist = _scenarios!;

} else {
newlist = _scenarios!.Where (s => s.GetCategories ().Contains (item)).ToList ();
}
ScenarioListView.SetSource (newlist.ToList ());
ScenarioList.Table = new EnumerableTableSource<Scenario> (newlist, new Dictionary<string, Func<Scenario, object>> () {
{ "Name", (s) => s.GetName() },
{ "Description", (s) => s.GetDescription() },
});
}
}

Expand Down