submit_feedback_button.rs

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