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 ¶
- Variables
- func New(scene *gfx.Scene, init func(*View), opts ...g.Option) g.Option
- func NewWithOptions(scene *gfx.Scene, comp Options, init func(*View), opts ...g.Option) g.Option
- func Tracef(format string, a ...any)
- type AutoScrollOptions
- type Cursor
- type EditOptions
- type Editor
- type FrameReport
- type Key
- type Magnify
- type Options
- type Overlay
- type Phase
- type Pointer
- type PointerKind
- type Scroll
- type SmartZoom
- type View
- func (v *View) AutoScroll(o AutoScrollOptions) (stop func())
- func (v *View) Canvas() *g.CanvasView
- func (v *View) Compositor() *gfx.Compositor
- func (v *View) Drag(p *Pointer, nodes ...*gfx.Node)
- func (v *View) Edit(opts EditOptions) *Editor
- func (v *View) HUDHitTest(x, y int) *gfx.Node
- func (v *View) HitTest(x, y int) *gfx.Node
- func (v *View) OnFrameReport(f func(FrameReport))
- func (v *View) OnKey(f func(*Key))
- func (v *View) OnMagnify(f func(*Magnify) bool)
- func (v *View) OnPointer(f func(*Pointer))
- func (v *View) OnResize(f func(w, h int))
- func (v *View) OnScroll(f func(*Scroll) bool)
- func (v *View) OnSmartZoom(f func(*SmartZoom) bool)
- func (v *View) Overlay(opt g.Option, rect gfx.Rect, font g.Font) *Overlay
- func (v *View) PaintTime() time.Duration
- func (v *View) Pan(dx, dy float64)
- func (v *View) Scene() *gfx.Scene
- func (v *View) SetCursor(c Cursor)
- func (v *View) SetZoomLimits(lo, hi float64)
Examples ¶
Constants ¶
This section is empty.
Variables ¶
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 ¶
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)
}
}
Output:
func NewWithOptions ¶
NewWithOptions is New with the options of the view's compositor; they are fixed when the compositor is created, before the first frame. The default zero Options is exactly New.
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 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.
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 ¶
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 Options ¶
Options are the gfx.Compositor options of the view: TilePx, Workers, Margin, Settle and ExactZoom; the zero value keeps the compositor defaults.
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.
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 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 ¶
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)
}
})
})
}
Output:
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 Shift+Enter types a line break, and Cmd+Enter or Ctrl+Enter also commits. 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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
PaintTime is how long the last widget paint took: the CPU compositing of the tiles on Linux, 0 on macOS.
func (*View) SetCursor ¶
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 ¶
SetZoomLimits bounds the zoom the built-in pinch, wheel zoom and smart zoom reach. The default is 0.02 to 64.