Add mouse support to presentation demo tabs

This commit is contained in:
Trevor Slocum 2020-01-03 18:46:23 -08:00
parent 1207c71f9f
commit 990a2abc33
3 changed files with 42 additions and 8 deletions

View File

@ -4,16 +4,16 @@ import (
"fmt"
"strings"
"github.com/gdamore/tcell"
"git.sr.ht/~tslocum/cview"
"github.com/gdamore/tcell"
)
const logo = `
__ _
/ /__ __(_)__ _ __
/ __/ | / / / _ \ | /| / /
/ /_ | |/ / / __/ |/ |/ /
\__/ |___/_/\___/|__/|__/
======= === === === ======== === === ===
=== === === === === === === ===
=== === === === ====== === === ===
=== ====== === === ===========
======= == === ======== ==== ====
`

View File

@ -9,6 +9,6 @@ func Introduction(nextSlide func()) (title string, content cview.Primitive) {
AddItem("Based on github.com/gdamore/tcell", "Like termbox but better (see tcell docs)", '2', nextSlide).
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)
AddItem("Extensive documentation", "Demo code is available for each widget", '5', nextSlide)
return "Introduction", Center(80, 10, list)
}

View File

@ -16,8 +16,8 @@ import (
"fmt"
"strconv"
"github.com/gdamore/tcell"
"git.sr.ht/~tslocum/cview"
"github.com/gdamore/tcell"
)
// Slide is a function which returns the slide's main primitive and its title.
@ -69,10 +69,17 @@ func main() {
ScrollToHighlight()
pages.SwitchToPage(strconv.Itoa(currentSlide))
}
cursor := 0
var slideRegions []int
for index, slide := range slides {
slideRegions = append(slideRegions, cursor)
title, primitive := slide(nextSlide)
pages.AddPage(strconv.Itoa(index), primitive, true, index == currentSlide)
fmt.Fprintf(info, `%d ["%d"][darkcyan]%s[white][""] `, index+1, index, title)
cursor += len(title) + 4
}
// Create the main layout.
@ -93,6 +100,33 @@ func main() {
app.EnableMouse()
var screenHeight int
app.SetAfterResizeFunc(func(screen tcell.Screen) {
_, screenHeight = screen.Size()
})
app.SetMouseCapture(func(event *cview.EventMouse) *cview.EventMouse {
atX, atY := event.Position()
if event.Action()&cview.MouseClick != 0 && atY == screenHeight-1 {
slideClicked := -1
for i, region := range slideRegions {
if atX >= region {
slideClicked = i
}
}
if slideClicked >= 0 {
currentSlide = slideClicked
info.Highlight(strconv.Itoa(currentSlide)).
ScrollToHighlight()
pages.SwitchToPage(strconv.Itoa(currentSlide))
}
return nil
}
return event
})
// Start the application.
if err := app.SetRoot(layout, true).Run(); err != nil {
panic(err)