From 7fbf2ef1ed210b26b34ee3e82a0c55e0f25523e3 Mon Sep 17 00:00:00 2001 From: mappu Date: Fri, 5 Jul 2024 19:35:24 +1200 Subject: [PATCH] gui: common column handling, set widths automatically --- db_badger.go | 4 +--- db_bolt.go | 6 +----- db_debconf.go | 2 +- db_pebble.go | 4 +--- db_redis.go | 9 +++------ db_sqlite.go | 9 +++------ main.go | 4 +++- util_vcl.go | 20 ++++++++++++++++++++ 8 files changed, 33 insertions(+), 25 deletions(-) diff --git a/db_badger.go b/db_badger.go index 488a0f6..e784dc0 100644 --- a/db_badger.go +++ b/db_badger.go @@ -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) } diff --git a/db_bolt.go b/db_bolt.go index 12a9364..208d85d 100644 --- a/db_bolt.go +++ b/db_bolt.go @@ -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) } diff --git a/db_debconf.go b/db_debconf.go index 22813bb..7361786 100644 --- a/db_debconf.go +++ b/db_debconf.go @@ -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) } diff --git a/db_pebble.go b/db_pebble.go index 601d93c..5ea8499 100644 --- a/db_pebble.go +++ b/db_pebble.go @@ -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) } diff --git a/db_redis.go b/db_redis.go index c8c5ca3..e49f3a2 100644 --- a/db_redis.go +++ b/db_redis.go @@ -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) } diff --git a/db_sqlite.go b/db_sqlite.go index 8e77e4a..ecc46e6 100644 --- a/db_sqlite.go +++ b/db_sqlite.go @@ -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) } diff --git a/main.go b/main.go index 5bd19af..06b0c92 100644 --- a/main.go +++ b/main.go @@ -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{} diff --git a/util_vcl.go b/util_vcl.go index c4d40a7..a556c1a 100644 --- a/util_vcl.go +++ b/util_vcl.go @@ -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) + } + } +}