deploy_feedback_button.rs

 1use gpui::{
 2    elements::*,
 3    platform::{CursorStyle, MouseButton},
 4    Entity, View, ViewContext, WeakViewHandle,
 5};
 6use workspace::{item::ItemHandle, StatusItemView, Workspace};
 7
 8use crate::feedback_editor::{FeedbackEditor, GiveFeedback};
 9
10pub struct DeployFeedbackButton {
11    active: bool,
12    workspace: WeakViewHandle<Workspace>,
13}
14
15impl Entity for DeployFeedbackButton {
16    type Event = ();
17}
18
19impl DeployFeedbackButton {
20    pub fn new(workspace: &Workspace) -> Self {
21        DeployFeedbackButton {
22            active: false,
23            workspace: workspace.weak_handle(),
24        }
25    }
26}
27
28impl View for DeployFeedbackButton {
29    fn ui_name() -> &'static str {
30        "DeployFeedbackButton"
31    }
32
33    fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
34        let active = self.active;
35        let theme = theme::current(cx).clone();
36        Stack::new()
37            .with_child(
38                MouseEventHandler::<Self, Self>::new(0, cx, |state, _| {
39                    let style = &theme
40                        .workspace
41                        .status_bar
42                        .panel_buttons
43                        .button
44                        .style_for(state, active);
45
46                    Svg::new("icons/feedback_16.svg")
47                        .with_color(style.icon_color)
48                        .constrained()
49                        .with_width(style.icon_size)
50                        .aligned()
51                        .constrained()
52                        .with_width(style.icon_size)
53                        .with_height(style.icon_size)
54                        .contained()
55                        .with_style(style.container)
56                })
57                .with_cursor_style(CursorStyle::PointingHand)
58                .on_click(MouseButton::Left, move |_, this, cx| {
59                    if !active {
60                        if let Some(workspace) = this.workspace.upgrade(cx) {
61                            workspace
62                                .update(cx, |workspace, cx| FeedbackEditor::deploy(workspace, cx))
63                        }
64                    }
65                })
66                .with_tooltip::<Self>(
67                    0,
68                    "Send Feedback".into(),
69                    Some(Box::new(GiveFeedback)),
70                    theme.tooltip.clone(),
71                    cx,
72                ),
73            )
74            .into_any()
75    }
76}
77
78impl StatusItemView for DeployFeedbackButton {
79    fn set_active_pane_item(&mut self, item: Option<&dyn ItemHandle>, cx: &mut ViewContext<Self>) {
80        if let Some(item) = item {
81            if let Some(_) = item.downcast::<FeedbackEditor>() {
82                self.active = true;
83                cx.notify();
84                return;
85            }
86        }
87        self.active = false;
88        cx.notify();
89    }
90}