Skip to content

Commit

Permalink
Merge pull request #2707 from cwensley/curtis/wpf-largecanvas-partial…
Browse files Browse the repository at this point in the history
…-invalidate

Wpf: Drawable updates for large canvases in a scrollable
  • Loading branch information
cwensley authored Nov 27, 2024
2 parents 5c614f4 + 46dc8f1 commit f43a185
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 19 deletions.
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

0 comments on commit f43a185

Please sign in to comment.