main: infra for custom context actions

This commit is contained in:
mappu 2024-06-28 11:34:00 +12:00
parent a14e58297a
commit feffa67677
8 changed files with 50 additions and 1 deletions

View File

@ -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) { func (ld *badgerLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) {
vcl.ShowMessage("Badger doesn't support querying") vcl.ShowMessage("Badger doesn't support querying")
} }

View File

@ -94,6 +94,10 @@ func (ld *boltLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
return boltChildBucketNames(ld.db, ndata.bucketPath) 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) { func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) {
vcl.ShowMessage("Bolt doesn't support querying") vcl.ShowMessage("Bolt doesn't support querying")
} }

View File

@ -31,6 +31,10 @@ func (n *noLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
return []string{}, nil 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) { func (n *noLoadedDatabase) Keepalive(ndata *navData) {
} }

View File

@ -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) { func (ld *pebbleLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) {
vcl.ShowMessage("pebble doesn't support querying") vcl.ShowMessage("pebble doesn't support querying")
} }

View File

@ -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) { func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView) {
ctx := context.Background() ctx := context.Background()

View File

@ -230,6 +230,10 @@ func (ld *sqliteLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
return nil, fmt.Errorf("unknown nav path %#v", ndata.bucketPath) 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() { func (ld *sqliteLoadedDatabase) Close() {
_ = ld.db.Close() _ = ld.db.Close()
ld.arena = nil ld.arena = nil

View File

@ -4,6 +4,11 @@ import (
"github.com/ying32/govcl/vcl" "github.com/ying32/govcl/vcl"
) )
type contextAction struct {
Name string
Callback func(ndata *navData)
}
// loadedDatabase is a DB-agnostic interface for each loaded database. // loadedDatabase is a DB-agnostic interface for each loaded database.
type loadedDatabase interface { type loadedDatabase interface {
DisplayName() string DisplayName() string
@ -12,6 +17,7 @@ type loadedDatabase interface {
RenderForNav(f *TMainForm, ndata *navData) RenderForNav(f *TMainForm, ndata *navData)
ExecQuery(query string, resultArea *vcl.TListView) ExecQuery(query string, resultArea *vcl.TListView)
NavChildren(ndata *navData) ([]string, error) NavChildren(ndata *navData) ([]string, error)
NavContext(ndata *navData) ([]contextAction, error)
Keepalive(ndata *navData) Keepalive(ndata *navData)
Close() Close()
} }

21
main.go
View File

@ -378,7 +378,26 @@ func (f *TMainForm) OnNavContextPopup(sender vcl.IObject, mousePos types.TPoint,
mnu.Items().Add(mnuRefresh) mnu.Items().Add(mnuRefresh)
// Check what custom actions the ndata->db itself wants to add // 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 { if curItem.Parent() == nil {
// Top-level item (database connection). Allow closing by right-click. // Top-level item (database connection). Allow closing by right-click.