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