gui: use helper function for setting up menus

This commit is contained in:
mappu 2024-06-28 11:11:34 +12:00
parent a82d5e6b26
commit 70db402cdf
2 changed files with 20 additions and 45 deletions

54
main.go
View File

@ -53,17 +53,8 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFileBadger.SetImageIndex(imgVendorDgraph)
mnuFile.Add(mnuFileBadger)
mnuFileBadgerOpen := vcl.NewMenuItem(mnuFileBadger)
mnuFileBadgerOpen.SetCaption("Open database...")
mnuFileBadgerOpen.SetImageIndex(imgDatabaseAdd)
mnuFileBadgerOpen.SetOnClick(f.OnMnuFileBadgerOpenClick)
mnuFileBadger.Add(mnuFileBadgerOpen)
mnuFileBadgerMemory := vcl.NewMenuItem(mnuFileBadger)
mnuFileBadgerMemory.SetCaption("New in-memory database")
mnuFileBadgerMemory.SetImageIndex(imgDatabaseAdd)
mnuFileBadgerMemory.SetOnClick(f.OnMnuFileBadgerMemoryClick)
mnuFileBadger.Add(mnuFileBadgerMemory)
vcl_menuitem(mnuFileBadger, "Open database...", imgDatabaseAdd, f.OnMnuFileBadgerOpenClick)
vcl_menuitem(mnuFileBadger, "New in-memory database", imgDatabaseAdd, f.OnMnuFileBadgerMemoryClick)
//
@ -72,12 +63,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFileBolt.SetImageIndex(imgVendorGithub)
mnuFile.Add(mnuFileBolt)
mnuFileBoltOpen := vcl.NewMenuItem(mnuFileBolt)
mnuFileBoltOpen.SetCaption("Open database...")
mnuFileBoltOpen.SetImageIndex(imgDatabaseAdd)
mnuFileBoltOpen.SetShortCutFromString("Ctrl+O")
mnuFileBoltOpen.SetOnClick(f.OnMnuFileOpenClick)
mnuFileBolt.Add(mnuFileBoltOpen)
vcl_menuitem(mnuFileBolt, "Open database...", imgDatabaseAdd, f.OnMnuFileBoltOpenClick)
//
@ -86,17 +72,8 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFilePebble.SetImageIndex(imgVendorCockroach)
mnuFile.Add(mnuFilePebble)
mnuFilePebbleOpen := vcl.NewMenuItem(mnuFilePebble)
mnuFilePebbleOpen.SetCaption("Open database...")
mnuFilePebbleOpen.SetImageIndex(imgDatabaseAdd)
mnuFilePebbleOpen.SetOnClick(f.OnMnuFilePebbleOpenClick)
mnuFilePebble.Add(mnuFilePebbleOpen)
mnuFilePebbleMemory := vcl.NewMenuItem(mnuFilePebble)
mnuFilePebbleMemory.SetCaption("New in-memory database")
mnuFilePebbleMemory.SetImageIndex(imgDatabaseAdd)
mnuFilePebbleMemory.SetOnClick(f.OnMnuFilePebbleMemoryClick)
mnuFilePebble.Add(mnuFilePebbleMemory)
vcl_menuitem(mnuFilePebble, "Open database...", imgDatabaseAdd, f.OnMnuFilePebbleOpenClick)
vcl_menuitem(mnuFilePebble, "New in-memory database", imgDatabaseAdd, f.OnMnuFilePebbleMemoryClick)
//
@ -105,11 +82,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFileRedis.SetImageIndex(imgVendorRedis)
mnuFile.Add(mnuFileRedis)
mnuFileRedisConnect := vcl.NewMenuItem(mnuFileRedis)
mnuFileRedisConnect.SetCaption("Connect...")
mnuFileRedisConnect.SetImageIndex(imgDatabaseAdd)
mnuFileRedisConnect.SetOnClick(f.OnMnuFileRedisConnectClick)
mnuFileRedis.Add(mnuFileRedisConnect)
vcl_menuitem(mnuFileRedis, "Connect...", imgDatabaseAdd, f.OnMnuFileRedisConnectClick)
//
@ -118,17 +91,8 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFileSqlite.SetImageIndex(imgVendorSqlite)
mnuFile.Add(mnuFileSqlite)
mnuFileSqliteOpen := vcl.NewMenuItem(mnuFileSqlite)
mnuFileSqliteOpen.SetCaption("Open database...")
mnuFileSqliteOpen.SetImageIndex(imgDatabaseAdd)
mnuFileSqliteOpen.SetOnClick(f.OnMnuFileSqliteOpenClick)
mnuFileSqlite.Add(mnuFileSqliteOpen)
mnuFileSqliteMemory := vcl.NewMenuItem(mnuFileSqlite)
mnuFileSqliteMemory.SetCaption("New in-memory database")
mnuFileSqliteMemory.SetImageIndex(imgDatabaseAdd)
mnuFileSqliteMemory.SetOnClick(f.OnMnuFileSqliteMemoryClick)
mnuFileSqlite.Add(mnuFileSqliteMemory)
vcl_menuitem(mnuFileSqlite, "Open database...", imgDatabaseAdd, f.OnMnuFileSqliteOpenClick)
vcl_menuitem(mnuFileSqlite, "New in-memory database", imgDatabaseAdd, f.OnMnuFileSqliteMemoryClick)
//
@ -275,7 +239,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
f.OnNavChange(f, nil) // calls f.none.RenderForNav and sets up status bar content
}
func (f *TMainForm) OnMnuFileOpenClick(sender vcl.IObject) {
func (f *TMainForm) OnMnuFileBoltOpenClick(sender vcl.IObject) {
dlg := vcl.NewOpenDialog(f)
dlg.SetTitle("Select a database file...")
dlg.SetFilter("Bolt database|*.db|All files|*.*")

View File

@ -25,3 +25,14 @@ func vcl_row(owner vcl.IWinControl, top int32) *vcl.TPanel {
return r
}
// vcl_menuitem makes a child TMenuItem inside the parent menu.
func vcl_menuitem(parent *vcl.TMenuItem, caption string, imageIndex int32, onClick vcl.TNotifyEvent) *vcl.TMenuItem {
m := vcl.NewMenuItem(parent)
m.SetCaption(caption)
m.SetImageIndex(imageIndex)
m.SetOnClick(onClick)
parent.Add(m)
return m
}