From 78fb934afe6c83fa8af377dd3a464981011aef1c Mon Sep 17 00:00:00 2001 From: Sasha Koshka Date: Thu, 19 Jan 2023 17:35:19 -0500 Subject: [PATCH] Ok this is overstepping the bounds of this branch --- elements/basic/container.go | 2 - elements/basic/scrollcontainer.go | 100 +++++++++++++++++++++++++++--- elements/basic/textbox.go | 4 ++ 3 files changed, 97 insertions(+), 9 deletions(-) diff --git a/elements/basic/container.go b/elements/basic/container.go index a7c2f34..f494192 100644 --- a/elements/basic/container.go +++ b/elements/basic/container.go @@ -59,8 +59,6 @@ func (element *Container) Adopt (child tomo.Element, expand bool) { child0.OnSelectionRequest (func () (granted bool) { return element.childSelectionRequestCallback(child0) }) - } - if child0, ok := child.(tomo.Selectable); ok { child0.OnSelectionMotionRequest ( func (direction tomo.SelectionDirection) (granted bool) { if element.onSelectionMotionRequest == nil { return } diff --git a/elements/basic/scrollcontainer.go b/elements/basic/scrollcontainer.go index b3c8b81..57ae279 100644 --- a/elements/basic/scrollcontainer.go +++ b/elements/basic/scrollcontainer.go @@ -24,7 +24,8 @@ type ScrollContainer struct { bounds image.Rectangle } - // TODO event handlers + onSelectionRequest func () (granted bool) + onSelectionMotionRequest func (tomo.SelectionDirection) (granted bool) } // NewScrollContainer creates a new scroll container with the specified scroll @@ -60,12 +61,14 @@ func (element *ScrollContainer) Adopt (child tomo.Scrollable) { // adopt new child element.child = child if child != nil { - // child.SetParentHooks (tomo.ParentHooks { - // Draw: window.childDrawCallback, - // MinimumSizeChange: window.childMinimumSizeChangeCallback, - // FlexibleHeightChange: window.resizeChildToFit, - // SelectionRequest: window.childSelectionRequestCallback, - // }) + child.OnDamage(element.childDamageCallback) + child.OnMinimumSizeChange(element.updateMinimumSize) + if newChild, ok := child.(tomo.Selectable); ok { + newChild.OnSelectionRequest ( + element.childSelectionRequestCallback) + newChild.OnSelectionMotionRequest ( + element.childSelectionMotionRequestCallback) + } // TODO: somehow inform the core that we do not in fact want to // redraw the element. @@ -81,6 +84,89 @@ func (element *ScrollContainer) Adopt (child tomo.Scrollable) { } } +func (element *ScrollContainer) HandleKeyDown ( + key tomo.Key, + modifiers tomo.Modifiers, + repeated bool, +) { + if child, ok := element.child.(tomo.KeyboardTarget); ok { + child.HandleKeyDown(key, modifiers, repeated) + } +} + +func (element *ScrollContainer) HandleKeyUp (key tomo.Key, modifiers tomo.Modifiers) { + if child, ok := element.child.(tomo.KeyboardTarget); ok { + child.HandleKeyUp(key, modifiers) + } +} + +func (element *ScrollContainer) Selected () (selected bool) { + return element.selected +} + +func (element *ScrollContainer) Select () { + if element.onSelectionRequest != nil { + element.onSelectionRequest() + } +} + +func (element *ScrollContainer) HandleSelection ( + direction tomo.SelectionDirection, +) ( + accepted bool, +) { + if child, ok := element.child.(tomo.Selectable); ok { + element.selected = true + return child.HandleSelection(direction) + } else { + element.selected = false + return false + } +} + +func (element *ScrollContainer) HandleDeselection () { + if child, ok := element.child.(tomo.Selectable); ok { + child.HandleDeselection() + } + element.selected = false +} + +func (element *ScrollContainer) OnSelectionRequest (callback func () (granted bool)) { + element.onSelectionRequest = callback +} + +func (element *ScrollContainer) OnSelectionMotionRequest ( + callback func (direction tomo.SelectionDirection) (granted bool), +) { + element.onSelectionMotionRequest = callback +} + +func (element *ScrollContainer) childDamageCallback (region tomo.Canvas) { + element.core.DamageRegion(artist.Paste(element, region, image.Point { })) +} + +func (element *ScrollContainer) childSelectionRequestCallback () (granted bool) { + child, ok := element.child.(tomo.Selectable) + if !ok { return false } + if element.onSelectionRequest != nil && element.onSelectionRequest() { + child.HandleSelection(tomo.SelectionDirectionNeutral) + return true + } else { + return false + } +} + +func (element *ScrollContainer) childSelectionMotionRequestCallback ( + direction tomo.SelectionDirection, +) ( + granted bool, +) { + if element.onSelectionMotionRequest == nil { + return + } + return element.onSelectionMotionRequest(direction) +} + func (element *ScrollContainer) clearChildEventHandlers (child tomo.Element) { child.OnDamage(nil) child.OnMinimumSizeChange(nil) diff --git a/elements/basic/textbox.go b/elements/basic/textbox.go index 7ba4d0f..537b742 100644 --- a/elements/basic/textbox.go +++ b/elements/basic/textbox.go @@ -273,6 +273,10 @@ func (element *TextBox) ScrollAxes () (horizontal, vertical bool) { return true, false } +func (element *TextBox) OnScrollBoundsChange (callback func ()) { + element.onScrollBoundsChange = callback +} + func (element *TextBox) updateMinimumSize () { textBounds := element.placeholderDrawer.LayoutBounds() element.core.SetMinimumSize (