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) mnuFileBadger.SetImageIndex(imgVendorDgraph)
mnuFile.Add(mnuFileBadger) mnuFile.Add(mnuFileBadger)
mnuFileBadgerOpen := vcl.NewMenuItem(mnuFileBadger) vcl_menuitem(mnuFileBadger, "Open database...", imgDatabaseAdd, f.OnMnuFileBadgerOpenClick)
mnuFileBadgerOpen.SetCaption("Open database...") vcl_menuitem(mnuFileBadger, "New in-memory database", imgDatabaseAdd, f.OnMnuFileBadgerMemoryClick)
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)
// //
@ -72,12 +63,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFileBolt.SetImageIndex(imgVendorGithub) mnuFileBolt.SetImageIndex(imgVendorGithub)
mnuFile.Add(mnuFileBolt) mnuFile.Add(mnuFileBolt)
mnuFileBoltOpen := vcl.NewMenuItem(mnuFileBolt) vcl_menuitem(mnuFileBolt, "Open database...", imgDatabaseAdd, f.OnMnuFileBoltOpenClick)
mnuFileBoltOpen.SetCaption("Open database...")
mnuFileBoltOpen.SetImageIndex(imgDatabaseAdd)
mnuFileBoltOpen.SetShortCutFromString("Ctrl+O")
mnuFileBoltOpen.SetOnClick(f.OnMnuFileOpenClick)
mnuFileBolt.Add(mnuFileBoltOpen)
// //
@ -86,17 +72,8 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFilePebble.SetImageIndex(imgVendorCockroach) mnuFilePebble.SetImageIndex(imgVendorCockroach)
mnuFile.Add(mnuFilePebble) mnuFile.Add(mnuFilePebble)
mnuFilePebbleOpen := vcl.NewMenuItem(mnuFilePebble) vcl_menuitem(mnuFilePebble, "Open database...", imgDatabaseAdd, f.OnMnuFilePebbleOpenClick)
mnuFilePebbleOpen.SetCaption("Open database...") vcl_menuitem(mnuFilePebble, "New in-memory database", imgDatabaseAdd, f.OnMnuFilePebbleMemoryClick)
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)
// //
@ -105,11 +82,7 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFileRedis.SetImageIndex(imgVendorRedis) mnuFileRedis.SetImageIndex(imgVendorRedis)
mnuFile.Add(mnuFileRedis) mnuFile.Add(mnuFileRedis)
mnuFileRedisConnect := vcl.NewMenuItem(mnuFileRedis) vcl_menuitem(mnuFileRedis, "Connect...", imgDatabaseAdd, f.OnMnuFileRedisConnectClick)
mnuFileRedisConnect.SetCaption("Connect...")
mnuFileRedisConnect.SetImageIndex(imgDatabaseAdd)
mnuFileRedisConnect.SetOnClick(f.OnMnuFileRedisConnectClick)
mnuFileRedis.Add(mnuFileRedisConnect)
// //
@ -118,17 +91,8 @@ func (f *TMainForm) OnFormCreate(sender vcl.IObject) {
mnuFileSqlite.SetImageIndex(imgVendorSqlite) mnuFileSqlite.SetImageIndex(imgVendorSqlite)
mnuFile.Add(mnuFileSqlite) mnuFile.Add(mnuFileSqlite)
mnuFileSqliteOpen := vcl.NewMenuItem(mnuFileSqlite) vcl_menuitem(mnuFileSqlite, "Open database...", imgDatabaseAdd, f.OnMnuFileSqliteOpenClick)
mnuFileSqliteOpen.SetCaption("Open database...") vcl_menuitem(mnuFileSqlite, "New in-memory database", imgDatabaseAdd, f.OnMnuFileSqliteMemoryClick)
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)
// //
@ -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 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 := vcl.NewOpenDialog(f)
dlg.SetTitle("Select a database file...") dlg.SetTitle("Select a database file...")
dlg.SetFilter("Bolt database|*.db|All files|*.*") 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 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
}