navml-components
How a navml component is put together and built -- the markup half and hand-written half, `*_nml.py` / `.pyi` generated artefacts, component directories and groups, `navml/_merge.py`'s splice and find
SKILL.md.claude/skills/navml-components/SKILL.md
GitHubnavml components and the toolchain
Four files, two optional halves
button.nml is the markup; button_nml.py is what the generator emits from it (tracked and shipped); button.py is
the hand-written handlers; button.pyi the generated stub. Markup alone, Python alone, and both are three peer shapes,
and from navml.widgets.dialog.button import Button is the same line for all three.
- A component that paints has two halves by construction. Markup declares and places; Python paints (
Labelgained alabel.pyfor this;field.nmlis the markup-only example). ConverselyCheckBoxes/RadioButtonslost their markup halves: a document holding nothing but a head says only what itsclassstatement says. - The merge is one line,
handwritten.__bases__ = (generated,); the generated class is always the base, so a hand-written__init__callssuper().__init__()and finds every id live. - The hand-written half never names the generated class --
class Button(Widget)is what a Python-only component says too. The loader refuses the pair if its base disagrees with the markup's (CPython would drop it silently). A class based only onobjectcannot be spliced (deallocator differs from 'object'). - The generated class always names
navml.component.Component(class Label(_Component),class FramedButton(Button, _Component)), which holds thelayout()override,__navml_source__, and the keyword-property constructor.Component { }is a live.nssselector matching every markup-built widget (type selectors match by class name); it reads built from markup, never is a component (spacer.pyis a component and does not match). - The generated class constructs children inline in
__init__, not a_build()method (a derived component's would override it). A derived component's own children land after its base's --Dialog.focusable()moves its buttons to the end for this. Component.built()runs once the whole tree exists.- Every generated
__init__ends withself._navml_built(__class__), which callsbuilt()only from the generated class nearest the instance's own. A derived document's children are there too. - It runs before the hand-written half's
__init__goes on pastsuper().__init__(). - Declared properties a caller passed are already set.
- Use it for work that needs every child but must not wait for mounting (
SettingsPageseeds its controls there).
- Every generated
A big hand-written half: mixins
- A hand-written half too big for one file is split by feature into mixins, each a
navkit.widget.Mixinin its own module in the component directory:class Manager(Encoding, Copying, ..., Views, Window)(navigator/widgets/manager/manager/). The merge keeps the leadingMixinbases and splices the generated class in after them,__bases__ = (*mixins, generated), so a mixin's method sits where a method of the class would. A leading base that is not aMixinis refused, as is a second widget base. - A mixin holds methods only.
Mixin.__init_subclass__refuses akeystable,checks/enables, and anyreactive/computed, which stay in the class itself, so state is read in one place. A syncon_*in a mixin is refused at import, as a widget's is. A constant only one mixin uses (FileEditor'sDRAW_KEYS) may live with it; a class-level default of instance state stays in the class. Zero-argumentsuper()works: the next class after a mixin is the next mixin, then the generated half. - A mixin is typed against the component's stub (its surface, for a Python-only class) with
if TYPE_CHECKING: from <package> import Manager as _Baseandelse: from navkit.widget import Mixin as _Base.class Copying(_Base)then seesself.leftand the other mixins' methods, with no cycle, because the stub flattens the mixins. navml/sibling.pyreads the mixins too, without importing them. A base shaped as a mixin, imported from a module of the same source tree (relative, or absolute under the same top-level package), is parsed, and its methods are folded into the class's own, each carryingoriginso messages name the right file, with its class-level names (a constant moved out with the methods using it). Its imports are merged in as well; a name two files bind differently is refused, and so is a mixin imported under a name the module already imports something else as (Shell's commandTerminal: the mixin isConsoleMixin, asCommandLineMixinis becauseCommandLineis a widget). So the handler-collision andawaitchecks see a method wherever it moved, and the.pyilists every mixin's methods as the class's own, keeping only the markup base. Editing a mixin's method or imports therefore makesnavml build --checkreport the stub stale, as editing the.pydoes.- Module-level helpers the mixins share go to a sibling module (
manager/manager/helpers.py), never into the component's.py: a mixin importing that module at import time would be a cycle. The component's module may re-export them where others import them from it (panel.panelstays the public face ofhuman_size,fit_text...), and a global a test patches on the module stays there with the code reading it (Panel's listing,SCAN_GRACE,_read_location). - What a stub says of the hand-written class (
.pyiand surface alike): everyself.name = ...a method of it or its mixins assigns, asname: T(_Anyunannotated), less what a live base already declares (Widget.width); a property as@property(with its setter, under the setter's own parameter names), so a class or mixin defining it is an override pyright takes;@staticmethod/@classmethodkept; and the imports of anif TYPE_CHECKING:block (not a mixin's_Base). Checked with pyright (./venv/bin/pyright FILES,[tool.pyright]inpyproject.toml): no errors in the stubs or from overrides; what remains is the code's own typing. - A Python-only class has a surface stub instead (
navml/surface.py):navml buildfinds a class in a module with no.nmlwhose bases include mixins, and writes<stem>_surface.pyibeside it -- the class with its own bases only, every method and class-level name of it and its mixins (assibling.pyfolds them), and what its module defines imported from it. A stub with no module of its own; its mixins sayif TYPE_CHECKING: from <module>_surface import <Class> as _Base, since typed against the class itself they would be a cycle (the class derives from them).--checkreports a stale one;tests/test_nml_build.pychecksnavkittoo. A mixin is recognised by shape --class X(_Base)or(Mixin)-- wherever in the same top-level package it is imported from (navkit.app.focus), so a real base from the tree (ContextMenuHost) is never folded in. - A Python-only class splits the same way (
FileEditor,FileViewer: nothing to merge), and so does navkit'sApplication, whose mixins live innavkit/app/and import what they share fromnavkit.applicationitself -- which therefore imports them just before its class, after everything they take (navkit/app/__init__.pyimportsnavkit.applicationfirst, so either may be imported first). Keep a method that reads a module global a test patches (REPEAT_DELAY,CLIPBOARD_ANSWER_TIMEOUT) in the class's own module. - A moved method may name nothing defined after the class it left -- a
NameErroronly when it runs (Shell'sUserScreenPeekmoved intoconsole_keys.pywith its one caller).tests/test_undefined_names.pyfails on it, for every module. - A mixin is not a type to a stylesheet.
navkit.stylesheet.type_namesleaves out every class that is a mixin (__navkit_mixin__in its own namespace), soLinks { }never matches the manager. Code that looks for "the first class in the MRO that is not this one" must skip mixins too (keybindings.Section.base).
A component is a directory; components come in groups
- The files live in
navml/widgets/dialog/button/beside an__init__.pyre-exporting the class (and any event it declares). Files repeat the directory's name (packaging/linux/build.shfinds siblings by stripping_nml.py). Every component gets a directory, Python-only ones too. The flat shape stays legal (tests/test_nml_build.py'spackagefixture is flat on purpose). - Groups: layouts under
navml/widgets/layout/, the Dialogs components undernavml/widgets/dialog/, menus undermenu/;Window,Desktop,Timer,Spacerat the top. Navigator groups the same way (shell/,manager/,file_ops/,tree/,viewer/,editor/). A group's__init__.pyis a docstring and never re-exports;_COMPONENTS(navml) /_WIDGETS(navigator) map a name to its dotted path ("dialog.button"), sofrom navml.widgets import Buttonis unchanged. - The library registers, not the component:
navml.register("navml.widgets")once;_registered()walks up the dotted name. A component package registers itself in its__init__.py(not a.pth: the.deb/.rpmusePYTHONPATH). - A package
__init__.pymust not re-export eagerly -- generating a component imports the ones it uses, so eager re-export would import everything.navml/widgets/__init__.pyuses a PEP 562__getattr__with aTYPE_CHECKINGblock. - Silent failure modes fixed once:
build.order()keys its graph on the directory name as well as the module's, andbuild._forget()evicts the component's package with its module.
The finder
- It keys on
button_nml.py, neverbutton.nml, so both halves are ordinary.pyfiles that reach a wheel. The markup ships anyway via one"*" = ["*.nml", "*.pyi"]entry in[tool.setuptools.package-data]. - A component is a module, never a package; the finder refuses to claim a package (a stale flat
button_nml.pybeside abutton/directory would otherwise splice onto an old base). - A component module must never be a test module,
conftest.pyor pytest plugin -- pytest's assertion rewriter bypassessys.meta_path, so the splice would not happen.
The parser and the generator
- The parser imports nothing the document names. Every check a document can fail alone is
navml/parser.py's (structure, identifiers, reserved words, self-collisions); every check needing a live class is the generator's.parse(text)/parse_file(path)return aDocument;imports_of(path)reads only the import block, which orders a cold build without executing anything.navml/errors.py'sMarkupErroris the one exception both raise. - The generator is seven modules in a one-way chain:
expression.pycompiles an expression or handler body by rewriting free names on the syntax tree;sibling.pyreads the hand-written.pywithout importing it;resolve.pyis the only module importing what a document names;checks.pyrefuses what a live class reveals;generator.pyandstubs.pyemit the two artefacts (surface.pya split Python-only class's stub);build.pyorders a directory by import graph and writes.navml/_alias.pyis run-time support besidecomponent.py. - Anything emitting code goes through
navml/coder.py'sCoder(line buffer, logical indents, trailing comments), never string assembly or whole-moduleast.unparse--astcarries no comments and the source map is comments (# button.nml:12). - The generated file never reads the sibling
.pyto decide what to emit, so there is no--checkdrift. - A model document takes a separate path:
build.compile_documentasksnavml.model.is_model(anyfieldline) and hands it tonavml/model.py. That module reusesresolve._namespace,Sibling,Coderand the merge, but not the widget resolver, checks or generator. Models live in their own library,navml/models/, registered likenavml.widgets. - navkit grew for navml: a public
Declaration,Binding.owned_by(owner),stylesheet.check_declarations(text, *, line, filename).
Read when
| Reference | Read when |
|---|---|
reference/overview.md |
where navml's inspiration comes from (QML, Kivy) |
reference/two-halves.md |
the two halves, directories, groups, deriving, the import machinery, shipping markup |
reference/shared-base.md |
Component, how a value gets in, no required ctor args, navigated properties, a component's sheet |
reference/parser.md |
the parser: comments, continuation, root id, node graph |
reference/generator.md |
what the generator emits, source mapping, converting Manager, what writing it settled |
reference/still-open.md |
open questions for the language/toolchain |
reference/appendix-transformer.md |
the expression transformer prototype |