Popover content supports setting the maximum width (#180)
Before:  After: 
This commit is contained in:
parent
7b5efd19bb
commit
a5217df13e
4 changed files with 61 additions and 51 deletions
|
|
@ -185,10 +185,12 @@ impl Render for PopupStory {
|
||||||
Popover::new("info-top-left")
|
Popover::new("info-top-left")
|
||||||
.trigger(Button::new("info-top-left", cx).label("Top Left"))
|
.trigger(Button::new("info-top-left", cx).label("Top Left"))
|
||||||
.content(|cx| {
|
.content(|cx| {
|
||||||
|
cx.new_view(|cx| {
|
||||||
PopoverContent::new(cx, |cx| {
|
PopoverContent::new(cx, |cx| {
|
||||||
v_flex()
|
v_flex()
|
||||||
.gap_4()
|
.gap_4()
|
||||||
.child("Hello, this is a Popover.")
|
.child("Hello, this is a Popover.")
|
||||||
|
.w(px(400.))
|
||||||
.child(Divider::horizontal())
|
.child(Divider::horizontal())
|
||||||
.child(
|
.child(
|
||||||
Button::new("info1", cx)
|
Button::new("info1", cx)
|
||||||
|
|
@ -197,8 +199,9 @@ impl Render for PopupStory {
|
||||||
.small(),
|
.small(),
|
||||||
)
|
)
|
||||||
.into_any()
|
.into_any()
|
||||||
|
}).max_w(px(600.))
|
||||||
|
})
|
||||||
})
|
})
|
||||||
}),
|
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
.child(
|
.child(
|
||||||
|
|
@ -206,6 +209,7 @@ impl Render for PopupStory {
|
||||||
.anchor(AnchorCorner::TopRight)
|
.anchor(AnchorCorner::TopRight)
|
||||||
.trigger(Button::new("info-top-right", cx).label("Top Right"))
|
.trigger(Button::new("info-top-right", cx).label("Top Right"))
|
||||||
.content(|cx| {
|
.content(|cx| {
|
||||||
|
cx.new_view(|cx|
|
||||||
PopoverContent::new(cx, |cx| {
|
PopoverContent::new(cx, |cx| {
|
||||||
v_flex()
|
v_flex()
|
||||||
.gap_4()
|
.gap_4()
|
||||||
|
|
@ -219,7 +223,7 @@ impl Render for PopupStory {
|
||||||
.small(),
|
.small(),
|
||||||
)
|
)
|
||||||
.into_any()
|
.into_any()
|
||||||
})
|
}))
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
)
|
)
|
||||||
|
|
@ -280,6 +284,7 @@ impl Render for PopupStory {
|
||||||
.w(px(300.)),
|
.w(px(300.)),
|
||||||
)
|
)
|
||||||
.content(|cx| {
|
.content(|cx| {
|
||||||
|
cx.new_view(|cx|
|
||||||
PopoverContent::new(cx, |cx| {
|
PopoverContent::new(cx, |cx| {
|
||||||
v_flex()
|
v_flex()
|
||||||
.gap_4()
|
.gap_4()
|
||||||
|
|
@ -292,7 +297,7 @@ impl Render for PopupStory {
|
||||||
.small(),
|
.small(),
|
||||||
)
|
)
|
||||||
.into_any()
|
.into_any()
|
||||||
})
|
}))
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
|
|
|
||||||
|
|
@ -102,7 +102,7 @@ impl Modal {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set the maximum width of the modal, defaults to `None`.
|
/// Set the maximum width of the modal, defaults to `None`.
|
||||||
pub fn max_width(mut self, max_width: Pixels) -> Self {
|
pub fn max_w(mut self, max_width: Pixels) -> Self {
|
||||||
self.max_width = Some(max_width);
|
self.max_width = Some(max_width);
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,21 +16,26 @@ 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 WindowContext) -> AnyElement>,
|
||||||
|
max_width: Option<Pixels>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl PopoverContent {
|
impl PopoverContent {
|
||||||
pub fn new<B>(cx: &mut WindowContext, content: B) -> View<Self>
|
pub fn new<B>(cx: &mut WindowContext, content: B) -> Self
|
||||||
where
|
where
|
||||||
B: Fn(&mut WindowContext) -> AnyElement + 'static,
|
B: Fn(&mut WindowContext) -> AnyElement + 'static,
|
||||||
{
|
{
|
||||||
cx.new_view(|cx| {
|
|
||||||
let focus_handle = cx.focus_handle();
|
let focus_handle = cx.focus_handle();
|
||||||
|
|
||||||
Self {
|
Self {
|
||||||
focus_handle,
|
focus_handle,
|
||||||
content: Rc::new(content),
|
content: Rc::new(content),
|
||||||
|
max_width: None,
|
||||||
}
|
}
|
||||||
})
|
}
|
||||||
|
|
||||||
|
pub fn max_w(mut self, max_width: Pixels) -> Self {
|
||||||
|
self.max_width = Some(max_width);
|
||||||
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl EventEmitter<DismissEvent> for PopoverContent {}
|
impl EventEmitter<DismissEvent> for PopoverContent {}
|
||||||
|
|
@ -43,7 +48,7 @@ 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().p_4().max_w_128().child(self.content.clone()(cx))
|
div().p_4().when_some(self.max_width, |this, v| this.max_w(v)).child(self.content.clone()(cx))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -309,8 +314,8 @@ impl<M: ManagedView> Element for Popover<M> {
|
||||||
|
|
||||||
fn paint(
|
fn paint(
|
||||||
&mut self,
|
&mut self,
|
||||||
id: Option<&gpui::GlobalElementId>,
|
id: Option<&GlobalElementId>,
|
||||||
_bounds: gpui::Bounds<gpui::Pixels>,
|
_bounds: Bounds<Pixels>,
|
||||||
request_layout: &mut Self::RequestLayoutState,
|
request_layout: &mut Self::RequestLayoutState,
|
||||||
prepaint: &mut Self::PrepaintState,
|
prepaint: &mut Self::PrepaintState,
|
||||||
cx: &mut WindowContext,
|
cx: &mut WindowContext,
|
||||||
|
|
|
||||||
|
|
@ -113,8 +113,8 @@ impl PopupMenu {
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Set max width of the popup menu, default is 500px
|
/// Set max width of the popup menu, default is 500px
|
||||||
pub fn max_w(mut self, height: impl Into<Pixels>) -> Self {
|
pub fn max_w(mut self, width: impl Into<Pixels>) -> Self {
|
||||||
self.max_width = height.into();
|
self.max_width = width.into();
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue