2024-05-10 05:24:24 +02:00
|
|
|
// Copyright 2024, Command Line Inc.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
// Note, main.go needs to be in the root of the project for the go:embed directive to work.
|
|
|
|
|
|
|
|
import (
|
2024-05-24 23:08:24 +02:00
|
|
|
"context"
|
2024-05-10 05:24:24 +02:00
|
|
|
"embed"
|
|
|
|
"log"
|
2024-05-17 07:48:23 +02:00
|
|
|
"net/http"
|
2024-05-21 00:28:47 +02:00
|
|
|
"runtime"
|
2024-05-17 07:48:23 +02:00
|
|
|
"strings"
|
2024-05-24 23:08:24 +02:00
|
|
|
"time"
|
2024-05-10 05:24:24 +02:00
|
|
|
|
2024-05-12 18:52:12 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/blockstore"
|
2024-05-14 22:34:41 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/eventbus"
|
|
|
|
"github.com/wavetermdev/thenextwave/pkg/service/blockservice"
|
2024-05-24 23:08:24 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/service/clientservice"
|
2024-05-14 18:37:41 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/service/fileservice"
|
2024-05-12 18:52:12 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wavebase"
|
2024-05-22 06:15:11 +02:00
|
|
|
"github.com/wavetermdev/thenextwave/pkg/wstore"
|
2024-05-12 18:52:12 +02:00
|
|
|
|
2024-05-10 05:24:24 +02:00
|
|
|
"github.com/wailsapp/wails/v3/pkg/application"
|
2024-05-16 23:00:39 +02:00
|
|
|
"github.com/wailsapp/wails/v3/pkg/events"
|
2024-05-10 05:24:24 +02:00
|
|
|
)
|
|
|
|
|
2024-05-14 06:42:25 +02:00
|
|
|
//go:embed dist
|
2024-05-10 05:24:24 +02:00
|
|
|
var assets embed.FS
|
|
|
|
|
2024-05-21 00:28:47 +02:00
|
|
|
//go:embed build/icons.icns
|
2024-05-10 05:24:24 +02:00
|
|
|
var appIcon []byte
|
|
|
|
|
2024-05-16 23:00:39 +02:00
|
|
|
func createAppMenu(app *application.App) *application.Menu {
|
|
|
|
menu := application.NewMenu()
|
|
|
|
menu.AddRole(application.AppMenu)
|
|
|
|
fileMenu := menu.AddSubmenu("File")
|
2024-05-24 23:08:24 +02:00
|
|
|
// newWindow := fileMenu.Add("New Window")
|
|
|
|
// newWindow.OnClick(func(appContext *application.Context) {
|
|
|
|
// createWindow(app)
|
|
|
|
// })
|
2024-05-16 23:00:39 +02:00
|
|
|
closeWindow := fileMenu.Add("Close Window")
|
|
|
|
closeWindow.OnClick(func(appContext *application.Context) {
|
|
|
|
app.CurrentWindow().Close()
|
|
|
|
})
|
|
|
|
menu.AddRole(application.EditMenu)
|
|
|
|
menu.AddRole(application.ViewMenu)
|
|
|
|
menu.AddRole(application.WindowMenu)
|
|
|
|
menu.AddRole(application.HelpMenu)
|
|
|
|
return menu
|
|
|
|
}
|
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
func createWindow(windowData *wstore.Window, app *application.App) {
|
2024-05-16 23:00:39 +02:00
|
|
|
window := app.NewWebviewWindowWithOptions(application.WebviewWindowOptions{
|
|
|
|
Title: "Wave Terminal",
|
|
|
|
Mac: application.MacWindow{
|
|
|
|
InvisibleTitleBarHeight: 50,
|
|
|
|
Backdrop: application.MacBackdropTranslucent,
|
|
|
|
TitleBar: application.MacTitleBarHiddenInset,
|
|
|
|
},
|
2024-05-24 23:08:24 +02:00
|
|
|
BackgroundColour: application.NewRGB(0, 0, 0),
|
2024-05-26 20:59:14 +02:00
|
|
|
URL: "/public/index.html?windowid=" + windowData.OID,
|
2024-05-24 23:08:24 +02:00
|
|
|
X: windowData.Pos.X,
|
|
|
|
Y: windowData.Pos.Y,
|
|
|
|
Width: windowData.WinSize.Width,
|
|
|
|
Height: windowData.WinSize.Height,
|
2024-05-16 23:00:39 +02:00
|
|
|
})
|
|
|
|
eventbus.RegisterWailsWindow(window)
|
|
|
|
window.On(events.Common.WindowClosing, func(event *application.WindowEvent) {
|
|
|
|
eventbus.UnregisterWailsWindow(window.ID())
|
|
|
|
})
|
2024-05-24 23:08:24 +02:00
|
|
|
window.Show()
|
2024-05-16 23:00:39 +02:00
|
|
|
}
|
|
|
|
|
2024-05-17 07:48:23 +02:00
|
|
|
type waveAssetHandler struct {
|
|
|
|
AssetHandler http.Handler
|
|
|
|
}
|
|
|
|
|
|
|
|
func serveWaveUrls(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.URL.Path == "/wave/stream-file" {
|
|
|
|
fileName := r.URL.Query().Get("path")
|
|
|
|
fileName = wavebase.ExpandHomeDir(fileName)
|
|
|
|
http.ServeFile(w, r, fileName)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
http.NotFound(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (wah waveAssetHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
|
|
if r.Method == "GET" && strings.HasPrefix(r.URL.Path, "/wave/") {
|
|
|
|
serveWaveUrls(w, r)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
wah.AssetHandler.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
2024-05-10 05:24:24 +02:00
|
|
|
func main() {
|
2024-05-12 18:52:12 +02:00
|
|
|
err := wavebase.EnsureWaveHomeDir()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring wave home dir: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-05-21 00:28:47 +02:00
|
|
|
waveLock, err := wavebase.AcquireWaveLock()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error acquiring wave lock (another instance of Wave is likely running): %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-05-12 18:52:12 +02:00
|
|
|
log.Printf("wave home dir: %s\n", wavebase.GetWaveHomeDir())
|
|
|
|
err = blockstore.InitBlockstore()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error initializing blockstore: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-05-22 06:15:11 +02:00
|
|
|
err = wstore.InitWStore()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error initializing wstore: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-05-24 23:08:24 +02:00
|
|
|
err = wstore.EnsureInitialData()
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("error ensuring initial data: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-05-12 18:52:12 +02:00
|
|
|
|
2024-05-10 05:24:24 +02:00
|
|
|
app := application.New(application.Options{
|
|
|
|
Name: "NextWave",
|
|
|
|
Description: "The Next Wave Terminal",
|
2024-05-21 20:09:22 +02:00
|
|
|
Services: []application.Service{
|
|
|
|
application.NewService(&fileservice.FileService{}),
|
|
|
|
application.NewService(&blockservice.BlockService{}),
|
2024-05-24 23:08:24 +02:00
|
|
|
application.NewService(&clientservice.ClientService{}),
|
2024-05-10 05:24:24 +02:00
|
|
|
},
|
|
|
|
Icon: appIcon,
|
|
|
|
Assets: application.AssetOptions{
|
2024-05-17 07:48:23 +02:00
|
|
|
Handler: waveAssetHandler{AssetHandler: application.AssetFileServerFS(assets)},
|
2024-05-10 05:24:24 +02:00
|
|
|
},
|
|
|
|
Mac: application.MacOptions{
|
|
|
|
ApplicationShouldTerminateAfterLastWindowClosed: true,
|
|
|
|
},
|
|
|
|
})
|
2024-05-16 23:00:39 +02:00
|
|
|
menu := createAppMenu(app)
|
|
|
|
app.SetMenu(menu)
|
2024-05-14 22:34:41 +02:00
|
|
|
eventbus.RegisterWailsApp(app)
|
2024-05-10 05:24:24 +02:00
|
|
|
|
2024-05-24 23:08:24 +02:00
|
|
|
setupCtx, cancelFn := context.WithTimeout(context.Background(), 2*time.Second)
|
|
|
|
defer cancelFn()
|
2024-05-26 20:59:14 +02:00
|
|
|
client, err := wstore.DBGetSingleton[*wstore.Client](setupCtx)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting client data: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
2024-05-26 20:59:14 +02:00
|
|
|
mainWindow, err := wstore.DBGet[*wstore.Window](setupCtx, client.MainWindowId)
|
2024-05-24 23:08:24 +02:00
|
|
|
if err != nil {
|
|
|
|
log.Printf("error getting main window: %v\n", err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if mainWindow == nil {
|
|
|
|
log.Printf("no main window data\n")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
createWindow(mainWindow, app)
|
2024-05-14 22:34:41 +02:00
|
|
|
|
|
|
|
eventbus.Start()
|
|
|
|
defer eventbus.Shutdown()
|
2024-05-10 05:24:24 +02:00
|
|
|
|
|
|
|
// blocking
|
2024-05-12 18:52:12 +02:00
|
|
|
err = app.Run()
|
2024-05-10 05:24:24 +02:00
|
|
|
|
|
|
|
// If an error occurred while running the application, log it and exit.
|
|
|
|
if err != nil {
|
|
|
|
log.Printf("run error: %v\n", err)
|
|
|
|
}
|
2024-05-21 00:28:47 +02:00
|
|
|
runtime.KeepAlive(waveLock)
|
2024-05-10 05:24:24 +02:00
|
|
|
}
|