Skip to content

Commit 174fc51

Browse files
authored
Added support for dismissing the toolchain path selector via esc (zed-industries#48201)
Release Notes: - Added support for dismissing the toolchain path selector via `esc`.
1 parent 324edc1 commit 174fc51

1 file changed

Lines changed: 26 additions & 13 deletions

File tree

crates/toolchain_selector/src/toolchain_selector.rs

Lines changed: 26 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -119,10 +119,20 @@ impl AddToolchainState {
119119
picker
120120
});
121121

122+
let weak_toolchain_selector = weak.clone();
123+
let window_handle = window.window_handle();
122124
Self {
123125
state: AddState::Path {
124-
_subscription: cx.subscribe(&picker, |_, _, _: &DismissEvent, cx| {
125-
cx.stop_propagation();
126+
_subscription: cx.subscribe(&picker, move |_, _, _: &DismissEvent, cx| {
127+
if let Some(toolchain_selector) = weak_toolchain_selector.upgrade() {
128+
window_handle
129+
.update(cx, |_, window, cx| {
130+
toolchain_selector.update(cx, |this, cx| {
131+
this.cancel(&menu::Cancel, window, cx);
132+
});
133+
})
134+
.ok();
135+
}
126136
}),
127137
picker,
128138
error: None,
@@ -384,7 +394,6 @@ impl Focusable for State {
384394
impl Render for AddToolchainState {
385395
fn render(&mut self, window: &mut Window, cx: &mut Context<Self>) -> impl IntoElement {
386396
let theme = cx.theme().clone();
387-
let weak = self.weak.upgrade();
388397
let label = SharedString::new_static("Add");
389398

390399
v_flex()
@@ -395,16 +404,6 @@ impl Render for AddToolchainState {
395404
.border_1()
396405
.border_color(cx.theme().colors().border_variant)
397406
.rounded_lg()
398-
.when_some(weak, |this, weak| {
399-
this.on_action(window.listener_for(
400-
&weak,
401-
|this: &mut ToolchainSelector, _: &menu::Cancel, window, cx| {
402-
this.state = State::Search((this.create_search_state)(window, cx));
403-
this.state.focus_handle(cx).focus(window, cx);
404-
cx.notify();
405-
},
406-
))
407-
})
408407
.on_action(cx.listener(Self::confirm_toolchain))
409408
.map(|this| match &self.state {
410409
AddState::Path { picker, .. } => this.child(picker.clone()),
@@ -727,6 +726,19 @@ impl ToolchainSelector {
727726
cx.notify();
728727
}
729728
}
729+
730+
fn cancel(&mut self, _: &menu::Cancel, window: &mut Window, cx: &mut Context<Self>) {
731+
match &self.state {
732+
State::Search(_) => {
733+
cx.emit(DismissEvent);
734+
}
735+
State::AddToolchain(_) => {
736+
self.state = State::Search((self.create_search_state)(window, cx));
737+
self.state.focus_handle(cx).focus(window, cx);
738+
cx.notify();
739+
}
740+
}
741+
}
730742
}
731743

732744
impl Render for ToolchainSelector {
@@ -737,6 +749,7 @@ impl Render for ToolchainSelector {
737749
v_flex()
738750
.key_context(key_context)
739751
.w(rems(34.))
752+
.on_action(cx.listener(Self::cancel))
740753
.on_action(cx.listener(Self::handle_add_toolchain))
741754
.child(self.state.clone().render(window, cx))
742755
}

0 commit comments

Comments
 (0)