Skip to content

Commit

Permalink
TUI syntax cleanup (#1951)
Browse files Browse the repository at this point in the history
* Console UI syntax cleanup

* Update ConsoleGuiCohortIdentificationConfigurationUI.cs

Update API

---------

Co-authored-by: James Friel <[email protected]>
  • Loading branch information
jas88 and JFriel authored Oct 17, 2024
1 parent 73049a1 commit 0d808a5
Show file tree
Hide file tree
Showing 3 changed files with 44 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -140,38 +140,40 @@ private void Tableview1_CellActivated(TableView.CellActivatedEventArgs obj)
var o = RowObjects[obj.Row];
if (o == null)
return;

var col = tableview1.Table.Columns[obj.Col];

if (col.ColumnName.Equals("Name"))
switch (col.ColumnName)
{
var factory = new ConsoleGuiContextMenuFactory(_activator);
var menu = factory.Create(Array.Empty<object>(), o);

if (menu != null)
case "Name":
{
var p = tableview1.CellToScreen(obj.Col, obj.Row);

if (p == null)
return;

menu.Position = p.Value;
_contextMenuShowing = true;
menu.Show();
menu.MenuBar.MenuAllClosed += () => _contextMenuShowing = false;
var factory = new ConsoleGuiContextMenuFactory(_activator);
var menu = factory.Create(p.Value.X, p.Value.Y, Array.Empty<object>(), o);
if (menu != null)
{
menu.Position = p.Value;
_contextMenuShowing = true;
menu.Show();
menu.MenuBar.MenuAllClosed += () => _contextMenuShowing = false;
}

break;
}
}

if (col.ColumnName.Equals("Working"))
{
var key = Common.GetKey(o);
if (key?.CrashMessage != null)
case "Working":
{
_activator.ShowException("Task Crashed", key.CrashMessage);
return;
var key = Common.GetKey(o);
if (key?.CrashMessage != null) _activator.ShowException("Task Crashed", key.CrashMessage);
break;
}
case "Execute":
Common.ExecuteOrCancel(o, int.MaxValue);
break;
}

if (col.ColumnName.Equals("Execute")) Common.ExecuteOrCancel(o, Common.Timeout);
}

private bool IsValidSelection(int col, int row)
Expand Down
10 changes: 4 additions & 6 deletions Tools/rdmp/CommandLine/Gui/ConsoleGuiContextMenuFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public ConsoleGuiContextMenuFactory(IBasicActivateItems activator)
this.activator = activator;
}

public ContextMenu Create(object[] many, object single)
public ContextMenu Create(int x,int y,object[] many, object single)
{
var commands = GetCommands(activator, many, single).ToArray();

Expand Down Expand Up @@ -65,18 +65,16 @@ public ContextMenu Create(object[] many, object single)
items.Add(bar);
}

// we can do nothing if theres no menu items
// we can do nothing if there are no menu items
if (items.Count == 0)
return null;

var withSpacers = AddSpacers(items, order);

var menu = new ContextMenu
return new ContextMenu(x, y, new MenuBarItem(withSpacers))
{
MenuItems = new MenuBarItem(withSpacers)
UseSubMenusSingleFrame = true
};

return menu;
}

private static MenuItem[] AddSpacers(List<MenuItem> items, Dictionary<MenuItem, float> order)
Expand Down
44 changes: 19 additions & 25 deletions Tools/rdmp/CommandLine/Gui/ConsoleMainWindow.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
using Rdmp.Core.MapsDirectlyToDatabaseTable;
using Rdmp.Core.Providers;
using Rdmp.Core.Providers.Nodes;
using Rdmp.Core.ReusableLibraryCode.Annotations;
using Rdmp.Core.ReusableLibraryCode.Settings;
using Terminal.Gui;
using Terminal.Gui.Trees;
Expand All @@ -27,7 +28,7 @@ internal class ConsoleMainWindow
{
private Window _win;
private TreeView<object> _treeView;
private IBasicActivateItems _activator;
private readonly IBasicActivateItems _activator;

/// <summary>
/// The last <see cref="IBasicActivateItems"/> passed to this UI.
Expand Down Expand Up @@ -167,7 +168,7 @@ internal void SetUp(Toplevel top)
var statusBar = new StatusBar(new StatusItem[]
{
new(Key.Q | Key.CtrlMask, "~^Q~ Quit", Quit),
new(Key.R | Key.CtrlMask, "~^R~ Run", action: Run),
new(Key.R | Key.CtrlMask, "~^R~ Run", Run),
new(Key.F | Key.CtrlMask, "~^F~ Find", Find),
new(Key.N | Key.CtrlMask, "~^N~ New", New),
new(Key.F5, "~F5~ Refresh", Publish)
Expand Down Expand Up @@ -252,21 +253,18 @@ private void _treeView_ObjectActivated(ObjectActivatedEventArgs<object> obj)

private string AspectGetter(object model)
{
if (model is IContainer container) return $"{container} ({container.Operation})";

if (model is CohortAggregateContainer setContainer) return $"{setContainer} ({setContainer.Operation})";

if (model is ExtractionInformation ei) return $"{ei} ({ei.ExtractionCategory})";

if (model is CatalogueItemsNode cin) return $"{cin} ({cin.CatalogueItems.Length})";

if (model is TableInfoServerNode server) return $"{server.ServerName} ({server.DatabaseType})";

if (model is IDisableable d) return d.IsDisabled ? $"{d} (Disabled)" : d.ToString();

return model is IArgument arg
? $"{arg} ({(string.IsNullOrWhiteSpace(arg.Value) ? "Null" : arg.Value)})"
: model?.ToString() ?? "Null Object";
return model switch
{
IContainer container => $"{container} ({container.Operation})",
CohortAggregateContainer setContainer => $"{setContainer} ({setContainer.Operation})",
ExtractionInformation ei => $"{ei} ({ei.ExtractionCategory})",
CatalogueItemsNode cin => $"{cin} ({cin.CatalogueItems.Length})",
TableInfoServerNode server => $"{server.ServerName} ({server.DatabaseType})",
IDisableable d => d.IsDisabled ? $"{d} (Disabled)" : d.ToString(),
_ => model is IArgument arg
? $"{arg} ({(string.IsNullOrWhiteSpace(arg.Value) ? "Null" : arg.Value)})"
: model?.ToString() ?? "Null Object"
};
}

private void Publish()
Expand Down Expand Up @@ -337,7 +335,7 @@ private void Show(object selected)
_treeView.SetNeedsDisplay();
}

private void _treeView_SelectionChanged(object sender, SelectionChangedEventArgs<object> e)
private void _treeView_SelectionChanged(object sender, [NotNull] SelectionChangedEventArgs<object> e)
{
if (e.NewValue != null)
_treeView.RefreshObject(e.NewValue);
Expand All @@ -346,13 +344,9 @@ private void _treeView_SelectionChanged(object sender, SelectionChangedEventArgs
private void Menu()
{
var factory = new ConsoleGuiContextMenuFactory(_activator);
var menu = factory.Create(_treeView.GetAllSelectedObjects().ToArray(), _treeView.SelectedObject);

if (menu == null)
return;

menu.Position = DateTime.Now.Subtract(_lastMouseMove).TotalSeconds < 1 ? _lastMousePos : new Point(10, 5);
menu.Show();
var position = DateTime.Now.Subtract(_lastMouseMove).TotalSeconds < 1 ? _lastMousePos : new Point(10, 5);
var menu = factory.Create(position.X,position.Y,_treeView.GetAllSelectedObjects().ToArray(), _treeView.SelectedObject);
menu?.Show();
}


Expand Down

0 comments on commit 0d808a5

Please sign in to comment.