submit_feedback_button.rs

 1use gpui::{
 2    elements::{Label, MouseEventHandler},
 3    platform::{CursorStyle, MouseButton},
 4    AnyElement, 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>) -> AnyElement<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        })
41        .with_cursor_style(CursorStyle::PointingHand)
42        .on_click(MouseButton::Left, |_, _, cx| {
43            cx.dispatch_action(SubmitFeedback)
44        })
45        .aligned()
46        .contained()
47        .with_margin_left(theme.feedback.button_margin)
48        .with_tooltip::<Self>(
49            0,
50            "cmd-s".into(),
51            Some(Box::new(SubmitFeedback)),
52            theme.tooltip.clone(),
53            cx,
54        )
55        .into_any()
56    }
57}
58
59impl ToolbarItemView for SubmitFeedbackButton {
60    fn set_active_pane_item(
61        &mut self,
62        active_pane_item: Option<&dyn ItemHandle>,
63        cx: &mut ViewContext<Self>,
64    ) -> workspace::ToolbarItemLocation {
65        cx.notify();
66        if let Some(feedback_editor) = active_pane_item.and_then(|i| i.downcast::<FeedbackEditor>())
67        {
68            self.active_item = Some(feedback_editor);
69            ToolbarItemLocation::PrimaryRight { flex: None }
70        } else {
71            self.active_item = None;
72            ToolbarItemLocation::Hidden
73        }
74    }
75}