date_picker: Avoid focus back to DatePicker, when dismiss picker by clicked on Input. (#567)
This commit is contained in:
parent
78ce573a52
commit
4028f46cb9
1 changed files with 25 additions and 4 deletions
|
|
@ -184,11 +184,30 @@ impl DatePicker {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn escape(&mut self, _: &Escape, cx: &mut ViewContext<Self>) {
|
fn escape(&mut self, _: &Escape, cx: &mut ViewContext<Self>) {
|
||||||
|
self.focus_back_if_need(cx);
|
||||||
self.open = false;
|
self.open = false;
|
||||||
self.focus_handle.focus(cx);
|
|
||||||
cx.notify();
|
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>) {
|
fn clean(&mut self, _: &gpui::ClickEvent, cx: &mut ViewContext<Self>) {
|
||||||
match self.date {
|
match self.date {
|
||||||
Date::Single(_) => {
|
Date::Single(_) => {
|
||||||
|
|
@ -232,7 +251,8 @@ impl FocusableView for DatePicker {
|
||||||
|
|
||||||
impl Render for DatePicker {
|
impl Render for DatePicker {
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl gpui::IntoElement {
|
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 show_clean = self.cleanable && self.date.is_some();
|
||||||
let placeholder = self
|
let placeholder = self
|
||||||
.placeholder
|
.placeholder
|
||||||
|
|
@ -304,7 +324,6 @@ impl Render for DatePicker {
|
||||||
deferred(
|
deferred(
|
||||||
anchored().snap_to_window_with_margin(px(8.)).child(
|
anchored().snap_to_window_with_margin(px(8.)).child(
|
||||||
div()
|
div()
|
||||||
.track_focus(&self.focus_handle)
|
|
||||||
.occlude()
|
.occlude()
|
||||||
.mt_1p5()
|
.mt_1p5()
|
||||||
.rounded_lg()
|
.rounded_lg()
|
||||||
|
|
@ -316,7 +335,9 @@ impl Render for DatePicker {
|
||||||
.bg(cx.theme().background)
|
.bg(cx.theme().background)
|
||||||
.on_mouse_up_out(
|
.on_mouse_up_out(
|
||||||
MouseButton::Left,
|
MouseButton::Left,
|
||||||
cx.listener(|view, _, cx| view.escape(&Escape, cx)),
|
cx.listener(|view, _, cx| {
|
||||||
|
view.escape(&Escape, cx);
|
||||||
|
}),
|
||||||
)
|
)
|
||||||
.child(
|
.child(
|
||||||
h_flex()
|
h_flex()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue