From 59603ab83b9888b09f8c9a513b5bc5845262e010 Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Mon, 15 Jul 2024 09:15:12 +0800 Subject: [PATCH] Add `min_w`, `max_w` to PopupMenu, and default min_w: 120, max_w: 500 --- crates/ui/src/popup_menu.rs | 25 ++++++++++++++++++++----- 1 file changed, 20 insertions(+), 5 deletions(-) diff --git a/crates/ui/src/popup_menu.rs b/crates/ui/src/popup_menu.rs index 98425230..fe3960a6 100644 --- a/crates/ui/src/popup_menu.rs +++ b/crates/ui/src/popup_menu.rs @@ -3,8 +3,8 @@ use std::rc::Rc; use gpui::{ actions, div, prelude::FluentBuilder, px, Action, AnyWindowHandle, AppContext, Context, DismissEvent, EventEmitter, FocusHandle, FocusableView, InteractiveElement, KeyBinding, - ParentElement, Render, SharedString, Styled as _, View, ViewContext, VisualContext as _, - WindowContext, + ParentElement, Pixels, Render, SharedString, Styled as _, View, ViewContext, + VisualContext as _, WindowContext, }; use crate::{h_flex, list::ListItem, theme::ActiveTheme, v_flex, Icon}; @@ -43,6 +43,8 @@ pub struct PopupMenu { action_context: Option, menu_items: Vec, selected_index: Option, + min_width: Pixels, + max_width: Pixels, _subscriptions: [gpui::Subscription; 1], } @@ -63,6 +65,8 @@ impl PopupMenu { window_handle: cx.window_handle(), menu_items: Vec::new(), selected_index: None, + min_width: px(120.), + max_width: px(500.), _subscriptions: [_on_blur_subscription], }; 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) -> &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) -> &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. /// 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 { @@ -202,10 +218,9 @@ impl Render for PopupMenu { .on_action(cx.listener(Self::confirm)) .on_action(cx.listener(Self::dismiss)) .on_mouse_down_out(cx.listener(|this, _, cx| this.dismiss(&Dismiss, cx))) - .max_h(px(550.)) - .min_w(px(230.)) + .max_h(self.max_width) + .min_w(self.min_width) .p_1p5() - .max_w_128() .gap_y_0p5() .children(self.menu_items.iter_mut().enumerate().map(|(ix, item)| { let this = ListItem::new(("menu-item", ix))