1use crate::feedback_editor::{FeedbackEditor, SubmitFeedback};
2use anyhow::Result;
3use gpui::{AppContext, Div, EventEmitter, Render, Task, View, ViewContext};
4use ui::prelude::*;
5use workspace::{item::ItemHandle, ToolbarItemEvent, ToolbarItemLocation, ToolbarItemView};
6
7pub fn init(cx: &mut AppContext) {
8 // cx.add_action(SubmitFeedbackButton::submit);
9}
10
11pub struct SubmitFeedbackButton {
12 pub(crate) active_item: Option<View<FeedbackEditor>>,
13}
14
15impl SubmitFeedbackButton {
16 pub fn new() -> Self {
17 Self {
18 active_item: Default::default(),
19 }
20 }
21
22 pub fn submit(
23 &mut self,
24 _: &SubmitFeedback,
25 cx: &mut ViewContext<Self>,
26 ) -> Option<Task<Result<()>>> {
27 if let Some(active_item) = self.active_item.as_ref() {
28 Some(active_item.update(cx, |feedback_editor, cx| feedback_editor.submit(cx)))
29 } else {
30 None
31 }
32 }
33}
34
35// TODO
36impl Render for SubmitFeedbackButton {
37 type Element = Div;
38
39 fn render(&mut self, cx: &mut ViewContext<Self>) -> Self::Element {
40 let allow_submission = self
41 .active_item
42 .as_ref()
43 .map_or(true, |i| i.read(cx).allow_submission);
44
45 div()
46 }
47}
48
49// TODO - delete
50// impl View for SubmitFeedbackButton {
51
52// fn render(&mut self, cx: &mut ViewContext<Self>) -> AnyElement<Self> {
53// let theme = theme::current(cx).clone();
54// let allow_submission = self
55// .active_item
56// .as_ref()
57// .map_or(true, |i| i.read(cx).allow_submission);
58
59// enum SubmitFeedbackButton {}
60// MouseEventHandler::new::<SubmitFeedbackButton, _>(0, cx, |state, _| {
61// let text;
62// let style = if allow_submission {
63// text = "Submit as Markdown";
64// theme.feedback.submit_button.style_for(state)
65// } else {
66// text = "Submitting...";
67// theme
68// .feedback
69// .submit_button
70// .disabled
71// .as_ref()
72// .unwrap_or(&theme.feedback.submit_button.default)
73// };
74
75// Label::new(text, style.text.clone())
76// .contained()
77// .with_style(style.container)
78// })
79// .with_cursor_style(CursorStyle::PointingHand)
80// .on_click(MouseButton::Left, |_, this, cx| {
81// this.submit(&Default::default(), cx);
82// })
83// .aligned()
84// .contained()
85// .with_margin_left(theme.feedback.button_margin)
86// .with_tooltip::<Self>(
87// 0,
88// "cmd-s",
89// Some(Box::new(SubmitFeedback)),
90// theme.tooltip.clone(),
91// cx,
92// )
93// .into_any()
94// }
95// }
96
97impl EventEmitter<ToolbarItemEvent> for SubmitFeedbackButton {}
98
99impl ToolbarItemView for SubmitFeedbackButton {
100 fn set_active_pane_item(
101 &mut self,
102 active_pane_item: Option<&dyn ItemHandle>,
103 cx: &mut ViewContext<Self>,
104 ) -> workspace::ToolbarItemLocation {
105 cx.notify();
106 if let Some(feedback_editor) = active_pane_item.and_then(|i| i.downcast::<FeedbackEditor>())
107 {
108 self.active_item = Some(feedback_editor);
109 ToolbarItemLocation::PrimaryRight
110 } else {
111 self.active_item = None;
112 ToolbarItemLocation::Hidden
113 }
114 }
115}