FindBar: a plain integrated field (no clear button) and a sliders options icon; TextField::clear_button (trial)

This commit is contained in:
Dylan M. Taylor
2026-11-22 21:04:25 -05:00
parent 4a0cbec2c3
commit d02b071002
3 changed files with 29 additions and 3 deletions
+2 -1
View File
@@ -178,6 +178,7 @@ impl<'a> FindBar<'a> {
}
Row::new().show(ui, |ui, _| {
let edit = TextField::search(words.find_text.clone(), Bind::given(query.clone()))
.clear_button(false)
.placeholder(words.find_text.clone())
.select_all(to_query)
.show(ui);
@@ -216,7 +217,7 @@ impl<'a> FindBar<'a> {
*open = !*open;
}
let active = options.match_case || options.whole_word;
let button = IconButton::new(IconId::new("settings")).label(words.options.clone()).popup(PopupKind::Menu).selected(active).show(ui);
let button = IconButton::new(IconId::new("sliders-horizontal")).label(words.options.clone()).popup(PopupKind::Menu).selected(active).show(ui);
let entries = [
Entry::new(BarOption::WholeWords, words.whole_words_only.clone()).checked(options.whole_word),
Entry::new(BarOption::MatchCase, words.case_sensitive.clone()).checked(options.match_case),
+14
View File
@@ -118,3 +118,17 @@ fn asking_for_the_bar_again_selects_its_query_unless_a_composition_runs() {
bar.fixture.type_text("dog").settle();
assert_eq!(bar.query(), "dog", "after the commit the next request selects the query again");
}
// Production's find field is a plain integrated one: no clear button beside the text, and Escape still clears it.
#[trial]
fn the_find_field_has_no_clear_button_and_escape_clears_it() {
let mut bar = Bar::new();
bar.ask();
bar.fixture.type_text("cat");
bar.fixture.frame(Vec::new()).settle();
assert_eq!(bar.query(), "cat");
assert!(bar.fixture.by_label("Clear").is_none(), "no clear button beside the text");
bar.fixture.key(Key::Escape, Modifiers::NONE);
bar.fixture.frame(Vec::new()).settle();
assert_eq!(bar.query(), "", "Escape clears the query");
}
+13 -2
View File
@@ -62,6 +62,8 @@ pub struct TextField<'a, C: Codec + Clone + 'static = PlainText> {
pub(super) empty: Option<C::Value>,
/// The search form: a clear part while there is text, and Escape clears the text.
search: bool,
/// Whether the search form shows its clear button (on); Escape still clears the text without it.
clear_button: bool,
/// The adornments before the text, in reading order.
leading: Vec<Adornment>,
/// The field reserves and paints a chevron at the end, for the host's popup.
@@ -103,6 +105,7 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> {
limit: Limit::Default,
empty: None,
search: false,
clear_button: true,
leading: Vec::new(),
arrow: false,
drives: None,
@@ -110,6 +113,14 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> {
.label(label)
}
/// Whether a search field shows its clear button while there is text. Off, the field is a plain integrated one
/// (a find bar's): Escape still clears it.
#[must_use]
pub fn clear_button(mut self, on: bool) -> Self {
self.clear_button = on;
self
}
/// Shown while the text is empty.
#[must_use]
pub fn placeholder(mut self, placeholder: impl Into<Label>) -> Self {
@@ -254,7 +265,7 @@ impl<'a, C: Codec + Clone + 'static> TextField<'a, C> {
impl<C: Codec + Clone + 'static> TextField<'_, C> {
/// The clear part shows while the field has text.
fn clear_shown(&self, value: Option<&C::Value>) -> bool {
self.search && value.is_some_and(|value| !self.codec.format(value).is_empty())
self.search && self.clear_button && value.is_some_and(|value| !self.codec.format(value).is_empty())
}
/// The adornments after the text, in reading order: the clear button when `clear` names it, then the arrow.
@@ -310,7 +321,7 @@ impl<C: Codec + Clone + 'static> Component for TextField<'_, C> {
if cx.proposal == Proposal::Min {
size.x = 0.0;
}
let clear = self.search.then(|| Self::clear_name(&cx.words::<words::TextWords>()));
let clear = (self.search && self.clear_button).then(|| Self::clear_name(&cx.words::<words::TextWords>()));
self.leading = std::mem::take(&mut self.leading).into_iter().map(|adornment| adornment.measured(cx)).collect();
size.x += adorn::reserve(cx.style, &self.leading, &self.trailing(clear));
// Short text keeps a field at `size.length.short`, so a column of fields ends at one edge; a content-sized field