Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Center #800

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
113 changes: 113 additions & 0 deletions center.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package tview

import (
"github.com/gdamore/tcell/v2"
)

// Center is a wrapper which adds space around another primitive to shot up in the middle.
type Center struct {
*Box

// the contained primitive
primitive Primitive

width, height int

// keep a reference in case we need it when we change the primitive
setFocus func(p Primitive)
}

// NewCenter returns a new frame around the given primitive.
func NewCenter(primitive Primitive, width, height int) *Center {
return &Center{Box: NewBox(), primitive: primitive, width: width, height: height}
}

func (c *Center) Resize(width, height int) *Center {
if width > 0 {
c.width = width
}
if height > 0 {
c.height = height
}
return c
}

// SetPrimitive replaces the contained primitive with the given one.
func (c *Center) SetPrimitive(p Primitive) *Center {
hasFocus := c.primitive.HasFocus()
c.primitive = p
if hasFocus && c.setFocus != nil {
c.setFocus(p) // Restore focus.
}
return c
}

// Primitive returns the primitive contained in this frame.
func (c *Center) Primitive() Primitive {
return c.primitive
}

// Draw draws this primitive onto the screen.
func (c *Center) Draw(screen tcell.Screen) {
x, y, inWidth, inHeight := c.GetInnerRect()
width, height := c.width, c.height
if width < inWidth {
x += (inWidth - width) >> 1
} else if width > inWidth {
width = inWidth
}
if height < inHeight {
y += (inHeight - height) >> 1
} else if height > inHeight {
height = inHeight
}

c.Box.Draw(screen)
c.primitive.SetRect(x, y, width, height)
c.primitive.Draw(screen)
}

// Focus is called when this primitive receives focus.
func (c *Center) Focus(delegate func(p Primitive)) {
c.setFocus = delegate
delegate(c.primitive)
c.Box.Focus(delegate)
}

// HasFocus returns whether or not this primitive has focus.
func (c *Center) HasFocus() bool {
return c.primitive.HasFocus()
}

// MouseHandler returns the mouse handler for this primitive.
func (c *Center) MouseHandler() func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
return c.WrapMouseHandler(func(action MouseAction, event *tcell.EventMouse, setFocus func(p Primitive)) (consumed bool, capture Primitive) {
if !c.InRect(event.Position()) {
return false, nil
}

// Pass mouse events on to contained primitive.
consumed, capture = c.primitive.MouseHandler()(action, event, setFocus)
if consumed {
return true, capture
}

// Clicking on the frame parts.
if action == MouseLeftDown {
setFocus(c)
consumed = true
}

return
})
}

// InputHandler returns the handler for this primitive.
func (c *Center) InputHandler() func(event *tcell.EventKey, setFocus func(p Primitive)) {
return c.WrapInputHandler(func(event *tcell.EventKey, setFocus func(p Primitive)) {
if handler := c.primitive.InputHandler(); handler != nil {
handler(event, setFocus)
return
}
})
}
1 change: 1 addition & 0 deletions demos/center/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
![Screenshot](screenshot.png)
29 changes: 29 additions & 0 deletions demos/center/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Demo code for the Centered Modal primitive.
package main

import (
"strings"

"github.com/gdamore/tcell/v2"
"github.com/rivo/tview"
)

func main() {
app := tview.NewApplication()

background := tview.NewTextView().
SetTextColor(tcell.ColorBlue).
SetText(strings.Repeat("background ", 1000))

box := tview.NewBox().
SetBorder(true).
SetTitle("Centered Box")

pages := tview.NewPages().
AddPage("background", background, true, true).
AddPage("modal", tview.NewCenter(box, 40, 10), true, true)

if err := app.SetRoot(pages, true).Run(); err != nil {
panic(err)
}
}
File renamed without changes
16 changes: 0 additions & 16 deletions demos/presentation/center.go

This file was deleted.

2 changes: 1 addition & 1 deletion demos/presentation/code.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,6 @@ func Code(p tview.Primitive, width, height int, code string) tview.Primitive {
fmt.Fprint(codeView, code)

return tview.NewFlex().
AddItem(Center(width, height, p), 0, 1, true).
AddItem(tview.NewCenter(p, width, height), 0, 1, true).
AddItem(codeView, codeWidth, 1, false)
}
2 changes: 1 addition & 1 deletion demos/presentation/colors.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,5 +29,5 @@ func Colors(nextSlide func()) (title string, content tview.Primitive) {
table.SetBorderPadding(1, 1, 2, 2).
SetBorder(true).
SetTitle("A [red]c[yellow]o[green]l[darkcyan]o[blue]r[darkmagenta]f[red]u[yellow]l[white] [black:red]c[:yellow]o[:green]l[:darkcyan]o[:blue]r[:darkmagenta]f[:red]u[:yellow]l[white:] [::bu]title")
return "Colors", Center(78, 19, table)
return "Colors", tview.NewCenter(table, 78, 19)
}
2 changes: 1 addition & 1 deletion demos/presentation/end.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@ func End(nextSlide func()) (title string, content tview.Primitive) {
})
url := "https://github.com/rivo/tview"
fmt.Fprint(textView, url)
return "End", Center(len(url), 1, textView)
return "End", tview.NewCenter(textView, len(url), 1)
}
2 changes: 1 addition & 1 deletion demos/presentation/introduction.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@ func Introduction(nextSlide func()) (title string, content tview.Primitive) {
AddItem("Designed to be simple", `"Hello world" is 5 lines of code`, '3', nextSlide).
AddItem("Good for data entry", `For charts, use "termui" - for low-level views, use "gocui" - ...`, '4', nextSlide).
AddItem("Extensive documentation", "Everything is documented, examples in GitHub wiki, demo code for each widget", '5', nextSlide)
return "Introduction", Center(80, 10, list)
return "Introduction", tview.NewCenter(list, 80, 10)
}