Skip to main content

hyprforge_ui/widgets/
suggestions.rs

1//! A list of answers under a field: where a typed path could go, which
2//! setting a search means.
3//!
4//! The panel only — what goes in it, and in what order, is the caller's,
5//! and so is where it hangs (see [`super::anchored`]). One look for
6//! every such list, so Files' path bar and Settings' search palette read
7//! as the same kind of control rather than two that happen to be similar.
8
9use crate::density;
10use crate::theme::{self, spacing, surface, FontScale};
11use crate::widgets::{meta_text, scaled_text, selectable_row_style};
12use iced::widget::{button, column, container, opaque, row};
13use iced::{Element, Length};
14
15/// One row: what it is, and an optional dim note at its right edge.
16#[derive(Debug, Clone, PartialEq, Eq)]
17pub struct Suggestion {
18    pub label: String,
19    pub hint: Option<String>,
20}
21
22/// The panel. `heading` is a dim line above the rows — Files puts the
23/// resolution there, `~/pr/hy → ~/projects/hyprsuite`. `empty` is what it
24/// says when there are no rows; `None` draws nothing at all instead,
25/// which is right while the first answer is still being worked out.
26///
27/// Opaque: a click on the panel's own padding must not fall through to
28/// whatever listing is underneath it.
29pub fn suggestions<'a, Message: Clone + 'a>(
30    heading: Option<String>,
31    rows: &[Suggestion],
32    highlighted: Option<usize>,
33    on_choose: impl Fn(usize) -> Message,
34    empty: Option<String>,
35    mono: bool,
36    scale: FontScale,
37) -> Option<Element<'a, Message>> {
38    if rows.is_empty() && empty.is_none() {
39        return None;
40    }
41    let font = if mono { theme::mono_font() } else { iced::Font::DEFAULT };
42    let mut list = column![].width(Length::Fill).spacing(1);
43    if let Some(heading) = heading {
44        list = list.push(
45            container(meta_text(heading, density::META_TEXT_BASE, scale).font(font))
46                .padding([spacing::XS as u16, spacing::SM as u16]),
47        );
48    }
49    if rows.is_empty() {
50        if let Some(empty) = empty {
51            list = list.push(
52                container(meta_text(empty, density::META_TEXT_BASE, scale))
53                    .padding([spacing::XS as u16, spacing::SM as u16]),
54            );
55        }
56    }
57    for (index, item) in rows.iter().enumerate() {
58        let selected = highlighted == Some(index);
59        let mut content = row![scaled_text(item.label.clone(), density::META_TEXT_BASE, scale)
60            .font(font)
61            .width(Length::Fill)]
62        // Full height, or `align_y` has nothing to centre in: the row
63        // shrinks to its text and the button draws it at the top of a
64        // 28px row. Seen in the first screenshot.
65        .height(Length::Fill)
66        .align_y(iced::Alignment::Center)
67        .spacing(spacing::MD);
68        if let Some(hint) = &item.hint {
69            // The full text colour on the selected row: dim text on the
70            // accent fill cannot be read — the rule Files' own list keeps,
71            // seen here as an unreadable "Ctrl+L" in the first screenshot.
72            let hint = meta_text(hint.clone(), density::META_TEXT_BASE, scale);
73            content = content.push(if selected { hint.color(theme::text()) } else { hint });
74        }
75        list = list.push(
76            button(content)
77                .on_press(on_choose(index))
78                .width(Length::Fill)
79                .height(Length::Fixed(density::row_height(scale)))
80                .padding([0, spacing::SM as u16])
81                .style(move |t: &iced::Theme, status| selectable_row_style(t, status, selected)),
82        );
83    }
84    // The context menu's surface: a step down from the listing it covers,
85    // outlined, at the inner radius — it is the same kind of thing, a
86    // short list floating over the window.
87    let panel = container(list)
88        .padding(spacing::XS)
89        .width(Length::Fill)
90        .style(|_t: &iced::Theme| container::Style {
91            background: Some(iced::Background::Color(surface::sidebar())),
92            border: iced::Border {
93                color: surface::card_border(),
94                width: 1.0,
95                radius: density::inner_radius().into(),
96            },
97            ..container::Style::default()
98        });
99    Some(opaque(panel))
100}