Add min_w, max_w to PopupMenu, and default min_w: 120, max_w: 500
This commit is contained in:
parent
4ca98029dd
commit
59603ab83b
1 changed files with 20 additions and 5 deletions
|
|
@ -3,8 +3,8 @@ use std::rc::Rc;
|
||||||
use gpui::{
|
use gpui::{
|
||||||
actions, div, prelude::FluentBuilder, px, Action, AnyWindowHandle, AppContext, Context,
|
actions, div, prelude::FluentBuilder, px, Action, AnyWindowHandle, AppContext, Context,
|
||||||
DismissEvent, EventEmitter, FocusHandle, FocusableView, InteractiveElement, KeyBinding,
|
DismissEvent, EventEmitter, FocusHandle, FocusableView, InteractiveElement, KeyBinding,
|
||||||
ParentElement, Render, SharedString, Styled as _, View, ViewContext, VisualContext as _,
|
ParentElement, Pixels, Render, SharedString, Styled as _, View, ViewContext,
|
||||||
WindowContext,
|
VisualContext as _, WindowContext,
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::{h_flex, list::ListItem, theme::ActiveTheme, v_flex, Icon};
|
use crate::{h_flex, list::ListItem, theme::ActiveTheme, v_flex, Icon};
|
||||||
|
|
@ -43,6 +43,8 @@ pub struct PopupMenu {
|
||||||
action_context: Option<FocusHandle>,
|
action_context: Option<FocusHandle>,
|
||||||
menu_items: Vec<PopupMenuItem>,
|
menu_items: Vec<PopupMenuItem>,
|
||||||
selected_index: Option<usize>,
|
selected_index: Option<usize>,
|
||||||
|
min_width: Pixels,
|
||||||
|
max_width: Pixels,
|
||||||
_subscriptions: [gpui::Subscription; 1],
|
_subscriptions: [gpui::Subscription; 1],
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -63,6 +65,8 @@ impl PopupMenu {
|
||||||
window_handle: cx.window_handle(),
|
window_handle: cx.window_handle(),
|
||||||
menu_items: Vec::new(),
|
menu_items: Vec::new(),
|
||||||
selected_index: None,
|
selected_index: None,
|
||||||
|
min_width: px(120.),
|
||||||
|
max_width: px(500.),
|
||||||
_subscriptions: [_on_blur_subscription],
|
_subscriptions: [_on_blur_subscription],
|
||||||
};
|
};
|
||||||
cx.refresh();
|
cx.refresh();
|
||||||
|
|
@ -70,6 +74,18 @@ impl PopupMenu {
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set min width of the popup menu, default is 120px
|
||||||
|
pub fn min_w(&mut self, width: impl Into<Pixels>) -> &mut Self {
|
||||||
|
self.min_width = width.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Set max width of the popup menu, default is 500px
|
||||||
|
pub fn max_w(&mut self, height: impl Into<Pixels>) -> &mut Self {
|
||||||
|
self.max_width = height.into();
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
/// You must set content (FocusHandle) with the parent view, if the menu action is listening on the parent view.
|
/// You must set content (FocusHandle) with the parent view, if the menu action is listening on the parent view.
|
||||||
/// When the Menu Item confirmed, the parent view will be focused again to ensure to receive the action.
|
/// When the Menu Item confirmed, the parent view will be focused again to ensure to receive the action.
|
||||||
pub fn content(&mut self, focus_handle: FocusHandle) -> &mut Self {
|
pub fn content(&mut self, focus_handle: FocusHandle) -> &mut Self {
|
||||||
|
|
@ -202,10 +218,9 @@ impl Render for PopupMenu {
|
||||||
.on_action(cx.listener(Self::confirm))
|
.on_action(cx.listener(Self::confirm))
|
||||||
.on_action(cx.listener(Self::dismiss))
|
.on_action(cx.listener(Self::dismiss))
|
||||||
.on_mouse_down_out(cx.listener(|this, _, cx| this.dismiss(&Dismiss, cx)))
|
.on_mouse_down_out(cx.listener(|this, _, cx| this.dismiss(&Dismiss, cx)))
|
||||||
.max_h(px(550.))
|
.max_h(self.max_width)
|
||||||
.min_w(px(230.))
|
.min_w(self.min_width)
|
||||||
.p_1p5()
|
.p_1p5()
|
||||||
.max_w_128()
|
|
||||||
.gap_y_0p5()
|
.gap_y_0p5()
|
||||||
.children(self.menu_items.iter_mut().enumerate().map(|(ix, item)| {
|
.children(self.menu_items.iter_mut().enumerate().map(|(ix, item)| {
|
||||||
let this = ListItem::new(("menu-item", ix))
|
let this = ListItem::new(("menu-item", ix))
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue