History

fix modal exchange (#3620)

- Fix opening GoToLine from Command - Extend test to cover go_to_line behavior too Release Notes: - N/A

Conrad Irwin committed 2 years ago

Commit e91ecec2 parents c94c661 979db256 files · +89 −37Browse files

Unified · Split

6 files
  1. Cargo.lock +2 −0
  2. crates/command_palette2/Cargo.toml +2 −0
  3. crates/command_palette2/src/command_palette.rs +36 −1
  4. crates/feedback2/src/feedback_modal.rs +21 −5
  5. crates/go_to_line2/src/go_to_line.rs +0 −1
  6. crates/workspace2/src/modal_layer.rs +28 −30
Cargo.lock+2−0
@@ -2046,8 +2046,10 @@ dependencies = [ · 2045 unmodified lines
20462046 "editor2",
20472047 "env_logger 0.9.3",
20482048 "fuzzy2",
2049+ "go_to_line2",
20492050 "gpui2",
20502051 "language2",
2052+ "menu2",
20512053 "picker2",
20522054 "project2",
20532055 "serde",
crates/command_palette2/Cargo.toml+2−0
@@ -28,6 +28,8 @@ gpui = { package = "gpui2", path = "../gpui2", features = ["test-support"] } · 27 unmodified lines
2828 editor = { package = "editor2", path = "../editor2", features = ["test-support"] }
2929 language = { package="language2", path = "../language2", features = ["test-support"] }
3030 project = { package="project2", path = "../project2", features = ["test-support"] }
31+menu = { package = "menu2", path = "../menu2" }
32+go_to_line = { package = "go_to_line2", path = "../go_to_line2" }
3133 serde_json.workspace = true
3234 workspace = { package="workspace2", path = "../workspace2", features = ["test-support"] }
3335 ctor.workspace = true
crates/command_palette2/src/command_palette.rs+36−1
@@ -362,7 +362,9 @@ mod tests { · 361 unmodified lines
362362
363363 use super::*;
364364 use editor::Editor;
365+ use go_to_line::GoToLine;
365366 use gpui::TestAppContext;
367+ use language::Point;
366368 use project::Project;
367369 use workspace::{AppState, Workspace};
368370
@@ -385,7 +387,6 @@ mod tests { · 16 unmodified lines
385387 #[gpui::test]
386388 async fn test_command_palette(cx: &mut TestAppContext) {
387389 let app_state = init_test(cx);
388−
389390 let project = Project::test(app_state.fs.clone(), [], cx).await;
390391 let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
391392
@@ -455,12 +456,46 @@ mod tests { · 63 unmodified lines
455456 });
456457 }
457458
459+ #[gpui::test]
460+ async fn test_go_to_line(cx: &mut TestAppContext) {
461+ let app_state = init_test(cx);
462+ let project = Project::test(app_state.fs.clone(), [], cx).await;
463+ let (workspace, cx) = cx.add_window_view(|cx| Workspace::test_new(project.clone(), cx));
464+
465+ cx.simulate_keystrokes("cmd-n");
466+
467+ let editor = workspace.update(cx, |workspace, cx| {
468+ workspace.active_item_as::<Editor>(cx).unwrap()
469+ });
470+ editor.update(cx, |editor, cx| editor.set_text("1\n2\n3\n4\n5\n6\n", cx));
471+
472+ cx.simulate_keystrokes("cmd-shift-p");
473+ cx.simulate_input("go to line: Toggle");
474+ cx.simulate_keystrokes("enter");
475+
476+ workspace.update(cx, |workspace, cx| {
477+ assert!(workspace.active_modal::<GoToLine>(cx).is_some())
478+ });
479+
480+ cx.simulate_keystrokes("3 enter");
481+
482+ editor.update(cx, |editor, cx| {
483+ assert!(editor.focus_handle(cx).is_focused(cx));
484+ assert_eq!(
485+ editor.selections.last::<Point>(cx).range().start,
486+ Point::new(2, 0)
487+ );
488+ });
489+ }
490+
458491 fn init_test(cx: &mut TestAppContext) -> Arc<AppState> {
459492 cx.update(|cx| {
460493 let app_state = AppState::test(cx);
461494 theme::init(theme::LoadThemes::JustBase, cx);
462495 language::init(cx);
463496 editor::init(cx);
497+ menu::init();
498+ go_to_line::init(cx);
464499 workspace::init(app_state.clone(), cx);
465500 init(cx);
466501 Project::init_settings(cx);
crates/feedback2/src/feedback_modal.rs+21−5
@@ -53,6 +53,7 @@ pub struct FeedbackModal { · 52 unmodified lines
5353 email_address_editor: View<Editor>,
5454 awaiting_submission: bool,
5555 user_submitted: bool,
56+ discarded: bool,
5657 character_count: i32,
5758 }
5859
@@ -64,21 +65,35 @@ impl FocusableView for FeedbackModal { · 5 unmodified lines
6465 impl EventEmitter<DismissEvent> for FeedbackModal {}
6566
6667 impl ModalView for FeedbackModal {
67− fn dismiss(&mut self, cx: &mut ViewContext<Self>) -> Task<bool> {
68+ fn on_before_dismiss(&mut self, cx: &mut ViewContext<Self>) -> bool {
6869 if self.user_submitted {
6970 self.set_user_submitted(false, cx);
70− return cx.spawn(|_, _| async { true });
71+ return true;
7172 }
7273
73− let has_feedback = self.feedback_editor.read(cx).text_option(cx).is_some();
74+ if self.discarded {
75+ return true;
76+ }
7477
78+ let has_feedback = self.feedback_editor.read(cx).text_option(cx).is_some();
7579 if !has_feedback {
76− return cx.spawn(|_, _| async { true });
80+ return true;
7781 }
7882
7983 let answer = cx.prompt(PromptLevel::Info, "Discard feedback?", &["Yes", "No"]);
8084
81− cx.spawn(|_, _| async { answer.await.ok() == Some(0) })
85+ cx.spawn(move |this, mut cx| async move {
86+ if answer.await.ok() == Some(0) {
87+ this.update(&mut cx, |this, cx| {
88+ this.discarded = true;
89+ cx.emit(DismissEvent)
90+ })
91+ .log_err();
92+ }
93+ })
94+ .detach();
95+
96+ false
8297 }
8398 }
8499
@@ -169,6 +184,7 @@ impl FeedbackModal { · 84 unmodified lines
169184 email_address_editor,
170185 awaiting_submission: false,
171186 user_submitted: false,
187+ discarded: false,
172188 character_count: 0,
173189 }
174190 }
crates/go_to_line2/src/go_to_line.rs+0−1
@@ -92,7 +92,6 @@ impl GoToLine { · 91 unmodified lines
9292 cx: &mut ViewContext<Self>,
9393 ) {
9494 match event {
95− // todo!() this isn't working...
9695 editor::EditorEvent::Blurred => cx.emit(DismissEvent),
9796 editor::EditorEvent::BufferEdited { .. } => self.highlight_current_line(cx),
9897 _ => {}
crates/workspace2/src/modal_layer.rs+28−30
@@ -1,23 +1,23 @@
11 use gpui::{
2− div, prelude::*, px, AnyView, Div, FocusHandle, ManagedView, Render, Subscription, Task, View,
3− ViewContext, WindowContext,
2+ div, prelude::*, px, AnyView, DismissEvent, Div, FocusHandle, ManagedView, Render,
3+ Subscription, View, ViewContext, WindowContext,
44 };
55 use ui::{h_stack, v_stack};
66
77 pub trait ModalView: ManagedView {
8− fn dismiss(&mut self, cx: &mut ViewContext<Self>) -> Task<bool> {
9− Task::ready(true)
8+ fn on_before_dismiss(&mut self, cx: &mut ViewContext<Self>) -> bool {
9+ true
1010 }
1111 }
1212
1313 trait ModalViewHandle {
14− fn should_dismiss(&mut self, cx: &mut WindowContext) -> Task<bool>;
14+ fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool;
1515 fn view(&self) -> AnyView;
1616 }
1717
1818 impl<V: ModalView> ModalViewHandle for View<V> {
19− fn should_dismiss(&mut self, cx: &mut WindowContext) -> Task<bool> {
20− self.update(cx, |this, cx| this.dismiss(cx))
19+ fn on_before_dismiss(&mut self, cx: &mut WindowContext) -> bool {
20+ self.update(cx, |this, cx| this.on_before_dismiss(cx))
2121 }
2222
2323 fn view(&self) -> AnyView {
@@ -48,8 +48,8 @@ impl ModalLayer { · 24 unmodified lines
4848 {
4949 if let Some(active_modal) = &self.active_modal {
5050 let is_close = active_modal.modal.view().downcast::<V>().is_ok();
51− self.hide_modal(cx);
52− if is_close {
51+ let did_close = self.hide_modal(cx);
52+ if is_close || !did_close {
5353 return;
5454 }
5555 }
@@ -57,13 +57,15 @@ impl ModalLayer { · 1 unmodified line
5757 self.show_modal(new_modal, cx);
5858 }
5959
60− pub fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
60+ fn show_modal<V>(&mut self, new_modal: View<V>, cx: &mut ViewContext<Self>)
6161 where
6262 V: ModalView,
6363 {
6464 self.active_modal = Some(ActiveModal {
6565 modal: Box::new(new_modal.clone()),
66− subscription: cx.subscribe(&new_modal, |this, modal, e, cx| this.hide_modal(cx)),
66+ subscription: cx.subscribe(&new_modal, |this, modal, _: &DismissEvent, cx| {
67+ this.hide_modal(cx);
68+ }),
6769 previous_focus_handle: cx.focused(),
6870 focus_handle: cx.focus_handle(),
6971 });
@@ -71,29 +73,25 @@ impl ModalLayer { · 1 unmodified line
7173 cx.notify();
7274 }
7375
74− pub fn hide_modal(&mut self, cx: &mut ViewContext<Self>) {
76+ fn hide_modal(&mut self, cx: &mut ViewContext<Self>) -> bool {
7577 let Some(active_modal) = self.active_modal.as_mut() else {
76− return;
78+ return false;
7779 };
7880
79− let dismiss = active_modal.modal.should_dismiss(cx);
80−
81− cx.spawn(|this, mut cx| async move {
82− if dismiss.await {
83− this.update(&mut cx, |this, cx| {
84− if let Some(active_modal) = this.active_modal.take() {
85− if let Some(previous_focus) = active_modal.previous_focus_handle {
86− if active_modal.focus_handle.contains_focused(cx) {
87− previous_focus.focus(cx);
88− }
89− }
90− cx.notify();
91− }
92− })
93− .ok();
81+ let dismiss = active_modal.modal.on_before_dismiss(cx);
82+ if !dismiss {
83+ return false;
84+ }
85+
86+ if let Some(active_modal) = self.active_modal.take() {
87+ if let Some(previous_focus) = active_modal.previous_focus_handle {
88+ if active_modal.focus_handle.contains_focused(cx) {
89+ previous_focus.focus(cx);
90+ }
9491 }
95− })
96− .detach();
92+ cx.notify();
93+ }
94+ true
9795 }
9896
9997 pub fn active_modal<V>(&self) -> Option<View<V>>

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t