Skip to content

Commit e6d6806

Browse files
committed
Tidy up some more modal behaviour
1 parent a73265a commit e6d6806

File tree

5 files changed

+27
-41
lines changed

5 files changed

+27
-41
lines changed

crates/command_palette2/src/command_palette.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,7 @@ impl CommandPalette {
6565
let delegate =
6666
CommandPaletteDelegate::new(cx.view().downgrade(), commands, previous_focus_handle, cx);
6767

68-
let picker = cx.build_view(|cx| {
69-
let picker = Picker::new(delegate, cx);
70-
picker.focus(cx);
71-
picker
72-
});
68+
let picker = cx.build_view(|cx| Picker::new(delegate, cx));
7369
Self { picker }
7470
}
7571
}

crates/go_to_line2/src/go_to_line.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,10 +126,6 @@ impl GoToLine {
126126
}
127127

128128
fn cancel(&mut self, _: &menu::Cancel, cx: &mut ViewContext<Self>) {
129-
self.active_editor.update(cx, |editor, cx| {
130-
editor.focus(cx);
131-
cx.notify();
132-
});
133129
cx.emit(ModalEvent::Dismissed);
134130
}
135131

crates/gpui2/src/elements/uniform_list.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -291,11 +291,3 @@ impl<V: 'static> Component<V> for UniformList<V> {
291291
AnyElement::new(self)
292292
}
293293
}
294-
295-
#[cfg(test)]
296-
mod test {
297-
use crate::{self as gpui, TestAppContext};
298-
299-
#[gpui::test]
300-
fn test_uniform_list(cx: &mut TestAppContext) {}
301-
}

crates/gpui2/src/interactive.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,6 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
9494

9595
fn on_mouse_down_out(
9696
mut self,
97-
button: MouseButton,
9897
handler: impl Fn(&mut V, &MouseDownEvent, &mut ViewContext<V>) + 'static,
9998
) -> Self
10099
where
@@ -103,10 +102,7 @@ pub trait StatelessInteractive<V: 'static>: Element<V> {
103102
self.stateless_interactivity()
104103
.mouse_down_listeners
105104
.push(Box::new(move |view, event, bounds, phase, cx| {
106-
if phase == DispatchPhase::Capture
107-
&& event.button == button
108-
&& !bounds.contains_point(&event.position)
109-
{
105+
if phase == DispatchPhase::Capture && !bounds.contains_point(&event.position) {
110106
handler(view, event, cx)
111107
}
112108
}));

crates/workspace2/src/modal_layer.rs

Lines changed: 25 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -55,31 +55,38 @@ impl ModalLayer {
5555
let build_view = build_view.clone();
5656

5757
div.on_action(move |workspace, event: &A, cx| {
58-
let previous_focus = cx.focused();
59-
if let Some(active_modal) = &workspace.modal_layer().active_modal {
60-
if active_modal.modal.clone().downcast::<V>().is_ok() {
61-
workspace.modal_layer().hide_modal(cx);
62-
return;
63-
}
64-
}
65-
let Some(new_modal) = (build_view)(cx) else {
66-
return;
67-
};
68-
workspace
69-
.modal_layer()
70-
.show_modal(previous_focus, new_modal, cx);
58+
workspace.modal_layer().toggle_modal(build_view.clone(), cx)
7159
})
7260
}),
7361
));
7462
}
7563

64+
pub fn toggle_modal<V, B>(&mut self, build_view: Arc<B>, cx: &mut ViewContext<Workspace>)
65+
where
66+
V: Modal,
67+
B: Fn(&mut WindowContext) -> Option<View<V>> + 'static,
68+
{
69+
let previous_focus = cx.focused();
70+
71+
if let Some(active_modal) = &self.active_modal {
72+
if active_modal.modal.clone().downcast::<V>().is_ok() {
73+
self.hide_modal(cx);
74+
return;
75+
}
76+
}
77+
let Some(new_modal) = (build_view)(cx) else {
78+
return;
79+
};
80+
self.show_modal(previous_focus, new_modal, cx);
81+
}
82+
7683
pub fn show_modal<V>(
7784
&mut self,
7885
previous_focus: Option<FocusHandle>,
7986
new_modal: View<V>,
8087
cx: &mut ViewContext<Workspace>,
8188
) where
82-
V: EventEmitter<ModalEvent> + Render,
89+
V: Modal,
8390
{
8491
self.active_modal = Some(ActiveModal {
8592
modal: new_modal.clone().into(),
@@ -93,13 +100,9 @@ impl ModalLayer {
93100
}
94101

95102
pub fn hide_modal(&mut self, cx: &mut ViewContext<Workspace>) {
96-
dbg!("hiding...");
97103
if let Some(active_modal) = self.active_modal.take() {
98-
dbg!("something");
99104
if let Some(previous_focus) = active_modal.previous_focus_handle {
100-
dbg!("oohthing");
101105
if active_modal.focus_handle.contains_focused(cx) {
102-
dbg!("aahthing");
103106
previous_focus.focus(cx);
104107
}
105108
}
@@ -133,7 +136,10 @@ impl ModalLayer {
133136
.h(px(0.0))
134137
.relative()
135138
.top_20()
136-
.track_focus(&open_modal.focus_handle);
139+
.track_focus(&open_modal.focus_handle)
140+
.on_mouse_down_out(|workspace: &mut Workspace, _, cx| {
141+
workspace.modal_layer().hide_modal(cx);
142+
});
137143

138144
parent.child(container1.child(container2.child(open_modal.modal.clone())))
139145
})

0 commit comments

Comments
 (0)