Input
A text field in four sizes, with a placeholder, a prefix and a suffix, a clear button, a password toggle, and disabled, read-only, invalid and loading states. InputGroup joins text and buttons to it. An InputStyle draws the frame of every kind of field.
Default
ui.add(Input::new(&mut state.name).cleanable(true));
ui.add(Input::new(&mut state.email).placeholder("Enter text here..."));
States
ui.add(Input::new(&mut state.disabled).disabled(true));
ui.add(Input::new(&mut state.readonly).readonly(true));
ui.add(
Input::new(&mut state.password)
.mask_toggle()
.cleanable(true)
.placeholder("Enter your password..."),
);
Search
ui.add(Input::search(&mut state.search).placeholder("Search tracks"));
Alignment
ui.add(
Input::new(&mut state.centered)
.align(egui::Align::Center)
.placeholder("Centred"),
);
ui.add(
Input::new(&mut state.right)
.align(egui::Align::RIGHT)
.placeholder("Aligned to the end"),
);
// Right-to-left text aligns to the right on its own.
ui.add(Input::new(&mut state.hebrew));
Prefix and suffix
ui.add(
Input::new(&mut state.prefixed)
.start_text("https://")
.placeholder("example.com")
.cleanable(true),
);
ui.add(
Input::new(&mut state.both)
.start_text("$")
.end_text("USD")
.placeholder("0.00"),
);
ui.add(Input::new(&mut state.checking).loading(true));
Validation
let valid = state.email_check.contains('@') && state.email_check.contains('.');
ui.add(Input::new(&mut state.email_check).invalid(!valid));
Clear on Escape
ui.add(Input::new(&mut state.clear_on_escape).clear_on_escape(true));
Inline
// A value typed in place: a line shows under it on hover and focus.
let inline = &input_styles::Inline;
ui.add(Input::new(&mut state.inline_name).style(inline));
ui.horizontal(|ui| {
ui.label("Played at");
ui.add(
Input::new(&mut state.inline_place)
.style(inline)
.fit_width()
.placeholder("venue"),
);
});
Sizes
let [xsmall, small, medium, large] = &mut state.sizes;
ui.add(Input::new(xsmall).xsmall().placeholder("Extra small"));
ui.add(Input::new(small).small().placeholder("Small"));
ui.add(Input::new(medium).placeholder("Medium"));
ui.add(Input::new(large).large().placeholder("Large"));
Input group: text
InputGroup::new()
.start_text("https://")
.end_text(".com")
.show(ui, Input::new(&mut state.site).placeholder("example"));
InputGroup::new()
.start_text("$")
.end_text("USD")
.show(ui, Input::new(&mut state.amount).placeholder("0.00"));
Input group: buttons
let searches = &mut state.searches;
InputGroup::new()
.end(|ui| {
if ui.add(Button::ghost("Search").xsmall()).clicked() {
*searches += 1;
}
})
.show(
ui,
Input::new(&mut state.query).placeholder("Type to search..."),
);
ui.label(format!("Searched {} times", state.searches));
let searching = !state.command.is_empty();
InputGroup::new()
.end(|ui| {
ui.add(Kbd::new("⌘K"));
})
.show(
ui,
Input::new(&mut state.command)
.placeholder("Search commands")
.loading(searching),
);
Input group: validation and disabled
InputGroup::new()
.start_text("https://")
.invalid(true)
.show(ui, Input::new(&mut state.group_invalid));
InputGroup::new()
.start_text("https://")
.disabled(true)
.show(ui, Input::new(&mut state.group_disabled));
Input group: text area toolbar
let count = state.note.chars().count();
InputGroup::new()
.bottom(|ui| {
ui.add(Button::ghost("Attach").xsmall());
ui.with_layout(egui::Layout::right_to_left(egui::Align::Center), |ui| {
ui.add(Button::primary("Send").xsmall().disabled(count == 0));
});
})
.size(Size::Medium)
.show_textarea(
ui,
Textarea::new(&mut state.note)
.auto_grow(2, 6)
.placeholder("Write a note..."),
);
Styles
let [bordered, inline, plain] = &mut state.styled;
ui.add(Input::new(bordered).style(&input_styles::Bordered));
ui.add(Input::new(inline).style(&input_styles::Inline));
ui.add(Input::new(plain).style(&input_styles::Plain));
A style for a whole area
// Every field inside draws its frame with the style and takes
// the size, unless it picks its own: text fields, text areas,
// number fields and one-time codes alike.
let [name, notes] = &mut state.area;
ui.control_size(Size::Small, |ui| {
ui.input_style(input_styles::Inline, |ui| {
ui.add(Input::new(name).placeholder("Name"));
ui.add(Textarea::new(notes).rows(2).placeholder("Notes"));
ui.add(NumberInput::new(&mut state.area_number).end_text("BPM"));
ui.add(OtpInput::new(&mut state.area_code, 4));
});
});
Your own style
let [name, empty, email] = &mut state.own;
ui.input_style(Well, |ui| {
ui.add(Input::new(name).cleanable(true));
ui.add(Input::search(empty).placeholder("Search the library"));
let valid = email.contains('@') && email.contains('.');
ui.add(Input::new(email).invalid(!valid));
});
/// A soft well, lit while it has the keyboard, with a line under it in the
/// ring colour, or the danger colour when the value is invalid.
struct Well;
impl InputStyle for Well {
fn paint(&self, ui: &egui::Ui, field: &InputConfiguration<'_>) {
let theme = tokens(ui.ctx());
let s = field.state;
let fill = if s.focused { theme.accent } else { theme.muted };
let rect = field.rect;
field.paint_behind(egui::Shape::rect_filled(rect, field.corner_radius, fill));
let line = if s.invalid {
Some(theme.danger.background)
} else if s.focused {
Some(theme.ring)
} else {
None
};
if let Some(line) = line {
let stroke = egui::Stroke::new(2.0, line);
field
.painter()
.hline(rect.x_range().shrink(4.0), rect.bottom() - 1.0, stroke);
}
}
}
The story above is gallery/src/stories/input.rs, running in your browser. Every snippet on this page is cut from it.