20 changed files with 259 additions and 83 deletions
@ -0,0 +1,147 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using System.Collections.Generic; |
||||
|
using System.Linq; |
||||
|
using Avalonia.Controls; |
||||
|
|
||||
|
namespace Avalonia.Diagnostics.Views |
||||
|
{ |
||||
|
/// <summary>
|
||||
|
/// A simple grid control that lays out columns with a equal width and rows to their desired
|
||||
|
/// size.
|
||||
|
/// </summary>
|
||||
|
/// <remarks>
|
||||
|
/// This is used in the devtools because our <see cref="Grid"/> performance sucks.
|
||||
|
/// </remarks>
|
||||
|
public class SimpleGrid : Panel |
||||
|
{ |
||||
|
private List<double> _columnWidths = new List<double>(); |
||||
|
private List<double> _rowHeights = new List<double>(); |
||||
|
private double _totalWidth; |
||||
|
private double _totalHeight; |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the Column attached property.
|
||||
|
/// </summary>
|
||||
|
public static readonly AttachedProperty<int> ColumnProperty = |
||||
|
AvaloniaProperty.RegisterAttached<SimpleGrid, Control, int>("Column"); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Defines the Row attached property.
|
||||
|
/// </summary>
|
||||
|
public static readonly AttachedProperty<int> RowProperty = |
||||
|
AvaloniaProperty.RegisterAttached<SimpleGrid, Control, int>("Row"); |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the value of the Column attached property for a control.
|
||||
|
/// </summary>
|
||||
|
/// <param name="control">The control.</param>
|
||||
|
/// <returns>The control's column.</returns>
|
||||
|
public static int GetColumn(IControl control) |
||||
|
{ |
||||
|
return control.GetValue(ColumnProperty); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Gets the value of the Row attached property for a control.
|
||||
|
/// </summary>
|
||||
|
/// <param name="control">The control.</param>
|
||||
|
/// <returns>The control's row.</returns>
|
||||
|
public static int GetRow(IControl control) |
||||
|
{ |
||||
|
return control.GetValue(RowProperty); |
||||
|
} |
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Sets the value of the Column attached property for a control.
|
||||
|
/// </summary>
|
||||
|
/// <param name="control">The control.</param>
|
||||
|
/// <param name="value">The column value.</param>
|
||||
|
public static void SetColumn(IControl control, int value) |
||||
|
{ |
||||
|
control.SetValue(ColumnProperty, value); |
||||
|
} |
||||
|
|
||||
|
|
||||
|
/// <summary>
|
||||
|
/// Sets the value of the Row attached property for a control.
|
||||
|
/// </summary>
|
||||
|
/// <param name="control">The control.</param>
|
||||
|
/// <param name="value">The row value.</param>
|
||||
|
public static void SetRow(IControl control, int value) |
||||
|
{ |
||||
|
control.SetValue(RowProperty, value); |
||||
|
} |
||||
|
|
||||
|
protected override Size MeasureOverride(Size availableSize) |
||||
|
{ |
||||
|
_columnWidths.Clear(); |
||||
|
_rowHeights.Clear(); |
||||
|
_totalWidth = 0; |
||||
|
_totalHeight = 0; |
||||
|
|
||||
|
foreach (var child in Children) |
||||
|
{ |
||||
|
var column = GetColumn(child); |
||||
|
var row = GetRow(child); |
||||
|
|
||||
|
child.Measure(availableSize); |
||||
|
|
||||
|
var desired = child.DesiredSize; |
||||
|
UpdateCell(_columnWidths, column, desired.Width, ref _totalWidth); |
||||
|
UpdateCell(_rowHeights, row, desired.Height, ref _totalHeight); |
||||
|
} |
||||
|
|
||||
|
return new Size(_totalWidth, _totalHeight); |
||||
|
} |
||||
|
|
||||
|
protected override Size ArrangeOverride(Size finalSize) |
||||
|
{ |
||||
|
var columnWidth = finalSize.Width / _columnWidths.Count; |
||||
|
|
||||
|
foreach (var child in Children) |
||||
|
{ |
||||
|
var column = GetColumn(child); |
||||
|
var row = GetRow(child); |
||||
|
var rect = new Rect(column * columnWidth, GetRowTop(row), columnWidth, _rowHeights[row]); |
||||
|
child.Arrange(rect); |
||||
|
} |
||||
|
|
||||
|
return new Size(finalSize.Width, _totalHeight); |
||||
|
} |
||||
|
|
||||
|
private double UpdateCell(IList<double> cells, int cell, double value, ref double total) |
||||
|
{ |
||||
|
while (cells.Count < cell + 1) |
||||
|
{ |
||||
|
cells.Add(0); |
||||
|
} |
||||
|
|
||||
|
var existing = cells[cell]; |
||||
|
|
||||
|
if (value > existing) |
||||
|
{ |
||||
|
cells[cell] = value; |
||||
|
total += value - existing; |
||||
|
return value; |
||||
|
} |
||||
|
else |
||||
|
{ |
||||
|
return existing; |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
private double GetRowTop(int row) |
||||
|
{ |
||||
|
var result = 0.0; |
||||
|
|
||||
|
for (var i = 0; i < row; ++i) |
||||
|
{ |
||||
|
result += _rowHeights[i]; |
||||
|
} |
||||
|
|
||||
|
return result; |
||||
|
} |
||||
|
} |
||||
|
} |
||||
@ -1,10 +1,10 @@ |
|||||
// Copyright (c) The Avalonia Project. All rights reserved.
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
namespace Avalonia.Controls |
namespace Avalonia.Media |
||||
{ |
{ |
||||
/// <summary>
|
/// <summary>
|
||||
/// Controls the wrapping mode in a <see cref="TextBlock"/>.
|
/// Controls the wrapping mode of text.
|
||||
/// </summary>
|
/// </summary>
|
||||
public enum TextWrapping |
public enum TextWrapping |
||||
{ |
{ |
||||
@ -0,0 +1,47 @@ |
|||||
|
// Copyright (c) The Avalonia Project. All rights reserved.
|
||||
|
// Licensed under the MIT license. See licence.md file in the project root for full license information.
|
||||
|
|
||||
|
using Avalonia.Controls; |
||||
|
using Avalonia.Layout; |
||||
|
using Avalonia.Media; |
||||
|
using Xunit; |
||||
|
|
||||
|
#if AVALONIA_CAIRO
|
||||
|
namespace Avalonia.Cairo.RenderTests.Controls |
||||
|
#elif AVALONIA_SKIA
|
||||
|
namespace Avalonia.Skia.RenderTests |
||||
|
#else
|
||||
|
namespace Avalonia.Direct2D1.RenderTests.Controls |
||||
|
#endif
|
||||
|
{ |
||||
|
public class TextBlockTests : TestBase |
||||
|
{ |
||||
|
public TextBlockTests() |
||||
|
: base(@"Controls\TextBlock") |
||||
|
{ |
||||
|
} |
||||
|
|
||||
|
[Fact] |
||||
|
public void Wrapping_NoWrap() |
||||
|
{ |
||||
|
Decorator target = new Decorator |
||||
|
{ |
||||
|
Padding = new Thickness(8), |
||||
|
Width = 200, |
||||
|
Height = 200, |
||||
|
Child = new TextBlock |
||||
|
{ |
||||
|
Background = Brushes.Red, |
||||
|
FontSize = 12, |
||||
|
Foreground = Brushes.Black, |
||||
|
Text = "Neque porro quisquam est qui dolorem ipsum quia dolor sit amet, consectetur, adipisci velit", |
||||
|
VerticalAlignment = VerticalAlignment.Top, |
||||
|
TextWrapping = TextWrapping.NoWrap, |
||||
|
} |
||||
|
}; |
||||
|
|
||||
|
RenderToFile(target); |
||||
|
CompareImages(); |
||||
|
} |
||||
|
} |
||||
|
} |
||||
|
After Width: | Height: | Size: 1.2 KiB |
Loading…
Reference in new issue