refactor: 桌面应用对话框代码拆分为平台专用文件
- 新增 dialog_windows.go、dialog_darwin.go、dialog_linux.go - 使用 Go 构建标签实现条件编译 - 修复跨平台编译错误(syscall.NewLazyDLL 在 macOS/Linux 未定义) - 实现 Linux 多工具降级策略(zenity → kdialog → notify-send → xmessage → stderr) - 实现 macOS AppleScript 字符转义 - 更新 messagebox_test.go 构建标签 - 更新 desktop-app spec 新增 Linux 降级策略和 macOS 字符转义规范
This commit is contained in:
88
backend/cmd/desktop/dialog_linux.go
Normal file
88
backend/cmd/desktop/dialog_linux.go
Normal file
@@ -0,0 +1,88 @@
|
||||
//go:build linux
|
||||
|
||||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"os/exec"
|
||||
"sync"
|
||||
)
|
||||
|
||||
type dialogToolType int
|
||||
|
||||
const (
|
||||
toolNone dialogToolType = iota
|
||||
toolZenity
|
||||
toolKdialog
|
||||
toolNotifySend
|
||||
toolXmessage
|
||||
)
|
||||
|
||||
var (
|
||||
dialogTool dialogToolType
|
||||
dialogToolOnce sync.Once
|
||||
)
|
||||
|
||||
func init() {
|
||||
dialogToolOnce.Do(detectDialogTool)
|
||||
}
|
||||
|
||||
func detectDialogTool() {
|
||||
tools := []struct {
|
||||
name string
|
||||
typ dialogToolType
|
||||
}{
|
||||
{"zenity", toolZenity},
|
||||
{"kdialog", toolKdialog},
|
||||
{"notify-send", toolNotifySend},
|
||||
{"xmessage", toolXmessage},
|
||||
}
|
||||
|
||||
for _, tool := range tools {
|
||||
if _, err := exec.LookPath(tool.name); err == nil {
|
||||
dialogTool = tool.typ
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
dialogTool = toolNone
|
||||
}
|
||||
|
||||
func showError(title, message string) {
|
||||
switch dialogTool {
|
||||
case toolZenity:
|
||||
exec.Command("zenity", "--error",
|
||||
fmt.Sprintf("--title=%s", title),
|
||||
fmt.Sprintf("--text=%s", message)).Run()
|
||||
case toolKdialog:
|
||||
exec.Command("kdialog", "--error", message, "--title", title).Run()
|
||||
case toolNotifySend:
|
||||
exec.Command("notify-send", "-u", "critical", title, message).Run()
|
||||
case toolXmessage:
|
||||
exec.Command("xmessage", "-center",
|
||||
fmt.Sprintf("%s: %s", title, message)).Run()
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "错误: %s: %s\n", title, message)
|
||||
}
|
||||
}
|
||||
|
||||
func showAbout() {
|
||||
message := "Nex Gateway\n\nAI Gateway - 统一的大模型 API 网关\n\nhttps://github.com/nex/gateway"
|
||||
|
||||
switch dialogTool {
|
||||
case toolZenity:
|
||||
exec.Command("zenity", "--info",
|
||||
"--title=关于 Nex Gateway",
|
||||
fmt.Sprintf("--text=%s", message)).Run()
|
||||
case toolKdialog:
|
||||
exec.Command("kdialog", "--msgbox", message, "--title", "关于 Nex Gateway").Run()
|
||||
case toolNotifySend:
|
||||
exec.Command("notify-send", "关于 Nex Gateway", message).Run()
|
||||
case toolXmessage:
|
||||
exec.Command("xmessage", "-center",
|
||||
fmt.Sprintf("关于 Nex Gateway: %s", message)).Run()
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "关于 Nex Gateway: %s\n", message)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user