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

Sentry image

Hands-on debugging session: instrument, monitor, and fix

Join Lazar for a hands-on session where you’ll build it, break it, debug it, and fix it. You’ll set up Sentry, track errors, use Session Replay and Tracing, and leverage some good ol’ AI to find and fix issues fast.

RSVP here →

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay