Skip to content
This repository has been archived by the owner on Sep 4, 2024. It is now read-only.

Fixes issue rendering when node is collapsed and text has new lines #794

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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
26 changes: 19 additions & 7 deletions TestApps/Samples/Samples/TreeViewEvents.cs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public TreeViewEvents ()
tree.Columns.Add (col);

var node = store.AddNode ().SetValue (nodeField, new TextNode ("Root 1"));
var child = node.AddChild ().SetValue (nodeField, new TextNode ("Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text."));
var child = node.AddChild ().SetValue (nodeField, new TextNode ("Very long text with NewLines. Very long text with NewLines Very long text with NewLines\n\nVery long text with NewLines.\n\nVery long text. Very long text. Very long text. Very long text."));
node = store.AddNode ().SetValue (nodeField, new TextNode ("Root 2"));
child = node.AddChild ().SetValue (nodeField, new TextNode ("Short text. Short text. Short text."));
child.AddChild ().SetValue (nodeField, new TextNode ("Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text. Very long text."));
Expand Down Expand Up @@ -113,18 +113,25 @@ protected override Size OnGetRequiredSize ()
var node = GetValue (NodeField);
var status = GetViewStatus (node);

TextLayout layout = new TextLayout ();
layout.Text = node.Text;
var layout = new TextLayout ();
var newLineIndex = node.Text.IndexOf('\n');
if (!status.Expanded && newLineIndex > -1) {
layout.Text = node.Text.Substring(0, newLineIndex);
} else {
layout.Text = node.Text;
}

var textSize = layout.GetSize ();

// When in expanded mode, the height of the row depends on the width. Since we don't know the width,
// let's use the last width that was used for rendering.

if (status.Expanded && status.LastRenderWidth != 0 && layout.GetSize ().Width > status.LastRenderWidth) {
if (status.Expanded && status.LastRenderWidth != 0 && textSize.Width > status.LastRenderWidth) {
layout.Width = status.LastRenderWidth - addImage.Width - MoreLinkSpacing;
textSize = layout.GetSize ();
}

//in cases when there are multiple lines and they don't execeed the max width we need to care height include the expand
status.LastCalculatedHeight = textSize.Height;

return new Size (30, textSize.Height);
Expand All @@ -147,9 +154,14 @@ protected override void OnDraw (Context ctx, Rectangle cellArea)
layout.SetBackground (Colors.LightBlue, Math.Min (selectionStart, selectionEnd), Math.Abs (selectionEnd - selectionStart));

// Text doesn't fit. We need to render the expand icon
if (textSize.Width > cellArea.Width || node.Text.IndexOf('\n') > -1) {

layout.Width = Math.Max(1, cellArea.Width - addImage.Width - MoreLinkSpacing);

if (textSize.Height > cellArea.Height) {
layout.Height = cellArea.Height;
}

if (textSize.Width > cellArea.Width) {
layout.Width = Math.Max (1, cellArea.Width - addImage.Width - MoreLinkSpacing);
if (!status.Expanded)
layout.Trimming = TextTrimming.WordElipsis;
else
Expand Down Expand Up @@ -191,7 +203,7 @@ void CalcLayout (out TextLayout layout, out Rectangle cellArea, out Rectangle ex
layout = new TextLayout ();
layout.Text = node.Text;
var textSize = layout.GetSize ();
if (textSize.Width > cellArea.Width) {
if (textSize.Width > cellArea.Width || node.Text.IndexOf('\n') > -1) {
layout.Width = Math.Max (1, cellArea.Width - addImage.Width - MoreLinkSpacing);
if (!status.Expanded)
layout.Trimming = TextTrimming.WordElipsis;
Expand Down