A lot of layout work
This commit is contained in:
BIN
example/example
Executable file
BIN
example/example
Executable file
Binary file not shown.
34
example/main.go
Normal file
34
example/main.go
Normal file
@@ -0,0 +1,34 @@
|
||||
/*
|
||||
Copyright © Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package main
|
||||
|
||||
func main() {
|
||||
app := NewUi()
|
||||
defer func() {
|
||||
maybePanic := recover()
|
||||
app.cleanup()
|
||||
if maybePanic != nil {
|
||||
panic(maybePanic)
|
||||
}
|
||||
}()
|
||||
app.run()
|
||||
}
|
||||
118
example/ui.go
Normal file
118
example/ui.go
Normal file
@@ -0,0 +1,118 @@
|
||||
/*
|
||||
Copyright © Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
type Ui struct {
|
||||
h, w int
|
||||
|
||||
running bool
|
||||
tScreen tcell.Screen
|
||||
style tcell.Style
|
||||
|
||||
screen *UiScreen
|
||||
|
||||
cursor int
|
||||
}
|
||||
|
||||
func NewUi() *Ui {
|
||||
a := Ui{}
|
||||
err := a.init()
|
||||
if err != nil {
|
||||
fmt.Println(err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
return &a
|
||||
}
|
||||
|
||||
func (ui *Ui) init() error {
|
||||
var err error
|
||||
if ui.tScreen, err = tcell.NewScreen(); err != nil {
|
||||
return err
|
||||
}
|
||||
if err = ui.tScreen.Init(); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
ui.style = tcell.StyleDefault
|
||||
ui.tScreen.SetStyle(ui.style)
|
||||
ui.tScreen.Clear()
|
||||
|
||||
ui.SetScreen(&UiScreen{})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (ui *Ui) run() error {
|
||||
ui.running = true
|
||||
|
||||
for {
|
||||
if !ui.running {
|
||||
return nil
|
||||
}
|
||||
|
||||
ui.tScreen.Clear()
|
||||
ui.screen.Draw()
|
||||
|
||||
ui.tScreen.Show()
|
||||
|
||||
// Poll Events
|
||||
ev := ui.tScreen.PollEvent()
|
||||
switch ev := ev.(type) {
|
||||
case *tcell.EventResize:
|
||||
ui.tScreen.Sync()
|
||||
ui.w, ui.h = ev.Size()
|
||||
ui.handleResize(ev)
|
||||
|
||||
case *tcell.EventKey:
|
||||
if ev.Key() == tcell.KeyCtrlC {
|
||||
ui.stop()
|
||||
return nil
|
||||
}
|
||||
ui.screen.HandleKey(ev)
|
||||
|
||||
case *tcell.EventTime:
|
||||
ui.screen.HandleTime(ev)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func (ui *Ui) handleResize(ev *tcell.EventResize) {
|
||||
ui.w, ui.h = ev.Size()
|
||||
ui.screen.HandleResize(ev)
|
||||
}
|
||||
|
||||
func (ui *Ui) GetSize() (int, int) { return ui.tScreen.Size() }
|
||||
func (ui *Ui) SetScreen(scr *UiScreen) {
|
||||
ui.screen = scr
|
||||
ui.screen.Init(ui)
|
||||
ui.screen.HandleResize(tcell.NewEventResize(ui.GetSize()))
|
||||
}
|
||||
|
||||
func (ui *Ui) stop() { ui.running = false }
|
||||
func (ui *Ui) cleanup() { ui.tScreen.Fini() }
|
||||
129
example/ui_screen.go
Normal file
129
example/ui_screen.go
Normal file
@@ -0,0 +1,129 @@
|
||||
/*
|
||||
Copyright © Brian Buller <brian@bullercodeworks.com>
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
||||
THE SOFTWARE.
|
||||
*/
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
w "git.bullercodeworks.com/brian/tcell-widgets"
|
||||
"github.com/gdamore/tcell"
|
||||
)
|
||||
|
||||
type UiScreen struct {
|
||||
ui *Ui
|
||||
w, h int
|
||||
|
||||
widget w.Widget
|
||||
log *w.Cli
|
||||
|
||||
widgets []w.Widget
|
||||
cursor int
|
||||
}
|
||||
|
||||
func (s *UiScreen) Init(ui *Ui) {
|
||||
// Set up our log viewer
|
||||
s.ui = ui
|
||||
s.log = w.NewCli("log", ui.style)
|
||||
|
||||
ll := w.NewLinearLayout("test", ui.style)
|
||||
ll.SetTabbable(true)
|
||||
ll.SetLogger(s.log.Log)
|
||||
|
||||
testList := w.NewList("test-list", ui.style)
|
||||
for i := 0; i < 10; i++ {
|
||||
testList.Add(fmt.Sprintf("Item %d", i))
|
||||
}
|
||||
testList.SetLogger(s.log.Log)
|
||||
ll.Add(testList)
|
||||
|
||||
btnL := w.NewLinearLayout("btn-ll", ui.style)
|
||||
btnL.SetTabbable(true)
|
||||
btnL.SetLogger(s.log.Log)
|
||||
btnL.SetOrientation(w.LinLayH)
|
||||
|
||||
btnCancel := w.NewButton("btn-cancel", ui.style)
|
||||
btnCancel.SetLabel("Cancel")
|
||||
btnL.Add(btnCancel)
|
||||
btnL.AddFlag(btnCancel, w.LFAlignVBottom)
|
||||
|
||||
btnOk := w.NewButton("btn-ok", ui.style)
|
||||
btnOk.SetLabel("Ok")
|
||||
btnL.Add(btnOk)
|
||||
btnL.AddFlag(btnOk, w.LFAlignVBottom)
|
||||
ll.Add(btnL)
|
||||
ll.AddFlag(btnL, w.LFAlignVBottom)
|
||||
|
||||
dw := w.NewDebugWidget("debug", ui.style)
|
||||
dw.SetWidget(ll)
|
||||
dw.SetSize(w.Coord{X: 20, Y: 30})
|
||||
dw.SetActive(true)
|
||||
s.widget = dw
|
||||
|
||||
s.widgets = append(s.widgets, dw)
|
||||
s.widgets = append(s.widgets, s.log)
|
||||
}
|
||||
|
||||
func (s *UiScreen) HandleResize(ev *tcell.EventResize) {
|
||||
s.w, s.h = ev.Size()
|
||||
s.widget.HandleResize(ev)
|
||||
lgH := 20
|
||||
s.log.SetPos(w.Coord{X: 0, Y: s.h - lgH - 1})
|
||||
s.log.HandleResize(w.Coord{X: s.w, Y: lgH}.ResizeEvent())
|
||||
}
|
||||
|
||||
func (s *UiScreen) HandleKey(ev *tcell.EventKey) bool {
|
||||
if ev.Key() == tcell.KeyCtrlJ {
|
||||
// Ctrl+J is the keypad 'Enter'
|
||||
ev = tcell.NewEventKey(tcell.KeyEnter, 0, 0)
|
||||
}
|
||||
|
||||
if s.cursor < len(s.widgets) {
|
||||
if s.widgets[s.cursor].HandleKey(ev) {
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
if ev.Key() == tcell.KeyTab {
|
||||
s.cursor = (s.cursor + 1) % len(s.widgets)
|
||||
if s.cursor == 0 {
|
||||
s.widget.SetActive(true)
|
||||
s.log.SetActive(false)
|
||||
} else {
|
||||
s.widget.SetActive(false)
|
||||
s.log.SetActive(true)
|
||||
}
|
||||
return true
|
||||
}
|
||||
if s.cursor == 0 {
|
||||
return s.widget.HandleKey(ev)
|
||||
} else {
|
||||
return s.log.HandleKey(ev)
|
||||
}
|
||||
}
|
||||
func (s *UiScreen) HandleTime(ev *tcell.EventTime) {}
|
||||
func (s *UiScreen) Draw() {
|
||||
s.widget.Draw(s.ui.tScreen)
|
||||
s.log.Draw(s.ui.tScreen)
|
||||
}
|
||||
func (s *UiScreen) Log(txt string, args ...any) {}
|
||||
|
||||
func (s *UiScreen) Exit() {}
|
||||
Reference in New Issue
Block a user