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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -12,6 +12,8 @@ const (
MY_SPACING = 6 MY_SPACING = 6
MY_HEIGHT = 90 MY_HEIGHT = 90
MY_WIDTH = 180 MY_WIDTH = 180
MAX_AUTO_COL_WIDTH = 240
) )
// vcl_row makes a TPanel row inside the target component. // 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.SetEnabled(false)
d.Columns().Clear() 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)
}
}
}