gui: common column handling, set widths automatically

This commit is contained in:
mappu 2024-07-05 19:35:24 +12:00
parent d7e3363173
commit 7fbf2ef1ed
8 changed files with 33 additions and 25 deletions

View File

@ -7,7 +7,6 @@ import (
"github.com/dgraph-io/badger/v4"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
)
type badgerLoadedDatabase struct {
@ -46,8 +45,6 @@ func (ld *badgerLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
colKey := f.contentBox.Columns().Add()
colKey.Title().SetCaption("Key")
colKey.SetWidth(MY_WIDTH)
colKey.SetAlignment(types.TaLeftJustify)
colVal := f.contentBox.Columns().Add()
colVal.Title().SetCaption("Value")
@ -82,6 +79,7 @@ func (ld *badgerLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
}
// Valid
vcl_stringgrid_columnwidths(f.contentBox)
f.contentBox.SetEnabled(true)
}

View File

@ -9,7 +9,6 @@ import (
"unsafe"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
"go.etcd.io/bbolt"
"go.etcd.io/bbolt/version"
)
@ -49,12 +48,8 @@ func (ld *boltLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
// Load data
// Bolt always uses Key + Value as the columns
f.contentBox.Columns().Clear()
colKey := f.contentBox.Columns().Add()
colKey.Title().SetCaption("Key")
colKey.SetWidth(MY_WIDTH)
colKey.SetAlignment(types.TaLeftJustify)
colVal := f.contentBox.Columns().Add()
colVal.Title().SetCaption("Value")
@ -81,6 +76,7 @@ func (ld *boltLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
}
// Valid
vcl_stringgrid_columnwidths(f.contentBox)
f.contentBox.SetEnabled(true)
}

View File

@ -51,7 +51,6 @@ func (ld *debconfLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
col := f.contentBox.Columns().Add()
col.Title().SetCaption(cname)
col.SetWidth(MY_WIDTH)
}
for _, entry := range ld.db.Entries {
@ -66,6 +65,7 @@ func (ld *debconfLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
}
// Valid
vcl_stringgrid_columnwidths(f.contentBox)
f.contentBox.SetEnabled(true)
}

View File

@ -9,7 +9,6 @@ import (
"github.com/cockroachdb/pebble"
"github.com/cockroachdb/pebble/vfs"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
)
type pebbleLoadedDatabase struct {
@ -50,8 +49,6 @@ func (ld *pebbleLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
colKey := f.contentBox.Columns().Add()
colKey.Title().SetCaption("Key")
colKey.SetWidth(MY_WIDTH)
colKey.SetAlignment(types.TaLeftJustify)
colVal := f.contentBox.Columns().Add()
colVal.Title().SetCaption("Value")
@ -73,6 +70,7 @@ func (ld *pebbleLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
}
// Valid
vcl_stringgrid_columnwidths(f.contentBox)
f.contentBox.SetEnabled(true)
}

View File

@ -10,7 +10,6 @@ import (
"github.com/redis/go-redis/v9"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
)
type redisLoadedDatabase struct {
@ -77,8 +76,6 @@ func (ld *redisLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
colKey := f.contentBox.Columns().Add()
colKey.Title().SetCaption("Key")
colKey.SetWidth(MY_WIDTH)
colKey.SetAlignment(types.TaLeftJustify)
colType := f.contentBox.Columns().Add()
colType.Title().SetCaption("Type")
colVal := f.contentBox.Columns().Add()
@ -130,6 +127,7 @@ func (ld *redisLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
}
// Valid
vcl_stringgrid_columnwidths(f.contentBox)
f.contentBox.SetEnabled(true)
} else {
@ -183,10 +181,8 @@ func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGr
return
}
resultArea.SetEnabled(false)
resultArea.Clear()
vcl_stringgrid_clear(resultArea)
resultArea.Columns().Clear()
colVal := resultArea.Columns().Add()
colVal.Title().SetCaption("Result")
@ -209,6 +205,7 @@ func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringGr
}
vcl_stringgrid_columnwidths(resultArea)
resultArea.SetEnabled(true)
}

View File

@ -9,7 +9,6 @@ import (
_ "yvbolt/sqliteclidriver"
"github.com/ying32/govcl/vcl"
"github.com/ying32/govcl/vcl/types"
)
const (
@ -87,6 +86,7 @@ func (ld *sqliteLoadedDatabase) RenderForNav(f *TMainForm, ndata *navData) {
populateRows(datar, f.contentBox)
// We successfully populated the data grid
vcl_stringgrid_columnwidths(f.contentBox)
f.contentBox.SetEnabled(true)
} else {
@ -123,12 +123,9 @@ func (ld *sqliteLoadedDatabase) sqliteGetColumnNamesForTable(tableName string) (
}
func populateColumns(names []string, dest *vcl.TStringGrid) {
dest.Columns().Clear()
for _, columnName := range names {
col := dest.Columns().Add()
col.Title().SetCaption(columnName)
col.SetWidth(MY_WIDTH)
col.SetAlignment(types.TaLeftJustify)
}
}
@ -171,8 +168,7 @@ func (ld *sqliteLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringG
defer rr.Close()
resultArea.SetEnabled(false)
resultArea.Clear()
vcl_stringgrid_clear(resultArea)
columns, err := rr.Columns()
if err != nil {
@ -184,6 +180,7 @@ func (ld *sqliteLoadedDatabase) ExecQuery(query string, resultArea *vcl.TStringG
populateRows(rr, resultArea)
vcl_stringgrid_columnwidths(resultArea)
resultArea.SetEnabled(true)
}

View File

@ -213,7 +213,8 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
f.contentBox.SetParent(dataTab)
f.contentBox.BorderSpacing().SetAround(MY_SPACING)
f.contentBox.SetAlign(types.AlClient) // fill remaining space
f.contentBox.SetOptions(f.contentBox.Options().Include(types.GoThumbTracking))
f.contentBox.SetOptions(f.contentBox.Options().Include(types.GoThumbTracking, types.GoColSizing, types.GoDblClickAutoSize))
f.contentBox.SetDefaultColWidth(MY_WIDTH)
vcl_stringgrid_clear(f.contentBox)
queryTab := vcl.NewTabSheet(f.Tabs)
@ -265,6 +266,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
f.queryResult.BorderSpacing().SetRight(MY_SPACING)
f.queryResult.BorderSpacing().SetBottom(MY_SPACING)
f.queryResult.SetOptions(f.queryResult.Options().Include(types.GoThumbTracking))
f.queryResult.SetDefaultColWidth(MY_WIDTH)
vcl_stringgrid_clear(f.queryResult)
f.none = &noLoadedDatabase{}

View File

@ -12,6 +12,8 @@ const (
MY_SPACING = 6
MY_HEIGHT = 90
MY_WIDTH = 180
MAX_AUTO_COL_WIDTH = 240
)
// vcl_row makes a TPanel row inside the target component.
@ -69,3 +71,21 @@ func vcl_stringgrid_clear(d *vcl.TStringGrid) {
d.SetEnabled(false)
d.Columns().Clear()
}
func vcl_stringgrid_columnwidths(d *vcl.TStringGrid) {
// Skip slow processing for very large result sets
if d.RowCount() > 1000 {
return
}
d.AutoAdjustColumns()
// AutoAdjustColumns will leave some columns massively too large by themselves
// Reign them back in
ct := d.Columns().Count()
for i := int32(0); i < ct; i++ {
if d.ColWidths(i) > MAX_AUTO_COL_WIDTH {
d.SetColWidths(i, MAX_AUTO_COL_WIDTH)
}
}
}