gowebview

package module
v1.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 15, 2022 License: MIT Imports: 5 Imported by: 0

README

gowebview

修改createWindow , 增加handle windows.Handle参数,可以将webview直接放到指定window上

GoDoc Go Report Card

A small WebView without CGO, based on webview/webview. The main goal was to avoid CGO and make it possible to embed the DLLs. Instead of relying directly on CGO as webview/webview, the inkeliz/gowebview uses golang.org/x/sys/windows, for Windows.

Why use inkeliz/gowebview?

  • If you like to avoid CGO on Windows.
  • If you like to have a single .exe.

Why use webview/webview?

  • If you need support for Darwin/Linux.
  • If you need binds from Javascript to Golang.
  • If you like to use a more battle-tested and stable library.
Getting started

Import the package and start using it:

package main

import "github.com/inkeliz/gowebview"

func main() {
	w, err := gowebview.New(&gowebview.Config{URL: "https://google.com", WindowConfig: &gowebview.WindowConfig{Title: "Hello World"}})
	if err != nil {
		panic(err)
	}

	defer w.Destroy()
	w.Run()
}

It will open the https://google.com webpage, without any additional setup.

TODO

  1. Add support to programmatically allow "locahost connections".

    Currently you must run CheckNetIsolation.exe LoopbackExempt -a -n="Microsoft.Win32WebViewHost_cw5n1h2txyewy" externally.

    DONE. It's now implemented for Windows, if you use Config.TransportConfig{IgnoreNetworkIsolation: true}. You don't need to execute the sofware as admin, it'll be request only when needed.

  2. Improve support for Android (currently it needs Gio)

    Currently it's supported, but needs Gio to works.

  3. Remove dependency of webview.dll by calling WebView2Loader.dll directly.

    Currently, it needs to extract both .dll.

  4. Improve security by restricting where look for DLL.

    Currently gowebview adds a new filepath on %PATH%)

  5. Improve error returns.

    Currently it returns errors directly from os, windows and ioutil libraries.

License

Code is distributed under MIT license, feel free to use it in your proprietary projects as well.

Credits

It's highly based on webview/webview and webview/webview_csharp. The idea of avoid CGO was inspired by Ebiten.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {

	// WindowConfig keeps configurations about the window
	WindowConfig *WindowConfig

	// TransportConfig keeps configurations about the network traffic
	TransportConfig *TransportConfig

	// URL defines the default page.
	URL string

	// Debug if is non-zero the Developer Tools will be enabled (if supported).
	Debug bool
}

Config are used to set the initial and default values to the WebView.

type HTTPProxy

type HTTPProxy struct {
	IP   string
	Port string
}

HTTPProxy are used to configure the Proxy

func (*HTTPProxy) Network

func (p *HTTPProxy) Network() string

Network implements net.Addr

func (*HTTPProxy) String

func (p *HTTPProxy) String() string

String implements net.Addr

type Hint

type Hint int

Hint are used to configure window sizing and resizing

const (
	// HintNone set the current and default width and height
	HintNone Hint = iota

	// HintFixed prevents the window size to be changed by a user
	HintFixed

	// HintMin set the minimum bounds
	HintMin

	// HintMax set the maximum bounds
	HintMax
)

type Point

type Point struct {
	X, Y int64
}

Point are used to configure the size or coordinates.

type TransportConfig

type TransportConfig struct {

	// Proxy defines the proxy which the connection will be pass through.
	Proxy *HTTPProxy

	// InsecureBypassCustomProxy if true it might ignore the Proxy settings provided, if fails it to be used. Otherwise
	// it could return ErrFeatureNotSupported or ErrImpossibleProxy when create the WebView.
	//
	// It doesn't have any effect if Proxy is undefined (nil).
	// WARNING: It's might be danger if you define an custom Proxy, since it expose the connection without any proxy.
	InsecureIgnoreCustomProxy bool

	// Authorities defines custom authorities that your app trusts. It could be combined with standard authorities from
	// the machine, it might adds the certificate persistent on the user machine and might requires user interaction
	// to approve such action.
	CertificateAuthorities []x509.Certificate

	// IgnoreNetworkIsolation if true will load pages from the local-network (such as 127.0.0.1).
	IgnoreNetworkIsolation bool
}

WindowConfig describes topics related to the network traffic.

type Visibility

type Visibility int

Visibility are used to configure if the window mode (maximized or minimized)

const (
	// VisibilityDefault will open the window at their default WindowMode
	VisibilityDefault Visibility = iota

	// VisibilityMaximized will open the window as maximized
	// Windowless systems (like Android) will open as fullscreen
	VisibilityMaximized

	// VisibilityMinimized will open the window as minimized
	// Windowless systems (like Android) will hides the webview (return the previous view)
	VisibilityMinimized
)

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()

	// 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() uintptr

	// 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(point *Point, hint Hint)

	// Navigate navigates webview to the given URL. URL may be a data URI, i.e.
	// "data:text/text,<html>...</html>".
	SetURL(url string)

	// SetVisibility updates the WindowMode, such as minimized or maximized
	SetVisibility(v Visibility)
}

WebView is the interface implemented by each webview.

func New

func New(config *Config) (WebView, error)

New calls NewWindow to create a new window and a new webview instance. If debug is non-zero - developer tools will be enabled (if the platform supports them).

func New2 added in v1.0.3

func New2(config *Config, handle windows.Handle) (WebView, error)

type WindowConfig

type WindowConfig struct {

	// Title defines the title of the window.
	Title string

	// Size defines the Width x Height of the window.
	Size *Point

	// Path defines the path where the DLL will be exported.
	Path string

	// Visibility defines how the page must open.
	Visibility Visibility

	// Window defines the window handle (GtkWindow, NSWindow, HWND pointer or View pointer for Android).
	// For Gio (Android):  it MUST point to `e.View` from `app.ViewEvent`
	Window uintptr

	// VM defines the JNI VM for Android
	// For Gio (Android):  it MUST point to `app.JavaVM()`
	VM uintptr
}

WindowConfig describes topics related to the Window/View.

Directories

Path Synopsis
internal

Jump to

Keyboard shortcuts

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