From f18eea791509c3246b68edb82a02d3974a5363d9 Mon Sep 17 00:00:00 2001 From: Romein van Buren Date: Sat, 27 May 2023 21:18:47 +0200 Subject: [PATCH] menu mess --- frontend/src/lib/stores/sharedstate.js | 16 +++++ .../src/organisms/connection/index.svelte | 5 ++ frontend/wailsjs/go/app/App.d.ts | 2 + frontend/wailsjs/go/app/App.js | 4 ++ internal/app/app.go | 64 ++++--------------- internal/app/app_menu.go | 60 +++++++++++++++++ main.go | 11 +++- 7 files changed, 110 insertions(+), 52 deletions(-) create mode 100644 frontend/src/lib/stores/sharedstate.js create mode 100644 internal/app/app_menu.go diff --git a/frontend/src/lib/stores/sharedstate.js b/frontend/src/lib/stores/sharedstate.js new file mode 100644 index 0000000..9c1f263 --- /dev/null +++ b/frontend/src/lib/stores/sharedstate.js @@ -0,0 +1,16 @@ +import { ReportSharedStateVariable } from '$wails/go/app/App'; +import { writable } from 'svelte/store'; + +function sharedStateStore(name) { + const { set, subscribe } = writable(); + subscribe(newValue => ReportSharedStateVariable(name, newValue)); + return { set, subscribe }; +} + +const sharedState = { + currentHost: sharedStateStore('currenthost'), + currentDb: sharedStateStore('currentdb'), + currentColl: sharedStateStore('currentcoll'), +}; + +export default sharedState; diff --git a/frontend/src/organisms/connection/index.svelte b/frontend/src/organisms/connection/index.svelte index 1a969f2..6213269 100644 --- a/frontend/src/organisms/connection/index.svelte +++ b/frontend/src/organisms/connection/index.svelte @@ -9,6 +9,7 @@ import DumpInfo from './dump.svelte'; import HostDetail from './hostdetail.svelte'; import HostTree from './hosttree.svelte'; + import sharedState from '$lib/stores/sharedstate'; export let hosts = {}; export let activeHostKey = ''; @@ -20,6 +21,10 @@ let hostDetailKey = ''; let exportInfo; + $: sharedState.currentHost.set(activeHostKey); + $: sharedState.currentDb.set(activeDbKey); + $: sharedState.currentColl.set(activeCollKey); + async function getHosts() { hosts = await Hosts(); } diff --git a/frontend/wailsjs/go/app/App.d.ts b/frontend/wailsjs/go/app/App.d.ts index 648d725..af346eb 100755 --- a/frontend/wailsjs/go/app/App.d.ts +++ b/frontend/wailsjs/go/app/App.d.ts @@ -55,6 +55,8 @@ export function RemoveView(arg1:string):Promise; export function RenameCollection(arg1:string,arg2:string,arg3:string,arg4:string):Promise; +export function ReportSharedStateVariable(arg1:string,arg2:string):Promise; + export function SaveQuery(arg1:string):Promise; export function SavedQueries():Promise; diff --git a/frontend/wailsjs/go/app/App.js b/frontend/wailsjs/go/app/App.js index 4cce520..ba254b8 100755 --- a/frontend/wailsjs/go/app/App.js +++ b/frontend/wailsjs/go/app/App.js @@ -98,6 +98,10 @@ export function RenameCollection(arg1, arg2, arg3, arg4) { return window['go']['app']['App']['RenameCollection'](arg1, arg2, arg3, arg4); } +export function ReportSharedStateVariable(arg1, arg2) { + return window['go']['app']['App']['ReportSharedStateVariable'](arg1, arg2); +} + export function SaveQuery(arg1) { return window['go']['app']['App']['SaveQuery'](arg1); } diff --git a/internal/app/app.go b/internal/app/app.go index b1f34ab..75dac69 100644 --- a/internal/app/app.go +++ b/internal/app/app.go @@ -3,6 +3,7 @@ package app import ( "context" "errors" + "fmt" "os" "os/exec" "path/filepath" @@ -10,8 +11,6 @@ import ( "github.com/garraflavatra/rolens/internal/ui" "github.com/ncruces/zenity" - "github.com/wailsapp/wails/v2/pkg/menu" - "github.com/wailsapp/wails/v2/pkg/menu/keys" wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime" ) @@ -29,13 +28,16 @@ type EnvironmentInfo struct { } type App struct { - ctx context.Context - ui *ui.UI - Env EnvironmentInfo + Env EnvironmentInfo + State map[string]string + ctx context.Context + ui *ui.UI } func NewApp() *App { - a := &App{} + a := &App{ + State: make(map[string]string), + } _, err := exec.LookPath("mongodump") a.Env.HasMongoDump = err == nil @@ -71,7 +73,7 @@ func NewApp() *App { func (a *App) Startup(ctx context.Context, ui *ui.UI) { a.ctx = ctx a.ui = ui - wailsRuntime.LogInfo(a.ctx, "Startup") + wailsRuntime.LogInfo(a.ctx, "Runcycle: Startup") wailsEnv := wailsRuntime.Environment(a.ctx) a.Env.Arch = wailsEnv.Arch @@ -80,7 +82,7 @@ func (a *App) Startup(ctx context.Context, ui *ui.UI) { } func (a *App) Shutdown(ctx context.Context) { - wailsRuntime.LogInfo(a.ctx, "Shutdown") + wailsRuntime.LogInfo(a.ctx, "Runcycle: Shutdown") } func (a *App) Environment() EnvironmentInfo { @@ -101,47 +103,7 @@ func (a *App) PurgeLogDirectory() { } } -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, menuEventEmitter(a, "OpenAboutModal")) - aboutMenu.AddText("Prefrences…", keys.CmdOrCtrl(","), menuEventEmitter(a, "OpenPrefrences")) - aboutMenu.AddText("Open data directory…", nil, func(cd *menu.CallbackData) { a.ui.Reveal(a.Env.DataDirectory) }) - aboutMenu.AddSeparator() - aboutMenu.AddText("Open log directory…", nil, func(cd *menu.CallbackData) { a.ui.Reveal(a.Env.LogDirectory) }) - aboutMenu.AddText("Purge logs", nil, func(cd *menu.CallbackData) { a.PurgeLogDirectory() }) - aboutMenu.AddSeparator() - 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, "") }) - - return appMenu +func (a *App) ReportSharedStateVariable(key, value string) { + a.State[key] = value + wailsRuntime.LogDebug(a.ctx, fmt.Sprintf("State has been reported: %s=\"%s\"", key, value)) } diff --git a/internal/app/app_menu.go b/internal/app/app_menu.go new file mode 100644 index 0000000..3638fb6 --- /dev/null +++ b/internal/app/app_menu.go @@ -0,0 +1,60 @@ +package app + +import ( + "runtime" + + "github.com/wailsapp/wails/v2/pkg/menu" + "github.com/wailsapp/wails/v2/pkg/menu/keys" + wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime" +) + +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("File") + aboutMenu.AddText("About Rolens", nil, menuEventEmitter(a, "OpenAboutModal")) + aboutMenu.AddSeparator() + aboutMenu.AddText("Prefrences…", keys.CmdOrCtrl(","), menuEventEmitter(a, "OpenPrefrences")) + aboutMenu.AddSeparator() + aboutMenu.AddText("Open data directory…", nil, func(cd *menu.CallbackData) { a.ui.Reveal(a.Env.DataDirectory) }) + aboutMenu.AddText("Open log directory…", nil, func(cd *menu.CallbackData) { a.ui.Reveal(a.Env.LogDirectory) }) + aboutMenu.AddText("Purge logs", nil, func(cd *menu.CallbackData) { a.PurgeLogDirectory() }) + aboutMenu.AddSeparator() + aboutMenu.AddText("Quit Rolens", 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.Append(&menu.MenuItem{ + Label: "New database…", + Accelerator: keys.CmdOrCtrl("u"), + Click: menuEventEmitter(a, "CreateDatabase"), + Disabled: a.State["currenthost"] == "", + }) + + 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, "") }) + + return appMenu +} diff --git a/main.go b/main.go index c65dbb5..b965331 100644 --- a/main.go +++ b/main.go @@ -7,11 +7,13 @@ import ( "github.com/garraflavatra/rolens/internal/app" uictrl "github.com/garraflavatra/rolens/internal/ui" + "github.com/ncruces/zenity" "github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options/assetserver" "github.com/wailsapp/wails/v2/pkg/options/mac" + "github.com/wailsapp/wails/v2/pkg/runtime" ) var ( @@ -40,8 +42,15 @@ func main() { AssetServer: &assetserver.Options{Assets: assets}, OnStartup: func(ctx context.Context) { - app.Startup(ctx, ui) + defer func() { + if p := recover(); p != nil { + runtime.LogFatalf(ctx, "Application panicked: %v", p) + zenity.Error("A fatal error occured.") + } + }() + ui.Startup(ctx) + app.Startup(ctx, ui) }, OnShutdown: app.Shutdown,