view

package
v0.14.2 Latest Latest
Warning

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

Go to latest
Published: Oct 30, 2026 License: EPL-2.0 Imports: 14 Imported by: 1 module, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package view puts a gfx scene into a gowt window as a widget.

Experimental, like gfx: the API may change without notice. macOS composites tiles as CALayers; Linux composites them on the CPU with cairo into the widget (GTK 3); Windows draws them with Direct2D into a DXGI swap chain.

Index

Examples

Constants

This section is empty.

Variables

View Source
var Tracing = os.Getenv("GFXDEMO_TRACE") != ""

Tracing is set by GFXDEMO_TRACE: the demos print what the view and the router see.

Functions

func New

func New(scene *gfx.Scene, init func(*View), opts ...g.Option) g.Option

New returns an Option that creates the scene canvas in the widget tree, like g.Canvas does. init runs on the UI goroutine once the view exists; it gets the View to set the camera and add overlay sprites. A frame clock failure (no display) goes to App.OnError.

Example

A scene in a window. Scrolling pans and pinching zooms without code; the init function sets what the app reacts to.

package main

import (
	"fmt"
	"log"

	g "github.com/haiodo/gowt"
	"github.com/haiodo/gowt/gfx"
	"github.com/haiodo/gowt/gfx/view"
)

func main() {
	scene := gfx.NewScene()
	rec := gfx.NewRecorder()
	rec.FillRRect(gfx.R(0, 0, 160, 80), 10, gfx.RGB(30, 120, 220))
	card := gfx.NewNode(1)
	scene.Update(func(tx *gfx.Tx) {
		tx.Add(nil, card)
		tx.SetPicture(card, rec.Finish())
	})

	err := g.Run(func(app *g.App) {
		w := app.Window("Canvas", view.New(scene, func(v *view.View) {
			v.Compositor().SetCamera(gfx.Camera{Zoom: 1})
			v.OnPointer(func(p *view.Pointer) {
				if p.Kind == view.PointerDown && p.Node != nil {
					fmt.Println("pressed node", p.Node.ID(), "at scene point", p.Scene)
				}
			})
		}, g.Grow()))
		w.Show()
	})
	if err != nil {
		log.Fatal(err)
	}
}

func Tracef

func Tracef(format string, a ...any)

Tracef prints a line to stderr when Tracing is set.

Types

type AutoScrollOptions

type AutoScrollOptions struct {
	// Band is how far inside the view edge the scrolling starts, view points. Default 32.
	Band float64
	// Ramp is the distance past the band start at which the speed reaches Max. Default 120.
	Ramp float64
	// Max is the speed cap, view points per second. Default 1000.
	Max float64
	// Curve maps the distance fraction 0..1 to the speed fraction 0..1. Default linear.
	Curve func(t float64) float64
}

AutoScrollOptions tune View.AutoScroll. Zero fields take the defaults.

type Cursor

type Cursor uint8

Cursor is a mouse cursor shape.

const (
	CursorArrow Cursor = iota
	CursorHand
	CursorGrab
	CursorGrabbing
	CursorCrosshair
	CursorText
	CursorResizeH
	CursorResizeV
	CursorNotAllowed
)

The cursor shapes of View.SetCursor.

type EditOptions

type EditOptions struct {
	Rect      gfx.Rect // scene rectangle of the field; make it at least one text line high
	Text      string
	Font      g.Font // font at zoom 1, as for Overlay
	Multiline bool
	OnCommit  func(text string)
	OnCancel  func()
	Opts      []g.Option // more options of the g.Text
}

EditOptions describe Edit.

type Editor

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

Editor is an inline text field over the view; see Edit.

func (*Editor) Cancel

func (h *Editor) Cancel()

Cancel ends the edit as Esc does. It does nothing once the edit has ended.

func (*Editor) Commit

func (h *Editor) Commit()

Commit ends the edit as Enter does. It does nothing once the edit has ended.

func (*Editor) SetRect

func (h *Editor) SetRect(r gfx.Rect)

SetRect moves the field to another scene rectangle.

func (*Editor) Widget

func (h *Editor) Widget() *g.TextView

Widget is the g.TextView.

type FrameReport

type FrameReport struct {
	Time     time.Duration // frame clock
	Interval time.Duration // since the previous frame the view ran, 0 for the first
	CPU      time.Duration // time the compositor took in Frame
	Events   int           // scroll events taken in since the previous frame
	DX, DY   float64       // pan applied by this frame, view points
	Origin   gfx.Point     // camera origin after the frame
	Zoom     float64
	Tiles    int // tile images put on layers by this frame
	// Paint is how long the last widget paint took: the CPU compositing of the tiles on Linux, 0 on macOS.
	Paint time.Duration
}

FrameReport describes one frame the view ran, for tuning: the numbers of the scroll path.

type Key

type Key struct {
	Down bool
	Code int
	Char rune
	Mods g.Mods
}

Key is a key press or release on the focused view. Code is SWT's key code (the constants of package swt) and Char the character typed, 0 for none. For shortcuts use Window.OnKey("Cmd+Z", f) of the gowt facade: it also works while the view has the focus.

type Magnify

type Magnify struct {
	Pos   gfx.Point
	Scene gfx.Point
	// Factor multiplies the zoom: 1 for no change.
	Factor float64
	Phase  Phase
	Mods   g.Mods
}

Magnify is one step of a pinch.

type Overlay

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

Overlay is a gowt control placed over the view at a scene rectangle. The view moves and sizes it on every frame with the camera, so it follows pan and zoom, and scales its font with the zoom. Wheel, pinch and smart zoom over it go to the control, not to the view.

func (*Overlay) Remove

func (o *Overlay) Remove()

Remove disposes the control. It is safe after the control or the view is gone.

func (*Overlay) SetRect

func (o *Overlay) SetRect(r gfx.Rect)

SetRect moves the control to another scene rectangle.

func (*Overlay) Widget

func (o *Overlay) Widget() g.Widget

Widget is the control, for its own methods: type-assert to *g.TextView and so on.

type Phase

type Phase uint8

Phase is where a trackpad gesture or a scroll stands.

const (
	PhaseNone     Phase = iota // a mouse wheel, or no momentum
	PhaseMayBegin              // fingers rest on the trackpad: coasting content should stop
	PhaseBegan
	PhaseChanged
	PhaseEnded
	PhaseCancelled
)

The phases of a trackpad gesture.

type Pointer

type Pointer struct {
	Kind PointerKind
	// Pos is in view points, y down. During a drag it may lie outside the view: the view keeps
	// the pointer captured until the last button is released.
	Pos   gfx.Point
	Scene gfx.Point
	// Outside is set when Pos lies outside the view. The coordinates stay exact, negative or past
	// the view size; what to do with them (clamp, cancel, drop there) is the app's call.
	Outside bool
	// Node is the topmost node under the pointer, nil on empty space and when HUD is set.
	Node *gfx.Node
	// HUD is the topmost node of the heads-up scene (Compositor.HUD) under the pointer. The HUD
	// is tested before the scene: a pointer over a HUD node has Node nil.
	HUD *gfx.Node
	// Button is SWT's numbering: 1 left, 2 middle, 3 right; on a move the button held, 0 for a hover.
	Button int
	// Count is the click count of a Down (2 for a double click).
	Count int
	Mods  g.Mods
	// Drag is set on moves, releases and cancels that belong to a press.
	Drag bool
	// Aborted is set on the PointerCancel that Escape caused: the app puts back what the drag moved.
	Aborted bool
	// Target and Origin are the node and the scene point under the press that began the drag.
	Target *gfx.Node
	Origin gfx.Point
}

Pointer is a mouse event with the scene point and the node under it.

type PointerKind

type PointerKind uint8

PointerKind tells what a Pointer event is.

const (
	PointerDown PointerKind = iota
	PointerMove
	PointerUp
	// PointerCancel ends a drag that lost its release: the view lost focus with a button held, or
	// Escape was pressed (Aborted).
	PointerCancel
)

The kinds of Pointer event.

type Scroll

type Scroll struct {
	Pos   gfx.Point
	Scene gfx.Point
	// DX, DY are how far the content should move, as the user's natural scrolling setting says:
	// view points when Precise (trackpad), lines for a wheel.
	DX, DY  float64
	Precise bool
	// Phase is the finger phase of a trackpad scroll; Momentum the phase of the coasting after the
	// fingers left. Both are PhaseNone for a wheel.
	Phase, Momentum Phase
	Mods            g.Mods
}

Scroll is a scroll gesture or wheel event.

type SmartZoom

type SmartZoom struct {
	Pos   gfx.Point
	Scene gfx.Point
	Node  *gfx.Node
	Mods  g.Mods
}

SmartZoom is the two finger double tap.

type View

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

View shows a gfx.Scene on a canvas. Its methods are for the UI goroutine, except Scene().Update, which any goroutine may call.

func (*View) AutoScroll

func (v *View) AutoScroll(o AutoScrollOptions) (stop func())

AutoScroll pans the camera each frame while the pointer is in the edge band or outside the view; call it from the PointerDown that starts a drag. After each step the view repeats the last position as a PointerMove, so the app re-places the dragged content. It ends on release, on cancel or when stop is called. The drop rule on a release with Outside set is the app's.

func (*View) Canvas

func (v *View) Canvas() *g.CanvasView

Canvas is the underlying gowt canvas: mouse listeners, focus, size.

func (*View) Compositor

func (v *View) Compositor() *gfx.Compositor

Compositor gives the camera, the lifting of nodes for drags, sprites and statistics.

func (*View) Drag

func (v *View) Drag(p *Pointer, nodes ...*gfx.Node)

Drag moves nodes with the pointer until the button is released. Call it from the PointerDown that starts the drag, with the nodes to move (for example p.Node). The nodes are lifted into the active layer and landed at the end, so the tiles under them are not redrawn each move. The app's OnPointer callback still gets every event, after the nodes moved: update what depends on them there. Releasing outside the view drops the nodes at the nearest visible point; Escape or a lost focus puts them back (Aborted). Positions are read from the last committed state, so call it from the UI goroutine.

Example

Drag moves the nodes with the pointer, lands them on release and puts them back on Escape.

package main

import (
	"github.com/haiodo/gowt/gfx"
	"github.com/haiodo/gowt/gfx/view"
)

func main() {
	var scene *gfx.Scene
	_ = view.New(scene, func(v *view.View) {
		v.OnPointer(func(p *view.Pointer) {
			if p.Kind == view.PointerDown && p.Button == 1 && p.Node != nil {
				v.Drag(p, p.Node)
			}
		})
	})
}

func (*View) Edit

func (v *View) Edit(opts EditOptions) *Editor

Edit puts a g.Text over the view for in-place editing, selects its text and gives it the focus. Esc cancels. Enter commits; in a Multiline field Cmd+Enter or Ctrl+Enter commits and Enter types a line break. The focus leaving the field commits: a click on the view or on another control, or the window losing the focus. OnCommit or OnCancel runs once, after the field is gone. Starting another Edit commits the one that is open.

func (*View) HUDHitTest

func (v *View) HUDHitTest(x, y int) *gfx.Node

HUDHitTest returns the topmost node of the heads-up scene under the view point (x, y), or nil; it is what to ask before HitTest.

func (*View) HitTest

func (v *View) HitTest(x, y int) *gfx.Node

HitTest returns the topmost node under the view point (x, y), with a 4 point tolerance.

func (*View) OnFrameReport

func (v *View) OnFrameReport(f func(FrameReport))

OnFrameReport sets f to run after every frame the view runs, on the UI goroutine.

func (*View) OnKey

func (v *View) OnKey(f func(*Key))

OnKey sets f to run for key presses and releases while the view has the keyboard focus. A press on the view gives it the focus.

func (*View) OnMagnify

func (v *View) OnMagnify(f func(*Magnify) bool)

OnMagnify sets f to run for pinch steps. f returns true when it used the event; otherwise the view zooms about the pointer.

func (*View) OnPointer

func (v *View) OnPointer(f func(*Pointer))

OnPointer sets f to run for every press, move, release and cancel, with the scene point and the node under the pointer. While a button is held the moves and the release keep coming outside the view. A Ctrl-click is Button 1 with Mods.Ctrl.

func (*View) OnResize

func (v *View) OnResize(f func(w, h int))

OnResize sets f to run with the view size in points when layout gave it a new one, and once for the first size known; see g.CanvasView.OnResize. The camera is yours to set there, for example to fit the content to the window the first time.

func (*View) OnScroll

func (v *View) OnScroll(f func(*Scroll) bool)

OnScroll sets f to run for scroll gestures and wheel events. f returns true when it used the event; otherwise the view pans (zooms with Cmd or Ctrl held).

func (*View) OnSmartZoom

func (v *View) OnSmartZoom(f func(*SmartZoom) bool)

OnSmartZoom sets f to run for the two finger double tap. f returns true when it used the event; otherwise the view toggles between zoom 1 and the zoom before.

func (*View) Overlay

func (v *View) Overlay(opt g.Option, rect gfx.Rect, font g.Font) *Overlay

Overlay creates the widget that opt describes (g.Text(...), g.Button(...)) as a child of the view at the scene rectangle rect. font is the font at zoom 1: Px, or Size in points, scaled by the zoom; Name, Weight, Bold and Italic are kept. A zero size leaves the control's font alone. It returns nil if opt is not a widget. Call it on the UI goroutine.

func (*View) PaintTime

func (v *View) PaintTime() time.Duration

PaintTime is how long the last widget paint took: the CPU compositing of the tiles on Linux, 0 on macOS.

func (*View) Pan

func (v *View) Pan(dx, dy float64)

Pan moves the content by (dx, dy) view points.

func (*View) Scene

func (v *View) Scene() *gfx.Scene

Scene returns the scene the view shows.

func (*View) SetCursor

func (v *View) SetCursor(c Cursor)

SetCursor sets the cursor over the view. The shapes are the system cursors; GTK has no open and closed hand in SWT's set, so the grab shapes are the move cursor.

func (*View) SetZoomLimits

func (v *View) SetZoomLimits(lo, hi float64)

SetZoomLimits bounds the zoom the built-in pinch, wheel zoom and smart zoom reach. The default is 0.02 to 64.

Jump to

Keyboard shortcuts

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