Trame

preview: content that zooms and pans

ui.preview shows content (a picture, a page, a diagram) in a box: it fits there first, then the user zooms and moves it. Every program that shows a picture or a document has the same gestures, and none draws them again.

Formula
app.pv: ui.Preview                                        // one per thing shown: ui.preview_reset when another comes
ui.next({width = ui.fill(), height = ui.fill()})
r := ui.preview_image("###shot", &app.pv, tex, {f32(w), f32(h)}, {buttons = true, radius = 6})
// or any drawing: the content's size in its own units, and a proc that draws it into a rectangle of the screen
r = ui.preview("###doc", &app.pv, {doc_w, doc_h}, draw_doc, doc, {buttons = true})
// r.percent: the zoom shown; r.changed: it moved this frame; r.fit: device pixels per unit when it fits
GestureWhat it does
the wheelzooms by PREVIEW_ZOOM_STEP a notch, around the pointer; with wheel = .Pans, moves the content (Shift: across)
Ctrl and the wheelthe interface's zoom; with .Pans: zooms around the pointer (a touchpad's pinch, which Windows sends as Ctrl and the wheel); with .Ctrl_Pans: moves the content (Shift too: across). With either, the interface keeps its own zoom (.Ctrl_Wheel)
Shift and the wheelas the wheel (with .Pans: across); with shift_wheel_free, the program's (ui.wheel_free, wheel.md)
a dragmoves the content, when it is larger than the box (the pointer is a hand); never past its edges
a double clickfrom what fits to the content's pixels (twice them when it fits at its pixels), and back
+, −, 0, the arrowswith the keyboard on it (a click gives it): zoom in, out, fit, move by PREVIEW_KEY_PAN of the box
the bar (buttons)over the bottom right corner: the zoom in percent, −, +, 1:1, Fit

The content's unit is a device pixel at 100 %: an image's pixel. Fitted, it is no larger than that unless grow (a small picture is not blown up). Preview_Options: buttons, grow, zoom_min (of what fits, PREVIEW_ZOOM_MIN by default), zoom_max (device pixels per unit, PREVIEW_NATIVE_MAX), radius (an image's corners), no_keys (the keys are the program's), wheel (Preview_Wheel: .Zooms by default; .Pans, the wheel moves and Ctrl with it zooms: a diagram or a document larger than its box, read by moving through it; .Ctrl_Pans, the wheel zooms and Ctrl with it moves: a viewer whose plain wheel stays a zoom), shift_wheel_free (Shift+wheel is the program's: a viewer's next picture). preview_fit, preview_zoom_to and preview_pan_clamp are its steps, for a view of a program's own. tests/preview tests them, and the gestures in a session.

The keys need the keyboard on the preview. A dialog that is all preview gives it at once: ui.set_focus(r.box.key) in the build that opens it.

Preview is the caller's state: a dialog's size and the preview's zoom and pan are separate, so the person makes the dialog any shape, and the content fits it again (preview_reset) or keeps its zoom. The node graph (node_graph) keeps its own view (Graph_View): a canvas without edges, whose zoom glides; it does not use Preview.

Edit this page on GitHub