Task 14: button
Goal
The button widget works with the pointer and the keyboard, runs its -command, and shows its states through the style.
Files
src/widgets/button.zigsrc/cmds.zig: the registration and the class bindingssrc/themes/modern.css: theButtonrules, if task 09 left them open
Interfaces
// src/widgets/button.zig
pub const class: Class; // command "button", type name "Button", focusable
/// Runs the -command of the button. Returns the result of the script.
pub fn invoke(w: *Widget, it: *ztcl.Interp) ztcl.Error!ztcl.Value;
The native commands of the class bindings:
| Command | Effect |
|---|---|
::ztk::priv::button enter path |
No effect. Task 12 sets the hover bit. |
::ztk::priv::button leave path |
Clears the armed flag |
::ztk::priv::button down path |
Arms the button and gives it the focus |
::ztk::priv::button up path |
When armed and the pointer is on the button: invoke |
::ztk::priv::button key path |
invoke |
Steps
- Read
doc/button.n. - The options of Stage 1: all options of
labelin task 11, and-command,-activebackground,-activeforeground,-disabledforeground. The default of-takefocusis empty, and the class isfocusable. The default-anchoriscenter. Each other option of the Tk button givesunknown option. Add them todocs/stage1-limits.md. - Measure and paint are those of
label: the box and the text. Share the code withlabel. - The states.
-state disabledsets thedisabledbit.-state activesets thehoverbit, as Tk shows the active colors. Thepressedbit is on while the button is armed and the pointer is on it. - The options
-activebackground,-activeforeground, and-disabledforeground, when the script set them, are declarations of the originwidgetthat apply only in that state:background-colorandcolorwithhover, andcolorwithdisabled. - The class bindings, on the tag
Button.wishhas the same six patterns:<Enter>,<Leave>,<Button-1>,<ButtonRelease-1>,<Key-space>, and<<Invoke>>. Each script is one call of::ztk::priv::buttonwith%W. downon a disabled button has no effect.downgives the focus to the button whenfocus.acceptsis true for it.upinvokes only when the button was armed bydownand the pointer is on the button at the release. A release outside the button does not invoke.invokeruns the-commandscript at the global level and returns its result. An error of the script is the error ofinvoke. From a class binding, that error goes to the background error handler. A disabled button returns an empty string and runs no script.wishgiveswrong # args: should be ".b invoke"for an extra argument.- The widget subcommands are
cget,configure, andinvoke.flashgives the errorflash is not supported in this stage. A wrong name gives thebad optionmessage ofwishfor a button. - A widget that is deleted by its own command gives no crash.
invokechecksdestroyedafter the script. - The
Buttonrules of the modern theme: a background, a 1 pixel border, the radius variable, the padding6px 12px, a:hoverbackground, an:activebackground, a:focusoutline of 2 pixels in the focus ring color with an offset of 1 pixel, and a:disabledcolor and opacity. A second style classprimaryuses the accent color.
Tests
zig build test -Dtest-filter=button
Script tests with a Harness. .b is button .b -text OK -command {incr n} in a grid.
.b invoketwo times:nis2. The result ofinvokeis the result of the script.button .r -command {return 7}:.r invokegives7..b configure -state disabled; .b invokegives an empty string and does not changen..b invoke extragives the message ofwish.h.head.clickon the center of.b:ngoes up by 1.- A press on
.b, a move outside, and a release:ndoes not change. A press, a move outside, a move back, and a release:ngoes up by 1. - A press and a release on a disabled button:
ndoes not change. - A click gives the focus to
.b:focusgives.b. focus .bandh.head.press("space"):ngoes up by 1.bind Buttongives the six patterns.bind .b <1> breakand a click:ndoes not change.button .d -text X -command {destroy .d}and a click gives no crash, andwinfo exists .dgives0.- A command with an error, run by a click, calls
bgerrorone time. Run by.b invoke, it gives the error to the caller. - While button 1 is down on
.b, the widget has thepressedandhoverbits. After a move outside it has neither. .b configure -activebackground red: the computed background is red only with thehoverbit.
Golden images and scene dumps, with the modern theme, 260 by 80. Each image has three buttons in a row: Save, Cancel, and a primary button.
button-normalbutton-hover: the pointer is on the first button.button-pressed: button 1 is down on the first button.button-focus: the first button has the focus.button-disabled: the first button is disabled.
In this task a transition has no duration yet (task 18), so each image shows the end state.
Acceptance
- All tests pass.
- The reviewer opens the five images. Each state is clearly different from the normal state, the text is in the center, and the focus outline is complete on all four sides.
src/widgets/button.zighas no layout code and no drawing code thatlabelalso has.
Out of scope
checkbutton,radiobutton,-image,-compound,-underline,-default,-overrelief,-repeatdelay, andflash.ttk::button. Stage 6.- The
Returnkey. Tk does not bind it for a button.