Task 11: The frame pipeline, ., frame, label, and wm
Goal
A script with frames and labels in a grid gives a picture. One function runs the stages of a frame: style, measure, layout, paint, and draw. The first golden images of real widgets exist.
Files
src/app.zig:frame, the dirty logicsrc/paint.zig: the box of a widget from its stylesrc/widgets/toplevel.zig,src/widgets/frame.zig,src/widgets/label.zigsrc/cmds.zig:wm, the geometry subcommands ofwinfo,tk scalingsrc/widget.zig:Class.measure,Class.paint
Interfaces
// added to Class
/// The requested size from the content, the style, and the options.
measure: ?*const fn (w: *Widget) geom.Size = null,
/// Adds the quads of the widget. The origin of the builder is the
/// top-left corner of the widget.
paint: ?*const fn (w: *Widget, b: *scene.Builder) anyerror!void = null,
// src/paint.zig
/// Adds the shadow, the background, the border, and the focus outline
/// of a widget from its style.
pub fn box(w: *const Widget, b: *scene.Builder, style: *const Style) !void;
/// The space inside the border and the padding.
pub fn contentRect(w: *const Widget, style: *const Style) geom.Rect;
/// The border and the padding as insets, rounded up to whole pixels.
pub fn insets(style: *const Style) geom.Insets;
/// Draws lines of text in a rectangle with an anchor and a justify.
pub fn label(w: *const Widget, b: *scene.Builder, r: geom.Rect, text: []const u8, style: *const Style, anchor: options.Anchor, justify: options.Justify) !void;
/// The size of lines of text in a font.
pub fn textSize(app: *App, text: []const u8, font: text.FontDesc, scale: f32) geom.Size;
// src/app.zig
/// Runs the stages that the dirty flags ask for, for one toplevel.
/// Returns true when it drew.
pub fn frame(app: *App, top: *Widget) !bool;
Steps
App.framefor a toplevel:- Style:
restyle(task 09). - Measure and layout:
layout.run(task 10). A class withmeasuregives the requested size of a widget that has no managed content. - Window size: when the requested size of the toplevel changed and the script set no size with
wm geometry, callPlatform.requestSize. The size is at least 1 by 1 and at leastwm minsize. - Paint: when a widget of the toplevel has the paint flag, build the scene of the complete toplevel. Go down the tree in stacking order. For each mapped widget: push the offset of its position, push the clip of its box, call
paint, paint the children, and pop. - Draw:
Platform.draw. Clear all flags.
- Style:
App.doOneEventcallsframefor a toplevel on aframeevent. A change that sets a dirty flag callsPlatform.requestFrameone time until the frame runs.updateandupdate idletaskscallframedirectly for each dirty toplevel, as Tk draws in idle time.- The clear color of the scene is the computed background of the toplevel.
frameclass:measuregives-widthand-height. A frame with managed content gets its size from the manager.paintcallspaint.box.- The toplevel class has the options of
framethat Stage 1 has. The class name of.isApp.Options.root_class, so a stylesheet selects.with:root. labelclass. The type name isLabel. Options in Stage 1:-text,-textvariable,-font,-foregroundand-fg,-backgroundand-bg,-anchor(defaultcenter),-justify(defaultcenter),-padx,-pady,-width(in characters),-height(in lines),-borderwidthand-bd,-relief,-state(normal,active,disabled),-takefocus,-cursor. Each other option of the Tk label givesunknown optionin Stage 1. List them indocs/stage1-limits.md.labelmeasure: the text size is the width of the longest line and the line height times the count of lines. Lines split at a newline. With-widthabove 0, the text width is that count times the width of the character0. With-heightabove 0, the text height is that count of lines. Add the insets of the style. Use at leastmin-widthandmin-height.labelpaint: the box, then the text in the content rectangle with-anchor. The text color is the computedcolor.-textvariable: the label shows the value of a global variable. On creation and onconfigure, when the variable does not exist, create it with the value of-text. Add a script trace:trace add variable <name> write {::ztk::priv::textvar <path>}. The native command reads the variable, sets the measure and paint flags, and asks for a frame. Remove the trace when the option changes and when the widget is deleted. A variable that is unset keeps the last text.-state disabledsets thedisabledstate bit.wm, for.only in Stage 1:wm title . ?text?wm geometry . ?WxH?and?WxH+X+Y?. The position is parsed and ignored. With no argument it givesWxH+0+0. A bad value givesbad geometry specifier "bogus". An empty string removes the size that the script set.wm minsize . ?w h?,wm maxsize . ?w h?,wm resizable . ?w h?(stored).wm protocol . WM_DELETE_WINDOW ?script?. Acloseevent runs the script. With no script, it deletes..wm withdraw .,wm deiconify ., andwm state .(normalorwithdrawn). A withdrawn toplevel draws no frame.- Each other subcommand gives the error
wm <name> is not supported in this stage. A name that Tk does not have gives thebad optionmessage ofwish, with the complete list.
winfo:width,height,x,y,reqwidth,reqheight,rootx,rooty(the position in the toplevel; the toplevel is at 0, 0),ismapped,viewable,geometry(WxH+X+Y). Before the first layout, a widget has the width 1 and the height 1, as in Tk.tk scaling ?number?gives and sets the logical pixels for each point.tk windowingsystemgivesztk. Record this indocs/stage1-limits.md: scripts that test forx11,win32, oraquaget none of them.
Tests
zig build test -Dtest-filter=frame
zig build test -Dtest-filter=label
zig build test -Dtest-filter=wm
Script tests with a Harness:
label .l -text Hello; grid .l; update:winfo reqwidth .lis the text width plus the insets of the style, andwinfo width .is equal towinfo reqwidth ..winfo width .lbeforeupdateis1..l configure -text "A much longer text"; updatemakes.wider.set v one; label .m -textvariable v; grid .m; update; set v "two words"; update: the scene dump has the glyphs of the new text, and the width changed.label .n -textvariable nv -text start; set nvgivesstart.destroy .m; set v threegives no error.- A label with
-text "a\nb"has two times the line height. label .w -width 10has the width of ten0characters plus the insets.wm title . Demo; wm title .givesDemo.wm geometry . 300x200; update; winfo width .gives300.wm geometry .gives300x200+0+0.wm protocol . WM_DELETE_WINDOW {set closed 1}and acloseevent:closedis1and.exists. With no protocol script,.does not exist after it.wm withdraw .; updatedraws no frame (h.head.frames_drawndoes not change).- Two
updatecalls with no change between them draw one frame in total. - A
resizeevent to 400 by 300 withgrid columnconfigure . 0 -weight 1and a-sticky ewlabel changes the width of the label. winfo rootxof a label in a frame at (10, 20) is the sum of the positions.
Golden images and scene dumps, with the modern theme, 240 by 120:
label-plain: one label.label-anchor: three labels with-anchor w,center, andein a wide column with-sticky ew.label-colors:-fgand-bgset, and a style class with a border and a radius.label-grid: a 2 by 2 grid of labels with-padxand-pady.label-disabled: a normal and a disabled label.label-scale2:label-plainat scale 2. The image is two times as large and the text is sharp.
Acceptance
- All tests pass.
- The reviewer opens the six images. The text is in the correct place for each anchor, and no quad is outside its widget.
App.framewith no dirty flag does no work and returns false.- After a change of one label text with the same size, the layout of the other widgets does not run (count the
arrangecalls in a test).
Out of scope
-image,-bitmap,-compound,-underline, and-wraplength.- The
toplevelcommand and a second window. - The window position and the other
wmsubcommands. - Paint of only the changed region. Stage 1 paints the complete toplevel.