deploy_feedback_button.rs

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