From 261b49782df9378016a31e616da84e352253c8d6 Mon Sep 17 00:00:00 2001 From: Jeff Carr Date: Tue, 7 May 2019 18:07:31 -0700 Subject: [PATCH] Move Cellvalue() into using standard callbacks Signed-off-by: Jeff Carr --- table.go | 59 ++++++++++++++++++----------------------------- tableCallbacks.go | 21 +++++++++++++++++ 2 files changed, 44 insertions(+), 36 deletions(-) diff --git a/table.go b/table.go index 4cd40a6..e4fad14 100644 --- a/table.go +++ b/table.go @@ -3,7 +3,6 @@ package main import "fmt" -// import "os" import "log" import "github.com/andlabs/ui" import _ "github.com/andlabs/ui/winmanifest" @@ -18,18 +17,6 @@ type vmRowData struct { disk int } -type uiColumn struct { - offset int - name string - what ui.TableValue - color ui.TableColor - -// ui.TableString(""), // column 4 button text -// ui.TableColor{}, // column 1 text color -// ui.TableImage{}, // column 1 image -// ui.TableInt(0), // column 3 checkbox state -} - type modelHandler struct { name string rows int @@ -39,17 +26,21 @@ type modelHandler struct { vms []vmRowData columnTypes string funcColumnTypes func() []ui.TableValue + scanCellValue func(*modelHandler, int, int) ui.TableValue + setCellValue func(*modelHandler, int, int) ui.TableValue } -func newDefaultModelHandler() *modelHandler { +func newDemoModelHandler() *modelHandler { mh := new(modelHandler) mh.rows = 20 + mh.funcColumnTypes = demoColumnTypes + mh.scanCellValue = demoCellValue mh.checkStates = make([]int, mh.rows) mh.vms = make([]vmRowData, mh.rows) mh.vms[8].hostname = "fire" mh.vms[9].hostname = "librem15.this.is.a.really.long.string.test" mh.yellowRow = -1 - log.Println("Called newDefaultModelhandler() with mh=", mh) + log.Println("Called newDemoModelhandler() with mh=", mh) spew.Dump(mh) return mh } @@ -63,11 +54,7 @@ func standardColumnTypes() []ui.TableValue { } } -func (mh *modelHandler) ColumnTypes(m *ui.TableModel) []ui.TableValue { - // log.Println("ColumnTypes() with m=", m, "mh=", mh) - if (mh.columnTypes == "standard") { - return standardColumnTypes() - } +func demoColumnTypes() []ui.TableValue { return []ui.TableValue{ ui.TableString(""), // column 0 text ui.TableString(""), // column 1 text @@ -83,22 +70,20 @@ func (mh *modelHandler) ColumnTypes(m *ui.TableModel) []ui.TableValue { var img [2]*ui.Image -func (mh *modelHandler) CellValue(m *ui.TableModel, row, column int) ui.TableValue { - // TODO: Figure out why this is being called 1000 times a second (10 times for each row & column) - // log.Println("CallValue() row=", row, "column=", column) - if (mh.columnTypes == "standard") { - switch column { - case 0: - if (row % 2) == 1 { - return ui.TableColor{0.5, 0.5, 0.5, .1} - } - return nil - case 1: - return ui.TableString(fmt.Sprintf("jcarrgood %d", row)) +func defaultCellValue(mh *modelHandler, row, column int) ui.TableValue { + switch column { + case 0: + if (row % 2) == 1 { + return ui.TableColor{0.5, 0.5, 0.5, .1} } - return ui.TableString(fmt.Sprintf("jcarrbad %d", row)) - // panic("unreachable") + return nil + case 1: + return ui.TableString(fmt.Sprintf("jcarrgood %d", row)) } + return ui.TableString(fmt.Sprintf("jcarrbad %d", row)) +} + +func demoCellValue(mh *modelHandler, row, column int) ui.TableValue { switch column { case 0: return ui.TableString(fmt.Sprintf("Row %d", row)) @@ -145,7 +130,7 @@ func (mh *modelHandler) CellValue(m *ui.TableModel, row, column int) ui.TableVal case 6: return ui.TableString("Make Yellow") } - panic("unreachable") + panic("unreachable in demoCellValue()") } func (mh *modelHandler) SetCellValue(m *ui.TableModel, row, column int, value ui.TableValue) { @@ -180,7 +165,7 @@ func makeDemotable(name string) *ui.Table { img[0] = ui.NewImage(16, 16) img[1] = ui.NewImage(16, 16) - mh := newDefaultModelHandler() + mh := newDemoModelHandler() mh.name = name model := ui.NewTableModel(mh) @@ -209,6 +194,8 @@ func newModelHandler(rows int) *modelHandler { mh := new(modelHandler) mh.rows = rows mh.columnTypes = "standard" + mh.funcColumnTypes = standardColumnTypes + mh.scanCellValue = defaultCellValue mh.bgcolorColumn = 0 mh.checkStates = make([]int, mh.rows) mh.vms = make([]vmRowData, mh.rows) diff --git a/tableCallbacks.go b/tableCallbacks.go index 440647d..511701d 100644 --- a/tableCallbacks.go +++ b/tableCallbacks.go @@ -1,5 +1,8 @@ package main +import "os" +import "log" + import "github.com/andlabs/ui" import _ "github.com/andlabs/ui/winmanifest" @@ -7,3 +10,21 @@ func (mh *modelHandler) NumRows(m *ui.TableModel) int { // log.Println("NumRows() with m=", m) return mh.rows } + +// FYI: this routine seems to be called around 10 to 100 times a second for each table +func (mh *modelHandler) ColumnTypes(m *ui.TableModel) []ui.TableValue { + if (mh.funcColumnTypes == nil) { + log.Println("ColumnTypes NOT DEFINED. This table wasn't setup correctly! mh.funcColmnTypes == nil") + os.Exit(-1) + } + return mh.funcColumnTypes() +} + +// TODO: Figure out why this is being called 1000 times a second (10 times for each row & column) +func (mh *modelHandler) CellValue(m *ui.TableModel, row, column int) ui.TableValue { + if (mh.scanCellValue == nil) { + log.Println("CellValue NOT DEFINED. This table wasn't setup correctly! mh.scanCellValue == nil") + os.Exit(-1) + } + return mh.scanCellValue(mh, row, column) +}