From feffa67677def3e6d091f1a6f6108bed8c239a12 Mon Sep 17 00:00:00 2001 From: mappu Date: Fri, 28 Jun 2024 11:34:00 +1200 Subject: [PATCH] main: infra for custom context actions --- db_badger.go | 4 ++++ db_bolt.go | 4 ++++ db_none.go | 4 ++++ db_pebble.go | 4 ++++ db_redis.go | 4 ++++ db_sqlite.go | 4 ++++ loadedDatabase.go | 6 ++++++ main.go | 21 ++++++++++++++++++++- 8 files changed, 50 insertions(+), 1 deletion(-) diff --git a/db_badger.go b/db_badger.go index 7241a0d..b5af2e7 100644 --- a/db_badger.go +++ b/db_badger.go @@ -103,6 +103,10 @@ func (ld *badgerLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { } } +func (ld *badgerLoadedDatabase) NavContext(ndata *navData) ([]contextAction, error) { + return nil, nil // No special actions are supported +} + func (ld *badgerLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) { vcl.ShowMessage("Badger doesn't support querying") } diff --git a/db_bolt.go b/db_bolt.go index 408cb2b..cc505ca 100644 --- a/db_bolt.go +++ b/db_bolt.go @@ -94,6 +94,10 @@ func (ld *boltLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { return boltChildBucketNames(ld.db, ndata.bucketPath) } +func (ld *boltLoadedDatabase) NavContext(ndata *navData) ([]contextAction, error) { + return nil, nil // No special actions are supported +} + func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) { vcl.ShowMessage("Bolt doesn't support querying") } diff --git a/db_none.go b/db_none.go index 037450b..38421d7 100644 --- a/db_none.go +++ b/db_none.go @@ -31,6 +31,10 @@ func (n *noLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { return []string{}, nil } +func (ld *noLoadedDatabase) NavContext(ndata *navData) ([]contextAction, error) { + return nil, nil // No special actions are supported +} + func (n *noLoadedDatabase) Keepalive(ndata *navData) { } diff --git a/db_pebble.go b/db_pebble.go index 4bf1902..10ab2a6 100644 --- a/db_pebble.go +++ b/db_pebble.go @@ -91,6 +91,10 @@ func (ld *pebbleLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { } } +func (ld *pebbleLoadedDatabase) NavContext(ndata *navData) ([]contextAction, error) { + return nil, nil // No special actions are supported +} + func (ld *pebbleLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) { vcl.ShowMessage("pebble doesn't support querying") } diff --git a/db_redis.go b/db_redis.go index 176d13c..da6228e 100644 --- a/db_redis.go +++ b/db_redis.go @@ -165,6 +165,10 @@ func (ld *redisLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { } } +func (ld *redisLoadedDatabase) NavContext(ndata *navData) ([]contextAction, error) { + return nil, nil // No special actions are supported +} + func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) { ctx := context.Background() diff --git a/db_sqlite.go b/db_sqlite.go index 59b9f9c..98b608d 100644 --- a/db_sqlite.go +++ b/db_sqlite.go @@ -230,6 +230,10 @@ func (ld *sqliteLoadedDatabase) NavChildren(ndata *navData) ([]string, error) { return nil, fmt.Errorf("unknown nav path %#v", ndata.bucketPath) } +func (ld *sqliteLoadedDatabase) NavContext(ndata *navData) ([]contextAction, error) { + return nil, nil // No special actions are supported +} + func (ld *sqliteLoadedDatabase) Close() { _ = ld.db.Close() ld.arena = nil diff --git a/loadedDatabase.go b/loadedDatabase.go index bcc6829..1e2dfa0 100644 --- a/loadedDatabase.go +++ b/loadedDatabase.go @@ -4,6 +4,11 @@ import ( "github.com/ying32/govcl/vcl" ) +type contextAction struct { + Name string + Callback func(ndata *navData) +} + // loadedDatabase is a DB-agnostic interface for each loaded database. type loadedDatabase interface { DisplayName() string @@ -12,6 +17,7 @@ type loadedDatabase interface { RenderForNav(f *TMainForm, ndata *navData) ExecQuery(query string, resultArea *vcl.TListView) NavChildren(ndata *navData) ([]string, error) + NavContext(ndata *navData) ([]contextAction, error) Keepalive(ndata *navData) Close() } diff --git a/main.go b/main.go index 4ac7ebc..bcd5c23 100644 --- a/main.go +++ b/main.go @@ -378,7 +378,26 @@ func (f *TMainForm) OnNavContextPopup(sender vcl.IObject, mousePos types.TPoint, mnu.Items().Add(mnuRefresh) // Check what custom actions the ndata->db itself wants to add - // ... + ndata := (*navData)(curItem.Data()) + actions, err := ndata.ld.NavContext(ndata) + if err != nil { + vcl.ShowMessage(err.Error()) + return + } + + if len(actions) > 0 { + mnuSep := vcl.NewMenuItem(mnu) + mnuSep.SetCaption("-") + mnu.Items().Add(mnuSep) + + for _, action := range actions { + mnuAction := vcl.NewMenuItem(mnu) + mnuAction.SetCaption(action.Name) + cb := action.Callback // Copy to avoid reuse of loop variable + mnuAction.SetOnClick(func(sender vcl.IObject) { cb(ndata) }) + mnu.Items().Add(mnuAction) + } + } if curItem.Parent() == nil { // Top-level item (database connection). Allow closing by right-click.