From 64d14d325dae20863156c4db6ccb9b9aa72bb5ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20VERBEKE?= <67188400+aurelienverbeke@users.noreply.github.com> Date: Sun, 6 Feb 2022 23:43:20 +0100 Subject: [PATCH] Fixes #1577 - Add Support for Colored ListView Items (#1578) * We can now choose the color (background and foreground/text) we want to display each item. * Added RowRender event. * Colored ListView in action. Co-authored-by: BDisp --- Terminal.Gui/Views/ListView.cs | 44 ++++++++++++++++++++ UICatalog/Scenarios/ListViewWithSelection.cs | 18 ++++++++ 2 files changed, 62 insertions(+) diff --git a/Terminal.Gui/Views/ListView.cs b/Terminal.Gui/Views/ListView.cs index b988cfb9d..df7bcd7af 100644 --- a/Terminal.Gui/Views/ListView.cs +++ b/Terminal.Gui/Views/ListView.cs @@ -347,6 +347,12 @@ namespace Terminal.Gui { for (int c = 0; c < f.Width; c++) Driver.AddRune (' '); } else { + var rowEventArgs = new ListViewRowEventArgs (item); + OnRowRender (rowEventArgs); + if (rowEventArgs.RowAttribute != null && current != rowEventArgs.RowAttribute) { + current = (Attribute)rowEventArgs.RowAttribute; + Driver.SetAttribute (current); + } if (allowsMarking) { Driver.AddRune (source.IsMarked (item) ? (AllowsMultipleSelection ? Driver.Checked : Driver.Selected) : (AllowsMultipleSelection ? Driver.UnChecked : Driver.UnSelected)); Driver.AddRune (' '); @@ -366,6 +372,11 @@ namespace Terminal.Gui { /// public event Action OpenSelectedItem; + /// + /// This event is invoked when this is being drawn before rendering. + /// + public event Action RowRender; + /// public override bool ProcessKey (KeyEvent kb) { @@ -665,6 +676,15 @@ namespace Terminal.Gui { return true; } + /// + /// Virtual method that will invoke the . + /// + /// + public virtual void OnRowRender (ListViewRowEventArgs rowEventArgs) + { + RowRender?.Invoke (rowEventArgs); + } + /// public override bool OnEnter (View view) { @@ -921,4 +941,28 @@ namespace Terminal.Gui { Value = value; } } + + /// + /// used by the event. + /// + public class ListViewRowEventArgs : EventArgs { + /// + /// The current row being rendered. + /// + public int Row { get; } + /// + /// The used by current row or + /// null to maintain the current attribute. + /// + public Attribute? RowAttribute { get; set; } + + /// + /// Initializes with the current row. + /// + /// + public ListViewRowEventArgs (int row) + { + Row = row; + } + } } diff --git a/UICatalog/Scenarios/ListViewWithSelection.cs b/UICatalog/Scenarios/ListViewWithSelection.cs index 1f7608acf..6f7e1f1be 100644 --- a/UICatalog/Scenarios/ListViewWithSelection.cs +++ b/UICatalog/Scenarios/ListViewWithSelection.cs @@ -4,6 +4,7 @@ using System.Collections; using System.Collections.Generic; using System.Linq; using Terminal.Gui; +using Attribute = Terminal.Gui.Attribute; namespace UICatalog.Scenarios { [ScenarioMetadata (Name: "List View With Selection", Description: "ListView with columns and selection")] @@ -53,6 +54,7 @@ namespace UICatalog.Scenarios { AllowsMarking = false, AllowsMultipleSelection = false }; + _listView.RowRender += ListView_RowRender; Win.Add (_listView); var _scrollBar = new ScrollBarView (_listView, true); @@ -92,6 +94,22 @@ namespace UICatalog.Scenarios { Win.Add (keepCheckBox); } + private void ListView_RowRender (ListViewRowEventArgs obj) + { + if (obj.Row == _listView.SelectedItem) { + return; + } + if (_listView.AllowsMarking && _listView.Source.IsMarked (obj.Row)) { + obj.RowAttribute = new Attribute (Color.BrightRed, Color.BrightYellow); + return; + } + if (obj.Row % 2 == 0) { + obj.RowAttribute = new Attribute (Color.BrightGreen, Color.Magenta); + } else { + obj.RowAttribute = new Attribute (Color.BrightMagenta, Color.Green); + } + } + private void _customRenderCB_Toggled (bool prev) { if (prev) {