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            content = content.push(meta_text(hint.clone(), density::META_TEXT_BASE, scale));
70        }
71        list = list.push(
72            button(content)
73                .on_press(on_choose(index))
74                .width(Length::Fill)
75                .height(Length::Fixed(density::row_height(scale)))
76                .padding([0, spacing::SM as u16])
77                .style(move |t: &iced::Theme, status| selectable_row_style(t, status, selected)),
78        );
79    }
80    // The context menu's surface: a step down from the listing it covers,
81    // outlined, at the inner radius — it is the same kind of thing, a
82    // short list floating over the window.
83    let panel = container(list)
84        .padding(spacing::XS)
85        .width(Length::Fill)
86        .style(|_t: &iced::Theme| container::Style {
87            background: Some(iced::Background::Color(surface::sidebar())),
88            border: iced::Border {
89                color: surface::card_border(),
90                width: 1.0,
91                radius: density::inner_radius().into(),
92            },
93            ..container::Style::default()
94        });
95    Some(opaque(panel))
96}