cloud-control-panel/example-UI-table/tableDemo.go

139 lines
3.3 KiB
Go
Raw Normal View History

// based off andlabs/ui/examples/table.go
package main
import "fmt"
import "log"
import "github.com/davecgh/go-spew/spew"
import "github.com/andlabs/ui"
import _ "github.com/andlabs/ui/winmanifest"
func newDemoModelHandler() *modelHandler {
mh := new(modelHandler)
mh.rows = 20
mh.funcColumnTypes = demoColumnTypes
mh.scanCellValue = demoCellValue
mh.setCellValue = demoSetCellValue
mh.checkStates = make([]int, mh.rows)
mh.vms = make([]vmRowData, mh.rows)
mh.vms[8].hostname = "host1"
mh.vms[9].hostname = "host3test"
mh.yellowRow = -1
log.Println("Called newDemoModelhandler() with mh=", mh)
spew.Dump(mh)
return mh
}
func demoColumnTypes() []ui.TableValue {
return []ui.TableValue{
ui.TableString(""), // column 0 text
ui.TableString(""), // column 1 text
ui.TableString(""), // column 2 text
ui.TableColor{}, // row background color
ui.TableColor{}, // column 1 text color
ui.TableImage{}, // column 1 image
ui.TableString(""), // column 4 button text
ui.TableInt(0), // column 3 checkbox state
ui.TableInt(0), // column 5 progress
}
}
var img [2]*ui.Image
func demoCellValue(mh *modelHandler, row, column int) ui.TableValue {
switch column {
case 0:
return ui.TableString(fmt.Sprintf("Row %d", row))
case 1:
return ui.TableString("Colors!")
case 7:
return ui.TableInt(mh.checkStates[row])
case 8:
if row == 0 {
return ui.TableInt(0)
}
if row == 13 {
return ui.TableInt(100)
}
if row == 14 {
return ui.TableInt(-1)
}
return ui.TableInt(50)
case 5:
if row < 8 {
return ui.TableImage{img[0]}
}
return ui.TableImage{img[1]}
case 4:
if (row % 2) == 1 {
return ui.TableColor{0.5, 0, 0.75, 1}
}
return nil
case 3:
if row == mh.yellowRow {
return ui.TableColor{1, 1, 0, .1}
}
if row == 3 {
// Red with .1 transparency
// R G B Trans
return ui.TableColor{1, 0, 0, .1}
}
if row == 11 {
return ui.TableColor{0, 0.5, 1, 0.5}
}
return nil
case 2:
return ui.TableString(mh.vms[row].hostname)
case 6:
return ui.TableString("Make Yellow")
}
panic("unreachable in demoCellValue()")
}
func makeDemotable(name string) *ui.Table {
img[0] = ui.NewImage(16, 16)
img[1] = ui.NewImage(16, 16)
mh := newDemoModelHandler()
mh.name = name
model := ui.NewTableModel(mh)
table := ui.NewTable(
&ui.TableParams{
Model: model,
RowBackgroundColorModelColumn: 3,
})
table.AppendTextColumn("Column 1", 0, ui.TableModelColumnNeverEditable, nil)
table.AppendImageTextColumn("Column 2", 5,
1, ui.TableModelColumnNeverEditable, &ui.TableTextColumnOptionalParams{
ColorModelColumn: 4,
});
table.AppendTextColumn("hostname", 2, ui.TableModelColumnAlwaysEditable, nil)
table.AppendCheckboxColumn("Checkboxes", 7, ui.TableModelColumnAlwaysEditable)
table.AppendButtonColumn("Buttons", 6, ui.TableModelColumnAlwaysEditable)
table.AppendProgressBarColumn("Progress Bar", 8)
return table
}
func demoSetCellValue(mh *modelHandler, m *ui.TableModel, row, column int, value ui.TableValue) {
if column == 2 {
mh.vms[row].hostname = string(value.(ui.TableString))
}
if column == 6 { // row background color
prevYellowRow := mh.yellowRow
mh.yellowRow = row
if prevYellowRow != -1 {
m.RowChanged(prevYellowRow)
}
m.RowChanged(mh.yellowRow)
}
if column == 7 { // checkboxes
mh.checkStates[row] = int(value.(ui.TableInt))
}
}