DEV Community

Cover image for Creating desktop applications in Golang
energy
energy

Posted on

Creating desktop applications in Golang


package main

import (
    "embed"
    "fmt"
    "github.com/energye/energy/v2/cef"
    "github.com/energye/energy/v2/cef/ipc"
    "github.com/energye/energy/v2/cef/ipc/context"
    "github.com/energye/energy/v2/common"
    "github.com/energye/energy/v2/pkgs/assetserve"
    "github.com/energye/golcl/lcl/rtl/version"
)

//go:embed resources
var resources embed.FS

func main() {
    cef.GlobalInit(nil, &resources)
    cefApp := cef.NewApplication()
    cef.BrowserWindow.Config.Url = "http://localhost:22022/index.html"
    if common.IsLinux() {
        cef.BrowserWindow.Config.IconFS = "resources/icon.png"
    } else {
        cef.BrowserWindow.Config.IconFS = "resources/icon.ico"
    }
    if !common.IsDarwin() { 
        cef.BrowserWindow.Config.EnableHideCaption = true
    }
    cef.BrowserWindow.Config.Title = "Energy Vue + ElementUI Demo"
    cef.BrowserWindow.Config.Width = 1200
    chromiumConfig := cef.BrowserWindow.Config.ChromiumConfig()
    chromiumConfig.SetEnableMenu(false)

    ipc.On("window-state", func(context context.IContext) {
        bw := cef.BrowserWindow.GetWindowInfo(context.BrowserId())
        state := context.ArgumentList().GetIntByIndex(0)
        if state == 0 {
            bw.Minimize()
        } else if state == 1 {
            bw.Maximize()
        }
    })
    ipc.On("window-close", func(context context.IContext) {
        bw := cef.BrowserWindow.GetWindowInfo(context.BrowserId())
        bw.CloseBrowserWindow()
    })
    ipc.On("os-info", func(context context.IContext) {version.OSVersion.ToString())
        context.Result(version.OSVersion.ToString())
    })

    cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
        //
    })
    cef.SetBrowserProcessStartAfterCallback(func(b bool) {
        server := assetserve.NewAssetsHttpServer()
        server.PORT = 22022               
        server.AssetsFSName = "resources" 
        server.Assets = &resources
        go server.StartHttpServer()
    })
    cef.Run(cefApp)
}

Enter fullscreen mode Exit fullscreen mode

demo preview

Top comments (0)