webview2

package module
v1.0.0 Latest Latest
Warning

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

Go to latest
Published: Jun 12, 2023 License: MIT Imports: 2 Imported by: 0

README

Go Go Report Card Go Reference

新增和修改:

  • 增加单实例锁,二开时将已开窗口置顶、居中
  • 增加自动检测系统webview2版本,没有安装时根据提示自动安装(win7+测试正常)
  • 增加弹窗MessageBox

go-webview2

This package provides an interface for using the Microsoft Edge WebView2 component with Go. It is based on webview/webview and provides a compatible API.

Please note that this package only supports Windows, since it provides functionality specific to WebView2. If you wish to use this library for Windows, but use webview/webview for all other operating systems, you could use the go-webview-selector package instead. However, you will not be able to use WebView2-specific functionality.

If you wish to build desktop applications in Go using web technologies, please consider Wails. It uses go-webview2 internally on Windows.

Demo

If you are using Windows 10+, the WebView2 runtime should already be installed. If you don't have it installed, you can download and install a copy from Microsoft's website:

WebView2 runtime

After that, you should be able to run go-webview2 directly:

go run ./cmd/demo

This will use go-winloader to load an embedded copy of WebView2Loader.dll. If you want, you can also provide a newer version of WebView2Loader.dll in the DLL search path and it should be picked up instead. It can be acquired from the WebView2 SDK (which is permissively licensed.)

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Hint

type Hint int

Hint is used to configure window sizing and resizing behavior.

const (
	// HintNone specifies that width and height are default size
	HintNone Hint = iota

	// HintFixed specifies that window size can not be changed by a user
	HintFixed

	// HintMin specifies that width and height are minimum bounds
	HintMin

	// HintMax specifies that width and height are maximum bounds
	HintMax
)

type WebView

type WebView interface {

	// Run runs the main loop until it's terminated. After this function exits -
	// you must destroy the webview.
	Run()

	// Terminate stops the main loop. It is safe to call this function from
	// a background thread.
	Terminate()

	// Dispatch posts a function to be executed on the main thread. You normally
	// do not need to call this function, unless you want to tweak the native
	// window.
	Dispatch(f func())

	// Destroy destroys a webview and closes the native window.
	Destroy()

	// Window returns a native window handle pointer. When using GTK backend the
	// pointer is GtkWindow pointer, when using Cocoa backend the pointer is
	// NSWindow pointer, when using Win32 backend the pointer is HWND pointer.
	Window() unsafe.Pointer

	// SetTitle updates the title of the native window. Must be called from the UI
	// thread.
	SetTitle(title string)

	// SetSize updates native window size. See Hint constants.
	SetSize(w int, h int, hint Hint)

	// Navigate navigates webview to the given URL. URL may be a data URI, i.e.
	// "data:text/text,<html>...</html>". It is often ok not to url-encode it
	// properly, webview will re-encode it for you.
	Navigate(url string)

	// SetHtml sets the webview HTML directly.
	// The origin of the page is `about:blank`.
	SetHtml(html string)

	// Init injects JavaScript code at the initialization of the new page. Every
	// time the webview will open a the new page - this initialization code will
	// be executed. It is guaranteed that code is executed before window.onload.
	Init(js string)

	// Eval evaluates arbitrary JavaScript code. Evaluation happens asynchronously,
	// also the result of the expression is ignored. Use RPC bindings if you want
	// to receive notifications about the results of the evaluation.
	Eval(js string)

	// Bind binds a callback function so that it will appear under the given name
	// as a global JavaScript function. Internally it uses webview_init().
	// Callback receives a request string and a user-provided argument pointer.
	// Request string is a JSON array of all the arguments passed to the
	// JavaScript function.
	//
	// f must be a function
	// f must return either value and error or just error
	Bind(name string, f interface{}) error

	// MessageBox windows消息弹窗
	MessageBox(caption, text string)

	// ToTop 显示到最上层(非强制)
	ToTop()

	// MostTop 移动到最上层(参数为true时,强制到最上层,否则显示在其他最上层窗口后)
	MostTop(isTop bool)

	// RestoreWindow 还原窗口(一般为最小化后执行此方法还原窗口)
	RestoreWindow()

	// MoveToCenter 窗口屏幕居中
	MoveToCenter()

	// GetHWnd HWND的uintptr转为win.HWND
	GetHWnd() win.HWND

	// Webview2AutoInstall 根据需要自动下载安装webview2依赖
	Webview2AutoInstall() error

	// RunCall 程序退出前执行自定义回调
	Start(callback func())

	GetBrowser() browser
}

WebView is the interface for the webview.

Directories

Path Synopsis
cmd
internal
w32
pkg

Jump to

Keyboard shortcuts

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