| 1 | use std::ops::Range; |
| 2 | |
| 3 | use gpui_kit::component::{h_flex, v_flex}; |
| 4 | use gpui_kit::prelude::FluentBuilder as _; |
| 5 | use gpui_kit::*; |
| 6 | |
| 7 | use super::widgets::symbol; |
| 8 | use crate::emoji::Match; |
| 9 | use crate::theme; |
| 10 | |
| 11 | pub const LIMIT: usize = 40; |
| 12 | const WIDTH: f32 = 300.; |
| 13 | const ROW_HEIGHT: f32 = 32.; |
| 14 | const VISIBLE_ROWS: usize = 7; |
| 15 | const HINT_HEIGHT: f32 = 28.; |
| 16 | const ALIAS_MAX_WIDTH: f32 = 120.; |
| 17 | |
| 18 | pub struct EmojiPopup { |
| 19 | pub range: Range<usize>, |
| 20 | pub query: String, |
| 21 | pub matches: Vec<Match>, |
| 22 | pub highlighted: usize, |
| 23 | scroll: ScrollHandle, |
| 24 | } |
| 25 | |
| 26 | impl EmojiPopup { |
| 27 | pub fn new(range: Range<usize>, query: String, matches: Vec<Match>) -> Self { |
| 28 | EmojiPopup { |
| 29 | range, |
| 30 | query, |
| 31 | matches, |
| 32 | highlighted: 0, |
| 33 | scroll: ScrollHandle::new(), |
| 34 | } |
| 35 | } |
| 36 | |
| 37 | pub fn move_highlight(&mut self, delta: isize) { |
| 38 | let count = self.matches.len() as isize; |
| 39 | if count > 0 { |
| 40 | self.highlighted = (self.highlighted as isize + delta).rem_euclid(count) as usize; |
| 41 | self.scroll.scroll_to_item(self.highlighted); |
| 42 | } |
| 43 | } |
| 44 | |
| 45 | pub fn selected(&self) -> Option<&Match> { |
| 46 | self.matches.get(self.highlighted) |
| 47 | } |
| 48 | } |
| 49 | |
| 50 | fn highlighted_text(text: String, highlight: Range<usize>, size: f32, color: Hsla) -> Div { |
| 51 | let marks = text |
| 52 | .get(highlight.clone()) |
| 53 | .filter(|marked| !marked.is_empty()) |
| 54 | .map(|_| { |
| 55 | ( |
| 56 | highlight, |
| 57 | HighlightStyle { |
| 58 | color: Some(theme::accent_text()), |
| 59 | font_weight: Some(FontWeight::BOLD), |
| 60 | ..Default::default() |
| 61 | }, |
| 62 | ) |
| 63 | }); |
| 64 | div() |
| 65 | .min_w_0() |
| 66 | .truncate() |
| 67 | .text_size(px(size)) |
| 68 | .text_color(color) |
| 69 | .child(StyledText::new(text).with_highlights(marks)) |
| 70 | } |
| 71 | |
| 72 | fn row(index: usize, found: &Match, selected: bool) -> Stateful<Div> { |
| 73 | let code = format!(":{}:", found.code); |
| 74 | let code_highlight = if found.alias.is_none() { |
| 75 | found.highlight.start + 1..found.highlight.end + 1 |
| 76 | } else { |
| 77 | 0..0 |
| 78 | }; |
| 79 | h_flex() |
| 80 | .id(ElementId::Name(format!("emoji-row-{index}").into())) |
| 81 | .relative() |
| 82 | .flex_none() |
| 83 | .h(px(ROW_HEIGHT)) |
| 84 | .pl(px(8.)) |
| 85 | .pr(px(10.)) |
| 86 | .gap(px(8.)) |
| 87 | .items_center() |
| 88 | .rounded(px(6.)) |
| 89 | .cursor_pointer() |
| 90 | .when(selected, |row| { |
| 91 | row.bg(theme::surface_raised()).child( |
| 92 | div() |
| 93 | .absolute() |
| 94 | .left_0() |
| 95 | .top_0() |
| 96 | .bottom_0() |
| 97 | .w(px(3.)) |
| 98 | .rounded_l(px(6.)) |
| 99 | .bg(theme::accent()), |
| 100 | ) |
| 101 | }) |
| 102 | .when(!selected, |row| row.hover(|row| row.bg(theme::row_hover()))) |
| 103 | .child( |
| 104 | div() |
| 105 | .w(px(28.)) |
| 106 | .flex_none() |
| 107 | .text_center() |
| 108 | .text_size(px(20.)) |
| 109 | .line_height(px(ROW_HEIGHT)) |
| 110 | .child(found.glyph), |
| 111 | ) |
| 112 | .child(highlighted_text(code, code_highlight, 13., theme::text())) |
| 113 | .when(found.recent, |row| { |
| 114 | row.child(symbol("schedule", 12., theme::text_muted())) |
| 115 | }) |
| 116 | .child(div().flex_1()) |
| 117 | .when_some(found.alias, |row, alias| { |
| 118 | row.child( |
| 119 | highlighted_text( |
| 120 | alias.to_owned(), |
| 121 | found.highlight.clone(), |
| 122 | 11.5, |
| 123 | theme::text_muted(), |
| 124 | ) |
| 125 | .flex_none() |
| 126 | .max_w(px(ALIAS_MAX_WIDTH)), |
| 127 | ) |
| 128 | }) |
| 129 | } |
| 130 | |
| 131 | fn key(label: &'static str) -> Div { |
| 132 | div() |
| 133 | .px(px(5.)) |
| 134 | .py(px(1.)) |
| 135 | .rounded(px(4.)) |
| 136 | .border_1() |
| 137 | .border_color(theme::border_strong()) |
| 138 | .bg(theme::surface_raised()) |
| 139 | .text_size(px(10.)) |
| 140 | .text_color(theme::text()) |
| 141 | .child(label) |
| 142 | } |
| 143 | |
| 144 | fn hint() -> Div { |
| 145 | h_flex() |
| 146 | .h(px(HINT_HEIGHT)) |
| 147 | .mt(px(4.)) |
| 148 | .px(px(8.)) |
| 149 | .gap(px(6.)) |
| 150 | .items_center() |
| 151 | .border_t_1() |
| 152 | .border_color(theme::border()) |
| 153 | .text_size(px(11.)) |
| 154 | .text_color(theme::text_muted()) |
| 155 | .child(key("↑↓")) |
| 156 | .child("select") |
| 157 | .child(key("Tab")) |
| 158 | .child(key("Enter")) |
| 159 | .child("insert") |
| 160 | .child(key("Esc")) |
| 161 | .child("close") |
| 162 | } |
| 163 | |
| 164 | /// Bottom-left corner sits above `anchor`, the colon's top-left in window coordinates. |
| 165 | pub fn render( |
| 166 | popup: &EmojiPopup, |
| 167 | anchor: Option<Point<Pixels>>, |
| 168 | on_pick: impl Fn(usize, &mut Window, &mut App) + 'static, |
| 169 | ) -> AnyElement { |
| 170 | let on_pick = std::rc::Rc::new(on_pick); |
| 171 | let rows = popup.matches.iter().enumerate().map(|(index, found)| { |
| 172 | let on_pick = on_pick.clone(); |
| 173 | row(index, found, index == popup.highlighted) |
| 174 | .on_click(move |_, window, cx| on_pick(index, window, cx)) |
| 175 | }); |
| 176 | let visible = popup.matches.len().min(VISIBLE_ROWS) as f32; |
| 177 | let panel = v_flex() |
| 178 | .id("emoji-popup") |
| 179 | .w(px(WIDTH)) |
| 180 | .p(px(4.)) |
| 181 | .rounded(px(10.)) |
| 182 | .border_1() |
| 183 | .border_color(theme::border_strong()) |
| 184 | .bg(theme::surface()) |
| 185 | .shadow_lg() |
| 186 | .occlude() |
| 187 | .child( |
| 188 | v_flex() |
| 189 | .id("emoji-rows") |
| 190 | .max_h(px(ROW_HEIGHT * visible)) |
| 191 | .overflow_y_scroll() |
| 192 | .track_scroll(&popup.scroll) |
| 193 | .children(rows), |
| 194 | ) |
| 195 | .child(hint()); |
| 196 | match anchor { |
| 197 | Some(anchor) => deferred( |
| 198 | anchored() |
| 199 | .position(anchor - point(px(12.), px(6.))) |
| 200 | .anchor(Anchor::BottomLeft) |
| 201 | .snap_to_window_with_margin(px(8.)) |
| 202 | .child(panel), |
| 203 | ) |
| 204 | .into_any_element(), |
| 205 | None => div() |
| 206 | .absolute() |
| 207 | .bottom(relative(1.)) |
| 208 | .left_0() |
| 209 | .mb(px(6.)) |
| 210 | .child(panel) |
| 211 | .into_any_element(), |
| 212 | } |
| 213 | } |