diff --git a/crates/ui/src/time/date_picker.rs b/crates/ui/src/time/date_picker.rs index ac55e79a..85c8b0f5 100644 --- a/crates/ui/src/time/date_picker.rs +++ b/crates/ui/src/time/date_picker.rs @@ -184,11 +184,30 @@ impl DatePicker { } fn escape(&mut self, _: &Escape, cx: &mut ViewContext) { + self.focus_back_if_need(cx); self.open = false; - self.focus_handle.focus(cx); + cx.notify(); } + // To focus the Picker Input, if current focus in is on the container. + // + // This is because mouse down out the Calendar, GPUI will move focus to the container. + // So we need to move focus back to the Picker Input. + // + // But if mouse down target is some other focusable element (e.g.: TextInput), we should not move focus. + fn focus_back_if_need(&mut self, cx: &mut ViewContext) { + if !self.open { + return; + } + + if let Some(focused) = cx.focused() { + if focused.contains(&self.focus_handle, cx) { + self.focus_handle.focus(cx); + } + } + } + fn clean(&mut self, _: &gpui::ClickEvent, cx: &mut ViewContext) { match self.date { Date::Single(_) => { @@ -232,7 +251,8 @@ impl FocusableView for DatePicker { impl Render for DatePicker { fn render(&mut self, cx: &mut ViewContext) -> impl gpui::IntoElement { - let is_focused = self.focus_handle.is_focused(cx); + // This for keep focus border style, when click on the popup. + let is_focused = self.focus_handle.contains_focused(cx); let show_clean = self.cleanable && self.date.is_some(); let placeholder = self .placeholder @@ -304,7 +324,6 @@ impl Render for DatePicker { deferred( anchored().snap_to_window_with_margin(px(8.)).child( div() - .track_focus(&self.focus_handle) .occlude() .mt_1p5() .rounded_lg() @@ -316,7 +335,9 @@ impl Render for DatePicker { .bg(cx.theme().background) .on_mouse_up_out( MouseButton::Left, - cx.listener(|view, _, cx| view.escape(&Escape, cx)), + cx.listener(|view, _, cx| { + view.escape(&Escape, cx); + }), ) .child( h_flex()