Move feedback items into a feedback crate

This commit is contained in:
Joseph Lyons 2023-01-09 13:54:37 -05:00
parent a73e264c3d
commit c1e61b479c
10 changed files with 128 additions and 58 deletions

View file

@ -1,281 +0,0 @@
use std::{ops::Range, sync::Arc};
use anyhow::bail;
use client::{Client, ZED_SECRET_CLIENT_TOKEN};
use editor::Editor;
use futures::AsyncReadExt;
use gpui::{
actions,
elements::{
AnchorCorner, ChildView, Flex, MouseEventHandler, Overlay, OverlayFitMode, ParentElement,
Stack, Text,
},
serde_json, CursorStyle, Element, ElementBox, Entity, MouseButton, MutableAppContext,
RenderContext, View, ViewContext, ViewHandle,
};
use isahc::Request;
use lazy_static::lazy_static;
use serde::Serialize;
use settings::Settings;
use workspace::{item::ItemHandle, StatusItemView};
use crate::{feedback_popover, system_specs::SystemSpecs};
lazy_static! {
pub static ref ZED_SERVER_URL: String =
std::env::var("ZED_SERVER_URL").unwrap_or_else(|_| "https://zed.dev".to_string());
}
const FEEDBACK_CHAR_COUNT_RANGE: Range<usize> = Range {
start: 5,
end: 1000,
};
actions!(feedback, [ToggleFeedbackPopover, SubmitFeedback]);
pub fn init(cx: &mut MutableAppContext) {
cx.add_action(FeedbackButton::toggle_feedback);
cx.add_action(FeedbackPopover::submit_feedback);
}
pub struct FeedbackButton {
feedback_popover: Option<ViewHandle<FeedbackPopover>>,
}
impl FeedbackButton {
pub fn new() -> Self {
Self {
feedback_popover: None,
}
}
pub fn toggle_feedback(&mut self, _: &ToggleFeedbackPopover, cx: &mut ViewContext<Self>) {
match self.feedback_popover.take() {
Some(_) => {}
None => {
let popover_view = cx.add_view(|_cx| FeedbackPopover::new(_cx));
self.feedback_popover = Some(popover_view.clone());
}
}
cx.notify();
}
}
impl Entity for FeedbackButton {
type Event = ();
}
impl View for FeedbackButton {
fn ui_name() -> &'static str {
"FeedbackButton"
}
fn render(&mut self, cx: &mut RenderContext<'_, Self>) -> ElementBox {
Stack::new()
.with_child(
MouseEventHandler::<Self>::new(0, cx, |state, cx| {
let theme = &cx.global::<Settings>().theme;
let theme = &theme.workspace.status_bar.feedback;
Text::new(
"Give Feedback".to_string(),
theme
.style_for(state, self.feedback_popover.is_some())
.clone(),
)
.boxed()
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, |_, cx| {
cx.dispatch_action(ToggleFeedbackPopover)
})
.boxed(),
)
.with_children(self.feedback_popover.as_ref().map(|popover| {
Overlay::new(ChildView::new(popover, cx).contained().boxed())
.with_fit_mode(OverlayFitMode::SwitchAnchor)
.with_anchor_corner(AnchorCorner::TopLeft)
.with_z_index(999)
.boxed()
}))
.boxed()
}
}
impl StatusItemView for FeedbackButton {
fn set_active_pane_item(
&mut self,
_: Option<&dyn ItemHandle>,
_: &mut gpui::ViewContext<Self>,
) {
// N/A
}
}
pub struct FeedbackPopover {
feedback_editor: ViewHandle<Editor>,
// _subscriptions: Vec<Subscription>,
}
impl Entity for FeedbackPopover {
type Event = ();
}
#[derive(Serialize)]
struct FeedbackRequestBody<'a> {
feedback_text: &'a str,
metrics_id: Option<Arc<str>>,
system_specs: SystemSpecs,
token: &'a str,
}
impl FeedbackPopover {
pub fn new(cx: &mut ViewContext<Self>) -> Self {
let feedback_editor = cx.add_view(|cx| {
let editor = Editor::multi_line(
Some(Arc::new(|theme| theme.feedback.feedback_editor.clone())),
cx,
);
editor
});
cx.focus(&feedback_editor);
cx.subscribe(&feedback_editor, |this, _, event, cx| {
if let editor::Event::BufferEdited = event {
let buffer_len = this.feedback_editor.read(cx).buffer().read(cx).len(cx);
let feedback_chars_remaining = FEEDBACK_CHAR_COUNT_RANGE.end - buffer_len;
dbg!(feedback_chars_remaining);
}
})
.detach();
// let active_call = ActiveCall::global(cx);
// let mut subscriptions = Vec::new();
// subscriptions.push(cx.observe(&user_store, |this, _, cx| this.update_entries(cx)));
// subscriptions.push(cx.observe(&active_call, |this, _, cx| this.update_entries(cx)));
let this = Self {
feedback_editor, // _subscriptions: subscriptions,
};
// this.update_entries(cx);
this
}
fn submit_feedback(&mut self, _: &SubmitFeedback, cx: &mut ViewContext<'_, Self>) {
let feedback_text = self.feedback_editor.read(cx).text(cx);
let zed_client = cx.global::<Arc<Client>>();
let system_specs = SystemSpecs::new(cx);
let feedback_endpoint = format!("{}/api/feedback", *ZED_SERVER_URL);
let metrics_id = zed_client.metrics_id();
let http_client = zed_client.http_client();
cx.spawn(|_, _| {
async move {
// TODO FEEDBACK: Use or remove
// this.read_with(&async_cx, |this, cx| {
// // Now we have a &self and a &AppContext
// });
let request = FeedbackRequestBody {
feedback_text: &feedback_text,
metrics_id,
system_specs,
token: ZED_SECRET_CLIENT_TOKEN,
};
let json_bytes = serde_json::to_vec(&request)?;
let request = Request::post(feedback_endpoint)
.header("content-type", "application/json")
.body(json_bytes.into())?;
let mut response = http_client.send(request).await?;
let mut body = String::new();
response.body_mut().read_to_string(&mut body).await?;
let response_status = response.status();
dbg!(response_status);
if !response_status.is_success() {
// TODO FEEDBACK: Do some sort of error reporting here for if store fails
bail!("Error")
}
// TODO FEEDBACK: Use or remove
// Will need to handle error cases
// async_cx.update(|cx| {
// this.update(cx, |this, cx| {
// this.handle_error(error);
// cx.notify();
// cx.dispatch_action(ShowErrorPopover);
// this.error_text = "Embedding failed"
// })
// });
Ok(())
}
})
.detach();
}
}
impl View for FeedbackPopover {
fn ui_name() -> &'static str {
"FeedbackPopover"
}
fn render(&mut self, cx: &mut RenderContext<Self>) -> ElementBox {
enum SubmitFeedback {}
let theme = cx.global::<Settings>().theme.clone();
let submit_feedback_text_button_height = 20.0;
Flex::column()
.with_child(
Flex::row()
.with_child(
ChildView::new(self.feedback_editor.clone(), cx)
.contained()
.with_style(theme.feedback.feedback_editor.container)
.flex(1., true)
.boxed(),
)
.constrained()
.with_width(theme.feedback.feedback_popover.width)
.with_height(
theme.feedback.feedback_popover.height - submit_feedback_text_button_height,
)
.boxed(),
)
.with_child(
MouseEventHandler::<SubmitFeedback>::new(0, cx, |state, _| {
let theme = &theme.workspace.status_bar.feedback;
Text::new(
"Submit Feedback".to_string(),
theme.style_for(state, true).clone(),
)
.constrained()
.with_height(submit_feedback_text_button_height)
.boxed()
})
.with_cursor_style(CursorStyle::PointingHand)
.on_click(MouseButton::Left, |_, cx| {
cx.dispatch_action(feedback_popover::SubmitFeedback)
})
.on_click(MouseButton::Left, |_, cx| {
cx.dispatch_action(feedback_popover::ToggleFeedbackPopover)
})
.boxed(),
)
.contained()
.with_style(theme.feedback.feedback_popover.container)
.constrained()
.with_width(theme.feedback.feedback_popover.width)
.with_height(theme.feedback.feedback_popover.height)
.boxed()
}
}

View file

@ -14,6 +14,7 @@ use client::{
http::{self, HttpClient},
UserStore, ZED_SECRET_CLIENT_TOKEN,
};
use futures::{
channel::{mpsc, oneshot},
FutureExt, SinkExt, StreamExt,
@ -41,7 +42,7 @@ use util::{channel::RELEASE_CHANNEL, paths, ResultExt, TryFutureExt};
use workspace::{
self, item::ItemHandle, notifications::NotifyResultExt, AppState, NewFile, OpenPaths, Workspace,
};
use zed::{self, build_window_options, feedback_popover, initialize_workspace, languages, menus};
use zed::{self, build_window_options, initialize_workspace, languages, menus};
fn main() {
let http = http::client();
@ -110,12 +111,12 @@ fn main() {
cx.set_global(client.clone());
feedback_popover::init(cx);
context_menu::init(cx);
project::Project::init(&client);
client::init(client.clone(), cx);
command_palette::init(cx);
editor::init(cx);
feedback::init(cx);
go_to_line::init(cx);
file_finder::init(cx);
outline::init(cx);

View file

@ -340,15 +340,15 @@ pub fn menus() -> Vec<Menu<'static>> {
MenuItem::Separator,
MenuItem::Action {
name: "Copy System Specs Into Clipboard",
action: Box::new(crate::CopySystemSpecsIntoClipboard),
action: Box::new(feedback::CopySystemSpecsIntoClipboard),
},
MenuItem::Action {
name: "File Bug Report",
action: Box::new(crate::FileBugReport),
action: Box::new(feedback::FileBugReport),
},
MenuItem::Action {
name: "Request Feature",
action: Box::new(crate::RequestFeature),
action: Box::new(feedback::RequestFeature),
},
MenuItem::Separator,
MenuItem::Action {

View file

@ -1,54 +0,0 @@
use std::{env, fmt::Display};
use gpui::AppContext;
use human_bytes::human_bytes;
use serde::Serialize;
use sysinfo::{System, SystemExt};
use util::channel::ReleaseChannel;
#[derive(Debug, Serialize)]
pub struct SystemSpecs {
app_version: &'static str,
release_channel: &'static str,
os_name: &'static str,
os_version: Option<String>,
memory: u64,
architecture: &'static str,
}
impl SystemSpecs {
pub fn new(cx: &AppContext) -> Self {
let platform = cx.platform();
let system = System::new_all();
SystemSpecs {
app_version: env!("CARGO_PKG_VERSION"),
release_channel: cx.global::<ReleaseChannel>().dev_name(),
os_name: platform.os_name(),
os_version: platform
.os_version()
.ok()
.map(|os_version| os_version.to_string()),
memory: system.total_memory(),
architecture: env::consts::ARCH,
}
}
}
impl Display for SystemSpecs {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let os_information = match &self.os_version {
Some(os_version) => format!("OS: {} {}", self.os_name, os_version),
None => format!("OS: {}", self.os_name),
};
let system_specs = [
format!("Zed: {} ({})", self.app_version, self.release_channel),
os_information,
format!("Memory: {}", human_bytes(self.memory as f64)),
format!("Architecture: {}", self.architecture),
]
.join("\n");
write!(f, "{system_specs}")
}
}

View file

@ -1,10 +1,7 @@
pub mod feedback_popover;
pub mod languages;
pub mod menus;
pub mod system_specs;
#[cfg(any(test, feature = "test-support"))]
pub mod test;
use anyhow::{anyhow, Context, Result};
use assets::Assets;
use breadcrumbs::Breadcrumbs;
@ -22,7 +19,7 @@ use gpui::{
},
impl_actions,
platform::{WindowBounds, WindowOptions},
AssetSource, AsyncAppContext, ClipboardItem, TitlebarOptions, ViewContext, WindowKind,
AssetSource, AsyncAppContext, TitlebarOptions, ViewContext, WindowKind,
};
use language::Rope;
use lazy_static::lazy_static;
@ -34,7 +31,6 @@ use serde::Deserialize;
use serde_json::to_string_pretty;
use settings::{keymap_file_json_schema, settings_file_json_schema, Settings};
use std::{env, path::Path, str, sync::Arc};
use system_specs::SystemSpecs;
use util::{channel::ReleaseChannel, paths, ResultExt};
pub use workspace;
use workspace::{sidebar::SidebarSide, AppState, Workspace};
@ -69,9 +65,6 @@ actions!(
ResetBufferFontSize,
InstallCommandLineInterface,
ResetDatabase,
CopySystemSpecsIntoClipboard,
RequestFeature,
FileBugReport
]
);
@ -250,41 +243,6 @@ pub fn init(app_state: &Arc<AppState>, cx: &mut gpui::MutableAppContext) {
},
);
cx.add_action(
|_: &mut Workspace, _: &CopySystemSpecsIntoClipboard, cx: &mut ViewContext<Workspace>| {
let system_specs = SystemSpecs::new(cx).to_string();
let item = ClipboardItem::new(system_specs.clone());
cx.prompt(
gpui::PromptLevel::Info,
&format!("Copied into clipboard:\n\n{system_specs}"),
&["OK"],
);
cx.write_to_clipboard(item);
},
);
cx.add_action(
|_: &mut Workspace, _: &RequestFeature, cx: &mut ViewContext<Workspace>| {
let url = "https://github.com/zed-industries/feedback/issues/new?assignees=&labels=enhancement%2Ctriage&template=0_feature_request.yml";
cx.dispatch_action(OpenBrowser {
url: url.into(),
});
},
);
cx.add_action(
|_: &mut Workspace, _: &FileBugReport, cx: &mut ViewContext<Workspace>| {
let system_specs_text = SystemSpecs::new(cx).to_string();
let url = format!(
"https://github.com/zed-industries/feedback/issues/new?assignees=&labels=defect%2Ctriage&template=2_bug_report.yml&environment={}",
urlencoding::encode(&system_specs_text)
);
cx.dispatch_action(OpenBrowser {
url: url.into(),
});
},
);
activity_indicator::init(cx);
call::init(app_state.client.clone(), app_state.user_store.clone(), cx);
settings::KeymapFileContent::load_defaults(cx);
@ -369,12 +327,12 @@ pub fn initialize_workspace(
let activity_indicator =
activity_indicator::ActivityIndicator::new(workspace, app_state.languages.clone(), cx);
let cursor_position = cx.add_view(|_| editor::items::CursorPosition::new());
let feedback_link = cx.add_view(|_| feedback_popover::FeedbackButton::new());
let feedback_button = cx.add_view(|_| feedback::feedback_popover::FeedbackButton::new());
workspace.status_bar().update(cx, |status_bar, cx| {
status_bar.add_left_item(diagnostic_summary, cx);
status_bar.add_left_item(activity_indicator, cx);
status_bar.add_right_item(cursor_position, cx);
status_bar.add_right_item(feedback_link, cx);
status_bar.add_right_item(feedback_button, cx);
});
auto_update::notify_of_any_new_update(cx.weak_handle(), cx);