Support press esc to close popover (#188)
This commit is contained in:
parent
311abe07fe
commit
b6ce44c349
1 changed files with 12 additions and 5 deletions
|
|
@ -1,17 +1,21 @@
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, anchored, deferred, div, prelude::FluentBuilder as _, AnchorCorner, AnyElement,
|
actions, anchored, deferred, div, prelude::FluentBuilder as _, AnchorCorner, AnyElement,
|
||||||
AppContext, Bounds, DismissEvent, DispatchPhase, Element, ElementId, EventEmitter, FocusHandle,
|
AppContext, Bounds, DismissEvent, DispatchPhase, Element, ElementId, EventEmitter, FocusHandle,
|
||||||
FocusableView, GlobalElementId, Hitbox, InteractiveElement as _, IntoElement, LayoutId,
|
FocusableView, GlobalElementId, Hitbox, InteractiveElement as _, IntoElement, KeyBinding,
|
||||||
ManagedView, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, Render, Style, Styled,
|
LayoutId, ManagedView, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, Render,
|
||||||
View, ViewContext, VisualContext, WindowContext,
|
Style, Styled, View, ViewContext, VisualContext, WindowContext,
|
||||||
};
|
};
|
||||||
use std::{cell::RefCell, rc::Rc};
|
use std::{cell::RefCell, rc::Rc};
|
||||||
|
|
||||||
use crate::{Selectable, StyledExt as _};
|
use crate::{Selectable, StyledExt as _};
|
||||||
|
|
||||||
actions!(popover, [Open, Dismiss]);
|
const CONTEXT: &str = "Popover";
|
||||||
|
|
||||||
pub fn init(_cx: &mut AppContext) {}
|
actions!(popover, [Escape]);
|
||||||
|
|
||||||
|
pub fn init(cx: &mut AppContext) {
|
||||||
|
cx.bind_keys([KeyBinding::new("escape", Escape, Some(CONTEXT))])
|
||||||
|
}
|
||||||
|
|
||||||
pub struct PopoverContent {
|
pub struct PopoverContent {
|
||||||
focus_handle: FocusHandle,
|
focus_handle: FocusHandle,
|
||||||
|
|
@ -49,6 +53,9 @@ impl FocusableView for PopoverContent {
|
||||||
impl Render for PopoverContent {
|
impl Render for PopoverContent {
|
||||||
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
|
||||||
div()
|
div()
|
||||||
|
.track_focus(&self.focus_handle)
|
||||||
|
.key_context(CONTEXT)
|
||||||
|
.on_action(cx.listener(|_, _: &Escape, cx| cx.emit(DismissEvent)))
|
||||||
.p_2()
|
.p_2()
|
||||||
.when_some(self.max_width, |this, v| this.max_w(v))
|
.when_some(self.max_width, |this, v| this.max_w(v))
|
||||||
.child(self.content.clone()(cx))
|
.child(self.content.clone()(cx))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue