ui.rs

  1use std::borrow::Cow;
  2
  3use gpui::{
  4    color::Color,
  5    elements::{
  6        ConstrainedBox, Container, ContainerStyle, Empty, Flex, KeystrokeLabel, Label,
  7        MouseEventHandler, ParentElement, Stack, Svg,
  8    },
  9    fonts::TextStyle,
 10    geometry::vector::{vec2f, Vector2F},
 11    platform,
 12    platform::MouseButton,
 13    scene::MouseClick,
 14    Action, Element, EventContext, MouseState, View, ViewContext,
 15};
 16use serde::Deserialize;
 17
 18use crate::{ContainedText, Interactive};
 19
 20#[derive(Clone, Deserialize, Default)]
 21pub struct CheckboxStyle {
 22    pub icon: SvgStyle,
 23    pub label: ContainedText,
 24    pub default: ContainerStyle,
 25    pub checked: ContainerStyle,
 26    pub hovered: ContainerStyle,
 27    pub hovered_and_checked: ContainerStyle,
 28}
 29
 30pub fn checkbox<Tag, V, F>(
 31    label: &'static str,
 32    style: &CheckboxStyle,
 33    checked: bool,
 34    id: usize,
 35    cx: &mut ViewContext<V>,
 36    change: F,
 37) -> MouseEventHandler<Tag, V>
 38where
 39    Tag: 'static,
 40    V: View,
 41    F: 'static + Fn(&mut V, bool, &mut EventContext<V>),
 42{
 43    let label = Label::new(label, style.label.text.clone())
 44        .contained()
 45        .with_style(style.label.container);
 46    checkbox_with_label(label, style, checked, id, cx, change)
 47}
 48
 49pub fn checkbox_with_label<Tag, D, V, F>(
 50    label: D,
 51    style: &CheckboxStyle,
 52    checked: bool,
 53    id: usize,
 54    cx: &mut ViewContext<V>,
 55    change: F,
 56) -> MouseEventHandler<Tag, V>
 57where
 58    Tag: 'static,
 59    D: Element<V>,
 60    V: View,
 61    F: 'static + Fn(&mut V, bool, &mut EventContext<V>),
 62{
 63    MouseEventHandler::new(id, cx, |state, _| {
 64        let indicator = if checked {
 65            svg(&style.icon)
 66        } else {
 67            Empty::new()
 68                .constrained()
 69                .with_width(style.icon.dimensions.width)
 70                .with_height(style.icon.dimensions.height)
 71        };
 72
 73        Flex::row()
 74            .with_child(indicator.contained().with_style(if checked {
 75                if state.hovered() {
 76                    style.hovered_and_checked
 77                } else {
 78                    style.checked
 79                }
 80            } else {
 81                if state.hovered() {
 82                    style.hovered
 83                } else {
 84                    style.default
 85                }
 86            }))
 87            .with_child(label)
 88            .align_children_center()
 89    })
 90    .on_click(platform::MouseButton::Left, move |_, view, cx| {
 91        change(view, !checked, cx)
 92    })
 93    .with_cursor_style(platform::CursorStyle::PointingHand)
 94}
 95
 96#[derive(Clone, Deserialize, Default)]
 97pub struct SvgStyle {
 98    pub color: Color,
 99    pub asset: String,
100    pub dimensions: Dimensions,
101}
102
103#[derive(Clone, Deserialize, Default)]
104pub struct Dimensions {
105    pub width: f32,
106    pub height: f32,
107}
108
109impl Dimensions {
110    pub fn to_vec(&self) -> Vector2F {
111        vec2f(self.width, self.height)
112    }
113}
114
115pub fn svg<V: View>(style: &SvgStyle) -> ConstrainedBox<V> {
116    Svg::new(style.asset.clone())
117        .with_color(style.color)
118        .constrained()
119        .with_width(style.dimensions.width)
120        .with_height(style.dimensions.height)
121}
122
123#[derive(Clone, Deserialize, Default)]
124pub struct IconStyle {
125    icon: SvgStyle,
126    container: ContainerStyle,
127}
128
129pub fn icon<V: View>(style: &IconStyle) -> Container<V> {
130    svg(&style.icon).contained().with_style(style.container)
131}
132
133pub fn keystroke_label<V: View>(
134    label_text: &'static str,
135    label_style: &ContainedText,
136    keystroke_style: &ContainedText,
137    action: Box<dyn Action>,
138    cx: &mut ViewContext<V>,
139) -> Container<V> {
140    // FIXME: Put the theme in it's own global so we can
141    // query the keystroke style on our own
142    Flex::row()
143        .with_child(Label::new(label_text, label_style.text.clone()).contained())
144        .with_child(
145            KeystrokeLabel::new(
146                cx.view_id(),
147                action,
148                keystroke_style.container,
149                keystroke_style.text.clone(),
150            )
151            .flex_float(),
152        )
153        .contained()
154        .with_style(label_style.container)
155}
156
157pub type ButtonStyle = Interactive<ContainedText>;
158
159pub fn cta_button<L, A, V>(
160    label: L,
161    action: A,
162    max_width: f32,
163    style: &ButtonStyle,
164    cx: &mut ViewContext<V>,
165) -> MouseEventHandler<A, V>
166where
167    L: Into<Cow<'static, str>>,
168    A: 'static + Action + Clone,
169    V: View,
170{
171    cta_button_with_click::<A, _, _, _>(label, max_width, style, cx, move |_, _, cx| {
172        cx.dispatch_action(action.clone())
173    })
174}
175
176pub fn cta_button_with_click<Tag, L, V, F>(
177    label: L,
178    max_width: f32,
179    style: &ButtonStyle,
180    cx: &mut ViewContext<V>,
181    f: F,
182) -> MouseEventHandler<Tag, V>
183where
184    Tag: 'static,
185    L: Into<Cow<'static, str>>,
186    V: View,
187    F: Fn(MouseClick, &mut V, &mut EventContext<V>) + 'static,
188{
189    MouseEventHandler::<Tag, V>::new(0, cx, |state, _| {
190        let style = style.style_for(state, false);
191        Label::new(label, style.text.to_owned())
192            .aligned()
193            .contained()
194            .with_style(style.container)
195            .constrained()
196            .with_max_width(max_width)
197    })
198    .on_click(MouseButton::Left, f)
199    .with_cursor_style(platform::CursorStyle::PointingHand)
200}
201
202#[derive(Clone, Deserialize, Default)]
203pub struct ModalStyle {
204    close_icon: Interactive<IconStyle>,
205    container: ContainerStyle,
206    titlebar: ContainerStyle,
207    title_text: Interactive<TextStyle>,
208    dimensions: Dimensions,
209}
210
211impl ModalStyle {
212    pub fn dimensions(&self) -> Vector2F {
213        self.dimensions.to_vec()
214    }
215}
216
217pub fn modal<Tag, V, I, D, F>(
218    title: I,
219    style: &ModalStyle,
220    cx: &mut ViewContext<V>,
221    build_modal: F,
222) -> impl Element<V>
223where
224    Tag: 'static,
225    V: View,
226    I: Into<Cow<'static, str>>,
227    D: Element<V>,
228    F: FnOnce(&mut gpui::ViewContext<V>) -> D,
229{
230    const TITLEBAR_HEIGHT: f32 = 28.;
231    // let active = cx.window_is_active(cx.window_id());
232
233    Flex::column()
234        .with_child(
235            Stack::new()
236                .with_child(Label::new(
237                    title,
238                    style
239                        .title_text
240                        .style_for(&mut MouseState::default(), false)
241                        .clone(),
242                ))
243                .with_child(
244                    // FIXME: Get a better tag type
245                    MouseEventHandler::<Tag, V>::new(999999, cx, |state, _cx| {
246                        let style = style.close_icon.style_for(state, false);
247                        icon(style)
248                    })
249                    .on_click(platform::MouseButton::Left, move |_, _, cx| {
250                        cx.remove_window();
251                    })
252                    .with_cursor_style(platform::CursorStyle::PointingHand)
253                    .aligned()
254                    .right(),
255                )
256                .contained()
257                .with_style(style.titlebar)
258                .constrained()
259                .with_height(TITLEBAR_HEIGHT),
260        )
261        .with_child(
262            build_modal(cx)
263                .contained()
264                .with_style(style.container)
265                .constrained()
266                .with_width(style.dimensions().x())
267                .with_height(style.dimensions().y() - TITLEBAR_HEIGHT),
268        )
269        .constrained()
270        .with_height(style.dimensions().y())
271}