date_picker: Avoid focus back to DatePicker, when dismiss picker by clicked on Input. (#567)

This commit is contained in:
Jason Lee 2025-01-23 11:52:58 +08:00 committed by GitHub
parent 78ce573a52
commit 4028f46cb9
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -184,11 +184,30 @@ impl DatePicker {
}
fn escape(&mut self, _: &Escape, cx: &mut ViewContext<Self>) {
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<Self>) {
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<Self>) {
match self.date {
Date::Single(_) => {
@ -232,7 +251,8 @@ impl FocusableView for DatePicker {
impl Render for DatePicker {
fn render(&mut self, cx: &mut ViewContext<Self>) -> 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()