Added callback functions for focus and blur events.

pull/669/head
Oliver 3 years ago
parent 175cc7d7ce
commit 1b3174ee3d

@ -45,9 +45,15 @@ type Box struct {
// The alignment of the title.
titleAlign int
// Whether or not this box has focus.
// Whether or not this box has focus. This is typically ignored for
// container primitives (e.g. Flex, Grid, Pages), as they will delegate
// focus to their children.
hasFocus bool
// Optional callback functions invoked when the primitive receives or loses
// focus.
focus, blur func()
// An optional capture function which receives a key event and returns the
// event to be forwarded to the primitive's default input handler (nil if
// nothing should be forwarded).
@ -398,13 +404,39 @@ func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) {
}
}
// SetFocusFunc sets a callback function which is invoked when this primitive
// receives focus. Container primitives such as Flex or Grid may not be notified
// if one of their descendents receive focus directly.
//
// Set to nil to remove the callback function.
func (b *Box) SetFocusFunc(callback func()) *Box {
b.focus = callback
return b
}
// SetBlurFunc sets a callback function which is invoked when this primitive
// loses focus. This does not apply to container primitives such as Flex or
// Grid.
//
// Set to nil to remove the callback function.
func (b *Box) SetBlurFunc(callback func()) *Box {
b.blur = callback
return b
}
// Focus is called when this primitive receives focus.
func (b *Box) Focus(delegate func(p Primitive)) {
b.hasFocus = true
if b.focus != nil {
b.focus()
}
}
// Blur is called when this primitive loses focus.
func (b *Box) Blur() {
if b.blur != nil {
b.blur()
}
b.hasFocus = false
}

@ -507,9 +507,10 @@ func (d *DropDown) closeList(setFocus func(Primitive)) {
// Focus is called by the application when the primitive receives focus.
func (d *DropDown) Focus(delegate func(p Primitive)) {
d.Box.Focus(delegate)
if d.open {
delegate(d.list)
} else {
d.Box.Focus(delegate)
}
}
@ -518,7 +519,7 @@ func (d *DropDown) HasFocus() bool {
if d.open {
return d.list.HasFocus()
}
return d.hasFocus
return d.Box.HasFocus()
}
// MouseHandler returns the mouse handler for this primitive.

@ -207,6 +207,7 @@ func (f *Flex) Focus(delegate func(p Primitive)) {
return
}
}
f.Box.Focus(delegate)
}
// HasFocus returns whether or not this primitive has focus.
@ -216,7 +217,7 @@ func (f *Flex) HasFocus() bool {
return true
}
}
return false
return f.Box.HasFocus()
}
// MouseHandler returns the mouse handler for this primitive.

@ -550,7 +550,7 @@ func (f *Form) Draw(screen tcell.Screen) {
// Focus is called by the application when the primitive receives focus.
func (f *Form) Focus(delegate func(p Primitive)) {
if len(f.items)+len(f.buttons) == 0 {
f.hasFocus = true
f.Box.Focus(delegate)
return
}
f.hasFocus = false
@ -595,10 +595,10 @@ func (f *Form) Focus(delegate func(p Primitive)) {
// HasFocus returns whether or not this primitive has focus.
func (f *Form) HasFocus() bool {
if f.hasFocus {
if f.focusIndex() >= 0 {
return true
}
return f.focusIndex() >= 0
return f.Box.HasFocus()
}
// focusIndex returns the index of the currently focused item, counting form

@ -148,14 +148,14 @@ func (f *Frame) Focus(delegate func(p Primitive)) {
if f.primitive != nil {
delegate(f.primitive)
} else {
f.hasFocus = true
f.Box.Focus(delegate)
}
}
// HasFocus returns whether or not this primitive has focus.
func (f *Frame) HasFocus() bool {
if f.primitive == nil {
return f.hasFocus
return f.Box.HasFocus()
}
return f.primitive.HasFocus()
}

@ -250,7 +250,7 @@ func (g *Grid) Focus(delegate func(p Primitive)) {
return
}
}
g.hasFocus = true
g.Box.Focus(delegate)
}
// HasFocus returns whether or not this primitive has focus.
@ -260,7 +260,7 @@ func (g *Grid) HasFocus() bool {
return true
}
}
return g.hasFocus
return g.Box.HasFocus()
}
// InputHandler returns the handler for this primitive.

@ -243,7 +243,7 @@ func (p *Pages) HasFocus() bool {
return true
}
}
return false
return p.Box.HasFocus()
}
// Focus is called by the application when the primitive receives focus.
@ -260,6 +260,8 @@ func (p *Pages) Focus(delegate func(p Primitive)) {
}
if topItem != nil {
delegate(topItem)
} else {
p.Box.Focus(delegate)
}
}

@ -648,7 +648,7 @@ func (t *TextView) Focus(delegate func(p Primitive)) {
// Implemented here with locking because this is used by layout primitives.
t.Lock()
defer t.Unlock()
t.hasFocus = true
t.Box.Focus(delegate)
}
// HasFocus returns whether or not this primitive has focus.
@ -657,7 +657,7 @@ func (t *TextView) HasFocus() bool {
// callback.
t.Lock()
defer t.Unlock()
return t.hasFocus
return t.Box.HasFocus()
}
// Write lets us implement the io.Writer interface. Tab characters will be

Loading…
Cancel
Save