popover: Fix PopoverContent content build to use ViewContext<Self>. (#254)

And add popover self dismiss example.

## How to dismiss popover from inside.

```rs
cx.emit(DismissEvent);
```
This commit is contained in:
Jason Lee 2024-09-19 10:54:04 +08:00 committed by GitHub
parent 9fc7b29f23
commit 20926b7077
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 29 additions and 7 deletions

View file

@ -14,7 +14,7 @@ use ui::{
popover::{Popover, PopoverContent}, popover::{Popover, PopoverContent},
popup_menu::PopupMenuExt, popup_menu::PopupMenuExt,
switch::Switch, switch::Switch,
v_flex, IconName, Sizable, v_flex, ContextModal, IconName, Sizable,
}; };
#[derive(Clone, PartialEq, Deserialize)] #[derive(Clone, PartialEq, Deserialize)]
@ -309,10 +309,32 @@ impl Render for PopupStory {
) )
.child(Divider::horizontal()) .child(Divider::horizontal())
.child( .child(
Button::new("info1", cx) h_flex()
.label("Yes") .gap_4()
.w(px(80.)) .child(
.small(), Button::new("info1", cx)
.label("Ok")
.w(px(80.))
.small()
.on_click(cx.listener(
|_, _, cx| {
cx.push_notification(
"You have clicked Ok.",
);
cx.emit(DismissEvent);
},
)),
)
.child(
Button::new("close", cx)
.label("Cancel")
.small()
.on_click(cx.listener(
|_, _, cx| {
cx.emit(DismissEvent);
},
)),
),
) )
.into_any() .into_any()
}) })

View file

@ -19,14 +19,14 @@ pub fn init(cx: &mut AppContext) {
pub struct PopoverContent { pub struct PopoverContent {
focus_handle: FocusHandle, focus_handle: FocusHandle,
content: Rc<dyn Fn(&mut WindowContext) -> AnyElement>, content: Rc<dyn Fn(&mut ViewContext<Self>) -> AnyElement>,
max_width: Option<Pixels>, max_width: Option<Pixels>,
} }
impl PopoverContent { impl PopoverContent {
pub fn new<B>(cx: &mut WindowContext, content: B) -> Self pub fn new<B>(cx: &mut WindowContext, content: B) -> Self
where where
B: Fn(&mut WindowContext) -> AnyElement + 'static, B: Fn(&mut ViewContext<Self>) -> AnyElement + 'static,
{ {
let focus_handle = cx.focus_handle(); let focus_handle = cx.focus_handle();