// SPDX-License-Identifier: AGPL-3.0-or-later package main import ( "context" "log/slog" "runtime" "github.com/wailsapp/wails/v3/pkg/application" ) // commandEvent carries a menu command to the frontend: "new-page", "search" // or "toggle-sidebar". const commandEvent = "docwell:command" // docsURL is where Help → Documentation goes; release builds set it with // -ldflags "-X main.docsURL=…". Without it the item is left out. var docsURL = "" // appMenu builds the native menu bar: File, Edit, View, Window and Help, // plus the app menu on macOS. Commands that act on the page are sent to the // frontend; switching servers saves the choice and reloads the window. type appMenu struct { app *application.App win *application.WebviewWindow settings *SettingsService updates bool menu *application.Menu } func newAppMenu(app *application.App, win *application.WebviewWindow, settings *SettingsService, updates bool) *appMenu { m := &appMenu{app: app, win: win, settings: settings, updates: updates, menu: app.NewMenu()} m.build() app.Menu.Set(m.menu) return m } func (m *appMenu) command(name string) func(*application.Context) { return func(*application.Context) { showWindow(m.win) m.app.Event.Emit(commandEvent, name) } } func (m *appMenu) route(route string) func(*application.Context) { return func(*application.Context) { showWindow(m.win) m.app.Event.Emit(openRouteEvent, route) } } // build fills the menu; it runs again when the server list changes. func (m *appMenu) build() { menu := m.menu menu.Clear() if runtime.GOOS == "darwin" { menu.AddRole(application.AppMenu) } file := menu.AddSubmenu("File") file.Add("New Page").SetAccelerator("CmdOrCtrl+N").OnClick(m.command("new-page")) // Search (Ctrl+K) and the sidebar (Ctrl+/) have shortcuts in the page // itself; an accelerator here as well would run them twice. file.Add("Search…").OnClick(m.command("search")) file.AddSeparator() servers := file.AddSubmenu("Server") if s, err := m.settings.Get(); err == nil { for _, sv := range s.Servers { id := sv.ID servers.AddRadio(sv.Name, sv.ID == s.Active).OnClick(func(*application.Context) { m.switchTo(id) }) } } servers.AddSeparator() servers.Add("Add or Manage Servers…").OnClick(m.route("/settings/servers")) file.Add("Account Settings…").SetAccelerator("CmdOrCtrl+,").OnClick(m.route("/settings/account")) file.AddSeparator() if runtime.GOOS == "darwin" { file.AddRole(application.CloseWindow) } else { file.AddRole(application.Quit) } menu.AddRole(application.EditMenu) view := menu.AddSubmenu("View") view.Add("Toggle Sidebar").OnClick(m.command("toggle-sidebar")) view.AddSeparator() view.AddRole(application.Reload) view.AddSeparator() view.AddRole(application.ResetZoom) view.AddRole(application.ZoomIn) view.AddRole(application.ZoomOut) view.AddSeparator() view.AddRole(application.ToggleFullscreen) menu.AddRole(application.WindowMenu) help := menu.AddSubmenu("Help") if docsURL != "" { help.Add("Docwell Documentation").OnClick(func(*application.Context) { if err := m.app.Browser.OpenURL(docsURL); err != nil { slog.Warn("could not open the documentation", "err", err) } }) } if m.updates { help.Add("Check for Updates…").OnClick(func(*application.Context) { go func() { if err := m.app.Updater.CheckAndInstall(context.Background()); err != nil { slog.Warn("update check failed", "err", err) } }() }) } if runtime.GOOS != "darwin" { help.AddSeparator() help.AddRole(application.About) } menu.Update() } // switchTo makes another saved server active and reloads the window, which // then loads that server's settings and keychain token. func (m *appMenu) switchTo(id string) { if _, err := m.settings.SetActive(id); err != nil { slog.Warn("could not switch server", "server", id, "err", err) return } showWindow(m.win) m.win.Reload() }