Remove ViewContext::dispatch_action

This commit is contained in:
Antonio Scandurra 2023-05-01 15:48:41 +02:00
parent d815fc88ae
commit c4472b0786
41 changed files with 574 additions and 670 deletions

View file

@ -1,10 +1,4 @@
use std::{
any::TypeId,
borrow::Cow,
ops::{Range, RangeInclusive},
sync::Arc,
};
use crate::system_specs::SystemSpecs;
use anyhow::bail;
use client::{Client, ZED_SECRET_CLIENT_TOKEN, ZED_SERVER_URL};
use editor::{Anchor, Editor};
@ -19,40 +13,34 @@ use gpui::{
use isahc::Request;
use language::Buffer;
use postage::prelude::Stream;
use project::Project;
use serde::Serialize;
use std::{
any::TypeId,
borrow::Cow,
ops::{Range, RangeInclusive},
sync::Arc,
};
use util::ResultExt;
use workspace::{
item::{Item, ItemHandle},
item::{Item, ItemEvent, ItemHandle},
searchable::{SearchableItem, SearchableItemHandle},
AppState, Workspace,
smallvec::SmallVec,
Workspace,
};
use crate::{submit_feedback_button::SubmitFeedbackButton, system_specs::SystemSpecs};
const FEEDBACK_CHAR_LIMIT: RangeInclusive<usize> = 10..=5000;
const FEEDBACK_SUBMISSION_ERROR_TEXT: &str =
"Feedback failed to submit, see error log for details.";
actions!(feedback, [GiveFeedback, SubmitFeedback]);
pub fn init(system_specs: SystemSpecs, app_state: Arc<AppState>, cx: &mut AppContext) {
pub fn init(cx: &mut AppContext) {
cx.add_action({
move |workspace: &mut Workspace, _: &GiveFeedback, cx: &mut ViewContext<Workspace>| {
FeedbackEditor::deploy(system_specs.clone(), workspace, app_state.clone(), cx);
FeedbackEditor::deploy(workspace, cx);
}
});
cx.add_async_action(
|submit_feedback_button: &mut SubmitFeedbackButton, _: &SubmitFeedback, cx| {
if let Some(active_item) = submit_feedback_button.active_item.as_ref() {
Some(active_item.update(cx, |feedback_editor, cx| feedback_editor.handle_save(cx)))
} else {
None
}
},
);
}
#[derive(Serialize)]
@ -94,7 +82,7 @@ impl FeedbackEditor {
}
}
fn handle_save(&mut self, cx: &mut ViewContext<Self>) -> Task<anyhow::Result<()>> {
pub fn submit(&mut self, cx: &mut ViewContext<Self>) -> Task<anyhow::Result<()>> {
let feedback_text = self.editor.read(cx).text(cx);
let feedback_char_count = feedback_text.chars().count();
let feedback_text = feedback_text.trim().to_string();
@ -133,10 +121,8 @@ impl FeedbackEditor {
if answer == Some(0) {
match FeedbackEditor::submit_feedback(&feedback_text, client, specs).await {
Ok(_) => {
this.update(&mut cx, |_, cx| {
cx.dispatch_action(workspace::CloseActiveItem);
})
.log_err();
this.update(&mut cx, |_, cx| cx.emit(editor::Event::Closed))
.log_err();
}
Err(error) => {
log::error!("{}", error);
@ -198,22 +184,21 @@ impl FeedbackEditor {
}
impl FeedbackEditor {
pub fn deploy(
system_specs: SystemSpecs,
_: &mut Workspace,
app_state: Arc<AppState>,
cx: &mut ViewContext<Workspace>,
) {
let markdown = app_state.languages.language_for_name("Markdown");
pub fn deploy(workspace: &mut Workspace, cx: &mut ViewContext<Workspace>) {
let markdown = workspace
.app_state()
.languages
.language_for_name("Markdown");
cx.spawn(|workspace, mut cx| async move {
let markdown = markdown.await.log_err();
workspace
.update(&mut cx, |workspace, cx| {
workspace.with_local_workspace(&app_state, cx, |workspace, cx| {
workspace.with_local_workspace(cx, |workspace, cx| {
let project = workspace.project().clone();
let buffer = project
.update(cx, |project, cx| project.create_buffer("", markdown, cx))
.expect("creating buffers on a local workspace always succeeds");
let system_specs = SystemSpecs::new(cx);
let feedback_editor = cx
.add_view(|cx| FeedbackEditor::new(system_specs, project, buffer, cx));
workspace.add_item(Box::new(feedback_editor), cx);
@ -291,7 +276,7 @@ impl Item for FeedbackEditor {
_: ModelHandle<Project>,
cx: &mut ViewContext<Self>,
) -> Task<anyhow::Result<()>> {
self.handle_save(cx)
self.submit(cx)
}
fn save_as(
@ -300,7 +285,7 @@ impl Item for FeedbackEditor {
_: std::path::PathBuf,
cx: &mut ViewContext<Self>,
) -> Task<anyhow::Result<()>> {
self.handle_save(cx)
self.submit(cx)
}
fn reload(
@ -353,6 +338,10 @@ impl Item for FeedbackEditor {
None
}
}
fn to_item_events(event: &Self::Event) -> SmallVec<[ItemEvent; 2]> {
Editor::to_item_events(event)
}
}
impl SearchableItem for FeedbackEditor {