main: add nav context menu, support closing open connections

This commit is contained in:
mappu 2024-06-23 15:28:15 +12:00
parent 7e5d17100d
commit b45faa2e73
7 changed files with 81 additions and 0 deletions

View File

@ -108,6 +108,11 @@ func (ld *badgerLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListVie
vcl.ShowMessage("Badger doesn't support querying")
}
func (ld *badgerLoadedDatabase) Close() {
_ = ld.db.Close()
ld.arena = nil
}
var _ loadedDatabase = &badgerLoadedDatabase{} // interface assertion
//

View File

@ -98,6 +98,11 @@ func (ld *boltLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView)
vcl.ShowMessage("Bolt doesn't support querying")
}
func (ld *boltLoadedDatabase) Close() {
_ = ld.db.Close()
ld.arena = nil
}
var _ loadedDatabase = &boltLoadedDatabase{} // interface assertion
//

View File

@ -33,3 +33,5 @@ func (n *noLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
func (n *noLoadedDatabase) Keepalive(ndata *navData) {
}
func (n *noLoadedDatabase) Close() {}

View File

@ -150,6 +150,11 @@ func (ld *redisLoadedDatabase) ExecQuery(query string, resultArea *vcl.TListView
fmt.Printf("result\n%#v\n", ret)
}
func (ld *redisLoadedDatabase) Close() {
_ = ld.db.Close()
ld.arena = nil
}
var _ loadedDatabase = &redisLoadedDatabase{} // interface assertion
//

View File

@ -230,6 +230,11 @@ func (ld *sqliteLoadedDatabase) NavChildren(ndata *navData) ([]string, error) {
return nil, fmt.Errorf("unknown nav path %#v", ndata.bucketPath)
}
func (ld *sqliteLoadedDatabase) Close() {
_ = ld.db.Close()
ld.arena = nil
}
var _ loadedDatabase = &sqliteLoadedDatabase{} // interface assertion
//

View File

@ -13,6 +13,7 @@ type loadedDatabase interface {
ExecQuery(query string, resultArea *vcl.TListView)
NavChildren(ndata *navData) ([]string, error)
Keepalive(ndata *navData)
Close()
}
// navData is the .Data() pointer for each TTreeNode in the left-hand tree.

58
main.go
View File

@ -115,6 +115,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
f.Buckets.SetReadOnly(true) // prevent click to rename on nodes
f.Buckets.SetOnExpanding(f.OnNavExpanding)
f.Buckets.SetOnChange(f.OnNavChange)
f.Buckets.SetOnContextPopup(f.OnNavContextPopup)
hsplit := vcl.NewSplitter(f)
hsplit.SetParent(f)
@ -262,6 +263,63 @@ func (f *TMainForm) OnMnuFileExitClick(sender vcl.IObject) {
f.Close()
}
func (f *TMainForm) OnNavContextPopup(sender vcl.IObject, mousePos types.TPoint, handled *bool) {
*handled = true
curItem := f.Buckets.Selected()
if curItem == nil {
// Nothing is selected at all
return
}
mnu := vcl.NewPopupMenu(f.Buckets)
mnuRefresh := vcl.NewMenuItem(mnu)
mnuRefresh.SetCaption("Refresh")
mnuRefresh.SetOnClick(f.OnNavContextRefresh)
mnu.Items().Add(mnuRefresh)
// Check what custom actions the ndata->db itself wants to add
// ...
if curItem.Parent() == nil {
// Top-level item (database connection). Allow closing by right-click.
mnuSep := vcl.NewMenuItem(mnu)
mnuSep.SetCaption("-")
mnu.Items().Add(mnuSep)
mnuClose := vcl.NewMenuItem(mnu)
mnuClose.SetCaption("Close")
mnuClose.SetOnClick(f.OnNavContextClose)
mnu.Items().Add(mnuClose)
}
// Show popup
mnu.Popup2()
}
func (f *TMainForm) OnNavContextRefresh(sender vcl.IObject) {
vcl.ShowMessage("TODO")
// TODO
}
func (f *TMainForm) OnNavContextClose(sender vcl.IObject) {
curItem := f.Buckets.Selected()
if curItem == nil {
return // Nothing selected (shouldn't happen)
}
if curItem.Parent() != nil {
return // Selection is not top-level DB connection (shouldn't happen)
}
ndata := (*navData)(curItem.Data())
ndata.ld.Close()
curItem.Delete()
// n.b. This triggers OnNavChange, which will then re-render from noLoadedDatabase{}
}
func (f *TMainForm) OnQueryExecute(sender vcl.IObject) {
// If query tab is not selected, switch to it, but do not exec
if f.Tabs.ActivePageIndex() != 2 {