Skip to main content

hyprforge_ui/widgets/
field.rs

1//! Text fields that sit recessed into a bar: search, a path, a filter.
2
3use crate::density;
4use crate::theme::{self, spacing, surface, FontScale};
5use iced::widget::{container, row, text_input, Container};
6use iced::Length;
7
8/// The look every inset field shares: recessed into the bar, outlined.
9///
10/// One function rather than identical closures, so Files' path bar, its
11/// search field and Settings' search cannot drift apart — they are
12/// siblings by construction, not by someone remembering to keep them
13/// matched. Also the look a dropdown's closed state copies, so a picker
14/// beside a text field reads as the same kind of control.
15pub fn inset_field_style(_t: &iced::Theme) -> container::Style {
16    container::Style {
17        background: Some(iced::Background::Color(surface::card())),
18        border: iced::Border {
19            radius: density::inner_radius().into(),
20            width: 1.0,
21            color: surface::card_border(),
22        },
23        ..container::Style::default()
24    }
25}
26
27/// A free-standing text input's look — a setting's value, a filter —
28/// matching the inset fields and the dropdowns beside it, rather than
29/// iced's stock input with its bright outline.
30///
31/// Focus outlines in the dim text colour, like an open dropdown, not the
32/// accent: typing into a field is not a selection.
33pub fn inset_input_style(t: &iced::Theme, status: text_input::Status) -> text_input::Style {
34    let border_color = match status {
35        text_input::Status::Focused { .. } | text_input::Status::Hovered => theme::text_dim(),
36        _ => surface::card_border(),
37    };
38    text_input::Style {
39        background: iced::Background::Color(surface::card()),
40        border: iced::Border {
41            radius: density::inner_radius().into(),
42            width: 1.0,
43            color: border_color,
44        },
45        icon: theme::text_dim(),
46        placeholder: theme::text_dim(),
47        value: theme::text(),
48        selection: t.extended_palette().primary.weak.color,
49    }
50}
51
52/// The magnifier ring's diameter at 100% scale.
53const SEARCH_RING_BASE: f32 = 9.0;
54
55/// A search field: a drawn magnifier ring and a transparent input inside
56/// an inset container, one field tall.
57///
58/// Returns the container so the caller decides the width — Files holds
59/// its search to a fixed width so the path bar flexes, where Settings'
60/// header search is the one thing that does.
61///
62/// `placeholder` should name the scope — "Search crates", "Search any
63/// setting or config key" — so what will be searched is stated before
64/// anything is typed, rather than discovered afterwards.
65///
66/// The magnifier is a sibling widget rather than the input's own `icon`,
67/// because iced's `text_input::Icon` takes its glyph from a `Font` and
68/// this suite ships none — the theme's font is whatever the desktop
69/// chose, and it may have no magnifier at all. A drawn ring always
70/// renders.
71pub fn search_field<'a, Message: Clone + 'a>(
72    placeholder: &str,
73    value: &str,
74    on_input: impl Fn(String) -> Message + 'a,
75    on_submit: Option<Message>,
76    id: Option<iced::widget::Id>,
77    scale: FontScale,
78) -> Container<'a, Message> {
79    token_field(placeholder, value, Vec::new(), on_input, on_submit, id, scale)
80}
81
82/// A [`search_field`] holding finished tokens ahead of the text — Files'
83/// `ext:rs` and `modified:<7d` chips, each a [`super::removable_chip`].
84///
85/// The tokens sit *inside* the field, between the magnifier and the
86/// cursor, because they are part of what was typed: a row of chips off
87/// to one side would read as a second control with a meaning of its own.
88/// The caller decides which text became a token and what removing one
89/// does; this only draws them. With no tokens it is exactly
90/// `search_field`, which is built on it so the two cannot drift apart.
91///
92/// The placeholder is dropped once a token is in: "Search crates" after
93/// `ext:rs` would read as though the token were not part of the search.
94pub fn token_field<'a, Message: Clone + 'a>(
95    placeholder: &str,
96    value: &str,
97    tokens: Vec<iced::Element<'a, Message>>,
98    on_input: impl Fn(String) -> Message + 'a,
99    on_submit: Option<Message>,
100    id: Option<iced::widget::Id>,
101    scale: FontScale,
102) -> Container<'a, Message> {
103    let ring_side = scale.apply(SEARCH_RING_BASE);
104    let ring = container(iced::widget::Space::new())
105        .width(Length::Fixed(ring_side))
106        .height(Length::Fixed(ring_side))
107        .style(move |_t: &iced::Theme| container::Style {
108            border: iced::Border {
109                // Half the side: a circle, not a rounded square.
110                radius: (ring_side / 2.0).into(),
111                width: 1.5,
112                color: theme::text_dim(),
113            },
114            ..container::Style::default()
115        });
116
117    let placeholder = if tokens.is_empty() { placeholder } else { "" };
118    let mut input = text_input(placeholder, value)
119        .on_input(on_input)
120        .size(scale.apply(density::META_TEXT_BASE))
121        .style(|t: &iced::Theme, _status| text_input::Style {
122            // Transparent: the bordered container around this row is the
123            // field. An input drawing its own background inside it would
124            // show as a box within a box.
125            background: iced::Background::Color(iced::Color::TRANSPARENT),
126            border: iced::Border::default(),
127            icon: theme::text_dim(),
128            placeholder: theme::text_dim(),
129            value: theme::text(),
130            selection: t.extended_palette().primary.weak.color,
131        })
132        .width(Length::Fill);
133    if let Some(id) = id {
134        input = input.id(id);
135    }
136    if let Some(submit) = on_submit {
137        input = input.on_submit(submit);
138    }
139
140    let mut parts = row![ring].spacing(spacing::XS).align_y(iced::Alignment::Center);
141    for token in tokens {
142        parts = parts.push(token);
143    }
144    container(parts.push(input))
145        .height(Length::Fixed(density::field_height(scale)))
146        .center_y(Length::Fixed(density::field_height(scale)))
147        .padding([0, spacing::SM as u16])
148        // A field narrower than its tokens cuts them off at its edge
149        // rather than drawing them over whatever sits beside it.
150        .clip(true)
151        .style(inset_field_style)
152}