Support press esc to close popover (#188)

This commit is contained in:
Floyd Wang 2024-08-29 18:07:26 +08:00 committed by GitHub
parent 311abe07fe
commit b6ce44c349
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,17 +1,21 @@
use gpui::{
actions, anchored, deferred, div, prelude::FluentBuilder as _, AnchorCorner, AnyElement,
AppContext, Bounds, DismissEvent, DispatchPhase, Element, ElementId, EventEmitter, FocusHandle,
FocusableView, GlobalElementId, Hitbox, InteractiveElement as _, IntoElement, LayoutId,
ManagedView, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, Render, Style, Styled,
View, ViewContext, VisualContext, WindowContext,
FocusableView, GlobalElementId, Hitbox, InteractiveElement as _, IntoElement, KeyBinding,
LayoutId, ManagedView, MouseButton, MouseDownEvent, ParentElement, Pixels, Point, Render,
Style, Styled, View, ViewContext, VisualContext, WindowContext,
};
use std::{cell::RefCell, rc::Rc};
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 {
focus_handle: FocusHandle,
@ -49,6 +53,9 @@ impl FocusableView for PopoverContent {
impl Render for PopoverContent {
fn render(&mut self, cx: &mut ViewContext<Self>) -> impl IntoElement {
div()
.track_focus(&self.focus_handle)
.key_context(CONTEXT)
.on_action(cx.listener(|_, _: &Escape, cx| cx.emit(DismissEvent)))
.p_2()
.when_some(self.max_width, |this, v| this.max_w(v))
.child(self.content.clone()(cx))