webkitgtk

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jan 27, 2024 License: MIT Imports: 30 Imported by: 0

README

webkitgtk Go Reference Release Go Report Card License

Pure Go WebKitGTK binding for Linux and FreeBSD.

This is pre release software so expect bugs and potentially API breaking changes but each release will be tagged to avoid breaking people's code.

Installation

# go 1.21.5+
go get github.com/malivvan/webkitgtk@latest

Example

The following example shows how to create a simple GTK window with a button that closes the application when clicked.

package main

import ui "github.com/malivvan/webkitgtk"

type API struct {
	app *ui.App
}

func (a *API) Quit() error {
	a.app.Quit()
	return nil
}

func main() {
	app := ui.New(ui.AppOptions{
		Name: "example",
	})
	app.Open(ui.WindowOptions{
		Title: "example",
		HTML:  `<button onclick="app.quit()">quit</button>`,
		Define: map[string]interface{}{
			"app": &API{app: app},
		},
	})
	if err := app.Run(); err != nil {
		panic(err)
	}
}

Running / Building

Running / building defaults to debug mode outputting logs to stderr. To build in release mode use the release build tag.

go build -tags release -ldflags "-s -w" -trimpath

The resulting release binary will be about ~6MB in size and cam be compressed further with UPX to about ~2.5MB.

Examples

  • echo - call go functions from javascript
  • dialog - application spawning different types of dialog windows
  • handle - handle requests on the app:// uri scheme to serve embedded files
  • notify - application sending different types of notifications
  • systray - example application showing how to use the systray

Dependencies

Either webkit2gtk-4.1 (stable) or webkitgtk-6.0 (unstable) is required at runtime. If both are installed the stable version will be used.

Debian / Ubuntu apt install libwebkit2gtk-4.1 apt install libwebkitgtk-6.0
RHEL / Fedora dnf install webkitgtk4 dnf install webkitgtk3
Arch pacman -S webkit2gtk-4.1 pacman -S webkitgtk-6.0
Alpine apk add webkit2gtk apk add webkit2gtk-6.0
Gentoo emerge -av net-libs/webkit-gtk
FreeBSD pkg install webkit2-gtk3 pkg install webkit2-gtk4

License

This project is licensed under the MIT License.

Documentation

Index

Constants

View Source
const (
	GSourceRemove int = 0

	// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/gdkwindow.h#L121
	GdkHintMinSize = 1 << 1
	GdkHintMaxSize = 1 << 2
	// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gdk/gdkevents.h#L512
	GdkWindowStateIconified  = 1 << 1
	GdkWindowStateMaximized  = 1 << 2
	GdkWindowStateFullscreen = 1 << 4

	// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gtk/gtkmessagedialog.h#L87
	GtkButtonsNone     int = 0
	GtkButtonsOk           = 1
	GtkButtonsClose        = 2
	GtkButtonsCancel       = 3
	GtkButtonsYesNo        = 4
	GtkButtonsOkCancel     = 5

	// https://gitlab.gnome.org/GNOME/gtk/-/blob/gtk-3-24/gtk/gtkdialog.h#L36
	GtkDialogModal             = 1 << 0
	GtkDialogDestroyWithParent = 1 << 1
	GtkDialogUseHeaderBar      = 1 << 2 // actions in header bar instead of action area

	GtkOrientationVertical = 1
)

Variables

View Source
var LogWriter = os.Stderr
View Source
var PanicHandler = func(v any) {
	panic(v)
}

Functions

func ToARGB added in v0.1.0

func ToARGB(img *image.RGBA) (int, int, []byte)

Types

type App

type App struct {
	// contains filtered or unexported fields
}

func New

func New(options AppOptions) *App

func (*App) CurrentWindow

func (a *App) CurrentWindow() *Window

func (*App) Dialog added in v0.1.0

func (a *App) Dialog() *Dialog

func (*App) Handle added in v0.1.0

func (a *App) Handle(host string, handler http.Handler)

func (*App) Menu added in v0.1.0

func (a *App) Menu(icon []byte) *TrayMenu

func (*App) Notify added in v0.1.0

func (a *App) Notify(title, message string) *Notification

func (*App) Open

func (a *App) Open(options WindowOptions) *Window

Open opens a new window with the given options.

func (*App) Quit

func (a *App) Quit()

func (*App) Run

func (a *App) Run() (err error)

type AppOptions

type AppOptions struct {

	// ID is the unique identifier of the app in reverse domain notation. e.g. com.github.malivvan.webkitgtk
	ID string

	// Name is the name of the app.
	Name string

	// Hold the app open after the last window is closed.
	Hold bool

	// The icon of the app.
	Icon []byte

	// Ephemeral mode disables all persistent storage.
	Ephemeral bool

	// DataDir is the directory where persistent data is stored.
	DataDir string

	// CacheDir is the directory where persistent cache is stored.
	CacheDir string

	// CacheModel is the cache model used by the webview.
	CacheModel WebkitCacheModel

	// CookiePolicy is the cookie store used by the webview.
	CookiePolicy WebkitCookiePolicy
}

type Dialog added in v0.1.0

type Dialog struct {
	// contains filtered or unexported fields
}

func (*Dialog) Ask added in v0.1.0

func (d *Dialog) Ask(title string, message string, actions ...string) *MessageDialog

func (*Dialog) Fail added in v0.1.0

func (d *Dialog) Fail(title string, message string, actions ...string) *MessageDialog

func (*Dialog) Info added in v0.1.0

func (d *Dialog) Info(title string, message string, actions ...string) *MessageDialog

func (*Dialog) Open added in v0.1.0

func (d *Dialog) Open(title string) *OpenFileDialog

func (*Dialog) Save added in v0.1.0

func (d *Dialog) Save(title string) *SaveFileDialog

func (*Dialog) Warn added in v0.1.0

func (d *Dialog) Warn(title string, message string, actions ...string) *MessageDialog

type JSObject added in v0.1.0

type JSObject struct {
	// contains filtered or unexported fields
}

func (*JSObject) Get added in v0.1.0

func (jso *JSObject) Get(name string) interface{}
type MenuItem struct {
	// contains filtered or unexported fields
}
func (item *MenuItem) Checked() bool
func (item *MenuItem) Enabled() bool
func (item *MenuItem) Hidden() bool
func (item *MenuItem) IsCheckbox() bool
func (item *MenuItem) IsRadio() bool
func (item *MenuItem) IsSeparator() bool
func (item *MenuItem) IsSubmenu() bool
func (item *MenuItem) Label() string
func (item *MenuItem) OnClick(f func(bool)) *MenuItem
func (item *MenuItem) SetChecked(checked bool) *MenuItem
func (item *MenuItem) SetDisabled(disabled bool) *MenuItem
func (item *MenuItem) SetHidden(hidden bool) *MenuItem
func (item *MenuItem) SetIcon(icon []byte) *MenuItem
func (item *MenuItem) SetLabel(label string) *MenuItem

type MessageDialog added in v0.1.0

type MessageDialog struct {
	// contains filtered or unexported fields
}

func (*MessageDialog) AddButton added in v0.1.0

func (d *MessageDialog) AddButton(action string, label string) *MessageDialog

func (*MessageDialog) AddCancel added in v0.1.0

func (d *MessageDialog) AddCancel(action string, label string) *MessageDialog

func (*MessageDialog) AddDefault added in v0.1.0

func (d *MessageDialog) AddDefault(action string, label string) *MessageDialog

func (*MessageDialog) SetIcon added in v0.1.0

func (d *MessageDialog) SetIcon(icon []byte) *MessageDialog

func (*MessageDialog) Show added in v0.1.0

func (d *MessageDialog) Show(callbacks ...func(int)) chan int

type Notification added in v0.1.0

type Notification struct {
	// contains filtered or unexported fields
}

func (*Notification) Action added in v0.1.0

func (n *Notification) Action(label string, callback func()) *Notification

func (*Notification) Closed added in v0.1.0

func (n *Notification) Closed(callback func()) *Notification

func (*Notification) Icon added in v0.1.0

func (n *Notification) Icon(icon []byte) *Notification

func (*Notification) Show added in v0.1.0

func (n *Notification) Show() (uint32, error)

Show sends the information in the notification object to the server to be displayed.

func (*Notification) Timeout added in v0.1.0

func (n *Notification) Timeout(timeout time.Duration) *Notification

type OpenFileDialog added in v0.1.0

type OpenFileDialog struct {
	// contains filtered or unexported fields
}

func (*OpenFileDialog) AddFilter added in v0.1.0

func (d *OpenFileDialog) AddFilter(displayName, pattern string) *OpenFileDialog

AddFilter adds a filter to the dialog. The filter is a display name and a semicolon separated list of extensions. EG: AddFilter("Image Files", "*.jpg;*.png")

func (*OpenFileDialog) AllowsMultipleSelection added in v0.1.0

func (d *OpenFileDialog) AllowsMultipleSelection(allowsMultipleSelection bool) *OpenFileDialog

func (*OpenFileDialog) AllowsOtherFileTypes added in v0.1.0

func (d *OpenFileDialog) AllowsOtherFileTypes(allowsOtherFileTypes bool) *OpenFileDialog

func (*OpenFileDialog) CanChooseDirectories added in v0.1.0

func (d *OpenFileDialog) CanChooseDirectories(canChooseDirectories bool) *OpenFileDialog

func (*OpenFileDialog) CanChooseFiles added in v0.1.0

func (d *OpenFileDialog) CanChooseFiles(canChooseFiles bool) *OpenFileDialog

func (*OpenFileDialog) CanCreateDirectories added in v0.1.0

func (d *OpenFileDialog) CanCreateDirectories(canCreateDirectories bool) *OpenFileDialog

func (*OpenFileDialog) CanSelectHiddenExtension added in v0.1.0

func (d *OpenFileDialog) CanSelectHiddenExtension(canSelectHiddenExtension bool) *OpenFileDialog

func (*OpenFileDialog) HideExtension added in v0.1.0

func (d *OpenFileDialog) HideExtension(hideExtension bool) *OpenFileDialog

func (*OpenFileDialog) ResolvesAliases added in v0.1.0

func (d *OpenFileDialog) ResolvesAliases(resolvesAliases bool) *OpenFileDialog

func (*OpenFileDialog) SetButtonText added in v0.1.0

func (d *OpenFileDialog) SetButtonText(text string) *OpenFileDialog

func (*OpenFileDialog) SetDirectory added in v0.1.0

func (d *OpenFileDialog) SetDirectory(directory string) *OpenFileDialog

func (*OpenFileDialog) Show added in v0.1.0

func (d *OpenFileDialog) Show(callbacks ...func([]string)) chan []string

func (*OpenFileDialog) ShowHiddenFiles added in v0.1.0

func (d *OpenFileDialog) ShowHiddenFiles(showHiddenFiles bool) *OpenFileDialog

func (*OpenFileDialog) TreatsFilePackagesAsDirectories added in v0.1.0

func (d *OpenFileDialog) TreatsFilePackagesAsDirectories(treatsFilePackagesAsDirectories bool) *OpenFileDialog

type SaveFileDialog added in v0.1.0

type SaveFileDialog struct {
	// contains filtered or unexported fields
}

func (*SaveFileDialog) AddFilter added in v0.1.0

func (d *SaveFileDialog) AddFilter(displayName, pattern string) *SaveFileDialog

AddFilter adds a filter to the dialog. The filter is a display name and a semicolon separated list of extensions. EG: AddFilter("Image Files", "*.jpg;*.png")

func (*SaveFileDialog) AllowsOtherFileTypes added in v0.1.0

func (d *SaveFileDialog) AllowsOtherFileTypes(allowOtherFileTypes bool) *SaveFileDialog

func (*SaveFileDialog) CanCreateDirectories added in v0.1.0

func (d *SaveFileDialog) CanCreateDirectories(canCreateDirectories bool) *SaveFileDialog

func (*SaveFileDialog) CanSelectHiddenExtension added in v0.1.0

func (d *SaveFileDialog) CanSelectHiddenExtension(canSelectHiddenExtension bool) *SaveFileDialog

func (*SaveFileDialog) HideExtension added in v0.1.0

func (d *SaveFileDialog) HideExtension(hideExtension bool) *SaveFileDialog

func (*SaveFileDialog) SetButtonText added in v0.1.0

func (d *SaveFileDialog) SetButtonText(text string) *SaveFileDialog

func (*SaveFileDialog) SetDirectory added in v0.1.0

func (d *SaveFileDialog) SetDirectory(directory string) *SaveFileDialog

func (*SaveFileDialog) SetFilename added in v0.1.0

func (d *SaveFileDialog) SetFilename(filename string) *SaveFileDialog

func (*SaveFileDialog) Show added in v0.1.0

func (d *SaveFileDialog) Show(callbacks ...func(string)) chan string

func (*SaveFileDialog) ShowHiddenFiles added in v0.1.0

func (d *SaveFileDialog) ShowHiddenFiles(showHiddenFiles bool) *SaveFileDialog

func (*SaveFileDialog) TreatsFilePackagesAsDirectories added in v0.1.0

func (d *SaveFileDialog) TreatsFilePackagesAsDirectories(treatsFilePackagesAsDirectories bool) *SaveFileDialog

type TrayMenu added in v0.1.0

type TrayMenu struct {
	// contains filtered or unexported fields
}

func (*TrayMenu) Add added in v0.1.0

func (m *TrayMenu) Add(label string) *MenuItem

func (*TrayMenu) AddCheckbox added in v0.1.0

func (m *TrayMenu) AddCheckbox(label string, checked bool) *MenuItem

func (*TrayMenu) AddRadio added in v0.1.0

func (m *TrayMenu) AddRadio(label string, checked bool) *MenuItem

func (*TrayMenu) AddSeparator added in v0.1.0

func (m *TrayMenu) AddSeparator()

func (*TrayMenu) AddSubmenu added in v0.1.0

func (m *TrayMenu) AddSubmenu(label string) *TrayMenu

func (*TrayMenu) Item added in v0.1.0

func (m *TrayMenu) Item() *MenuItem

func (*TrayMenu) SetLabel added in v0.1.0

func (m *TrayMenu) SetLabel(label string)

func (*TrayMenu) Update added in v0.1.0

func (m *TrayMenu) Update()

type WebkitCacheModel added in v0.1.0

type WebkitCacheModel int
const (
	CacheNone WebkitCacheModel = iota
	CacheLite
	CacheFull
)

type WebkitCookiePolicy added in v0.1.0

type WebkitCookiePolicy int
const (
	CookiesAcceptAll WebkitCookiePolicy = iota
	CookiesRejectAll
	CookiesNoThirdParty
)

type WebkitSettings

type WebkitSettings struct {
	EnableJavascript                          bool
	AutoLoadImages                            bool
	LoadIconsIgnoringImageLoadSetting         bool
	EnableOfflineWebApplicationCache          bool
	EnableHtml5LocalStorage                   bool
	EnableHtml5Database                       bool
	EnableXssAuditor                          bool
	EnableFrameFlattening                     bool
	EnablePlugins                             bool
	EnableJava                                bool
	JavascriptCanOpenWindowsAutomatically     bool
	EnableHyperlinkAuditing                   bool
	DefaultFontFamily                         string
	MonospaceFontFamily                       string
	SerifFontFamily                           string
	SansSerifFontFamily                       string
	CursiveFontFamily                         string
	FantasyFontFamily                         string
	PictographFontFamily                      string
	DefaultFontSize                           uint32
	DefaultMonospaceFontSize                  uint32
	MinimumFontSize                           uint32
	DefaultCharset                            string
	EnableDeveloperExtras                     bool
	EnableResizableTextAreas                  bool
	EnableTabsToLinks                         bool
	EnableDnsPrefetching                      bool
	EnableCaretBrowsing                       bool
	EnableFullscreen                          bool
	PrintBackgrounds                          bool
	EnableWebAudio                            bool
	EnableWebgl                               bool
	AllowModalDialogs                         bool
	ZoomTextOnly                              bool
	JavascriptCanAccessClipboard              bool
	MediaPlaybackRequiresUserGesture          bool
	MediaPlaybackAllowsInline                 bool
	DrawCompositingIndicators                 bool
	EnableSiteSpecificQuirks                  bool
	EnablePageCache                           bool
	UserAgent                                 string
	EnableSmoothScrolling                     bool
	EnableAccelerated2DCanvas                 bool
	EnableWriteConsoleMessagesToStdout        bool
	EnableMediaStream                         bool
	EnableMockCaptureDevices                  bool
	EnableSpatialNavigation                   bool
	EnableMediaSource                         bool
	EnableEncryptedMedia                      bool
	EnableMediaCapabilities                   bool
	AllowFileAccessFromFileUrls               bool
	AllowUniversalAccessFromFileUrls          bool
	AllowTopNavigationToDataUrls              bool
	HardwareAccelerationPolicy                int
	EnableBackForwardNavigationGestures       bool
	EnableJavascriptMarkup                    bool
	EnableMedia                               bool
	MediaContentTypesRequiringHardwareSupport string
	EnableWebRTC                              bool
	DisableWebSecurity                        bool
}

type Window

type Window struct {
	// contains filtered or unexported fields
}

func (*Window) AddCSS

func (w *Window) AddCSS(css string)

func (*Window) Center

func (w *Window) Center()

func (*Window) Close

func (w *Window) Close()

func (*Window) Dialog added in v0.1.0

func (w *Window) Dialog() *Dialog

func (*Window) DisableSizeConstraints

func (w *Window) DisableSizeConstraints()

func (*Window) ExecJS

func (w *Window) ExecJS(js string)

func (*Window) Focus

func (w *Window) Focus()

func (*Window) Fullscreen

func (w *Window) Fullscreen()

func (*Window) GetSize

func (w *Window) GetSize() (int, int)

func (*Window) GetZoom

func (w *Window) GetZoom() float64

func (*Window) Hide

func (w *Window) Hide()

func (*Window) ID

func (w *Window) ID() uint

func (*Window) IsFocused

func (w *Window) IsFocused() bool

func (*Window) IsFullscreen

func (w *Window) IsFullscreen() bool

func (*Window) IsMaximised

func (w *Window) IsMaximised() bool

func (*Window) IsMinimised

func (w *Window) IsMinimised() bool

func (*Window) JSCall added in v0.1.0

func (w *Window) JSCall(js string, fn func(interface{})) func()

func (*Window) JSEval added in v0.1.0

func (w *Window) JSEval(js string, fn func(interface{})) func()

func (*Window) JSPromise added in v0.1.0

func (w *Window) JSPromise(js string, fn func(interface{})) func()

func (*Window) Maximise

func (w *Window) Maximise()

func (*Window) Minimise

func (w *Window) Minimise()

func (*Window) Restore

func (w *Window) Restore()

func (*Window) SetFrameless

func (w *Window) SetFrameless(frameless bool)

func (*Window) SetHTML added in v0.1.0

func (w *Window) SetHTML(html string)

func (*Window) SetMaxSize

func (w *Window) SetMaxSize(width, height int)

func (*Window) SetMinMaxSize

func (w *Window) SetMinMaxSize(minWidth, minHeight, maxWidth, maxHeight int)

func (*Window) SetMinSize

func (w *Window) SetMinSize(width, height int)

func (*Window) SetOverlay

func (w *Window) SetOverlay(alwaysOnTop bool)

func (*Window) SetSize

func (w *Window) SetSize(width, height int)

func (*Window) SetTitle

func (w *Window) SetTitle(title string)

func (*Window) SetURL

func (w *Window) SetURL(uri string)

func (*Window) SetZoom

func (w *Window) SetZoom(zoom float64)

func (*Window) Show

func (w *Window) Show()

func (*Window) ToggleDevTools

func (w *Window) ToggleDevTools()

func (*Window) Unfullscreen

func (w *Window) Unfullscreen()

func (*Window) Unmaximise

func (w *Window) Unmaximise()

func (*Window) Unminimise

func (w *Window) Unminimise()

func (*Window) ZoomIn

func (w *Window) ZoomIn()

func (*Window) ZoomOut

func (w *Window) ZoomOut()

func (*Window) ZoomReset

func (w *Window) ZoomReset()

type WindowOptions

type WindowOptions struct {
	// Name is a unique identifier of the window.
	Name string

	// Title is the title of the page.
	Title string

	// URL is the URL of the page to load.
	URL string

	// HTML Content to load (if URL is not set).
	HTML string

	// CSS to load after the page has loaded.
	CSS []string

	// JS to load after the page has loaded.
	JS []string

	// Define global Variables and APIs.
	Define map[string]interface{}

	// Width is the starting width of the window.
	Width int

	// Height is the starting height of the window.
	Height int

	// Overlay will make the window float above other windows.
	Overlay bool

	// Frameless will remove the window frame.
	Frameless bool

	// MinWidth is the minimum width of the window.
	MinWidth int

	// MinHeight is the minimum height of the window.
	MinHeight int

	// MaxWidth is the maximum width of the window.
	MaxWidth int

	// MaxHeight is the maximum height of the window.
	MaxHeight int

	// State indicates the state of the window when it is first shown.
	// Default: WindowStateNormal
	State WindowState

	// Centered will Center the window on the screen.
	Centered bool

	// Color specified the window color as a hex string (#RGB, #RGBA, #RRGGBB, #RRGGBBAA)
	Color string

	// X is the starting X position of the window.
	X int

	// Y is the starting Y position  of the window.
	Y int

	// Hidden will Hide the window when it is first created.
	Hidden bool

	// Zoom is the initial Zoom level of the window.
	Zoom float64

	// ZoomControlEnabled will enable the Zoom control.
	ZoomControlEnabled bool

	// OpenInspectorOnStartup will open the inspector when the window is first shown.
	OpenInspectorOnStartup bool

	// Focused indicates the window should be focused when initially shown
	Focused bool

	// If true, the window's devtools will be available
	DevToolsEnabled bool

	// HideOnClose will hide the window when it is closed instead of destroying it.
	HideOnClose bool

	// WebkitSettings exposes the underlying WebkitSettings object.
	WebkitSettings WebkitSettings
}

type WindowState

type WindowState int
const (
	WindowStateNormal WindowState = iota
	WindowStateMinimised
	WindowStateMaximised
	WindowStateFullscreen
)

Directories

Path Synopsis
examples

Jump to

Keyboard shortcuts

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