refactor: inline intermediate element builders in render methods
Rust / build (macos-latest, stable) (push) Waiting to run
Rust / build (ubuntu-latest, stable) (push) Waiting to run
Rust / build (windows-latest, stable) (push) Waiting to run

This commit is contained in:
2026-11-22 19:06:45 +07:00
parent 967273c402
commit 531493008e
5 changed files with 161 additions and 171 deletions
+4 -6
View File
@@ -153,7 +153,7 @@ impl RenderOnce for Badge {
let size = self.size; let size = self.size;
let (background, foreground, border) = self.variant.tokens(theme); let (background, foreground, border) = self.variant.tokens(theme);
let badge = h_flex() h_flex()
.id(self.id.clone()) .id(self.id.clone())
.flex_none() .flex_none()
.justify_center() .justify_center()
@@ -163,14 +163,12 @@ impl RenderOnce for Badge {
.border_color(border) .border_color(border)
.bg(background) .bg(background)
.text_color(foreground) .text_color(foreground)
.control_caption_text(size); .control_caption_text(size)
let badge = match size { .map(|badge| match size {
Size::XSmall | Size::Small => badge.h_5().min_w_5().px_1(), Size::XSmall | Size::Small => badge.h_5().min_w_5().px_1(),
Size::Medium => badge.h_6().min_w_6().px_1p5(), Size::Medium => badge.h_6().min_w_6().px_1p5(),
Size::Large => badge.h_7().min_w_7().px_2(), Size::Large => badge.h_7().min_w_7().px_2(),
}; })
badge
.when_some(self.icon, |badge, icon| { .when_some(self.icon, |badge, icon| {
badge.child(icon.size(size.dense_icon_rems()).color(foreground)) badge.child(icon.size(size.dense_icon_rems()).color(foreground))
}) })
+31 -33
View File
@@ -242,7 +242,25 @@ impl RenderOnce for DropdownMenu {
let trigger_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None)); let trigger_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None));
let chevron_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None)); let chevron_bounds: TriggerBoundsCell = Rc::new(RefCell::new(None));
let main = h_flex() let trigger = h_flex()
.debug_selector({
let id = self.id.clone();
move || format!("{id}-trigger")
})
.control_height(self.size)
.when(icon_only, |trigger| trigger.control_width(self.size))
.rounded(radius)
.border_1()
.border_color(border)
.when_some(background, |trigger, background| trigger.bg(background))
.when(self.disabled, |trigger| trigger.opacity(DISABLED_OPACITY))
.when(!inert, |trigger| {
trigger
.track_focus(&focus)
.focus_visible(move |style| style.shadow(focus_ring(ring)))
})
.child(
h_flex()
.id(self.id.clone()) .id(self.id.clone())
.debug_selector({ .debug_selector({
let id = self.id.clone(); let id = self.id.clone();
@@ -288,11 +306,8 @@ impl RenderOnce for DropdownMenu {
.color(foreground), .color(foreground),
), ),
) )
}); })
.when(!inert, |main| {
let main = if inert {
main
} else {
main.track_focus(&focus) main.track_focus(&focus)
.cursor_pointer() .cursor_pointer()
.hover(move |style| style.bg(hover)) .hover(move |style| style.bg(hover))
@@ -305,7 +320,9 @@ impl RenderOnce for DropdownMenu {
move |event, window, cx| match (on_click.as_ref(), event) { move |event, window, cx| match (on_click.as_ref(), event) {
(Some(on_click), _) => on_click(event, window, cx), (Some(on_click), _) => on_click(event, window, cx),
(None, ClickEvent::Keyboard(_)) => { (None, ClickEvent::Keyboard(_)) => {
keyboard_open.update(cx, |keyboard_open, _| *keyboard_open = true); keyboard_open.update(cx, |keyboard_open, _| {
*keyboard_open = true;
});
on_open(!open, window, cx); on_open(!open, window, cx);
} }
(None, _) => {} (None, _) => {}
@@ -316,34 +333,17 @@ impl RenderOnce for DropdownMenu {
let keyboard_open = keyboard_open.clone(); let keyboard_open = keyboard_open.clone();
move |event: &KeyDownEvent, window, cx| { move |event: &KeyDownEvent, window, cx| {
if event.keystroke.key == "down" { if event.keystroke.key == "down" {
keyboard_open.update(cx, |keyboard_open, _| *keyboard_open = true); keyboard_open.update(cx, |keyboard_open, _| {
*keyboard_open = true;
});
on_open(true, window, cx); on_open(true, window, cx);
cx.stop_propagation(); cx.stop_propagation();
} }
} }
}) })
}; }),
)
let trigger = h_flex() .when(split, |trigger| {
.debug_selector({
let id = self.id.clone();
move || format!("{id}-trigger")
})
.control_height(self.size)
.when(icon_only, |trigger| trigger.control_width(self.size))
.rounded(radius)
.border_1()
.border_color(border)
.when_some(background, |trigger, background| trigger.bg(background))
.when(self.disabled, |trigger| trigger.opacity(DISABLED_OPACITY))
.when(!inert, |trigger| {
trigger
.track_focus(&focus)
.focus_visible(move |style| style.shadow(focus_ring(ring)))
})
.child(main);
let trigger = if split {
trigger.child( trigger.child(
h_flex() h_flex()
.id((self.id.clone(), "chevron")) .id((self.id.clone(), "chevron"))
@@ -381,9 +381,7 @@ impl RenderOnce for DropdownMenu {
.inset_0(), .inset_0(),
), ),
) )
} else { });
trigger
};
let items = if self.open { let items = if self.open {
self.items self.items
+6 -7
View File
@@ -1426,19 +1426,18 @@ fn heading_row(
ctx: &BlockContext, ctx: &BlockContext,
cx: &App, cx: &App,
) -> AnyElement { ) -> AnyElement {
let frame = block_frame(quote, top, rems(0.75), ctx.block_id, cx); indented_frame(
let frame = match level { indent,
block_frame(quote, top, rems(0.75), ctx.block_id, cx)
.map(|frame| match level {
MarkdownHeading::H1 => frame.text_xl().font_semibold(), MarkdownHeading::H1 => frame.text_xl().font_semibold(),
MarkdownHeading::H2 => frame.text_lg().font_semibold(), MarkdownHeading::H2 => frame.text_lg().font_semibold(),
MarkdownHeading::H3 => frame.text_base().font_semibold(), MarkdownHeading::H3 => frame.text_base().font_semibold(),
MarkdownHeading::H4 | MarkdownHeading::H5 | MarkdownHeading::H6 => { MarkdownHeading::H4 | MarkdownHeading::H5 | MarkdownHeading::H6 => {
frame.text_sm().font_semibold() frame.text_sm().font_semibold()
} }
}; })
.child(text_child(spans, ctx.block_id, ctx.piece_context(0), cx)),
indented_frame(
indent,
frame.child(text_child(spans, ctx.block_id, ctx.piece_context(0), cx)),
) )
.into_any_element() .into_any_element()
} }
+9 -13
View File
@@ -732,26 +732,22 @@ pub(crate) fn anchored_overlay(
panel: AnyElement, panel: AnyElement,
fade: Option<(&'static str, std::time::Duration)>, fade: Option<(&'static str, std::time::Duration)>,
) -> AnyElement { ) -> AnyElement {
let anchored = match position { deferred(
Some(position) => anchored().position(position), anchored()
None => anchored(), .when_some(position, |anchored, position| anchored.position(position))
} .offset(point(px(0.), px(4.)))
.offset(point(px(0.), px(4.)));
// AnyElement lacks `Styled`, so the opacity animation rides a wrapper. // AnyElement lacks `Styled`, so the opacity animation rides a wrapper.
let panel = match fade { .child(panel.when_some(fade, |panel, (id, duration)| {
Some((id, duration)) => div() div()
.child(panel) .child(panel)
.with_animation( .with_animation(
id, id,
Animation::new(duration).with_easing(ease_out_quint()), Animation::new(duration).with_easing(ease_out_quint()),
|panel, delta| panel.opacity(delta), |panel, delta| panel.opacity(delta),
) )
.into_any_element(), .into_any_element()
None => panel, })),
}; )
deferred(anchored.child(panel))
.with_priority(OVERLAY_LAYER) .with_priority(OVERLAY_LAYER)
.into_any_element() .into_any_element()
} }
+6 -7
View File
@@ -107,21 +107,20 @@ impl RenderOnce for StatusDot {
let size = self.size; let size = self.size;
let color = self.variant.color(theme); let color = self.variant.color(theme);
let dot = div() div()
.id(self.id.clone()) .id(self.id.clone())
.debug_selector(move || self.id.to_string()) .debug_selector(move || self.id.to_string())
.flex_none() .flex_none()
.rounded(theme.radius.full) .rounded(theme.radius.full)
.bg(color); .bg(color)
let dot = match size { .map(|dot| match size {
Size::XSmall => dot.size_1p5(), Size::XSmall => dot.size_1p5(),
Size::Small | Size::Medium => dot.size_2(), Size::Small | Size::Medium => dot.size_2(),
Size::Large => dot.size_2p5(), Size::Large => dot.size_2p5(),
};
dot.when_some(self.label, |dot, label| {
dot.role(Role::Status).aria_label(label)
}) })
.refine_style(&self.style) .refine_style(&self.style)
.when_some(self.label, |dot, label| {
dot.role(Role::Status).aria_label(label)
})
} }
} }