ext-slate · PHP API
Text input
TextField: one line of Latin-1 text the user types on the on-screen keyboard, with a placeholder, a blinking cursor, a horizontal scroll and a password mode with a reveal key. One field at a time has the focus.
A TextField is a control for one line of text: a name, a Wi-Fi password, a search. Like every
control it is one PHP object whose parts live in the compositor, it draws itself with the theme and it
goes in a scene, a group, a Card or a Scroll. TextField is not final.
<?php
use Baremetal\Slate\{Slate, Scene, TextField};
$slate = Slate::onboard();
$name = new TextField(24, 60, 312, placeholder: 'Device name', maxLength: 24);
$pass = new TextField(24, 140, 312, placeholder: 'Wi-Fi password', password: true, reveal: true);
$name->changed(fn (string $text) => preview($text));
$pass->submitted(fn (string $text) => wifi_join($text));
$slate->show((new Scene())->add($name, $pass));
$name->focus();
TextField
TextField::__construct(int|float $x, int|float $y, int $w, string $text = '', string $placeholder = '', int $maxLength = 64, bool $password = false, bool $reveal = false)
- The look.
$x,$yare the top-left; the field is$w(80 or more) × 54. A well (well, a 1.5 px border inborder, the theme's$radius) with the text in the theme'sfontKey12 px from the left, centred in the height. While the text is empty the placeholder shows intextMuted.$wand$hread the size and cannot be written. - The text is printable Latin-1: U+0020 to U+007E and U+00A0 to U+00FF, the letters and signs of the western
European languages (
é,ñ,ß,£,°). PHP holds it as UTF-8;$maxLength(1 to 256) counts characters, not bytes. A character outside that range (a tab,€, an emoji) is refused: the constructor and a write of$textthrow, the keyboard does not type it. - The cursor.
$cursorcounts characters from the start, 0 to the text's length. A write of$textputs it at the end. A text wider than the field scrolls sideways so the cursor stays 12 px inside the field. - Password. With
$passwordthe text shows as dots, one every 14 px. With$revealas well an eye key (44 px) at the right of the field shows and hides the text; a tap on it gives the field the focus when it lacks it. The text shows only while the field has the focus: it is hidden again when the field loses it. The dots cover fields up to about 1600 px wide (a control has at most 127 parts); a wider password field shows no dots past that.
The focus
One field at a time has the focus ($focused): it gets what the on-screen keyboard types. With the focus the border
turns accent, a ring of accent at 12 % shows 4 px around the field (both in 150 ms) and a 2 px cursor in accent
blinks every 500 ms.
focus()gives the focus to the field: it must be in the scene shown, visible with all its groups and enabled and, while aDialogis open, inside it; else it throws\Error. The field that had the focus loses it (its change held back goes out first). A tap on a field without the focus does the same, while the keyboard is closed. With the focus, a tap places the cursor at the nearest character boundary.- The focus goes at
blur(), after Enter (onSubmit()), at a tap outside the keyboard (onScreenShape::Rect) or on its × key (onScreenShape::Round), when the field (or a group it is in) is hidden, removed or disabled ($enabled = false), when aDialogopens over it (a field inside the dialog keeps it), when another scene is shown and when the Slate stops.
Notifications
Each change the user types calls onChange(string $text), then the callback of changed() (both are
Control's). They run at most once per frame of the compositor: a second change within the
same frame is held back and goes out 20 ms later, at the next change after a frame, before onSubmit() and when the
field loses the focus (not when another scene is shown: the field is gone from the screen). Enter calls
onSubmit(string $text), then the callback of submitted(), and the field loses the focus. A write from your code
($text, $cursor, $maxLength, $password, $reveal, $placeholder) redraws the field and calls no onChange().
| Member | Type | Meaning |
|---|---|---|
$text |
string |
The text (printable Latin-1, UTF-8); a write puts the cursor at the end. |
$placeholder |
string |
Shown in textMuted while the text is empty. |
$maxLength |
int |
The most characters, 1..256, not less than the text's length. |
$cursor |
int |
The cursor in characters, 0..the text's length. |
$password |
bool |
The text shown as dots. |
$reveal |
bool |
With $password: the eye key that shows and hides the text. |
$focused |
bool |
Read only: the field has the focus. |
focus(), blur() |
method | Give and take the focus; both return the field. |
onSubmit(string $text) |
method | Enter; empty, override it. |
submitted(?\Closure $fn) |
method | The callback of Enter, after onSubmit() (one; null removes it). |
The on-screen keyboard
The field with the focus gets Slate's on-screen keyboard: there is no class for it. It opens when a field takes the
focus and closes when no field has it. It is drawn above the scene, its dialogs and the toasts, in the theme's colours:
the keys in key (Enter in accent), their labels in fontKey, keyH high and keyGap apart (see
Theme).
<?php
use Baremetal\Slate\{Slate, Scene, TextField, Theme, ScreenShape};
$slate = Slate::onboard();
$slate->theme(new Theme(shape: ScreenShape::Round, keyH: 44));
$ssid = new TextField(40, 150, 280, placeholder: 'SSID');
$ssid->submitted(fn (string $text) => save_ssid($text));
$slate->show((new Scene())->add($ssid));
$ssid->focus(); // the keyboard fades in over the whole screen
ScreenShape::Rect. A panel4 × keyH + 5 × keyGaphigh slides up from the bottom of the screen in 250 ms. When it would cover the field, the scene shown (with its open dialogs) slides up with it, so that the field ends 8 px above the panel (never so far that the field's top leaves the screen); it slides back when the keyboard closes. ChoosekeyHandkeyGapso that the panel leaves room above it for a field: the screen's height less4 × keyH + 5 × keyGapshould be at least 66 px (the field's 54, its 4 px ring, 8 px of margin); else the field ends partly under the keyboard. A toast (Slate::toast()) shown while the keyboard is open is drawn under it: the panel covers it until the keyboard closes. A tap inside the panel is the keyboard's; a tap on the field places the cursor; a tap anywhere else takes the focus away (blur()) and reaches nothing else, not even another field: a second tap focuses it.ScreenShape::Round. The keyboard fades in over the whole screen in 200 ms: a copy of the field at the top (its text or dots, its placeholder and its blinking cursor) with an × key that takes the focus away, and the rows of keys cut to the circle. Every touch is the keyboard's while it is open: nothing reaches the scene.- Pages. Letters (
qwerty,,,.), numbers and signs (?123), symbols (#+=, with£,¥,§). The page keys switch between them; the keyboard always opens on the letters. - Shift. A tap: the next letter is uppercase, then the shift goes off. Two taps within 300 ms: locked (a bar under the arrow) until a third tap. A page key turns it off.
- Accents. Hold
a,e,i,o,u,y,c,norsfor 400 ms: a row of their Latin-1 variants opens above the key (à á â ä ã å æ,è é ê ë,ç,ñ,ß...; uppercase with the shift). Slide to one and lift the finger to type it; lift it where it is, or anywhere outside the row, to type nothing. - Typing. The key under the finger is pressed, and the one under it when the finger lifts is typed: slide to
correct a miss. Backspace deletes at once and again every 80 ms after 400 ms while the finger stays on it. Enter
calls
onSubmit()andsubmitted(), then the field loses the focus and the keyboard closes. - Closing. It closes with the focus (animated); at once when another scene is shown or the Slate stops.
There is no numeric-only layout, no magnified preview of the key pressed and no physical keyboard support.
ScreenShape::Round needs a screen of at least 360 px: on 360 x 360 the narrowest keys (, and .) are 16 px wide,
and on a smaller round screen the keys of the bottom rows get too narrow to hit. The 06-display/slate-controls-2
example types a Wi-Fi password on the round keyboard of a 360 x 360 panel.
The keyboard is built the first time a field takes the focus, and stays until the Slate stops: about 100 compositor
elements on Rect, about 120 on Round (about 26 KB of external RAM on the board, measured on Round at
360 x 360). A closed keyboard is hidden and costs nothing per frame. On Round its fade covers the whole screen:
about 38 ms a frame for its 200 ms (see Performance).
What a field costs
A still screen costs nothing in Slate, with one declared exception: the cursor of the field that has the focus.
It blinks on the controls' timer, twice a second, and each blink redraws only the cursor's 2 px rectangle; no tween
runs and nothing else is composed. A field without the focus costs nothing while nothing changes. Call blur() (or
let the field lose the focus) when the user is done, and the screen is still again.
Errors
| When | Throws | Message |
|---|---|---|
$w outside 80..32767 |
\ValueError |
Baremetal\Slate\TextField::$w must be between 80 and 32767 |
$maxLength outside 1..256 (constructor or write) |
\ValueError |
Baremetal\Slate\TextField::$maxLength must be between 1 and 256 |
$maxLength written below the text's length |
\ValueError |
Baremetal\Slate\TextField::$maxLength must be at least the text's length (5) |
$text with a character that is not printable Latin-1 (constructor or write) |
\ValueError |
Baremetal\Slate\TextField::$text must hold only printable Latin-1 characters (U+0020-U+007E, U+00A0-U+00FF) |
$text longer than $maxLength |
\ValueError |
Baremetal\Slate\TextField::$text must be at most 5 characters |
$cursor outside 0..the text's length |
\ValueError |
Baremetal\Slate\TextField::$cursor must be between 0 and 5 |
focus() of a field not in the scene shown, hidden (or in a hidden group), disabled or under an open Dialog |
\Error |
Baremetal\Slate\TextField::focus() needs the field in the scene shown |
A write of $focused |
\Error |
Cannot modify readonly property Baremetal\Slate\TextField::$focused |
A write that throws leaves the old value. TextField is built with the slate-controls build option, like every
control.
Related
- Controls API
- Theme API
- Performance API