forked from reya/gpui-native-kit
chore: reduce component overhead (#3)
Reviewed-on: reya/gpui-native-kit#3
This commit is contained in:
+15
-14
@@ -10,7 +10,7 @@ use gpui::{
|
||||
ease_out_quint, point, px, relative,
|
||||
};
|
||||
use gpui_native_icons::{IconRole, icon as icon_glyph};
|
||||
use gpui_native_theme::ActiveTheme as _;
|
||||
use gpui_native_theme::{ActiveTheme as _, POPUP_MAX_HEIGHT};
|
||||
|
||||
use crate::common::v_flex;
|
||||
use crate::scroll_area::{ScrollArea, ScrollAxes};
|
||||
@@ -35,7 +35,7 @@ pub struct MenuItem {
|
||||
handler: Option<ItemHandler>,
|
||||
disabled: bool,
|
||||
destructive: bool,
|
||||
checked: Option<bool>,
|
||||
checked: bool,
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
@@ -58,7 +58,7 @@ impl MenuItem {
|
||||
handler: None,
|
||||
disabled: false,
|
||||
destructive: false,
|
||||
checked: None,
|
||||
checked: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -72,7 +72,7 @@ impl MenuItem {
|
||||
handler: None,
|
||||
disabled: false,
|
||||
destructive: false,
|
||||
checked: None,
|
||||
checked: false,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -114,7 +114,7 @@ impl MenuItem {
|
||||
|
||||
/// Shows a check slot before the label.
|
||||
pub fn checked(mut self, checked: bool) -> Self {
|
||||
self.checked = Some(checked);
|
||||
self.checked = checked;
|
||||
self
|
||||
}
|
||||
|
||||
@@ -336,11 +336,11 @@ impl RenderOnce for Menu {
|
||||
|
||||
let items = if self.open {
|
||||
match self.items {
|
||||
Some(build) => build(window, cx),
|
||||
None => Vec::new(),
|
||||
Some(build) => Rc::from(build(window, cx)),
|
||||
None => Rc::from([]),
|
||||
}
|
||||
} else {
|
||||
Vec::new()
|
||||
Rc::from([])
|
||||
};
|
||||
|
||||
if let Some(panel) = anchored_menu_panel(
|
||||
@@ -372,7 +372,7 @@ pub(crate) fn anchored_menu_panel(
|
||||
cx: &mut App,
|
||||
id: &ElementId,
|
||||
open: bool,
|
||||
items: Vec<MenuItem>,
|
||||
items: Rc<[MenuItem]>,
|
||||
initial_selection: Option<usize>,
|
||||
preselect: bool,
|
||||
on_change: Option<ChangeHandler>,
|
||||
@@ -382,7 +382,7 @@ pub(crate) fn anchored_menu_panel(
|
||||
chrome: &MenuChrome,
|
||||
enter_duration: std::time::Duration,
|
||||
) -> Option<AnyElement> {
|
||||
let was_open = window.use_keyed_state("menu-was-open", cx, |_, _| false);
|
||||
let was_open = window.use_keyed_state((id.clone(), "menu-was-open"), cx, |_, _| false);
|
||||
|
||||
if !open {
|
||||
was_open.update(cx, |was_open, _| *was_open = false);
|
||||
@@ -392,8 +392,9 @@ pub(crate) fn anchored_menu_panel(
|
||||
let on_change = on_change.clone();
|
||||
// Keyboard focus rides the panel across frames
|
||||
let focus = gpui_native_utils::focus_handle(&(id.clone(), "panel").into(), window, cx);
|
||||
let scroll = window.use_keyed_state("menu-scroll", cx, |_, _| ScrollHandle::new());
|
||||
let selection = window.use_keyed_state("menu-selection", cx, |_, _| None);
|
||||
let scroll =
|
||||
window.use_keyed_state((id.clone(), "menu-scroll"), cx, |_, _| ScrollHandle::new());
|
||||
let selection = window.use_keyed_state((id.clone(), "menu-selection"), cx, |_, _| None);
|
||||
|
||||
let selected = if *was_open.read(cx) {
|
||||
*selection.read(cx)
|
||||
@@ -469,7 +470,7 @@ pub(crate) fn anchored_menu_panel(
|
||||
ScrollArea::new("menu-items")
|
||||
.handle(scroll.read(cx).clone())
|
||||
.axes(ScrollAxes::Vertical)
|
||||
.max_h(px(320.))
|
||||
.max_h(POPUP_MAX_HEIGHT)
|
||||
.child(v_flex().gap_0p5().children(rows)),
|
||||
)
|
||||
.child(
|
||||
@@ -595,7 +596,7 @@ fn render_item(
|
||||
.child(shortcut),
|
||||
)
|
||||
})
|
||||
.when(checked == Some(true), |row| {
|
||||
.when(checked, |row| {
|
||||
row.child(
|
||||
icon_glyph(IconRole::Check)
|
||||
.size(chrome.dense_icon_size)
|
||||
|
||||
Reference in New Issue
Block a user