diff --git a/elements/basic/button.go b/elements/basic/button.go index 55e0c15..f9a011f 100644 --- a/elements/basic/button.go +++ b/elements/basic/button.go @@ -121,7 +121,7 @@ func (element *Button) SetText (text string) { } func (element *Button) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() pattern, inset := theme.ButtonPattern(theme.PatternState { Case: buttonCase, @@ -130,7 +130,7 @@ func (element *Button) draw () { Pressed: element.pressed, }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) innerBounds := inset.Apply(bounds) @@ -149,5 +149,5 @@ func (element *Button) draw () { Case: buttonCase, Disabled: !element.Enabled(), }) - element.drawer.Draw(element.core, foreground, offset) + element.drawer.Draw(element, foreground, offset) } diff --git a/elements/basic/checkbox.go b/elements/basic/checkbox.go index c3bbae9..839bf21 100644 --- a/elements/basic/checkbox.go +++ b/elements/basic/checkbox.go @@ -133,13 +133,13 @@ func (element *Checkbox) SetText (text string) { } func (element *Checkbox) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() boxBounds := image.Rect(0, 0, bounds.Dy(), bounds.Dy()) backgroundPattern, _ := theme.BackgroundPattern(theme.PatternState { Case: checkboxCase, }) - artist.FillRectangle ( element.core, backgroundPattern, bounds) + artist.FillRectangle(element, backgroundPattern, bounds) pattern, inset := theme.ButtonPattern(theme.PatternState { Case: checkboxCase, @@ -147,7 +147,7 @@ func (element *Checkbox) draw () { Focused: element.Focused(), Pressed: element.pressed, }) - artist.FillRectangle(element.core, pattern, boxBounds) + artist.FillRectangle(element, pattern, boxBounds) textBounds := element.drawer.LayoutBounds() offset := image.Point { @@ -161,10 +161,10 @@ func (element *Checkbox) draw () { Case: checkboxCase, Disabled: !element.Enabled(), }) - element.drawer.Draw(element.core, foreground, offset) + element.drawer.Draw(element, foreground, offset) if element.checked { checkBounds := inset.Apply(boxBounds).Inset(2) - artist.FillRectangle(element.core, foreground, checkBounds) + artist.FillRectangle(element, foreground, checkBounds) } } diff --git a/elements/basic/container.go b/elements/basic/container.go index 822f282..2d30952 100644 --- a/elements/basic/container.go +++ b/elements/basic/container.go @@ -201,11 +201,11 @@ func (element *Container) redoAll () { element.recalculate() // draw a background - bounds := element.core.Bounds() + bounds := element.Bounds() pattern, _ := theme.BackgroundPattern (theme.PatternState { Case: containerCase, }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) // resize all elements, having them draw onto us for _, entry := range element.children { diff --git a/elements/basic/label.go b/elements/basic/label.go index 70c5a21..70a8581 100644 --- a/elements/basic/label.go +++ b/elements/basic/label.go @@ -107,19 +107,19 @@ func (element *Label) updateMinimumSize () { } func (element *Label) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() pattern, _ := theme.BackgroundPattern(theme.PatternState { Case: labelCase, }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) textBounds := element.drawer.LayoutBounds() foreground, _ := theme.ForegroundPattern (theme.PatternState { Case: labelCase, }) - element.drawer.Draw (element.core, foreground, image.Point { + element.drawer.Draw (element, foreground, image.Point { X: 0 - textBounds.Min.X, Y: 0 - textBounds.Min.Y, }) diff --git a/elements/basic/list.go b/elements/basic/list.go index aebdd67..854aa93 100644 --- a/elements/basic/list.go +++ b/elements/basic/list.go @@ -376,7 +376,7 @@ func (element *List) draw () { Disabled: !element.Enabled(), Focused: element.Focused(), }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) bounds = inset.Apply(bounds) dot := image.Point { diff --git a/elements/basic/progressbar.go b/elements/basic/progressbar.go index 24e3307..4344f2c 100644 --- a/elements/basic/progressbar.go +++ b/elements/basic/progressbar.go @@ -32,15 +32,15 @@ func (element *ProgressBar) SetProgress (progress float64) { } func (element *ProgressBar) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() pattern, inset := theme.SunkenPattern(theme.PatternState { }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) bounds = inset.Apply(bounds) meterBounds := image.Rect ( bounds.Min.X, bounds.Min.Y, bounds.Min.X + int(float64(bounds.Dx()) * element.progress), bounds.Max.Y) accent, _ := theme.AccentPattern(theme.PatternState { }) - artist.FillRectangle(element.core, accent, meterBounds) + artist.FillRectangle(element, accent, meterBounds) } diff --git a/elements/basic/scrollcontainer.go b/elements/basic/scrollcontainer.go index 69d8250..f5cb8c3 100644 --- a/elements/basic/scrollcontainer.go +++ b/elements/basic/scrollcontainer.go @@ -358,7 +358,7 @@ func (element *ScrollContainer) recalculate () { } func (element *ScrollContainer) draw () { - artist.Paste(element.core, element.child, image.Point { }) + artist.Paste(element, element.child, image.Point { }) deadPattern, _ := theme.DeadPattern(theme.PatternState { Case: scrollContainerCase, }) diff --git a/elements/basic/spacer.go b/elements/basic/spacer.go index bf56979..940c462 100644 --- a/elements/basic/spacer.go +++ b/elements/basic/spacer.go @@ -34,19 +34,19 @@ func (element *Spacer) SetLine (line bool) { } func (element *Spacer) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() if element.line { pattern, _ := theme.ForegroundPattern(theme.PatternState { Case: spacerCase, Disabled: true, }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) } else { pattern, _ := theme.BackgroundPattern(theme.PatternState { Case: spacerCase, Disabled: true, }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) } } diff --git a/elements/basic/switch.go b/elements/basic/switch.go index 85610ba..d3ac782 100644 --- a/elements/basic/switch.go +++ b/elements/basic/switch.go @@ -140,13 +140,13 @@ func (element *Switch) calculateMinimumSize () { } func (element *Switch) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() handleBounds := image.Rect(0, 0, bounds.Dy(), bounds.Dy()) gutterBounds := image.Rect(0, 0, bounds.Dy() * 2, bounds.Dy()) backgroundPattern, _ := theme.BackgroundPattern(theme.PatternState { Case: switchCase, }) - artist.FillRectangle ( element.core, backgroundPattern, bounds) + artist.FillRectangle (element, backgroundPattern, bounds) if element.checked { handleBounds.Min.X += bounds.Dy() @@ -168,7 +168,7 @@ func (element *Switch) draw () { Focused: element.Focused(), Pressed: element.pressed, }) - artist.FillRectangle(element.core, gutterPattern, gutterBounds) + artist.FillRectangle(element, gutterPattern, gutterBounds) handlePattern, _ := theme.HandlePattern(theme.PatternState { Case: switchCase, @@ -176,7 +176,7 @@ func (element *Switch) draw () { Focused: element.Focused(), Pressed: element.pressed, }) - artist.FillRectangle(element.core, handlePattern, handleBounds) + artist.FillRectangle(element, handlePattern, handleBounds) textBounds := element.drawer.LayoutBounds() offset := image.Point { @@ -190,5 +190,5 @@ func (element *Switch) draw () { Case: switchCase, Disabled: !element.Enabled(), }) - element.drawer.Draw(element.core, foreground, offset) + element.drawer.Draw(element, foreground, offset) } diff --git a/elements/basic/textbox.go b/elements/basic/textbox.go index fab0bb8..67cccef 100644 --- a/elements/basic/textbox.go +++ b/elements/basic/textbox.go @@ -257,7 +257,7 @@ func (element *TextBox) runOnChange () { func (element *TextBox) scrollToCursor () { if !element.core.HasImage() { return } - bounds := element.core.Bounds().Inset(theme.Padding()) + bounds := element.Bounds().Inset(theme.Padding()) bounds.Max.X -= element.valueDrawer.Em().Round() cursorPosition := element.valueDrawer.PositionOf(element.cursor) cursorPosition.X -= element.scroll @@ -272,7 +272,7 @@ func (element *TextBox) scrollToCursor () { } func (element *TextBox) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() // FIXME: take index into account pattern, inset := theme.InputPattern(theme.PatternState { @@ -280,7 +280,7 @@ func (element *TextBox) draw () { Disabled: !element.Enabled(), Focused: element.Focused(), }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) if len(element.text) == 0 && !element.Focused() { // draw placeholder @@ -294,7 +294,7 @@ func (element *TextBox) draw () { Disabled: true, }) element.placeholderDrawer.Draw ( - element.core, + element, foreground, offset.Sub(textBounds.Min)) } else { @@ -309,7 +309,7 @@ func (element *TextBox) draw () { Disabled: !element.Enabled(), }) element.valueDrawer.Draw ( - element.core, + element, foreground, offset.Sub(textBounds.Min)) @@ -321,7 +321,7 @@ func (element *TextBox) draw () { Case: textBoxCase, }) artist.Line ( - element.core, + element, foreground, 1, cursorPosition.Add(offset), image.Pt ( diff --git a/elements/core/core.go b/elements/core/core.go index faa2194..6a42623 100644 --- a/elements/core/core.go +++ b/elements/core/core.go @@ -65,6 +65,9 @@ func (core *Core) MinimumSize () (width, height int) { // overridden. func (core *Core) DrawTo (canvas tomo.Canvas) { core.canvas = canvas + if core.drawSizeChange != nil { + core.drawSizeChange() + } } // OnDamage fulfils the tomo.Element interface. This should not need to be @@ -84,28 +87,27 @@ func (core *Core) OnMinimumSizeChange (callback func ()) { // instead kept as a private member. When a Core struct is created, a // corresponding CoreControl struct is linked to it and returned alongside it. type CoreControl struct { - tomo.BasicCanvas core *Core } // HasImage returns true if the core has an allocated image buffer, and false if // it doesn't. func (control CoreControl) HasImage () (has bool) { - return control.core.canvas != nil + return control.core.canvas != nil && !control.core.canvas.Bounds().Empty() } // DamageRegion pushes the selected region of pixels to the parent element. This // does not need to be called when responding to a resize event. func (control CoreControl) DamageRegion (bounds image.Rectangle) { if control.core.onDamage != nil { - control.core.onDamage(tomo.Cut(control, bounds)) + control.core.onDamage(tomo.Cut(control.core, bounds)) } } // DamageAll pushes all pixels to the parent element. This does not need to be // called when redrawing in response to a change in size. func (control CoreControl) DamageAll () { - control.DamageRegion(control.Bounds()) + control.DamageRegion(control.core.Bounds()) } // SetMinimumSize sets the minimum size of this element, notifying the parent diff --git a/elements/fun/clock.go b/elements/fun/clock.go index 1e98029..e41c930 100644 --- a/elements/fun/clock.go +++ b/elements/fun/clock.go @@ -35,7 +35,7 @@ func (element *AnalogClock) SetTime (newTime time.Time) { } func (element *AnalogClock) draw () { - bounds := element.core.Bounds() + bounds := element.Bounds() pattern, inset := theme.SunkenPattern(theme.PatternState { Case: clockCase, @@ -81,7 +81,7 @@ func (element *AnalogClock) radialLine ( outer float64, radian float64, ) { - bounds := element.core.Bounds() + bounds := element.Bounds() width := float64(bounds.Dx()) / 2 height := float64(bounds.Dy()) / 2 min := image.Pt ( @@ -91,5 +91,5 @@ func (element *AnalogClock) radialLine ( int(math.Cos(radian) * outer * width + width), int(math.Sin(radian) * outer * height + height)) // println(min.String(), max.String()) - artist.Line(element.core, source, 1, min, max) + artist.Line(element, source, 1, min, max) } diff --git a/elements/testing/mouse.go b/elements/testing/mouse.go index 138238a..c0cdece 100644 --- a/elements/testing/mouse.go +++ b/elements/testing/mouse.go @@ -29,17 +29,17 @@ func NewMouse () (element *Mouse) { func (element *Mouse) draw () { bounds := element.Bounds() pattern, _ := theme.AccentPattern(theme.PatternState { }) - artist.FillRectangle(element.core, pattern, bounds) + artist.FillRectangle(element, pattern, bounds) artist.StrokeRectangle ( - element.core, + element, artist.NewUniform(color.Black), 1, bounds) artist.Line ( - element.core, artist.NewUniform(color.White), 1, + element, artist.NewUniform(color.White), 1, image.Pt(1, 1), image.Pt(bounds.Dx() - 2, bounds.Dy() - 2)) artist.Line ( - element.core, artist.NewUniform(color.White), 1, + element, artist.NewUniform(color.White), 1, image.Pt(1, bounds.Dy() - 2), image.Pt(bounds.Dx() - 2, 1)) } @@ -53,7 +53,7 @@ func (element *Mouse) HandleMouseUp (x, y int, button tomo.Button) { element.drawing = false mousePos := image.Pt(x, y) element.core.DamageRegion (artist.Line ( - element.core, element.color, 1, + element, element.color, 1, element.lastMousePos, mousePos)) element.lastMousePos = mousePos } @@ -62,7 +62,7 @@ func (element *Mouse) HandleMouseMove (x, y int) { if !element.drawing { return } mousePos := image.Pt(x, y) element.core.DamageRegion (artist.Line ( - element.core, element.color, 1, + element, element.color, 1, element.lastMousePos, mousePos)) element.lastMousePos = mousePos }