submit_feedback_button.rs

 1use gpui::{
 2    elements::{Label, MouseEventHandler},
 3    platform::{CursorStyle, MouseButton},
 4    Drawable, Element, Entity, View, ViewContext, ViewHandle,
 5};
 6use settings::Settings;
 7use workspace::{item::ItemHandle, ToolbarItemLocation, ToolbarItemView};
 8
 9use crate::feedback_editor::{FeedbackEditor, SubmitFeedback};
10
11pub struct SubmitFeedbackButton {
12    pub(crate) active_item: Option<ViewHandle<FeedbackEditor>>,
13}
14
15impl SubmitFeedbackButton {
16    pub fn new() -> Self {
17        Self {
18            active_item: Default::default(),
19        }
20    }
21}
22
23impl Entity for SubmitFeedbackButton {
24    type Event = ();
25}
26
27impl View for SubmitFeedbackButton {
28    fn ui_name() -> &'static str {
29        "SubmitFeedbackButton"
30    }
31
32    fn render(&mut self, cx: &mut ViewContext<Self>) -> Element<Self> {
33        let theme = cx.global::<Settings>().theme.clone();
34        enum SubmitFeedbackButton {}
35        MouseEventHandler::<SubmitFeedbackButton, Self>::new(0, cx, |state, _| {
36            let style = theme.feedback.submit_button.style_for(state, false);
37            Label::new("Submit as Markdown", style.text.clone())
38                .contained()
39                .with_style(style.container)
40                .boxed()
41        })
42        .with_cursor_style(CursorStyle::PointingHand)
43        .on_click(MouseButton::Left, |_, _, cx| {
44            cx.dispatch_action(SubmitFeedback)
45        })
46        .aligned()
47        .contained()
48        .with_margin_left(theme.feedback.button_margin)
49        .with_tooltip::<Self>(
50            0,
51            "cmd-s".into(),
52            Some(Box::new(SubmitFeedback)),
53            theme.tooltip.clone(),
54            cx,
55        )
56        .boxed()
57    }
58}
59
60impl ToolbarItemView for SubmitFeedbackButton {
61    fn set_active_pane_item(
62        &mut self,
63        active_pane_item: Option<&dyn ItemHandle>,
64        cx: &mut ViewContext<Self>,
65    ) -> workspace::ToolbarItemLocation {
66        cx.notify();
67        if let Some(feedback_editor) = active_pane_item.and_then(|i| i.downcast::<FeedbackEditor>())
68        {
69            self.active_item = Some(feedback_editor);
70            ToolbarItemLocation::PrimaryRight { flex: None }
71        } else {
72            self.active_item = None;
73            ToolbarItemLocation::Hidden
74        }
75    }
76}