zntg/ui/scroll.go
2018-09-22 16:31:09 +02:00

78 lines
1.8 KiB
Go

package ui
import (
"opslag.de/schobers/zntg/allg5"
"opslag.de/schobers/geom"
)
type scroll struct {
Wrapper
Content Control
Bar *ContentScrollbar
}
func Scroll(c Control, o Orientation) Control {
var dock = NewDockPanel(nil)
var bar = &ContentScrollbar{Orientation: o}
switch o {
case OrientationHorizontal:
dock.Append(DockBottom, TopMargin(bar, ScrollbarWidth))
dock.Append(DockBottom, c)
case OrientationVertical:
dock.Append(DockRight, LeftMargin(bar, ScrollbarWidth))
dock.Append(DockRight, c)
}
var s = &scroll{Wrap(dock), c, bar}
bar.OnChanged = func(v float64) {
}
return s
}
func (s *scroll) Handle(ctx Context, ev allg5.Event) {
s.Wrapper.Handle(ctx, ev)
switch e := ev.(type) {
case *allg5.MouseMoveEvent:
if 0 != e.DeltaZ && !s.Bar.IsOver && geom.PtF(float64(e.X), float64(e.Y)).In(s.Bounds) {
var d = e.DeltaZ
if allg5.IsAnyKeyDown(allg5.KeyLShift, allg5.KeyRShift) {
d *= 10
}
s.Bar.increment(d)
}
}
}
func (s *scroll) Render(ctx Context) {
var bounds = s.Content.Rect()
var w, h = bounds.Dx(), bounds.Dy()
var bmp, err = allg5.NewVideoBitmap(int(w), int(h))
if nil != err {
return
}
defer bmp.Destroy()
bmp.SetAsTarget()
switch s.Bar.Orientation {
case OrientationHorizontal:
Arrange(ctx, s.Content, geom.RectF(-s.Bar.Value, 0, w, h))
case OrientationVertical:
Arrange(ctx, s.Content, geom.RectF(0, -s.Bar.Value, w, h))
}
s.Content.Render(ctx)
ctx.Display().SetAsTarget()
var min = s.Bounds.Min.To32()
bmp.Draw(min.X, min.Y)
s.Bar.Render(ctx)
}
func (s *scroll) Arrange(ctx Context, rect geom.RectangleF) {
var sz = s.Content.DesiredSize(ctx)
switch s.Bar.Orientation {
case OrientationHorizontal:
s.Bar.Length = sz.X
case OrientationVertical:
s.Bar.Length = sz.Y
}
s.Wrapper.Arrange(ctx, rect)
}