Embed Pages in Root

This commit is contained in:
aditya-K2
2023-05-01 13:09:07 +05:30
parent 7d20da67d9
commit cc4971543c
4 changed files with 12 additions and 17 deletions

View File

@@ -389,7 +389,7 @@ func NewApplication() *tview.Application {
AddItem(progressBar, 5, 1, false) AddItem(progressBar, 5, 1, false)
root.Primitive("Main", Flex) root.Primitive("Main", Flex)
App.SetRoot(root.Root, true).SetFocus(Main) App.SetRoot(root, true).SetFocus(Main)
// Start Routines // Start Routines
InitNotifier() InitNotifier()

View File

@@ -136,7 +136,7 @@ func notify(n *notification) {
npos = posArr.GetNextPosition() npos = posArr.GetNextPosition()
} }
n.position = npos n.position = npos
root.Root.AddPage(currentTime, n, false, true) root.AddPage(currentTime, n, false, true)
App.Draw() App.Draw()
App.SetFocus(Main) App.SetFocus(Main)
if n.msg != nil { if n.msg != nil {
@@ -144,7 +144,7 @@ func notify(n *notification) {
App.Draw() App.Draw()
} }
time.Sleep(n.timer) time.Sleep(n.timer)
root.Root.RemovePage(currentTime) root.RemovePage(currentTime)
posArr.Free(npos) posArr.Free(npos)
App.SetFocus(Main) App.SetFocus(Main)
App.Draw() App.Draw()

View File

@@ -4,26 +4,21 @@ import (
"time" "time"
"github.com/aditya-K2/gspt/config" "github.com/aditya-K2/gspt/config"
"github.com/gdamore/tcell/v2"
"github.com/aditya-K2/tview" "github.com/aditya-K2/tview"
"github.com/gdamore/tcell/v2"
) )
type Root struct { type Root struct {
Root *tview.Pages *tview.Pages
after func() after func()
} }
func NewRoot() *Root { func NewRoot() *Root {
m := &Root{} return &Root{tview.NewPages(), nil}
Root := tview.NewPages()
m.Root = Root
return m
} }
func (m *Root) Primitive(name string, t tview.Primitive) { func (m *Root) Primitive(name string, t tview.Primitive) {
m.Root.AddPage(name, t, true, true) m.AddPage(name, t, true, true)
} }
func (m *Root) AfterContextClose(f func()) { func (m *Root) AfterContextClose(f func()) {
@@ -42,16 +37,16 @@ func (m *Root) AddCenteredWidget(t CenteredWidget) {
closec := make(chan bool) closec := make(chan bool)
currentTime := time.Now().String() currentTime := time.Now().String()
sHandler := t.SelectionHandler() sHandler := t.SelectionHandler()
_, _, w, h := m.Root.GetRect() _, _, w, h := m.GetRect()
closeCtx := func() { closeCtx := func() {
m.Root.RemovePage(currentTime) m.RemovePage(currentTime)
if m.after != nil { if m.after != nil {
m.after() m.after()
} }
} }
drawCtx := func() { drawCtx := func() {
m.Root.AddPage(currentTime, t.Primitive(), false, true) m.AddPage(currentTime, t.Primitive(), false, true)
p.SetRect(t.Size(w, h)) p.SetRect(t.Size(w, h))
} }
redraw := func() { redraw := func() {
@@ -87,7 +82,7 @@ func (m *Root) AddCenteredWidget(t CenteredWidget) {
select { select {
case <-tck.C: case <-tck.C:
{ {
_, _, _w, _h := m.Root.GetRect() _, _, _w, _h := m.GetRect()
if _w != w || _h != h { if _w != w || _h != h {
w = _w w = _w
h = _h h = _h

View File

@@ -57,7 +57,7 @@ func getFontWidth() (int, int, error) {
if err != nil { if err != nil {
return 0, 0, err return 0, 0, err
} }
_, _, rw, rh := root.Root.GetRect() _, _, rw, rh := root.GetRect()
if rw == 0 || rh == 0 { if rw == 0 || rh == 0 {
return 0, 0, errors.New("Unable to get row width and height") return 0, 0, errors.New("Unable to get row width and height")
} }