Split element core into two separate structs - public and private

The public core half can be directly embedded, which means no
wrapper methods!
This commit is contained in:
Sasha Koshka 2023-01-09 13:06:55 -05:00
parent 1526772a05
commit d1ec5f2cec
3 changed files with 52 additions and 92 deletions

View File

@ -7,7 +7,9 @@ import "git.tebibyte.media/sashakoshka/tomo/theme"
import "git.tebibyte.media/sashakoshka/tomo/artist" import "git.tebibyte.media/sashakoshka/tomo/artist"
type Button struct { type Button struct {
core Core *Core
core CoreControl
pressed bool pressed bool
enabled bool enabled bool
onClick func () onClick func ()
@ -18,7 +20,7 @@ type Button struct {
func NewButton (text string) (element *Button) { func NewButton (text string) (element *Button) {
element = &Button { enabled: true } element = &Button { enabled: true }
element.core = NewCore(element) element.Core, element.core = NewCore(element)
element.drawer.SetFace(theme.FontFaceRegular()) element.drawer.SetFace(theme.FontFaceRegular())
element.SetText(text) element.SetText(text)
return return
@ -94,48 +96,12 @@ func (element *Button) OnClick (callback func ()) {
element.onClick = callback element.onClick = callback
} }
func (element *Button) ColorModel () (model color.Model) { func (element *Button) AdvanceSelection (direction int) (ok bool) {
return color.RGBAModel
}
func (element *Button) At (x, y int) (pixel color.Color) {
pixel = element.core.At(x, y)
return return
} }
func (element *Button) RGBAAt (x, y int) (pixel color.RGBA) {
pixel = element.core.RGBAAt(x, y)
return
}
func (element *Button) Bounds () (bounds image.Rectangle) {
bounds = element.core.Bounds()
return
}
func (element *Button) SetDrawCallback (draw func (region tomo.Image)) {
element.core.SetDrawCallback(draw)
}
func (element *Button) SetMinimumSizeChangeCallback (
notify func (width, height int),
) {
element.core.SetMinimumSizeChangeCallback(notify)
}
func (element *Button) Selectable () (selectable bool) { func (element *Button) Selectable () (selectable bool) {
selectable = true return true
return
}
func (element *Button) MinimumWidth () (minimum int) {
minimum = element.core.MinimumWidth()
return
}
func (element *Button) MinimumHeight () (minimum int) {
minimum = element.core.MinimumHeight()
return
} }
func (element *Button) draw () { func (element *Button) draw () {

View File

@ -5,24 +5,23 @@ import "image/color"
import "git.tebibyte.media/sashakoshka/tomo" import "git.tebibyte.media/sashakoshka/tomo"
// Core is a struct that implements some core functionality common to most // Core is a struct that implements some core functionality common to most
// widgets. It is possible to embed this directly into a struct, but this is not // widgets. It is meant to be embedded directly into a struct.
// reccomended as it exposes internal functionality.
type Core struct { type Core struct {
*image.RGBA canvas *image.RGBA
parent tomo.Element parent tomo.Element
drawCallback func (region tomo.Image)
minimumSizeChangeCallback func (width, height int)
metrics struct { metrics struct {
minimumWidth int minimumWidth int
minimumHeight int minimumHeight int
} }
hooks tomo.ParentHooks
} }
// Core creates a new element core. // NewCore creates a new element core and its corresponding control.
func NewCore (parent tomo.Element) (core Core) { func NewCore (parent tomo.Element) (core *Core, control CoreControl) {
core = Core { parent: parent } core = &Core { parent: parent }
control = CoreControl { core: core }
return return
} }
@ -31,81 +30,75 @@ func (core Core) ColorModel () (model color.Model) {
} }
func (core Core) At (x, y int) (pixel color.Color) { func (core Core) At (x, y int) (pixel color.Color) {
if core.RGBA == nil { return color.RGBA { } } if core.canvas == nil { return color.RGBA { } }
pixel = core.RGBA.At(x, y) pixel = core.canvas.At(x, y)
return return
} }
func (core Core) RGBAAt (x, y int) (pixel color.RGBA) { func (core Core) RGBAAt (x, y int) (pixel color.RGBA) {
if core.RGBA == nil { return color.RGBA { } } if core.canvas == nil { return color.RGBA { } }
pixel = core.RGBA.RGBAAt(x, y) pixel = core.canvas.RGBAAt(x, y)
return return
} }
func (core Core) Bounds () (bounds image.Rectangle) { func (core Core) Bounds () (bounds image.Rectangle) {
if core.RGBA != nil { bounds = core.RGBA.Bounds() } if core.canvas != nil { bounds = core.canvas.Bounds() }
return return
} }
func (core *Core) SetDrawCallback (draw func (region tomo.Image)) { func (core *Core) SetParentHooks (hooks tomo.ParentHooks) {
core.drawCallback = draw core.hooks = hooks
} }
func (core *Core) SetMinimumSizeChangeCallback ( func (core Core) MinimumSize () (width, height int) {
notify func (width, height int), return core.metrics.minimumWidth, core.metrics.minimumHeight
) {
core.minimumSizeChangeCallback = notify
} }
func (core Core) HasImage () (has bool) { // CoreControl is a struct that can exert control over a control struct. It can
has = core.RGBA != nil // be used as a canvas. It must not be directly embedded into an element, but
// instead kept as a private member.
type CoreControl struct {
*image.RGBA
core *Core
}
func (control CoreControl) HasImage () (has bool) {
has = control.RGBA != nil
return return
} }
func (core Core) PushRegion (bounds image.Rectangle) { func (control CoreControl) PushRegion (bounds image.Rectangle) {
if core.drawCallback != nil { core := control.core
core.drawCallback(core.SubImage(bounds). core.hooks.RunDraw(control.SubImage(bounds).(*image.RGBA))
(*image.RGBA))
}
} }
func (core Core) PushAll () { func (control CoreControl) PushAll () {
core.PushRegion(core.Bounds()) control.PushRegion(control.Bounds())
} }
func (core *Core) AllocateCanvas (width, height int) { func (control CoreControl) AllocateCanvas (width, height int) {
width, height, _ = core.ConstrainSize(width, height) core := control.core
core.RGBA = image.NewRGBA(image.Rect (0, 0, width, height)) width, height, _ = control.ConstrainSize(width, height)
core.canvas = image.NewRGBA(image.Rect (0, 0, width, height))
control.RGBA = core.canvas
} }
func (core Core) MinimumWidth () (minimum int) { func (control CoreControl) SetMinimumSize (width, height int) {
minimum = core.metrics.minimumWidth core := control.core
return
}
func (core Core) MinimumHeight () (minimum int) {
minimum = core.metrics.minimumHeight
return
}
func (core *Core) SetMinimumSize (width, height int) {
if width != core.metrics.minimumWidth || if width != core.metrics.minimumWidth ||
height != core.metrics.minimumHeight { height != core.metrics.minimumHeight {
core.metrics.minimumWidth = width core.metrics.minimumWidth = width
core.metrics.minimumHeight = height core.metrics.minimumHeight = height
core.hooks.RunMinimumSizeChange(width, height)
if core.minimumSizeChangeCallback != nil {
core.minimumSizeChangeCallback(width, height)
}
// if there is an image buffer, and the current size is less // if there is an image buffer, and the current size is less
// than this new minimum size, send core.parent a resize event. // than this new minimum size, send core.parent a resize event.
if core.HasImage() { if control.HasImage() {
bounds := core.Bounds() bounds := control.Bounds()
imageWidth, imageWidth,
imageHeight, imageHeight,
constrained := core.ConstrainSize ( constrained := control.ConstrainSize (
bounds.Dx(), bounds.Dx(),
bounds.Dy()) bounds.Dy())
if constrained { if constrained {
@ -118,12 +111,13 @@ func (core *Core) SetMinimumSize (width, height int) {
} }
} }
func (core Core) ConstrainSize ( func (control CoreControl) ConstrainSize (
inWidth, inHeight int, inWidth, inHeight int,
) ( ) (
outWidth, outHeight int, outWidth, outHeight int,
constrained bool, constrained bool,
) { ) {
core := control.core
outWidth = inWidth outWidth = inWidth
outHeight = inHeight outHeight = inHeight
if outWidth < core.metrics.minimumWidth { if outWidth < core.metrics.minimumWidth {

View File

@ -121,7 +121,7 @@ type Window interface {
// SetIcon taks in a list different sizes of the same icon and selects // SetIcon taks in a list different sizes of the same icon and selects
// the best one to display on the window title bar, dock, or whatever is // the best one to display on the window title bar, dock, or whatever is
// applicable for the given backend. This method might have no effect // applicable for the given backend. This method might have no effect
// with some backends. // for some backends.
SetIcon (sizes []image.Image) SetIcon (sizes []image.Image)
// Show shows the window. The window starts off hidden, so this must be // Show shows the window. The window starts off hidden, so this must be