Skip to content
Open
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
6 changes: 4 additions & 2 deletions src/Runtime/Runtime/System.Windows.Controls/TextBlock.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,9 +356,11 @@ protected override Size MeasureOverride(Size availableSize)
//return actualSize;
Size BorderThicknessSize = new Size(BorderThickness.Left + BorderThickness.Right, BorderThickness.Top + BorderThickness.Bottom);

string uniqueIdentifier = ((INTERNAL_HtmlDomElementReference)this.INTERNAL_OuterDomElement).UniqueIdentifier;

if (noWrapSize == Size.Empty)
{
noWrapSize = Application.Current.TextMeasurementService.MeasureTextBlock(Text ?? String.Empty, FontSize, FontFamily, FontStyle, FontWeight, /*FontStretch, */TextWrapping.NoWrap, Padding, Double.PositiveInfinity);
noWrapSize = Application.Current.TextMeasurementService.MeasureTextBlock(uniqueIdentifier, Text, TextWrapping.NoWrap, Padding, Double.PositiveInfinity);
noWrapSize = noWrapSize.Add(BorderThicknessSize);
}

Expand All @@ -367,7 +369,7 @@ protected override Size MeasureOverride(Size availableSize)
return noWrapSize;
}

Size TextSize = Application.Current.TextMeasurementService.MeasureTextBlock(Text ?? String.Empty, FontSize, FontFamily, FontStyle, FontWeight, /*FontStretch, */TextWrapping, Padding, (availableSize.Width - BorderThicknessSize.Width).Max(0));
Size TextSize = Application.Current.TextMeasurementService.MeasureTextBlock(uniqueIdentifier, Text, TextWrapping, Padding, (availableSize.Width - BorderThicknessSize.Width).Max(0));
TextSize = TextSize.Add(BorderThicknessSize);

return TextSize;
Expand Down
8 changes: 2 additions & 6 deletions src/Runtime/Runtime/System.Windows.Controls/TextBox.cs
Original file line number Diff line number Diff line change
Expand Up @@ -567,13 +567,9 @@ public void Select(int start, int length)

protected override Size MeasureOverride(Size availableSize)
{
Size BorderThicknessSize = new Size(BorderThickness.Left + BorderThickness.Right, BorderThickness.Top + BorderThickness.Bottom);
Size TextSize = Application.Current.TextMeasurementService.Measure(Text ?? String.Empty, FontSize, FontFamily, FontStyle, FontWeight, /*FontStretch, */TextWrapping, Padding, (availableSize.Width - BorderThicknessSize.Width).Max(0));
TextSize.Width = TextSize.Width + BorderThicknessSize.Width;
TextSize.Height = TextSize.Height + BorderThicknessSize.Height;
return TextSize;
return base.MeasureOverride(availableSize);
}

internal override void UpdateVisualStates()
{
if (!IsEnabled)
Expand Down
7 changes: 7 additions & 0 deletions src/Runtime/Runtime/System.Windows.Controls/TextBoxView.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1026,5 +1026,12 @@ private static bool IsRunningInJavaScript()
{
return false;
}

protected override Size MeasureOverride(Size availableSize)
{
string uniqueIdentifier = ((INTERNAL_HtmlDomElementReference)this.INTERNAL_OuterDomElement).UniqueIdentifier;
Size TextSize = Application.Current.TextMeasurementService.MeasureTextBlock(uniqueIdentifier, Host.Text, Host.TextWrapping, Margin, availableSize.Width);
return TextSize;
}
}
}
16 changes: 16 additions & 0 deletions src/Runtime/Runtime/System.Windows/LayoutManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -167,6 +167,22 @@ public void UpdateLayout()
}
}

public bool CheckChildMeasureValidation(UIElement parent)
{
lock (queueLock)
{
foreach (UIElement element in measureQueue)
{
foreach (UIElement pathElement in GetElementPath(element))
{
if (pathElement == parent)
return true;
}
}
}
return false;
}

private UIElement GetTopElement(IEnumerable<UIElement> measureQueue)
{
UIElement topElement = null;
Expand Down
55 changes: 6 additions & 49 deletions src/Runtime/Runtime/System.Windows/TextMeasurementService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,12 +30,8 @@ Size Measure(string text,
Thickness padding,
double maxWidth);

Size MeasureTextBlock(string text,
double fontSize,
FontFamily fontFamily,
FontStyle style,
FontWeight weight,
/*FontStretch stretch,*/
Size MeasureTextBlock(string uid,
string text,
TextWrapping wrapping,
Thickness padding,
double maxWidth);
Expand Down Expand Up @@ -72,10 +68,6 @@ internal class TextMeasurementService : ITextMeasurementService
private string measureTextBoxElementID;
private string measureTextBlockElementID;

private double savedTextBlockFontSize;
private string savedTextBlockFontFamily;
private FontStyle savedTextBlockFontStyle;
private FontWeight savedTextBlockFontWeight;
private TextWrapping savedTextBlockTextWrapping;
private Thickness savedTextBlockPadding;

Expand All @@ -84,14 +76,6 @@ public TextMeasurementService()
measureTextBoxElementID = "";
measureTextBlockElementID = "";

savedTextBlockFontSize = 0;
savedTextBlockFontFamily = "";
#if MIGRATION
savedTextBlockFontStyle = FontStyles.Normal;
#else
savedTextBlockFontStyle = FontStyle.Normal;
#endif
savedTextBlockFontWeight.Weight = 0;
savedTextBlockTextWrapping = TextWrapping.NoWrap;
savedTextBlockPadding = new Thickness(double.NegativeInfinity);
}
Expand Down Expand Up @@ -197,12 +181,8 @@ public Size Measure(string text,
return new Size(associatedTextBox.ActualWidth + 2, associatedTextBox.ActualHeight);
}

public Size MeasureTextBlock(string text,
double fontSize,
FontFamily fontFamily,
FontStyle style,
FontWeight weight,
/*FontStretch stretch,*/
public Size MeasureTextBlock(string uid,
string text,
TextWrapping wrapping,
Thickness padding,
double maxWidth)
Expand All @@ -211,15 +191,12 @@ public Size MeasureTextBlock(string text,
{
return new Size();
}

#if OPENSILVER
string strText = String.IsNullOrEmpty(text) ? "A" : INTERNAL_HtmlDomManager.EscapeStringForUseInJavaScript(text);
#elif BRIDGE
string strText = String.IsNullOrEmpty(text) ? "A" : text;
#endif
string strFontSize = (Math.Floor(fontSize * 1000) / 1000).ToString(CultureInfo.InvariantCulture) + "px";
string strFontFamily = fontFamily != null ? INTERNAL_FontsHelper.LoadFont(fontFamily.Source, (UIElement)associatedTextBlock) : "-";
string strFontStyle = style.ToString().ToLower();
string strFontWeight = weight.Weight.ToInvariantString();
string strTextWrapping = wrapping == TextWrapping.Wrap ? "pre-wrap" : "pre";
string strPadding = $"{padding.Top.ToInvariantString()}px {padding.Right.ToInvariantString()}px {padding.Bottom.ToInvariantString()}px {padding.Left.ToInvariantString()}px";
string strWidth = "";
Expand All @@ -235,26 +212,6 @@ public Size MeasureTextBlock(string text,
strMaxWidth = maxWidth.ToInvariantString() + "px";
}

if (savedTextBlockFontSize == fontSize)
strFontSize = "";
else
savedTextBlockFontSize = fontSize;

if (savedTextBlockFontFamily == strFontFamily)
strFontFamily = "";
else
savedTextBlockFontFamily = strFontFamily;

if (savedTextBlockFontStyle == style)
strFontStyle = "";
else
savedTextBlockFontStyle = style;

if (savedTextBlockFontWeight == weight)
strFontWeight = "";
else
savedTextBlockFontWeight = weight;

if (savedTextBlockTextWrapping == wrapping)
strTextWrapping = "";
else
Expand All @@ -265,7 +222,7 @@ public Size MeasureTextBlock(string text,
else
savedTextBlockPadding = padding;

string javaScriptCodeToExecute = $@"document.measureTextBlock(""{strText}"",""{strFontSize}"",""{strFontFamily}"",""{strFontStyle}"",""{strFontWeight}"",""{strTextWrapping}"",""{strPadding}"",""{strWidth}"",""{strMaxWidth}"")";
string javaScriptCodeToExecute = $@"document.measureTextBlock(""{uid}"",""{strText}"",""{strTextWrapping}"",""{strPadding}"",""{strWidth}"",""{strMaxWidth}"")";
#if OPENSILVER
string strTextSize = Convert.ToString(OpenSilver.Interop.ExecuteJavaScript(javaScriptCodeToExecute));
#elif BRIDGE
Expand Down
23 changes: 13 additions & 10 deletions src/Runtime/Runtime/System.Windows/UIElement.cs
Original file line number Diff line number Diff line change
Expand Up @@ -1606,23 +1606,26 @@ public void Measure(Size availableSize)
{
DesiredSize = new Size();
previousDesiredSize = Size.Empty;
return;
}
else if (previousMeasureValid && savedPreviousAvailableSize.IsClose(availableSize) && previousDesiredSize != Size.Empty)
{
DesiredSize = previousDesiredSize;
}
else
{
Size previousDesiredSizeInMeasure = this.DesiredSize;
DesiredSize = MeasureCore(availableSize);
if (previousDesiredSizeInMeasure != DesiredSize)
if (LayoutManager.Current.CheckChildMeasureValidation(this) == false)
{
this.InvalidateArrange();
DesiredSize = previousDesiredSize;
return;
}
}

PreviousAvailableSize = availableSize;
previousDesiredSize = DesiredSize;
Size previousDesiredSizeInMeasure = this.DesiredSize;
DesiredSize = MeasureCore(availableSize);
if (previousDesiredSizeInMeasure != DesiredSize)
{
this.InvalidateArrange();
}

PreviousAvailableSize = availableSize;
previousDesiredSize = DesiredSize;
}
}

Expand Down
29 changes: 10 additions & 19 deletions src/Runtime/Scripts/cshtml5.js
Original file line number Diff line number Diff line change
Expand Up @@ -478,32 +478,23 @@ document.setPosition = function(id, left, top, bSetAbsolutePosition, bSetZeroMar
}
}

document.measureTextBlock = function(text, fontSize, fontFamily, fontStyle, fontWeight, textWrapping, padding, width, maxWidth) {
document.measureTextBlock = function(uid, text, textWrapping, padding, width, maxWidth) {
var element = document.measureTextBlockElement;
if (element)
var elToMeasure = document.getElementById(uid);
if (element && elToMeasure)
{
var computedStyle = getComputedStyle(elToMeasure);

var runElement = element.firstElementChild;
if (runElement != null) {
runElement.innerText = text;
runElement.style.fontSize = fontSize;
runElement.style.fontWeight = fontWeight;
}

if (fontSize.length > 0) {
element.style.fontSize = fontSize;
}
if (fontFamily.length > 0) {
if (fontFamily === "-") {
fontFamily = "";
}
element.style.fontFamily = fontFamily;
}
if (fontStyle.length > 0) {
element.style.fontStyle = fontStyle;
}
if (fontWeight.length > 0) {
element.style.fontWeight = fontWeight;
}
element.style.fontSize = computedStyle.fontSize;
element.style.fontWeight = computedStyle.fontWeight;
element.style.fontFamily = computedStyle.fontFamily;
element.style.fontStyle = computedStyle.fontStyle;

if (textWrapping.length > 0) {
element.style.whiteSpace = textWrapping;
}
Expand Down