From 5dcecb8cc1f500001879ae144e3dc904b3a1481f Mon Sep 17 00:00:00 2001 From: Sander Schobers Date: Mon, 19 Jul 2021 07:59:02 +0200 Subject: [PATCH] Added clipping for Overflow/ScrollControl. --- ui/overflow.go | 36 ++++++++++++++++++++++++++++++++---- 1 file changed, 32 insertions(+), 4 deletions(-) diff --git a/ui/overflow.go b/ui/overflow.go index 3f8b7ad..133ec2b 100644 --- a/ui/overflow.go +++ b/ui/overflow.go @@ -11,6 +11,9 @@ type overflow struct { Background color.Color + ClipHorizontal bool + ClipVertical bool + barWidth float32 desired geom.PointF32 bounds geom.RectangleF32 @@ -26,18 +29,30 @@ type overflow struct { type ScrollControl interface { Control + SetBackgroundColor(color.Color) + SetClipHorizontal(bool) + SetClipVertical(bool) SetScrollbarColor(bar color.Color, hover color.Color) } +func BuildOverflow(content Control, build func(c ScrollControl)) ScrollControl { + o := &overflow{Proxy: Proxy{Content: content}} + o.hor = BuildScrollbar(OrientationHorizontal, func(*Scrollbar) {}) + o.ver = BuildScrollbar(OrientationVertical, func(*Scrollbar) {}) + if build != nil { + build(o) + } + return o +} + func Overflow(content Control) ScrollControl { return OverflowBackground(content, nil) } func OverflowBackground(content Control, back color.Color) ScrollControl { - var o = &overflow{Proxy: Proxy{Content: content}, Background: back} - o.hor = BuildScrollbar(OrientationHorizontal, func(*Scrollbar) {}) - o.ver = BuildScrollbar(OrientationVertical, func(*Scrollbar) {}) - return o + return BuildOverflow(content, func(c ScrollControl) { + c.SetBackgroundColor(back) + }) } func (o *overflow) shouldScroll(bounds geom.RectangleF32) (hor bool, ver bool) { @@ -53,6 +68,8 @@ func (o *overflow) shouldScroll(bounds geom.RectangleF32) (hor bool, ver bool) { if hor && !ver { ver = scroll(size.Y+o.barWidth, bounds.Dy()) } + hor = hor && !o.ClipHorizontal + ver = ver && !o.ClipVertical return } @@ -158,6 +175,17 @@ func (o *overflow) Render(ctx Context) { bar.Render(ctx) }) } +func (o *overflow) SetBackgroundColor(c color.Color) { + o.Background = c +} + +func (o *overflow) SetClipHorizontal(clip bool) { + o.ClipHorizontal = clip +} + +func (o *overflow) SetClipVertical(clip bool) { + o.ClipVertical = clip +} func (o *overflow) SetScrollbarColor(bar color.Color, hover color.Color) { o.hor.BarColor = bar