BlueGit

ict rusty-teams

Files

rusty-teams / crates / app / src / views / emoji_popup.rs

emoji_popup.rs

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