Task 13: Focus
Goal
One widget has the keyboard focus. Key events go to it. focus sets and reads it. Tab and Shift-Tab move it. The focus state bit drives the style.
Files
src/focus.zig: the focus state, thefocus,tk_focusNext, andtk_focusPrevcommandssrc/dispatch.zig: key events, focus events of the windowsrc/cmds.zig: the registration, the bindings ofall
Interfaces
// src/focus.zig
pub const Focus = struct {
/// The widget with the focus in the toplevel, or null.
current: ?*Widget = null,
/// True when the platform window has the keyboard.
window_focused: bool = false,
};
/// Gives the focus to a widget. Sends FocusOut and FocusIn and changes
/// the state bits.
pub fn set(app: *App, w: ?*Widget) void;
/// The next widget in the traversal order that accepts the focus.
pub fn next(app: *App, w: *Widget) *Widget;
pub fn prev(app: *App, w: *Widget) *Widget;
/// True when a traversal can stop at the widget.
pub fn accepts(w: *const Widget) bool;
/// Clears the focus of a widget that is deleted.
pub fn forget(app: *App, w: *Widget) void;
Steps
- Read
doc/focus.nanddoc/focusNext.n. - The App has one
Focusfor.in Stage 1. focusreturns the path of the focus widget, or an empty string when the window does not have the keyboard.focus pathsets it.focus -force pathsets it too (Stage 1 cannot take the keyboard from another program).focus -displayof pathisfocus.focus -lastfor pathgives the focus widget of the toplevel of the path, also when the window does not have the keyboard, and the toplevel when no widget had the focus. A path that does not exist givesbad window path name ".zz".setdeliversFocusOutto the old widget andFocusInto the new one, changes thefocusstate bits, and sets the style flags.- A
focusevent of the backend withfocused == falsedeliversFocusOutto the focus widget and clears itsfocusbit.currentstays. Withfocused == true, it deliversFocusIntocurrent, or to the toplevel whencurrentis null. The toplevel gets thebackgroundstate bit while the window does not have the keyboard. - A
keyevent goes tocurrent, or to the toplevel whencurrentis null. It becomesKeyPressorKeyReleasewith the key symbol, the key code, the text, and the modifiers.xandyare the pointer position in the widget. - The traversal order is the order of
focusNext.n: the widget tree in depth-first order, children in stacking order, starting after the widget and going around through the toplevel.acceptsis true when the widget is mapped, is not disabled, and:-takefocusis1, or-takefocusis empty and the class hasfocusable, or the widget has a key binding on its own path.-takefocus0is false. A script value of-takefocusis Stage 2 and counts as an empty value. When no widget accepts,nextreturns the widget itself.
tk_focusNext pathandtk_focusPrev pathreturn the path.wishgives.efortk_focusNext .bwhen.eis the next entry.- The bindings of the tag
all:<Key-Tab>runs::ztk::priv::focus_move %W nextand<Shift-Key-Tab>and<Key-ISO_Left_Tab>run::ztk::priv::focus_move %W prev. The native command sets the focus to the next widget. Tk uses the virtual events<<NextWindow>>and<<PrevWindow>>for this. Stage 2 changes to them whenevent addexists. focus.forgetruns indestroy. When the deleted widget had the focus,currentbecomes null and no event runs.- The headless window starts with the keyboard (
window_focused = true).
Tests
zig build test -Dtest-filter=focus
Script tests with a Harness. .a, .b, and .c are frames with -takefocus 1, and .n is a frame with -takefocus 0, in a grid.
focus .a; focusgives.a.focus .zzgives the error.focus .a; focus .bruns<FocusOut>on.aand then<FocusIn>on.b..ahas thefocusstate bit, and aFrame:focusrule changes its style.h.head.press("x")runs a<Key>binding of the focus widget with%Kequal toxand%Aequal tox.- A key with no focus widget goes to
.. tk_focusNext .agives.b.tk_focusNext .cgives.a.tk_focusPrev .agives.c..nis never a result.h.head.press("Tab")moves the focus from.ato.b. Withshiftit moves back.- A widget binding for
<Key-Tab>withbreakstops the move. h.head.setFocused(false):focusgives an empty string,focus -lastfor .gives.b, and.has thebackgroundbit.setFocused(true)gives.bagain with one<FocusIn>.destroy .bwhile it has the focus:focus -lastfor .gives., and the next key goes to..- A disabled widget and a widget that is not mapped are not results of
tk_focusNext.
Acceptance
- All tests pass.
- The key path does not search the tree:
currentis a pointer.
Out of scope
grab,tk_focusFollowsMouse, and a script value of-takefocus.- Focus on a click. Each class binding does that (tasks 14 and 15).
:focus-visibleas a separate state.- More than one toplevel.