Skip to content

Commit

Permalink
U: touchbar demo
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangli10 committed Dec 6, 2023
1 parent 3eca0fa commit 6af7f2b
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 8 deletions.
16 changes: 16 additions & 0 deletions example/macos/touchbar/assets/touchbar.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>touchbar</title>
<script type="application/javascript">
ipc.on("touchbar", function (state, message) {
let msg = document.getElementById("message");
msg.innerHTML = state + " : " + message;
});
</script>
</head>
<body style="overflow: hidden;margin: 0px;padding: 0px;background-color: #110022;width: 100vw; height: 100vh;text-align: center; display: flex; justify-content: center; align-items: center;">
<div id="message" style="width: 100%;color: white;"> - </div>
</body>
</html>
35 changes: 27 additions & 8 deletions example/macos/touchbar/touchbar.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,37 @@
package main

import (
"embed"
"fmt"
"github.com/energye/energy/v2/cef"
"github.com/energye/energy/v2/cef/ipc"
"github.com/energye/energy/v2/common"
"github.com/energye/energy/v2/example/macos/touchbar/bar"
"github.com/energye/energy/v2/pkgs/touchbar"
"github.com/energye/energy/v2/pkgs/touchbar/barbuilder"
"github.com/energye/energy/v2/pkgs/touchbar/barutils"
"os"
)

//go:embed assets
var assets embed.FS

func main() {
cef.GlobalInit(nil, nil)
cef.GlobalInit(nil, &assets)

//create application
app := cef.NewApplication()
app.SetUseMockKeyChain(true)
cef.BrowserWindow.Config.Url = "https://www.baidu.com"
if common.IsDarwin() {
app.SetUseMockKeyChain(true)
}
cef.BrowserWindow.Config.LocalResource(cef.LocalLoadConfig{
ResRootDir: "assets",
FS: &assets,
Home: "touchbar.html",
}.Build())
cef.BrowserWindow.Config.Width = 400
cef.BrowserWindow.Config.Height = 600

var tb barbuilder.TouchBar
var freeTb = func() {
if tb != nil {
Expand All @@ -29,8 +44,6 @@ func main() {
}
}
cef.BrowserWindow.SetBrowserInit(func(event *cef.BrowserEvent, window cef.IBrowserWindow) {
//bw := window.AsLCLBrowserWindow().BrowserWindow()

tb = touchbar.New(barbuilder.Options{
EventErrorLogger: func(err error) {
fmt.Println("EventErrorLogger", err)
Expand Down Expand Up @@ -58,6 +71,7 @@ func main() {
}
window.Maximize()
isMax = !isMax
ipc.Emit("touchbar", 1, "touch bar max button")
switcher.Update()
},
}
Expand All @@ -72,6 +86,7 @@ func main() {
minBtn.Title = "还原"
}
isMin = !isMin
ipc.Emit("touchbar", 2, "touch bar min button")
switcher.Update()
},
}
Expand All @@ -81,11 +96,16 @@ func main() {
if isFull {
window.ExitFullScreen()
fullScreenBtn.Title = "全屏"
minBtn.Disabled = false
maxBtn.Disabled = false
} else {
window.FullScreen()
fullScreenBtn.Title = "退出全屏"
minBtn.Disabled = true
maxBtn.Disabled = true
}
isFull = !isFull
ipc.Emit("touchbar", 3, "touch bar full screen button")
switcher.Update()
},
}
Expand All @@ -107,21 +127,20 @@ func main() {
Title: "关闭",
OnClick: func() {
window.CloseBrowserWindow()
freeTb()
freeTb() // free
os.Exit(0) // 在这里关闭时失败, 所以这样退出
},
},
}
})

err := tb.Install(config)
fmt.Println("install err", err)
if err != nil {
panic(err)
}

})
//run application
cef.Run(app)
// free
freeTb()
}

0 comments on commit 6af7f2b

Please sign in to comment.