1
0
mirror of https://github.com/garraflavatra/rolens.git synced 2025-01-18 13:07:58 +00:00

menu mess

This commit is contained in:
Romein van Buren 2023-05-27 21:18:47 +02:00
parent ab1891be27
commit f18eea7915
Signed by: romein
GPG Key ID: 0EFF8478ADDF6C49
7 changed files with 110 additions and 52 deletions

View File

@ -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;

View File

@ -9,6 +9,7 @@
import DumpInfo from './dump.svelte'; import DumpInfo from './dump.svelte';
import HostDetail from './hostdetail.svelte'; import HostDetail from './hostdetail.svelte';
import HostTree from './hosttree.svelte'; import HostTree from './hosttree.svelte';
import sharedState from '$lib/stores/sharedstate';
export let hosts = {}; export let hosts = {};
export let activeHostKey = ''; export let activeHostKey = '';
@ -20,6 +21,10 @@
let hostDetailKey = ''; let hostDetailKey = '';
let exportInfo; let exportInfo;
$: sharedState.currentHost.set(activeHostKey);
$: sharedState.currentDb.set(activeDbKey);
$: sharedState.currentColl.set(activeCollKey);
async function getHosts() { async function getHosts() {
hosts = await Hosts(); hosts = await Hosts();
} }

2
frontend/wailsjs/go/app/App.d.ts generated vendored
View File

@ -55,6 +55,8 @@ export function RemoveView(arg1:string):Promise<void>;
export function RenameCollection(arg1:string,arg2:string,arg3:string,arg4:string):Promise<boolean>; export function RenameCollection(arg1:string,arg2:string,arg3:string,arg4:string):Promise<boolean>;
export function ReportSharedStateVariable(arg1:string,arg2:string):Promise<void>;
export function SaveQuery(arg1:string):Promise<string>; export function SaveQuery(arg1:string):Promise<string>;
export function SavedQueries():Promise<map[string]app.SavedQuery>; export function SavedQueries():Promise<map[string]app.SavedQuery>;

View File

@ -98,6 +98,10 @@ export function RenameCollection(arg1, arg2, arg3, arg4) {
return window['go']['app']['App']['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) { export function SaveQuery(arg1) {
return window['go']['app']['App']['SaveQuery'](arg1); return window['go']['app']['App']['SaveQuery'](arg1);
} }

View File

@ -3,6 +3,7 @@ package app
import ( import (
"context" "context"
"errors" "errors"
"fmt"
"os" "os"
"os/exec" "os/exec"
"path/filepath" "path/filepath"
@ -10,8 +11,6 @@ import (
"github.com/garraflavatra/rolens/internal/ui" "github.com/garraflavatra/rolens/internal/ui"
"github.com/ncruces/zenity" "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" wailsRuntime "github.com/wailsapp/wails/v2/pkg/runtime"
) )
@ -29,13 +28,16 @@ type EnvironmentInfo struct {
} }
type App struct { type App struct {
ctx context.Context Env EnvironmentInfo
ui *ui.UI State map[string]string
Env EnvironmentInfo ctx context.Context
ui *ui.UI
} }
func NewApp() *App { func NewApp() *App {
a := &App{} a := &App{
State: make(map[string]string),
}
_, err := exec.LookPath("mongodump") _, err := exec.LookPath("mongodump")
a.Env.HasMongoDump = err == nil a.Env.HasMongoDump = err == nil
@ -71,7 +73,7 @@ func NewApp() *App {
func (a *App) Startup(ctx context.Context, ui *ui.UI) { func (a *App) Startup(ctx context.Context, ui *ui.UI) {
a.ctx = ctx a.ctx = ctx
a.ui = ui a.ui = ui
wailsRuntime.LogInfo(a.ctx, "Startup") wailsRuntime.LogInfo(a.ctx, "Runcycle: Startup")
wailsEnv := wailsRuntime.Environment(a.ctx) wailsEnv := wailsRuntime.Environment(a.ctx)
a.Env.Arch = wailsEnv.Arch 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) { func (a *App) Shutdown(ctx context.Context) {
wailsRuntime.LogInfo(a.ctx, "Shutdown") wailsRuntime.LogInfo(a.ctx, "Runcycle: Shutdown")
} }
func (a *App) Environment() EnvironmentInfo { 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) { func (a *App) ReportSharedStateVariable(key, value string) {
return func(cd *menu.CallbackData) { a.State[key] = value
wailsRuntime.EventsEmit(a.ctx, eventName, data...) wailsRuntime.LogDebug(a.ctx, fmt.Sprintf("State has been reported: %s=\"%s\"", key, value))
}
}
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
} }

60
internal/app/app_menu.go Normal file
View File

@ -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
}

11
main.go
View File

@ -7,11 +7,13 @@ import (
"github.com/garraflavatra/rolens/internal/app" "github.com/garraflavatra/rolens/internal/app"
uictrl "github.com/garraflavatra/rolens/internal/ui" uictrl "github.com/garraflavatra/rolens/internal/ui"
"github.com/ncruces/zenity"
"github.com/wailsapp/wails/v2" "github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/logger" "github.com/wailsapp/wails/v2/pkg/logger"
"github.com/wailsapp/wails/v2/pkg/options" "github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver" "github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/mac" "github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/runtime"
) )
var ( var (
@ -40,8 +42,15 @@ func main() {
AssetServer: &assetserver.Options{Assets: assets}, AssetServer: &assetserver.Options{Assets: assets},
OnStartup: func(ctx context.Context) { 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) ui.Startup(ctx)
app.Startup(ctx, ui)
}, },
OnShutdown: app.Shutdown, OnShutdown: app.Shutdown,