From 29e93dcab147f328f7e19e7d0375e0626b9d448a Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Mon, 23 Jan 2023 14:12:14 +0100 Subject: [PATCH] Menus and keyboard shortcuts --- .../connection/collection/index.svelte | 3 ++ .../src/organisms/connection/index.svelte | 4 ++ frontend/vite.config.js | 8 ++-- internal/app/app.go | 43 +++++++++++-------- 4 files changed, 37 insertions(+), 21 deletions(-) diff --git a/frontend/src/organisms/connection/collection/index.svelte b/frontend/src/organisms/connection/collection/index.svelte index f4dac9b..7c418af 100644 --- a/frontend/src/organisms/connection/collection/index.svelte +++ b/frontend/src/organisms/connection/collection/index.svelte @@ -8,6 +8,7 @@ import Remove from './remove.svelte'; import Stats from './stats.svelte'; import Update from './update.svelte'; + import { EventsOn } from '../../../../wailsjs/runtime/runtime'; export let collection; export let hostKey; @@ -23,6 +24,8 @@ collection.key = collectionKey; } + EventsOn('OpenCollectionTab', name => (tab = name || tab)); + async function catchQuery(event) { tab = 'find'; await tick(); diff --git a/frontend/src/organisms/connection/index.svelte b/frontend/src/organisms/connection/index.svelte index e2f6608..dac4019 100644 --- a/frontend/src/organisms/connection/index.svelte +++ b/frontend/src/organisms/connection/index.svelte @@ -8,6 +8,7 @@ import CollectionDetail from './collection/index.svelte'; import HostDetail from './hostdetail.svelte'; import Icon from '../../components/icon.svelte'; + import { EventsOn } from '../../../wailsjs/runtime/runtime'; export let hosts = {}; export let activeHostKey = ''; @@ -72,6 +73,9 @@ busy.end(); } + EventsOn('CreateHost', createHost); + EventsOn('CreateDatabase', () => newDb = {}); + EventsOn('CreateCollection', () => newColl = {}); onMount(getHosts); diff --git a/frontend/vite.config.js b/frontend/vite.config.js index d37616f..f7acf5d 100644 --- a/frontend/vite.config.js +++ b/frontend/vite.config.js @@ -1,7 +1,7 @@ -import {defineConfig} from 'vite' -import {svelte} from '@sveltejs/vite-plugin-svelte' +import { defineConfig } from 'vite'; +import { svelte } from '@sveltejs/vite-plugin-svelte'; // https://vitejs.dev/config/ export default defineConfig({ - plugins: [svelte()] -}) + plugins: [ svelte() ], +}); diff --git a/internal/app/app.go b/internal/app/app.go index 3d45771..2a63c64 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -21,34 +21,43 @@ func (a *App) Startup(ctx context.Context) { a.ctx = ctx } +func menuEventEmitter(a *App, eventName string, data ...interface{}) func(cd *menu.CallbackData) { + return func(cd *menu.CallbackData) { + wailsRuntime.EventsEmit(a.ctx, eventName, data...) + } +} + func (a *App) Menu() *menu.Menu { appMenu := menu.NewMenu() aboutMenu := appMenu.AddSubmenu("About") - aboutMenu.AddText("About…", nil, func(cd *menu.CallbackData) { - wailsRuntime.EventsEmit(a.ctx, "OpenAboutModal") - }) - aboutMenu.AddText("Prefrences…", keys.CmdOrCtrl(","), func(cd *menu.CallbackData) { - wailsRuntime.EventsEmit(a.ctx, "OpenPrefrences") - }) + aboutMenu.AddText("About…", nil, menuEventEmitter(a, "OpenAboutModal")) + aboutMenu.AddText("Prefrences…", keys.CmdOrCtrl(","), menuEventEmitter(a, "OpenPrefrences")) aboutMenu.AddSeparator() - aboutMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(cd *menu.CallbackData) { - wailsRuntime.Quit(a.ctx) - }) - - // fileMenu := appMenu.AddSubmenu("File") - // fileMenu.AddText("Hosts…", keys.CmdOrCtrl("k"), func(cd *menu.CallbackData) { - // wailsRuntime.EventsEmit(a.ctx, "OpenHostsModal") - // }) + aboutMenu.AddText("Quit", keys.CmdOrCtrl("q"), func(cd *menu.CallbackData) { wailsRuntime.Quit(a.ctx) }) if runtime.GOOS == "darwin" { appMenu.Append(menu.EditMenu()) } + hostMenu := appMenu.AddSubmenu("Host") + hostMenu.AddText("New host", keys.CmdOrCtrl("y"), menuEventEmitter(a, "CreateHost")) + + databaseMenu := appMenu.AddSubmenu("Database") + databaseMenu.AddText("New database", keys.CmdOrCtrl("u"), menuEventEmitter(a, "CreateDatabase")) + + collectionMenu := appMenu.AddSubmenu("Collection") + collectionMenu.AddText("New collection", keys.CmdOrCtrl("i"), menuEventEmitter(a, "CreateCollection")) + collectionMenu.AddSeparator() + collectionMenu.AddText("Stats", keys.Combo("h", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "stats")) + collectionMenu.AddText("Find", keys.Combo("f", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "find")) + collectionMenu.AddText("Insert", keys.Combo("i", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "insert")) + collectionMenu.AddText("Update", keys.Combo("u", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "update")) + collectionMenu.AddText("Remove", keys.Combo("r", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "remove")) + collectionMenu.AddText("Indexes", keys.Combo("x", keys.CmdOrCtrlKey, keys.OptionOrAltKey), menuEventEmitter(a, "OpenCollectionTab", "indexes")) + helpMenu := appMenu.AddSubmenu("Help") - helpMenu.AddText("User guide", nil, func(cd *menu.CallbackData) { - wailsRuntime.BrowserOpenURL(a.ctx, "") - }) + helpMenu.AddText("User guide", nil, func(cd *menu.CallbackData) { wailsRuntime.BrowserOpenURL(a.ctx, "") }) return appMenu }