move to more common code

This commit is contained in:
Jeff Carr 2025-03-17 08:18:56 -05:00
parent 46472fa868
commit a97379d76f
3 changed files with 88 additions and 166 deletions

View File

@ -52,16 +52,33 @@ func drawWindow(win *gadgets.BasicWindow) {
group1 := vbox.NewGroup("Virtigo Settings")
grid := group1.NewGrid("buildOptions", 0, 0)
var hyperWin *GenericWindow
grid.NewButton("hypervisors", func() {
if hyperWin != nil {
hyperWin.Toggle()
/*
var hyperWin *gadgets.GenericWindow
grid.NewButton("hypervisors", func() {
if hyperWin != nil {
hyperWin.Toggle()
return
}
hyperWin = makeHypervisorsWindow(me.cluster.H)
})
*/
var newHyperWin *stdHypervisorTableWin
grid.NewButton("show hypervisors", func() {
if newHyperWin != nil {
log.Info("redraw hypervisors")
newHyperWin.doNewStdHypervisors(me.cluster.H)
return
}
hyperWin = makeHypervisorsWindow(me.cluster.H)
log.Info("Hypervisors len=", me.cluster.H.Len())
newHyperWin = newHypervisorsWindow()
newHyperWin.doNewStdHypervisors(me.cluster.H)
newHyperWin.win.Custom = func() {
log.Info("hiding table window")
}
})
var dropWin *GenericWindow
var dropWin *gadgets.GenericWindow
grid.NewButton("droplets", func() {
if dropWin != nil {
dropWin.Toggle()
@ -84,7 +101,7 @@ func drawWindow(win *gadgets.BasicWindow) {
}
})
var eventWin *GenericWindow
var eventWin *gadgets.GenericWindow
grid.NewButton("events)", func() {
log.Info("todo: make code for this")
if eventWin != nil {
@ -99,7 +116,7 @@ func drawWindow(win *gadgets.BasicWindow) {
log.Info("todo: make code for this")
})
var testWin *GenericWindow
var testWin *gadgets.GenericWindow
grid.NewButton("create droplet", func() {
if testWin != nil {
testWin.Toggle()
@ -127,8 +144,8 @@ func updateUptimeGui(uptime string) {
me.lastuptime.SetLabel("last uptime at " + datestamp)
}
func makeDropletsWindow(pb *virtpb.Droplets) (*GenericWindow, *virtpb.DropletsTable) {
win := NewGenericWindow("Droplets registered with Virtigo", "Buttons of things")
func makeDropletsWindow(pb *virtpb.Droplets) (*gadgets.GenericWindow, *virtpb.DropletsTable) {
win := gadgets.NewGenericWindow("Droplets registered with Virtigo", "Buttons of things")
t := pb.NewTable("testDroptable")
t.NewUuid()
@ -181,55 +198,8 @@ func makeDropletsWindow(pb *virtpb.Droplets) (*GenericWindow, *virtpb.DropletsTa
return win, t
}
func makeHypervisorsWindow(pb *virtpb.Hypervisors) *GenericWindow {
win := NewGenericWindow("Hypervisors registered with Virtigo", "Buttons of things")
t := pb.NewTable("testHyper")
grid := win.Group.RawGrid()
grid.NewButton("List", func() {
log.Info("list...")
})
/*
grid.NewButton("Update", func() {
t.Update()
})
*/
tbox := win.Bottom.Box() // a vertical box (like a stack of books)
t.NewUuid()
t.SetParent(tbox)
t.AddHostname()
t.AddMemory()
t.AddCpus()
t.AddTimeFunc("last poll", func(h *virtpb.Hypervisor) time.Time {
hm := me.hmap[h]
tmp := hm.lastpoll
log.Info("poll age", h.Hostname, virtpb.FormatDuration(time.Since(tmp)))
return tmp
})
t.AddKillcount()
t.AddStringFunc("droplets", func(h *virtpb.Hypervisor) string {
var totalDroplets int
var totalUnknownDroplets int
// dur := time.Since(h.lastpoll)
// tmp := virtpb.FormatDuration(dur)
// fmt.Fprintln(w, h.pb.Hostname, "killcount =", h.killcount, "lastpoll:", tmp)
hm := me.hmap[h]
for name, _ := range hm.lastDroplets {
totalDroplets += 1
d := me.cluster.FindDropletByName(name)
if d == nil {
totalUnknownDroplets += 1
}
}
log.Printf("Total Droplets %d total libvirt only droplets = %d\n", totalDroplets, totalUnknownDroplets)
return fmt.Sprintf("%d", totalDroplets)
})
t.ShowTable()
return win
}
func makeEventsWindow(pb *virtpb.Events) *GenericWindow {
win := NewGenericWindow("Cluster Events", "Buttons of things")
func makeEventsWindow(pb *virtpb.Events) *gadgets.GenericWindow {
win := gadgets.NewGenericWindow("Cluster Events", "Buttons of things")
grid := win.Group.RawGrid()
grid.NewButton("List", func() {
log.Info("list...")

View File

@ -1,107 +0,0 @@
// Copyright 2017-2025 WIT.COM Inc. All rights reserved.
// Use of this source code is governed by the GPL 3.0
package main
// This model works for 99.9% of all windows
// This is the Default Standard Window Model
import (
"go.wit.com/lib/gadgets"
"go.wit.com/log"
"go.wit.com/gui"
)
type GenericWindow struct {
Win *gadgets.BasicWindow // the window widget itself
Shelf *gui.Node // the overall box: the shelf
Stack *gui.Node // the first box is a stack
Top *gui.Node // the first item in the stack is always a shelf like box
Group *gui.Node // the first item top box is always a group
Middle *gui.Node // the middle box (shelf style)
Bottom *gui.Node // the bottom box (stack style)
}
func (gw *GenericWindow) Hidden() bool {
if gw == nil {
return true
}
if gw.Win == nil {
return true
}
return gw.Win.Hidden()
}
func (gw *GenericWindow) Toggle() {
if gw.Hidden() {
gw.Show()
} else {
gw.Hide()
}
}
func (gw *GenericWindow) Show() {
if gw == nil {
return
}
if gw.Win == nil {
return
}
gw.Win.Show()
}
func (gw *GenericWindow) Hide() {
if gw == nil {
return
}
if gw.Win == nil {
return
}
gw.Win.Hide()
}
func (gw *GenericWindow) Disable() {
if gw == nil {
return
}
if gw.Shelf == nil {
return
}
gw.Shelf.Disable()
}
func (gw *GenericWindow) Enable() {
if gw == nil {
return
}
if gw.Shelf == nil {
return
}
gw.Shelf.Enable()
}
func NewGenericWindow(title string, grouptxt string) *GenericWindow {
gw := new(GenericWindow)
gw.Win = gadgets.RawBasicWindow(title)
gw.Win.Make()
gw.Win.Custom = func() {
log.Warn("Found Window close. setting hidden=true")
// sets the hidden flag to false so Toggle() works
gw.Win.Hide()
}
gw.Shelf = gw.Win.Box()
// gw.Shelf.Vertical().SetProgName("ShelfBox")
gw.Stack = gw.Shelf.NewVerticalBox("Stackbox")
gw.Top = gw.Stack.NewVerticalBox("Stackbox")
gw.Middle = gw.Stack.Box()
gw.Bottom = gw.Stack.Box()
gw.Group = gw.Top.NewGroup(grouptxt)
gw.Show()
return gw
}

View File

@ -4,6 +4,7 @@
package main
import (
"fmt"
"sync"
"time"
@ -103,3 +104,61 @@ func (dw *stdHypervisorTableWin) doStdHypervisors(pb *virtpb.Hypervisors) {
dw.TB.ShowTable()
}
// default table protobuf window
func (dw *stdHypervisorTableWin) doNewStdHypervisors(pb *virtpb.Hypervisors) {
dw.Lock()
defer dw.Unlock()
// erase the old table
if dw.TB != nil {
dw.TB.Delete()
dw.TB = nil
}
// init the table
dw.pb = pb
t := dw.pb.NewTable("HypervisorsPB Off")
t.NewUuid()
t.SetParent(dw.box)
// pick the columns
t.AddHostname()
t.AddMemory()
t.AddCpus()
t.AddKillcount()
t.AddTimeFunc("last poll", func(h *virtpb.Hypervisor) time.Time {
// hm := me.hmap[h]
// tmp := hm.lastpoll
// log.Info("poll age", h.Hostname, virtpb.FormatDuration(time.Since(tmp)))
return time.Now()
})
t.AddStringFunc("droplets", func(h *virtpb.Hypervisor) string {
var totalDroplets int
var totalUnknownDroplets int
// dur := time.Since(h.lastpoll)
// tmp := virtpb.FormatDuration(dur)
// fmt.Fprintln(w, h.pb.Hostname, "killcount =", h.killcount, "lastpoll:", tmp)
hm := me.hmap[h]
for name, _ := range hm.lastDroplets {
totalDroplets += 1
d := me.cluster.FindDropletByName(name)
if d == nil {
totalUnknownDroplets += 1
}
}
log.Printf("Total Droplets %d total libvirt only droplets = %d\n", totalDroplets, totalUnknownDroplets)
return fmt.Sprintf("%d", totalDroplets)
// return "todo"
})
// display the protobuf
dw.TB = t
f := func(e *virtpb.Hypervisor) {
log.Info("std HypervisorWindow() something here", e.Hostname)
// m.Enabled = true
}
dw.TB.Custom(f)
dw.TB.ShowTable()
}