This repository has been archived on 2023-08-08. You can view files and clone it, but cannot push or open issues or pull requests.
tomo-old/elements/core/core.go

179 lines
5.4 KiB
Go
Raw Normal View History

2023-01-09 19:25:11 -07:00
package core
2023-01-08 23:03:19 -07:00
import "image"
import "image/color"
import "git.tebibyte.media/sashakoshka/tomo"
// Core is a struct that implements some core functionality common to most
// widgets. It is meant to be embedded directly into a struct.
2023-01-08 23:03:19 -07:00
type Core struct {
canvas tomo.BasicCanvas
2023-01-08 23:03:19 -07:00
parent tomo.Element
metrics struct {
minimumWidth int
minimumHeight int
}
2023-01-10 09:51:46 -07:00
selectable bool
selected bool
hooks tomo.ParentHooks
2023-01-08 23:03:19 -07:00
}
// NewCore creates a new element core and its corresponding control.
func NewCore (parent tomo.Element) (core *Core, control CoreControl) {
core = &Core { parent: parent }
control = CoreControl { core: core }
2023-01-08 23:03:19 -07:00
return
}
// ColorModel fulfills the draw.Image interface.
func (core *Core) ColorModel () (model color.Model) {
2023-01-08 23:03:19 -07:00
return color.RGBAModel
}
// ColorModel fulfills the draw.Image interface.
func (core *Core) At (x, y int) (pixel color.Color) {
return core.canvas.At(x, y)
2023-01-08 23:03:19 -07:00
}
// ColorModel fulfills the draw.Image interface.
func (core *Core) Bounds () (bounds image.Rectangle) {
return core.canvas.Bounds()
2023-01-08 23:03:19 -07:00
}
// ColorModel fulfills the draw.Image interface.
func (core *Core) Set (x, y int, c color.Color) () {
core.canvas.Set(x, y, c)
}
// Buffer fulfills the tomo.Canvas interface.
func (core *Core) Buffer () (data []color.RGBA, stride int) {
return core.canvas.Buffer()
2023-01-08 23:03:19 -07:00
}
// MinimumSize fulfils the tomo.Element interface. This should not need to be
// overridden.
func (core *Core) MinimumSize () (width, height int) {
return core.metrics.minimumWidth, core.metrics.minimumHeight
}
// SetParentHooks fulfils the tomo.Element interface. This should not need to be
// overridden.
func (core *Core) SetParentHooks (hooks tomo.ParentHooks) {
core.hooks = hooks
}
// CoreControl is a struct that can exert control over a Core struct. It can be
// used as a canvas. It must not be directly embedded into an element, but
// 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
2023-01-08 23:03:19 -07:00
}
2023-01-15 22:31:04 -07:00
// RequestSelection requests that the element's parent send it a selection
// event. If the request was granted, it returns true. If it was denied, it
// returns false.
func (control CoreControl) RequestSelection () (granted bool) {
return control.core.hooks.RunSelectionRequest()
}
// RequestSelectionMotion requests that the element's parent deselect this
// element and select the one to the left or right of it, depending on the
// direction. If the requests was granted, it returns true. If it was denied, it
// returns false.
func (control CoreControl) RequestSelectionMotion (
direction tomo.SelectionDirection,
) (
granted bool,
) {
return control.core.hooks.RunSelectionMotionRequest(direction)
}
// 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.Bounds().Empty()
2023-01-08 23:03:19 -07:00
}
// PushRegion 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) PushRegion (bounds image.Rectangle) {
control.core.hooks.RunDraw(tomo.Cut(control, bounds))
2023-01-08 23:03:19 -07:00
}
// PushAll pushes all pixels to the parent element. This does not need to be
// called when responding to a resize event.
func (control CoreControl) PushAll () {
control.PushRegion(control.Bounds())
2023-01-08 23:03:19 -07:00
}
// AllocateCanvas resizes the canvas, constraining the width and height so that
// they are not less than the specified minimum width and height.
2023-01-09 13:14:36 -07:00
func (control *CoreControl) AllocateCanvas (width, height int) {
control.core.canvas = tomo.NewBasicCanvas(width, height)
control.BasicCanvas = control.core.canvas
2023-01-08 23:03:19 -07:00
}
// SetMinimumSize sets the minimum size of this element, notifying the parent
// element in the process.
func (control CoreControl) SetMinimumSize (width, height int) {
core := control.core
if width == core.metrics.minimumWidth &&
height == core.metrics.minimumHeight {
return
}
core.metrics.minimumWidth = width
core.metrics.minimumHeight = height
core.hooks.RunMinimumSizeChange(width, height)
// if there is an image buffer, and the current size is less
// than this new minimum size, send core.parent a resize event.
if control.HasImage() {
bounds := control.Bounds()
imageWidth,
imageHeight,
constrained := control.ConstrainSize(bounds.Dx(), bounds.Dy())
if constrained {
core.parent.Resize(imageWidth, imageHeight)
}
2023-01-08 23:03:19 -07:00
}
}
// NotifyFlexibleHeightChange notifies the parent element that this element's
// flexible height has changed.
func (control CoreControl) NotifyFlexibleHeightChange () {
control.core.hooks.RunFlexibleHeightChange()
}
// NotifyContentBoundsChange notifies the parent element that this element's
// inner content bounds or scroll position have changed.
func (control CoreControl) NotifyContentBoundsChange () {
control.core.hooks.RunContentBoundsChange()
}
// ConstrainSize contstrains the specified width and height to the minimum width
// and height, and returns wether or not anything ended up being constrained.
func (control CoreControl) ConstrainSize (
2023-01-08 23:03:19 -07:00
inWidth, inHeight int,
) (
outWidth, outHeight int,
constrained bool,
) {
core := control.core
2023-01-08 23:03:19 -07:00
outWidth = inWidth
outHeight = inHeight
if outWidth < core.metrics.minimumWidth {
outWidth = core.metrics.minimumWidth
constrained = true
}
if outHeight < core.metrics.minimumHeight {
outHeight = core.metrics.minimumHeight
constrained = true
}
return
}