pub struct TextInput<M> { /* private fields */ }Expand description
A single-line text field with a caret.
§Selecting
Taking focus selects everything, which is what makes Tab-and-type replace a setpoint rather than append to it. A press places the caret, a second on the same spot takes the word under it, a third takes the whole field, and dragging — with a mouse or a finger — extends from where the press landed. Shift extends with the arrows, Home and End; ⌘A or Ctrl+A takes everything. Typing, Backspace and Delete replace what is selected.
§The clipboard
⌘C, ⌘X and ⌘V — or Ctrl — when the application has said where they go with
with_clipboard. The widget has no clipboard to
reach, so it asks: copy and cut hand over the text, paste emits
ClipboardRequest::Paste and the application answers with
insert_text. Without that wiring the three keys do
nothing at all, which is what a panel with no clipboard wants.
A password field refuses copy and cut. Its whole job is that what is on the screen cannot be read, and handing the value to the system clipboard would undo that for one keystroke. Pasting into one is still allowed.
§Moving by word
Ctrl and an arrow move by word, and so does Option on a Mac — the widget cannot ask which keyboard it is in front of, so it takes both spellings. Command and an arrow go to the start or the end, which is what Command does on the machine that has it and what Home and End do everywhere. Shift extends with all of them.
§What it still does not do
No undo. A kiosk field takes a name, a PIN or a setpoint;
TextArea is where an editor’s machinery lives.
§Blinking
The caret blinks only while the field has focus: taking focus requests
animation, and losing it makes Widget::animate answer None, which is
how a widget hands the CPU back. An unfocused panel therefore has nothing
running on a timer at all — the difference between a device that idles and
one that keeps a core awake for its whole service life. Typing resets the
phase so the caret stays solid while it is moving.
A blink damages the whole field rather than the caret, because
Widget::animate reports that something changed, not where. On a Pi 3
that is 26 kpx twice a second — 58 µs, or 0.35% of one 60 Hz frame — against
the 32 px the caret actually occupies. The 800× coarseness is real and the
cost of removing it is a wider trait; the measurement is why it has not been
paid.
Implementations§
Source§impl<M> TextInput<M>
impl<M> TextInput<M>
Sourcepub fn with_placeholder(self, placeholder: impl Into<String>) -> Self
pub fn with_placeholder(self, placeholder: impl Into<String>) -> Self
Sets the text shown when the field is empty.
Sourcepub fn with_submit(self, message: M) -> Self
pub fn with_submit(self, message: M) -> Self
Sets the message emitted when Enter is pressed.
Sourcepub fn with_clipboard(self, message: fn(ClipboardRequest) -> M) -> Self
pub fn with_clipboard(self, message: fn(ClipboardRequest) -> M) -> Self
Wires copy, cut and paste to the application, which owns the clipboard.
Without this the three keys do nothing: a panel with no window system has nowhere to copy to, and a widget that pretended otherwise would be lying about where the text went.
Sourcepub fn with_max_chars(self, max: usize) -> Self
pub fn with_max_chars(self, max: usize) -> Self
Caps the number of characters the field will hold.
Sourcepub fn with_style(self, style: TextStyle) -> Self
pub fn with_style(self, style: TextStyle) -> Self
Sets the font and size.
Sourcepub fn with_password(self, password: bool) -> Self
pub fn with_password(self, password: bool) -> Self
Draws every character as *. The text is still stored in the clear —
this hides a PIN from someone standing behind the panel, and nothing more.
Sourcepub fn set_text(&mut self, text: impl Into<String>)
pub fn set_text(&mut self, text: impl Into<String>)
Replaces the contents, putting the caret at the end.
Sourcepub fn insert_text(&mut self, text: &str)
pub fn insert_text(&mut self, text: &str)
Inserts text at the caret, replacing the selection: the answer to
ClipboardRequest::Paste.
A field is one line, so this takes what it is given up to the first line break and drops the rest, along with any other control characters. Pasting three lines into a setpoint has no meaning a widget could guess at, and joining them into one would invent a value nobody copied.
What is left is truncated to fit max_chars rather than refused: a
paste one character too long is still mostly what somebody wanted.
Sourcepub fn selection(&self) -> Option<(usize, usize)>
pub fn selection(&self) -> Option<(usize, usize)>
The selection as character indices, low end first, or None when there
is only a caret.
Sourcepub fn selected_text(&self) -> Option<&str>
pub fn selected_text(&self) -> Option<&str>
The selected text, or None when nothing is selected.
Sourcepub fn select_all(&mut self)
pub fn select_all(&mut self)
Selects everything, with the caret at the end.
Sourcepub fn select_range(&mut self, from: usize, to: usize)
pub fn select_range(&mut self, from: usize, to: usize)
Selects from..to, clamped to the text, with the caret at to.
The pair may be given either way round: the caret lands on to, which is
the end a further Shift-arrow moves.
Sourcepub fn clear_selection(&mut self)
pub fn clear_selection(&mut self)
Drops the selection, leaving the caret where it is.
Sourcepub fn caret_x(&self, engine: &mut TextEngine, bounds: Rect) -> i32
pub fn caret_x(&self, engine: &mut TextEngine, bounds: Rect) -> i32
Horizontal offset of the caret from the field’s left edge.
Measured through the engine rather than counted as characters times a width, which is the only thing that works with a proportional font.
Trait Implementations§
Source§impl<M> Describe for TextInput<M>
impl<M> Describe for TextInput<M>
Source§const KIND: &'static str = "text-input"
const KIND: &'static str = "text-input"
Source§const DOC: &'static str = "A line of text somebody types into."
const DOC: &'static str = "A line of text somebody types into."
Source§const ICON: &'static Icon
const ICON: &'static Icon
Source§const PROPERTIES: &'static [Property]
const PROPERTIES: &'static [Property]
Source§impl<M: Clone + 'static> Widget<M> for TextInput<M>
impl<M: Clone + 'static> Widget<M> for TextInput<M>
Source§fn snap(&mut self, now_ms: u64) -> Animation
fn snap(&mut self, now_ms: u64) -> Animation
Blinking is a schedule, so it survives Motion::None
unchanged — there is nothing to land, and stopping it would be a
regression dressed up as a preference.
Source§fn describe(&self) -> Option<&dyn DynDescribe>
fn describe(&self) -> Option<&dyn DynDescribe>
Source§fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
fn describe_mut(&mut self) -> Option<&mut dyn DynDescribe>
describe.Source§fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
fn measure(&self, ctx: &mut MeasureCtx<'_>, _offered: Offer) -> Measured
Source§fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
fn paint(&self, ctx: &mut PaintCtx<'_>, canvas: &mut Pen<'_>)
canvas, which is already clipped to this widget’s bounds
intersected with the damage region being repainted. Read moreSource§fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
fn on_event(&mut self, event: &Event<'_>, ctx: &mut EventCtx<'_, M>) -> Handled
Source§fn accepts_pointer(&self) -> bool
fn accepts_pointer(&self) -> bool
true if the pointer can hit this widget. Read moreSource§fn animate(&mut self, now_ms: u64) -> Animation
fn animate(&mut self, now_ms: u64) -> Animation
EventCtx::request_animation — and stops being called
the moment it answers Wake::Never. Read moreSource§fn preserves_focus(&self) -> bool
fn preserves_focus(&self) -> bool
true if a press on this widget should leave focus exactly where
it is. Read moreAuto Trait Implementations§
impl<M> Freeze for TextInput<M>
impl<M> RefUnwindSafe for TextInput<M>
impl<M> Send for TextInput<M>
impl<M> Sync for TextInput<M>
impl<M> Unpin for TextInput<M>
impl<M> UnsafeUnpin for TextInput<M>
impl<M> UnwindSafe for TextInput<M>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> DynDescribe for Twhere
T: Describe,
impl<T> DynDescribe for Twhere
T: Describe,
Source§fn kind(&self) -> &'static str
fn kind(&self) -> &'static str
Describe::KIND.Source§fn properties(&self) -> &'static [Property]
fn properties(&self) -> &'static [Property]
Describe::PROPERTIES.Source§fn get_property(&self, name: &str) -> Option<Value>
fn get_property(&self, name: &str) -> Option<Value>
Describe::get.Source§fn set_property(
&mut self,
name: &str,
value: Value,
) -> Result<(), PropertyError>
fn set_property( &mut self, name: &str, value: Value, ) -> Result<(), PropertyError>
Describe::set.