Skip to content

Instantly share code, notes, and snippets.

@asticode
Created March 21, 2020 10:17
Show Gist options
  • Save asticode/6f9ebf8169ef773551321c158dfdb26f to your computer and use it in GitHub Desktop.
Save asticode/6f9ebf8169ef773551321c158dfdb26f to your computer and use it in GitHub Desktop.
astilectron demo for development purposes
package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"os"
"time"
"github.com/asticode/go-astikit"
"github.com/asticode/go-astilectron"
bootstrap "github.com/asticode/go-astilectron-bootstrap"
)
// Constants
const htmlAbout = `Welcome on <b>Astilectron</b> demo!<br>
This is using the bootstrap and the bundler.`
// Vars injected via ldflags by bundler
var (
AppName string
BuiltAt string
VersionAstilectron string
VersionElectron string
)
// Application Vars
var (
debug = flag.Bool("d", true, "enables the debug mode")
w *astilectron.Window
)
func main() {
// Parse flags
flag.Parse()
// Create logger
l := log.New(log.Writer(), log.Prefix(), log.Flags())
wd, _ := os.Getwd()
// Run bootstrap
l.Printf("Running app built at %s\n", BuiltAt)
if err := bootstrap.Run(bootstrap.Options{
//Asset: Asset,
//AssetDir: AssetDir,
AstilectronOptions: astilectron.Options{
AppName: AppName,
AppIconDarwinPath: "resources/icon.icns",
AppIconDefaultPath: "resources/icon.png",
BaseDirectoryPath: wd,
SingleInstance: true,
VersionAstilectron: VersionAstilectron,
VersionElectron: VersionElectron,
},
Debug: *debug,
Logger: l,
MenuOptions: []*astilectron.MenuItemOptions{{
Label: astikit.StrPtr("File"),
SubMenu: []*astilectron.MenuItemOptions{
{
Label: astikit.StrPtr("About"),
OnClick: func(e astilectron.Event) (deleteListener bool) {
if err := bootstrap.SendMessage(w, "about", htmlAbout, func(m *bootstrap.MessageIn) {
// Unmarshal payload
var s string
if err := json.Unmarshal(m.Payload, &s); err != nil {
l.Println(fmt.Errorf("unmarshaling payload failed: %w", err))
return
}
l.Printf("About modal has been displayed and payload is %s!\n", s)
}); err != nil {
l.Println(fmt.Errorf("sending about event failed: %w", err))
}
return
},
},
{Role: astilectron.MenuItemRoleClose},
},
}},
OnWait: func(_ *astilectron.Astilectron, ws []*astilectron.Window, _ *astilectron.Menu, _ *astilectron.Tray, _ *astilectron.Menu) error {
w = ws[0]
go func() {
time.Sleep(5 * time.Second)
if err := bootstrap.SendMessage(w, "check.out.menu", "Don't forget to check out the menu!"); err != nil {
l.Println(fmt.Errorf("sending check.out.menu event failed: %w", err))
}
}()
return nil
},
//RestoreAssets: RestoreAssets,
Windows: []*bootstrap.Window{{
Homepage: "index.html",
MessageHandler: handleMessages,
Options: &astilectron.WindowOptions{
BackgroundColor: astikit.StrPtr("#333"),
Center: astikit.BoolPtr(true),
Height: astikit.IntPtr(700),
Width: astikit.IntPtr(700),
},
}},
}); err != nil {
l.Fatal(fmt.Errorf("running bootstrap failed: %w", err))
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment