Skip to content

Commit

Permalink
Removed Focusable interface.
Browse files Browse the repository at this point in the history
  • Loading branch information
Oliver committed Nov 17, 2020
1 parent c3a4950 commit 675ed5b
Show file tree
Hide file tree
Showing 23 changed files with 79 additions and 79 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -99,7 +99,7 @@ I try really hard to keep this project backwards compatible. Your software shoul

- a new version of an imported package (most likely [`tcell`](https://github.com/gdamore/tcell)) changes in such a way that forces me to make changes in `tview` as well,
- I fix something that I consider a bug, rather than a feature, something that does not work as originally intended,
- I make changes to "internal" interfaces such as [`Primitive`](https://pkg.go.dev/github.com/rivo/tview#Primitive) or [`Focusable`](https://pkg.go.dev/github.com/rivo/tview#Focusable). You shouldn't need these interfaces unless you're writing your own primitives for `tview`. (Yes, I realize these are public interfaces. This has advantages as well as disadvantages. For the time being, it is what it is.)
- I make changes to "internal" interfaces such as [`Primitive`](https://pkg.go.dev/github.com/rivo/tview#Primitive). You shouldn't need these interfaces unless you're writing your own primitives for `tview`. (Yes, I realize these are public interfaces. This has advantages as well as disadvantages. For the time being, it is what it is.)

## Your Feedback

Expand Down
2 changes: 1 addition & 1 deletion application.go
Expand Up @@ -325,7 +325,7 @@ EventLoop:
}

// Pass other key events to the root primitive.
if root != nil && root.GetFocusable().HasFocus() {
if root != nil && root.HasFocus() {
if handler := root.InputHandler(); handler != nil {
handler(event, func(p Primitive) {
a.SetFocus(p)
Expand Down
22 changes: 11 additions & 11 deletions box.go
Expand Up @@ -42,10 +42,6 @@ type Box struct {
// The alignment of the title.
titleAlign int

// Provides a way to find out if this box has focus. We always go through
// this interface because it may be overridden by implementing classes.
focus Focusable

// Whether or not this box has focus.
hasFocus bool

Expand Down Expand Up @@ -74,7 +70,6 @@ func NewBox() *Box {
titleColor: Styles.TitleColor,
titleAlign: AlignCenter,
}
b.focus = b
return b
}

Expand Down Expand Up @@ -320,6 +315,16 @@ func (b *Box) SetTitleAlign(align int) *Box {

// Draw draws this primitive onto the screen.
func (b *Box) Draw(screen tcell.Screen) {
b.DrawForSubclass(screen, b)
}

// DrawForSubclass draws this box under the assumption that primitive p is a
// subclass of this box. This is needed e.g. to draw proper box frames which
// depend on the subclass's focus.
//
// Only call this function from your own custom primitives. It is not needed in
// applications that have no custom primitives.
func (b *Box) DrawForSubclass(screen tcell.Screen, p Primitive) {
// Don't draw anything if there is no space.
if b.width <= 0 || b.height <= 0 {
return
Expand All @@ -340,7 +345,7 @@ func (b *Box) Draw(screen tcell.Screen) {
// Draw border.
if b.border && b.width >= 2 && b.height >= 2 {
var vertical, horizontal, topLeft, topRight, bottomLeft, bottomRight rune
if b.focus.HasFocus() {
if p.HasFocus() {
horizontal = Borders.HorizontalFocus
vertical = Borders.VerticalFocus
topLeft = Borders.TopLeftFocus
Expand Down Expand Up @@ -403,8 +408,3 @@ func (b *Box) Blur() {
func (b *Box) HasFocus() bool {
return b.hasFocus
}

// GetFocusable returns the item's Focusable.
func (b *Box) GetFocusable() Focusable {
return b.focus
}
6 changes: 3 additions & 3 deletions button.go
Expand Up @@ -97,22 +97,22 @@ func (b *Button) Draw(screen tcell.Screen) {
// Draw the box.
borderColor := b.GetBorderColor()
backgroundColor := b.GetBackgroundColor()
if b.focus.HasFocus() {
if b.HasFocus() {
b.SetBackgroundColor(b.backgroundColorActivated)
b.SetBorderColor(b.labelColorActivated)
defer func() {
b.SetBorderColor(borderColor)
}()
}
b.Box.Draw(screen)
b.Box.DrawForSubclass(screen, b)
b.backgroundColor = backgroundColor

// Draw label.
x, y, width, height := b.GetInnerRect()
if width > 0 && height > 0 {
y = y + height/2
labelColor := b.labelColor
if b.focus.HasFocus() {
if b.HasFocus() {
labelColor = b.labelColorActivated
}
Print(screen, b.label, x, y, width, AlignCenter, labelColor)
Expand Down
4 changes: 2 additions & 2 deletions checkbox.go
Expand Up @@ -144,7 +144,7 @@ func (c *Checkbox) SetFinishedFunc(handler func(key tcell.Key)) FormItem {

// Draw draws this primitive onto the screen.
func (c *Checkbox) Draw(screen tcell.Screen) {
c.Box.Draw(screen)
c.Box.DrawForSubclass(screen, c)

// Prepare
x, y, width, height := c.GetInnerRect()
Expand All @@ -168,7 +168,7 @@ func (c *Checkbox) Draw(screen tcell.Screen) {

// Draw checkbox.
fieldStyle := tcell.StyleDefault.Background(c.fieldBackgroundColor).Foreground(c.fieldTextColor)
if c.focus.HasFocus() {
if c.HasFocus() {
fieldStyle = fieldStyle.Background(c.fieldTextColor).Foreground(c.fieldBackgroundColor)
}
checkedRune := 'X'
Expand Down
File renamed without changes.
File renamed without changes.
26 changes: 24 additions & 2 deletions demos/primitive/main.go
Expand Up @@ -25,7 +25,7 @@ func NewRadioButtons(options []string) *RadioButtons {

// Draw draws this primitive onto the screen.
func (r *RadioButtons) Draw(screen tcell.Screen) {
r.Box.Draw(screen)
r.Box.DrawForSubclass(screen, r)
x, y, width, height := r.GetInnerRect()

for index, option := range r.options {
Expand Down Expand Up @@ -59,12 +59,34 @@ func (r *RadioButtons) InputHandler() func(event *tcell.EventKey, setFocus func(
})
}

// MouseHandler returns the mouse handler for this primitive.
func (r *RadioButtons) MouseHandler() func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
return r.WrapMouseHandler(func(action tview.MouseAction, event *tcell.EventMouse, setFocus func(p tview.Primitive)) (consumed bool, capture tview.Primitive) {
x, y := event.Position()
_, rectY, _, _ := r.GetInnerRect()
if !r.InRect(x, y) {
return false, nil
}

if action == tview.MouseLeftClick {
setFocus(r)
index := y - rectY
if index >= 0 && index < len(r.options) {
r.currentOption = index
consumed = true
}
}

return
})
}

func main() {
radioButtons := NewRadioButtons([]string{"Lions", "Elephants", "Giraffes"})
radioButtons.SetBorder(true).
SetTitle("Radio Button Demo").
SetRect(0, 0, 30, 5)
if err := tview.NewApplication().SetRoot(radioButtons, false).Run(); err != nil {
if err := tview.NewApplication().SetRoot(radioButtons, false).EnableMouse(true).Run(); err != nil {
panic(err)
}
}
3 changes: 1 addition & 2 deletions doc.go
Expand Up @@ -169,8 +169,7 @@ Type Hierarchy
All widgets listed above contain the Box type. All of Box's functions are
therefore available for all widgets, too.
All widgets also implement the Primitive interface. There is also the Focusable
interface which is used to override functions in subclassing types.
All widgets also implement the Primitive interface.
The tview package is based on https://github.com/gdamore/tcell. It uses types
and constants from that package (e.g. colors and keyboard values).
Expand Down
10 changes: 4 additions & 6 deletions dropdown.go
Expand Up @@ -103,8 +103,6 @@ func NewDropDown() *DropDown {
prefixTextColor: Styles.ContrastSecondaryTextColor,
}

d.focus = d

return d
}

Expand Down Expand Up @@ -288,7 +286,7 @@ func (d *DropDown) SetFinishedFunc(handler func(key tcell.Key)) FormItem {

// Draw draws this primitive onto the screen.
func (d *DropDown) Draw(screen tcell.Screen) {
d.Box.Draw(screen)
d.Box.DrawForSubclass(screen, d)

// Prepare.
x, y, width, height := d.GetInnerRect()
Expand Down Expand Up @@ -340,7 +338,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
fieldWidth = rightLimit - x
}
fieldStyle := tcell.StyleDefault.Background(d.fieldBackgroundColor)
if d.GetFocusable().HasFocus() && !d.open {
if d.HasFocus() && !d.open {
fieldStyle = fieldStyle.Background(d.fieldTextColor)
}
for index := 0; index < fieldWidth; index++ {
Expand All @@ -365,7 +363,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
text = d.currentOptionPrefix + d.options[d.currentOption].Text + d.currentOptionSuffix
}
// Just show the current selection.
if d.GetFocusable().HasFocus() && !d.open {
if d.HasFocus() && !d.open {
color = d.fieldBackgroundColor
}
Print(screen, text, x, y, fieldWidth, AlignLeft, color)
Expand Down Expand Up @@ -397,7 +395,7 @@ func (d *DropDown) Draw(screen tcell.Screen) {
func (d *DropDown) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return d.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
// If the list has focus, let it process its own key events.
if d.list.GetFocusable().HasFocus() {
if d.list.HasFocus() {
if handler := d.list.InputHandler(); handler != nil {
handler(event, setFocus)
}
Expand Down
9 changes: 4 additions & 5 deletions flex.go
Expand Up @@ -53,7 +53,6 @@ func NewFlex() *Flex {
Box: NewBox().SetBackgroundColor(tcell.ColorDefault),
direction: FlexColumn,
}
f.focus = f
return f
}

Expand Down Expand Up @@ -122,7 +121,7 @@ func (f *Flex) ResizeItem(p Primitive, fixedSize, proportion int) *Flex {

// Draw draws this primitive onto the screen.
func (f *Flex) Draw(screen tcell.Screen) {
f.Box.Draw(screen)
f.Box.DrawForSubclass(screen, f)

// Calculate size and position of the items.

Expand Down Expand Up @@ -173,7 +172,7 @@ func (f *Flex) Draw(screen tcell.Screen) {
pos += size

if item.Item != nil {
if item.Item.GetFocusable().HasFocus() {
if item.Item.HasFocus() {
defer item.Item.Draw(screen)
} else {
item.Item.Draw(screen)
Expand All @@ -195,7 +194,7 @@ func (f *Flex) Focus(delegate func(p Primitive)) {
// HasFocus returns whether or not this primitive has focus.
func (f *Flex) HasFocus() bool {
for _, item := range f.items {
if item.Item != nil && item.Item.GetFocusable().HasFocus() {
if item.Item != nil && item.Item.HasFocus() {
return true
}
}
Expand Down Expand Up @@ -228,7 +227,7 @@ func (f *Flex) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
func (f *Flex) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return f.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
for _, item := range f.items {
if item.Item != nil && item.Item.GetFocusable().HasFocus() {
if item.Item != nil && item.Item.HasFocus() {
if handler := item.Item.InputHandler(); handler != nil {
handler(event, setFocus)
return
Expand Down
8 changes: 0 additions & 8 deletions focusable.go

This file was deleted.

16 changes: 7 additions & 9 deletions form.go
Expand Up @@ -96,8 +96,6 @@ func NewForm() *Form {
buttonTextColor: Styles.PrimaryTextColor,
}

f.focus = f

return f
}

Expand Down Expand Up @@ -363,7 +361,7 @@ func (f *Form) SetCancelFunc(callback func()) *Form {

// Draw draws this primitive onto the screen.
func (f *Form) Draw(screen tcell.Screen) {
f.Box.Draw(screen)
f.Box.DrawForSubclass(screen, f)

// Determine the actual item that has focus.
if index := f.focusIndex(); index >= 0 {
Expand Down Expand Up @@ -430,7 +428,7 @@ func (f *Form) Draw(screen tcell.Screen) {
positions[index].y = y
positions[index].width = itemWidth
positions[index].height = 1
if item.GetFocusable().HasFocus() {
if item.HasFocus() {
focusedPosition = positions[index]
}

Expand Down Expand Up @@ -524,7 +522,7 @@ func (f *Form) Draw(screen tcell.Screen) {
}

// Draw items with focus last (in case of overlaps).
if item.GetFocusable().HasFocus() {
if item.HasFocus() {
defer item.Draw(screen)
} else {
item.Draw(screen)
Expand Down Expand Up @@ -608,12 +606,12 @@ func (f *Form) HasFocus() bool {
// has focus.
func (f *Form) focusIndex() int {
for index, item := range f.items {
if item.GetFocusable().HasFocus() {
if item.HasFocus() {
return index
}
}
for index, button := range f.buttons {
if button.focus.HasFocus() {
if button.HasFocus() {
return len(f.items) + index
}
}
Expand Down Expand Up @@ -666,7 +664,7 @@ func (f *Form) MouseHandler() func(action MouseAction, event *tcell.EventMouse,
func (f *Form) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return f.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
for _, item := range f.items {
if item != nil && item.GetFocusable().HasFocus() {
if item != nil && item.HasFocus() {
if handler := item.InputHandler(); handler != nil {
handler(event, setFocus)
return
Expand All @@ -675,7 +673,7 @@ func (f *Form) InputHandler() func(event *tcell.EventKey, setFocus func(p Primit
}

for _, button := range f.buttons {
if button.GetFocusable().HasFocus() {
if button.HasFocus() {
if handler := button.InputHandler(); handler != nil {
handler(event, setFocus)
return
Expand Down
12 changes: 3 additions & 9 deletions frame.go
Expand Up @@ -46,8 +46,6 @@ func NewFrame(primitive Primitive) *Frame {
right: 1,
}

f.focus = f

return f
}

Expand Down Expand Up @@ -83,7 +81,7 @@ func (f *Frame) SetBorders(top, bottom, header, footer, left, right int) *Frame

// Draw draws this primitive onto the screen.
func (f *Frame) Draw(screen tcell.Screen) {
f.Box.Draw(screen)
f.Box.DrawForSubclass(screen, f)

// Calculate start positions.
x, top, width, height := f.GetInnerRect()
Expand Down Expand Up @@ -159,11 +157,7 @@ func (f *Frame) HasFocus() bool {
if f.primitive == nil {
return f.hasFocus
}
focusable, ok := f.primitive.(Focusable)
if ok {
return focusable.HasFocus()
}
return f.hasFocus
return f.primitive.HasFocus()
}

// MouseHandler returns the mouse handler for this primitive.
Expand All @@ -188,7 +182,7 @@ func (f *Frame) InputHandler() func(event *tcell.EventKey, setFocus func(p Primi
if f.primitive == nil {
return
}
if f.primitive.GetFocusable().HasFocus() {
if f.primitive.HasFocus() {
if handler := f.primitive.InputHandler(); handler != nil {
handler(event, setFocus)
return
Expand Down

0 comments on commit 675ed5b

Please sign in to comment.