linux.go 1.9 KB
Newer Older
yanghye's avatar
yanghye 已提交
1 2 3
package main

import (
4
	"embed"
5
	"fmt"
yanghye's avatar
yanghye 已提交
6
	"github.com/energye/energy/cef"
7
	"github.com/energye/energy/common/assetserve"
8 9
	"github.com/energye/energy/consts"
	"github.com/energye/golcl/lcl"
yanghye's avatar
yanghye 已提交
10 11
)

12 13 14
//go:embed resources
var resources embed.FS

yanghye's avatar
yanghye 已提交
15 16
func main() {
	//全局初始化 每个应用都必须调用的
17
	cef.GlobalCEFInit(nil, &resources)
yanghye's avatar
yanghye 已提交
18
	//创建应用
19 20 21 22
	config := cef.NewApplicationConfig()
	config.SetMultiThreadedMessageLoop(false)
	config.SetExternalMessagePump(false)
	cefApp := cef.NewApplication(config)
yanghye's avatar
yanghye 已提交
23
	//指定一个URL地址,或本地html文件目录
24
	cef.BrowserWindow.Config.Url = "http://localhost:22022/index.html"
25
	cef.BrowserWindow.Config.IconFS = "resources/icon.png"
26 27 28
	cef.BrowserWindow.SetViewFrameBrowserInit(func(event *cef.BrowserEvent, window *cef.ViewsFrameworkBrowserWindow) {
		fmt.Println("cef.BrowserWindow.SetViewFrameBrowserInit", window)
		fmt.Printf("%+v\n", window)
29 30 31 32 33 34
		event.SetOnBeforeContextMenu(func(sender lcl.IObject, browser *cef.ICefBrowser, frame *cef.ICefFrame, params *cef.ICefContextMenuParams, model *cef.ICefMenuModel) {
			model.AddCheckItem(model.CefMis.NextCommandId(), "测试")
		})
		event.SetOnContextMenuCommand(func(sender lcl.IObject, browser *cef.ICefBrowser, frame *cef.ICefFrame, params *cef.ICefContextMenuParams, commandId consts.MenuId, eventFlags uint32, result *bool) {
			fmt.Println("SetOnContextMenuCommand", commandId)
		})
35
	})
36 37 38 39 40 41 42 43 44 45 46 47
	//在主进程启动成功之后执行
	//在这里启动内置http服务
	//内置http服务需要使用 go:embed resources 内置资源到执行程序中
	cef.SetBrowserProcessStartAfterCallback(func(b bool) {
		fmt.Println("主进程启动 创建一个内置http服务")
		//通过内置http服务加载资源
		server := assetserve.NewAssetsHttpServer()
		server.PORT = 22022               //服务端口号
		server.AssetsFSName = "resources" //必须设置目录名和资源文件夹同名
		server.Assets = &resources
		go server.StartHttpServer()
	})
yanghye's avatar
yanghye 已提交
48 49 50
	//运行应用
	cef.Run(cefApp)
}