Skip to content

Commit

Permalink
Made IListDataSource render usable without external fields set (#161)
Browse files Browse the repository at this point in the history
  • Loading branch information
distantcam authored and migueldeicaza committed Jan 8, 2019
1 parent a88cb16 commit 7f94997
Showing 1 changed file with 10 additions and 18 deletions.
28 changes: 10 additions & 18 deletions Terminal.Gui/Views/ListView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public interface IListDataSource {
/// <remarks>
/// The default color will be set before this method is invoked, and will be based on whether the item is selected or not.
/// </remarks>
void Render (bool selected, int item, int col, int line, int width);
void Render (ListView container, ConsoleDriver driver, bool selected, int item, int col, int line, int width);

/// <summary>
/// Should return whether the specified item is currently marked.
Expand Down Expand Up @@ -96,8 +96,6 @@ public class ListView : View {
//
class ListWrapper : IListDataSource {
IList src;
public ListView Container;
public ConsoleDriver Driver;
BitArray marks;
int count;

Expand All @@ -110,7 +108,7 @@ public ListWrapper (IList source)

public int Count => src.Count;

void RenderUstr (ustring ustr, int col, int line, int width)
void RenderUstr (ConsoleDriver driver, ustring ustr, int col, int line, int width)
{
int byteLen = ustr.Length;
int used = 0;
Expand All @@ -119,25 +117,25 @@ void RenderUstr (ustring ustr, int col, int line, int width)
var count = Rune.ColumnWidth (rune);
if (used+count >= width)
break;
Driver.AddRune (rune);
driver.AddRune (rune);
used += count;
i += size;
}
for (; used < width; used++) {
Driver.AddRune (' ');
driver.AddRune (' ');
}
}

public void Render (bool marked, int item, int col, int line, int width)
public void Render (ListView container, ConsoleDriver driver, bool marked, int item, int col, int line, int width)
{
Container.Move (col, line);
container.Move (col, line);
var t = src [item];
if (t is ustring) {
RenderUstr (t as ustring, col, line, width);
RenderUstr (driver, (ustring)t, col, line, width);
} else if (t is string) {
RenderUstr (t as string, col, line, width);
RenderUstr (driver, (string)t, col, line, width);
} else
RenderUstr (t.ToString (), col, line, width);
RenderUstr (driver, t.ToString (), col, line, width);
}

public bool IsMarked (int item)
Expand Down Expand Up @@ -179,8 +177,6 @@ public void SetSource (IList source)
Source = null;
else {
Source = MakeWrapper (source);
((ListWrapper)Source).Container = this;
((ListWrapper)Source).Driver = Driver;
}
}

Expand Down Expand Up @@ -245,8 +241,6 @@ static IListDataSource MakeWrapper (IList source)
/// <param name="source">An IList data source, if the elements of the IList are strings or ustrings, the string is rendered, otherwise the ToString() method is invoked on the result.</param>
public ListView (IList source) : this (MakeWrapper (source))
{
((ListWrapper)(Source)).Container = this;
((ListWrapper)(Source)).Driver = Driver;
}

/// <summary>
Expand All @@ -273,8 +267,6 @@ public ListView () : base ()
/// <param name="source">An IList data source, if the elements of the IList are strings or ustrings, the string is rendered, otherwise the ToString() method is invoked on the result.</param>
public ListView (Rect rect, IList source) : this (rect, MakeWrapper (source))
{
((ListWrapper)(Source)).Container = this;
((ListWrapper)(Source)).Driver = Driver;
}

/// <summary>
Expand Down Expand Up @@ -315,7 +307,7 @@ public override void Redraw(Rect region)
for (int c = 0; c < f.Width; c++)
Driver.AddRune(' ');
} else {
Source.Render(isSelected, item, 0, row, f.Width);
Source.Render(this, Driver, isSelected, item, 0, row, f.Width);
}
}
}
Expand Down

0 comments on commit 7f94997

Please sign in to comment.