desktop

package module
v0.0.8 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 27, 2023 License: MIT Imports: 9 Imported by: 0

README

桌面开发

一套可定制化的桌面开发工具,集成了 webview 窗口、系统托盘的机制

仅支持 windows 系统

特性

  • 基于 webview 的桌面开发工具,使用 webview2 驱动,纯 Go 语言实现,无 CGO 依赖
  • 启动窗口时自动检测 webview2 环境,如果未安装,则自动运行安装 webview2 引导
  • 支持 webview 常规操作,如跳转,注入 js,js 与 go 交互等操作
  • 支持多个窗口管理,支持无边框窗口
  • 支持 css 设置 -webkit-app-region: drag 后拖拽窗口
  • 系统托盘支持,托盘支持菜单,支持无限级子菜单
  • TODO: 自更新机制

DEMO

https://github.com/eyasliu/desktop/assets/4774683/d07c2d09-fe41-4ea1-ad5f-b095e9915e19

可使用以下命令快速启动示例程序

go run github.com/eyasliu/desktop/example/basic

demo 源码

func main() {
  var app desktop.WebView
  app = desktop.New(&desktop.Options{
    Debug:             true,
    AutoFocus:         true,
    Width:             1280,
    Height:            768,
    HideWindowOnClose: true,
    StartURL:          "https://www.wps.cn",
    Tray: &tray.Tray{
      Title:   "托盘演示",
      Tooltip: "提示文字,点击激活显示窗口",
      OnClick: func() {
        app.Show() // 显示窗口
      },
      Items: []*tray.TrayItem{
        {
          Title:   "跳转到 wps 365 官网",
          OnClick: func() { app.Navigate("https://365.wps.cn") },
        },
        {
          Title:   "打开本地页面",
          OnClick: func() { app.SetHtml("<h1>这是个本地页面</h1>") },
        },
        {
          Title:   "执行js alert('hello')",
          OnClick: func() { app.Eval("alert('hello')") },
        },

        {
          Title: "窗口操作",
          Items: []*tray.TrayItem{
            {
              Title: "新窗口打开 WPS AI",
              OnClick: func() {
                go func() {
                  wpsai := desktop.New(&desktop.Options{
                    StartURL: "https://ai.wps.cn",
                    Frameless:         true, // 去掉边框
                  })
                  wpsai.Run()
                }()
              },
            },
            {
              Title: "显示窗口",
              OnClick: func() {
                app.Show()
              },
            },
            {
              Title: "隐藏窗口",
              OnClick: func() {
                app.Hide()
              },
            },
            {
              Title: "设置窗口标题",
              OnClick: func() {
                app.SetTitle("这是新的标题")
              },
            },
          },
        },
        {
          Title: "退出程序",
          OnClick: func() {
            app.Destroy()
          },
        },
      },
    },
  })

  app.Run()
}

API

点击查看文档

注意事项

windows 的桌面开发对于操作线程

  • desktop.New 和 app.Run 必须要在同一个 goroutine 协程执行
  • 一个 goroutine 只能 Run 一个 desktop.WebView

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func IsHeadless

func IsHeadless() bool

func IsSupportTray

func IsSupportTray() bool

Types

type Options

type Options struct {
	// 系统托盘图片设置,可使用 IconPath 和 IconBytes 二选其一方式设置
	// 系统托盘图标路径,建议使用绝对路径,如果相对路径取执行文件相对位置
	IconPath string
	// 系统托盘图标文件内容
	IconBytes []byte
	// 是否启用调试模式,启用后webview支持右键菜单,并且支持打开 chrome 开发工具
	Debug bool
	// 启动webview后访问的url
	StartURL string
	// 当webview访问错误时显示的错误页面
	FallbackPage string
	// webview2 底层使用的用户数据目录
	DataPath string
	// webview 窗口默认显示的标题文字
	Title string
	// webview窗口宽度
	Width int
	// webview 窗口高度
	Height int
	// 刚启动webview时是否隐藏状态,可通过 Show() 方法显示
	StartHidden bool
	// 当用户关闭了webview窗口时是否视为隐藏窗口,如果为false,用户关闭窗口后会触发销毁应用
	HideWindowOnClose bool
	// webview 窗口是否总是在最顶层
	AlwaysOnTop bool
	// 系统托盘设置
	Tray *tray.Tray
	// 打印日志的实例
	Logger logger
	// 是否去掉webview窗口的边框,注意无边框会把右上角最大化最小化等按钮去掉
	Frameless bool
	// 打开窗口时是否自动在屏幕中间
	Center bool
	// 打开窗口时是否自动聚焦
	AutoFocus bool
}

Options 打开的窗口和系统托盘配置

func (*Options) GetIcon

func (o *Options) GetIcon() string

GetIcon 获取托盘图标路径

type WebView

type WebView interface {

	// Run 开始运行webview,调用后 webview 才会显示
	//
	//  注: 必须要和 New 在同一个 goroutine 执行
	Run()

	// Destroy 销毁当前的窗口
	Destroy()

	// Terminate 销毁当前的窗口
	Terminate()

	// SetTitle 设置窗口的标题文字
	SetTitle(title string)

	// Navigate webview窗口跳转到指定url
	Navigate(url string)

	// SetHtml webview窗口显示为指定的 html 内容
	SetHtml(html string)

	// Init 在页面初始化的时候注入的js代码,页面无论是跳转还是刷新后都会重新执行 Init 注入的代码
	// 触发的时机会在 window.onload 之前
	Init(js string)

	// Eval 在webview页面执行js代码
	Eval(js string)

	// Bind 注入JS函数,底层通过 Init 实现,用于往页面注入函数,实现 JS 和 Go 互相调用
	// name 是函数名,
	// fn 必须是 go 函数,否则无效,
	// 注入的函数调用后返回 Promise,在Promise resolve 获取go函数返回值,
	// 注入的函数参数个数必须和js调用时传入的参数类型和个数一致,否则 reject
	Bind(name string, f interface{})

	// Hide 隐藏窗口
	Hide()
	// Show 显示窗口
	Show()
}

WebView 定义webview 功能操作,暴露给外部调用

Directories

Path Synopsis
example
systray
From github.com/getlantern/systray Package systray is a cross-platform Go library to place an icon and menu in the notification area.
From github.com/getlantern/systray Package systray is a cross-platform Go library to place an icon and menu in the notification area.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL