Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wpf: Drawable updates for large canvases in a scrollable #2707

Merged
Merged
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
49 changes: 35 additions & 14 deletions src/Eto.Wpf/Forms/Controls/DrawableHandler.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ namespace Eto.Wpf.Forms.Controls
public class DrawableHandler : DrawableHandler<swc.Canvas, Drawable, Drawable.ICallback> { }

public class DrawableHandler<TControl, TWidget, TCallback> : WpfPanel<TControl, TWidget, TCallback>, Drawable.IHandler
where TControl : swc.Canvas
where TWidget : Drawable
where TCallback : Drawable.ICallback
where TControl : swc.Canvas
where TWidget : Drawable
where TCallback : Drawable.ICallback
{
bool tiled;
sw.FrameworkElement content;
Expand All @@ -27,6 +27,8 @@ public bool OptimizedInvalidateRect

public bool AllowTiling { get; set; }

public bool PartialInvalidate { get; set; }

public bool SupportsCreateGraphics { get { return false; } }

public Size TileSize
Expand Down Expand Up @@ -66,13 +68,9 @@ protected override void OnRender(swm.DrawingContext dc)
if (!Handler.tiled)
{
var rect = new sw.Rect(0, 0, ActualWidth, ActualHeight);
var cliprect = rect.ToEto();
if (!cliprect.IsEmpty)
if (!rect.IsEmpty)
{
using (var graphics = new Graphics(new GraphicsHandler(this, dc, rect, new RectangleF(Handler.ClientSize), false)))
{
Handler.Callback.OnPaint(Handler.Widget, new PaintEventArgs(graphics, cliprect));
}
Handler.Render(this, dc, rect);
}
}
}
Expand All @@ -88,7 +86,7 @@ private sw.Size ContentMeasureOverride(sw.Size constraint)
var content = Handler.content;
if (content == null)
return size;

// content should be used to measure, if present
content.Measure(constraint);
return content.DesiredSize;
Expand All @@ -104,6 +102,22 @@ protected override sw.Size ArrangeOverride(sw.Size arrangeSize)
}
}

private void Render(swm.Visual canvas, swm.DrawingContext dc, sw.Rect rect)
{
var cliprect = rect.ToEtoF();
if (PartialInvalidate && scrollable != null)
{
var visibleRect = new RectangleF(scrollable.VisibleRect.Size);
visibleRect = scrollable.RectangleToScreen(visibleRect);
visibleRect = Widget.RectangleFromScreen(visibleRect);
cliprect.Intersect(visibleRect);
}
using (var graphics = new Graphics(new GraphicsHandler(canvas, dc, rect, cliprect, false)))
{
Callback.OnPaint(Widget, new PaintEventArgs(graphics, cliprect));
}
}

class EtoTile : sw.FrameworkElement
{
Rectangle bounds;
Expand Down Expand Up @@ -181,7 +195,8 @@ public void Create() { }

public void Create(bool largeCanvas)
{
AllowTiling = largeCanvas;
// AllowTiling = largeCanvas;
PartialInvalidate = largeCanvas;
Create();
}

Expand Down Expand Up @@ -237,12 +252,14 @@ void UnRegisterScrollable()
void RegisterScrollable()
{
UnRegisterScrollable();
if (AllowTiling)
if (AllowTiling || PartialInvalidate)
{
scrollable = Widget.FindParent<Scrollable>();
if (scrollable != null)
scrollable.Scroll += scrollable_Scroll;

}
if (AllowTiling)
{
Control.SizeChanged += Control_SizeChanged;
SetMaxTiles();
tiled = true;
Expand Down Expand Up @@ -274,7 +291,7 @@ void UpdateTiles(bool rebuildKeys = false)
if (scroll != null)
{
// only show tiles in the visible rect of the scrollable
var visibleRect = new Rectangle(scroll.ClientSize);
var visibleRect = new Rectangle(scroll.VisibleRect.Size);
var scrollableHandler = (ScrollableHandler)scroll.Handler;
visibleRect.Offset(-Control.TranslatePoint(new sw.Point(), scrollableHandler.ContentControl).ToEtoPoint());
rect.Intersect(visibleRect);
Expand Down Expand Up @@ -365,6 +382,10 @@ void UpdateTiles(bool rebuildKeys = false)

void scrollable_Scroll(object sender, ScrollEventArgs e)
{
if (PartialInvalidate)
{
Invalidate(false);
}
UpdateTiles();
}

Expand Down
3 changes: 0 additions & 3 deletions src/Eto.Wpf/Forms/Controls/ScrollableHandler.cs
100644 → 100755
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,6 @@ public Point ScrollPosition
{
get
{
EnsureLoaded();
return new Point((int)scroller.HorizontalOffset, (int)scroller.VerticalOffset);
}
set
Expand All @@ -124,7 +123,6 @@ public Size ScrollSize
{
get
{
EnsureLoaded();
return new Size((int)scroller.ExtentWidth, (int)scroller.ExtentHeight);
}
set
Expand All @@ -149,7 +147,6 @@ public override Size ClientSize
{
if (!Widget.Loaded)
return Size;
EnsureLoaded();
var info = scroller.GetScrollInfo();
return info != null ? new Size((int)info.ViewportWidth, (int)info.ViewportHeight) : Size.Empty;
}
Expand Down
4 changes: 2 additions & 2 deletions test/Eto.Test/Sections/Controls/DrawableSection.cs
Original file line number Diff line number Diff line change
Expand Up @@ -92,9 +92,9 @@ Control WithBackground()

Control LargeCanvas()
{
var control = new Drawable
var control = new Drawable(true)
{
Size = new Size(1000, 1000),
Size = new Size(2000, 2000),
BackgroundColor = Colors.Blue
};
var image = TestIcons.TestImage;
Expand Down
Loading