Golang中如何使用按钮执行Toast操作
Golang中如何使用按钮执行Toast操作 以下代码来自 https://github.com/go-toast/toast 这个GO包,它会打开地图。我该如何使用那个操作按钮来打开终端并执行一个命令呢?
package main
import (
"gopkg.in/toast.v1"
"log"
)
func main() {
mivo()
}
func mivo() {
notification := toast.Notification{
AppID: "Mivo",
Title: "Break, boss",
Message: "30 minutes passed! You are to go for a walk now.",
Actions: []toast.Action{
{"protocol", "Alright", "bingmaps:?=sushi"},
{"protocol", "GG", ""},
},
}
err := notification.Push()
if err != nil {
log.Fatalln(err)
}
}
更多关于Golang中如何使用按钮执行Toast操作的实战教程也可以访问 https://www.itying.com/category-94-b0.html
1 回复
更多关于Golang中如何使用按钮执行Toast操作的实战系列教程也可以访问 https://www.itying.com/category-94-b0.html
在Golang中,使用go-toast/toast包的按钮执行命令操作,可以通过protocol动作调用自定义URI协议处理器来实现。以下是修改后的代码示例:
package main
import (
"gopkg.in/toast.v1"
"log"
"os/exec"
"runtime"
)
func main() {
mivo()
}
func mivo() {
// 注册自定义协议处理器(实际应用中需要在系统层面配置)
// 这里演示通过按钮触发命令执行
notification := toast.Notification{
AppID: "Mivo",
Title: "Break, boss",
Message: "30 minutes passed! You are to go for a walk now.",
Actions: []toast.Action{
{"protocol", "Open Terminal", "cmd://open-terminal"},
{"protocol", "Run Command", "cmd://echo Hello"},
},
}
// 监听协议调用(实际需要系统级协议处理器)
// 这里使用模拟处理方式
handleProtocolActions(notification.Actions)
err := notification.Push()
if err != nil {
log.Fatalln(err)
}
}
// 模拟协议处理器函数
func handleProtocolActions(actions []toast.Action) {
for _, action := range actions {
if action.Arguments != "" {
switch action.Arguments {
case "cmd://open-terminal":
openTerminal()
case "cmd://echo Hello":
runCommand("echo", "Hello from Toast!")
}
}
}
}
// 打开终端函数
func openTerminal() {
var cmd *exec.Cmd
switch runtime.GOOS {
case "windows":
cmd = exec.Command("cmd.exe")
case "darwin":
cmd = exec.Command("open", "-a", "Terminal")
case "linux":
cmd = exec.Command("gnome-terminal", "--", "bash")
default:
log.Println("Unsupported platform")
return
}
err := cmd.Start()
if err != nil {
log.Printf("Failed to open terminal: %v", err)
}
}
// 执行命令函数
func runCommand(name string, arg ...string) {
cmd := exec.Command(name, arg...)
output, err := cmd.CombinedOutput()
if err != nil {
log.Printf("Command failed: %v", err)
return
}
log.Printf("Command output: %s", output)
}
对于实际的系统集成,需要在Windows系统中注册自定义协议处理器。以下是Windows注册表示例:
// windows_registry.go
// 需要在Windows系统中以管理员权限运行此代码注册协议处理器
package main
import (
"golang.org/x/sys/windows/registry"
"log"
"path/filepath"
)
func registerProtocolHandler() {
exePath, _ := filepath.Abs("./your-app.exe")
// 注册 cmd:// 协议
key, _, err := registry.CreateKey(registry.CLASSES_ROOT, "cmd", registry.ALL_ACCESS)
if err != nil {
log.Fatal(err)
}
defer key.Close()
key.SetStringValue("", "URL:Command Protocol")
key.SetStringValue("URL Protocol", "")
// 创建shell/open/command子键
commandKey, _, err := registry.CreateKey(key, `shell\open\command`, registry.ALL_ACCESS)
if err != nil {
log.Fatal(err)
}
defer commandKey.Close()
commandKey.SetStringValue("", `"`+exePath+`" "%1"`)
}
实际应用中的主程序需要解析传入的URI参数:
package main
import (
"fmt"
"gopkg.in/toast.v1"
"log"
"os"
"os/exec"
"strings"
)
func main() {
// 检查是否通过协议调用
if len(os.Args) > 1 && strings.HasPrefix(os.Args[1], "cmd://") {
handleProtocolCall(os.Args[1])
return
}
// 正常显示Toast
showToast()
}
func handleProtocolCall(uri string) {
switch uri {
case "cmd://open-terminal":
exec.Command("cmd.exe").Start()
case "cmd://run-ping":
cmd := exec.Command("ping", "google.com")
output, _ := cmd.CombinedOutput()
fmt.Printf("Ping output:\n%s\n", output)
}
}
func showToast() {
notification := toast.Notification{
AppID: "CommandApp",
Title: "Execute Commands",
Message: "Click buttons to execute commands",
Actions: []toast.Action{
{"protocol", "Open CMD", "cmd://open-terminal"},
{"protocol", "Ping Test", "cmd://run-ping"},
},
}
err := notification.Push()
if err != nil {
log.Fatal(err)
}
}
注意:实际部署时需要将程序编译为exe,并在系统中注册协议处理器,这样当用户点击Toast按钮时,系统会通过注册的协议调用你的程序并传递相应的URI参数。

