refactor: inline intermediate element builders in render methods
This commit is contained in:
@@ -153,7 +153,7 @@ impl RenderOnce for Badge {
|
||||
let size = self.size;
|
||||
let (background, foreground, border) = self.variant.tokens(theme);
|
||||
|
||||
let badge = h_flex()
|
||||
h_flex()
|
||||
.id(self.id.clone())
|
||||
.flex_none()
|
||||
.justify_center()
|
||||
@@ -163,14 +163,12 @@ impl RenderOnce for Badge {
|
||||
.border_color(border)
|
||||
.bg(background)
|
||||
.text_color(foreground)
|
||||
.control_caption_text(size);
|
||||
let badge = match size {
|
||||
.control_caption_text(size)
|
||||
.map(|badge| match size {
|
||||
Size::XSmall | Size::Small => badge.h_5().min_w_5().px_1(),
|
||||
Size::Medium => badge.h_6().min_w_6().px_1p5(),
|
||||
Size::Large => badge.h_7().min_w_7().px_2(),
|
||||
};
|
||||
|
||||
badge
|
||||
})
|
||||
.when_some(self.icon, |badge, icon| {
|
||||
badge.child(icon.size(size.dense_icon_rems()).color(foreground))
|
||||
})
|
||||
|
||||
@@ -242,7 +242,25 @@ impl RenderOnce for DropdownMenu {
|
||||
let trigger_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())
|
||||
.debug_selector({
|
||||
let id = self.id.clone();
|
||||
@@ -288,11 +306,8 @@ impl RenderOnce for DropdownMenu {
|
||||
.color(foreground),
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
let main = if inert {
|
||||
main
|
||||
} else {
|
||||
})
|
||||
.when(!inert, |main| {
|
||||
main.track_focus(&focus)
|
||||
.cursor_pointer()
|
||||
.hover(move |style| style.bg(hover))
|
||||
@@ -305,7 +320,9 @@ impl RenderOnce for DropdownMenu {
|
||||
move |event, window, cx| match (on_click.as_ref(), event) {
|
||||
(Some(on_click), _) => on_click(event, window, cx),
|
||||
(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);
|
||||
}
|
||||
(None, _) => {}
|
||||
@@ -316,34 +333,17 @@ impl RenderOnce for DropdownMenu {
|
||||
let keyboard_open = keyboard_open.clone();
|
||||
move |event: &KeyDownEvent, window, cx| {
|
||||
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);
|
||||
cx.stop_propagation();
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
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(main);
|
||||
|
||||
let trigger = if split {
|
||||
}),
|
||||
)
|
||||
.when(split, |trigger| {
|
||||
trigger.child(
|
||||
h_flex()
|
||||
.id((self.id.clone(), "chevron"))
|
||||
@@ -381,9 +381,7 @@ impl RenderOnce for DropdownMenu {
|
||||
.inset_0(),
|
||||
),
|
||||
)
|
||||
} else {
|
||||
trigger
|
||||
};
|
||||
});
|
||||
|
||||
let items = if self.open {
|
||||
self.items
|
||||
|
||||
@@ -1426,19 +1426,18 @@ fn heading_row(
|
||||
ctx: &BlockContext,
|
||||
cx: &App,
|
||||
) -> AnyElement {
|
||||
let frame = block_frame(quote, top, rems(0.75), ctx.block_id, cx);
|
||||
let frame = match level {
|
||||
indented_frame(
|
||||
indent,
|
||||
block_frame(quote, top, rems(0.75), ctx.block_id, cx)
|
||||
.map(|frame| match level {
|
||||
MarkdownHeading::H1 => frame.text_xl().font_semibold(),
|
||||
MarkdownHeading::H2 => frame.text_lg().font_semibold(),
|
||||
MarkdownHeading::H3 => frame.text_base().font_semibold(),
|
||||
MarkdownHeading::H4 | MarkdownHeading::H5 | MarkdownHeading::H6 => {
|
||||
frame.text_sm().font_semibold()
|
||||
}
|
||||
};
|
||||
|
||||
indented_frame(
|
||||
indent,
|
||||
frame.child(text_child(spans, ctx.block_id, ctx.piece_context(0), cx)),
|
||||
})
|
||||
.child(text_child(spans, ctx.block_id, ctx.piece_context(0), cx)),
|
||||
)
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
@@ -732,26 +732,22 @@ pub(crate) fn anchored_overlay(
|
||||
panel: AnyElement,
|
||||
fade: Option<(&'static str, std::time::Duration)>,
|
||||
) -> AnyElement {
|
||||
let anchored = match position {
|
||||
Some(position) => anchored().position(position),
|
||||
None => anchored(),
|
||||
}
|
||||
.offset(point(px(0.), px(4.)));
|
||||
|
||||
deferred(
|
||||
anchored()
|
||||
.when_some(position, |anchored, position| anchored.position(position))
|
||||
.offset(point(px(0.), px(4.)))
|
||||
// AnyElement lacks `Styled`, so the opacity animation rides a wrapper.
|
||||
let panel = match fade {
|
||||
Some((id, duration)) => div()
|
||||
.child(panel.when_some(fade, |panel, (id, duration)| {
|
||||
div()
|
||||
.child(panel)
|
||||
.with_animation(
|
||||
id,
|
||||
Animation::new(duration).with_easing(ease_out_quint()),
|
||||
|panel, delta| panel.opacity(delta),
|
||||
)
|
||||
.into_any_element(),
|
||||
None => panel,
|
||||
};
|
||||
|
||||
deferred(anchored.child(panel))
|
||||
.into_any_element()
|
||||
})),
|
||||
)
|
||||
.with_priority(OVERLAY_LAYER)
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
@@ -107,21 +107,20 @@ impl RenderOnce for StatusDot {
|
||||
let size = self.size;
|
||||
let color = self.variant.color(theme);
|
||||
|
||||
let dot = div()
|
||||
div()
|
||||
.id(self.id.clone())
|
||||
.debug_selector(move || self.id.to_string())
|
||||
.flex_none()
|
||||
.rounded(theme.radius.full)
|
||||
.bg(color);
|
||||
let dot = match size {
|
||||
.bg(color)
|
||||
.map(|dot| match size {
|
||||
Size::XSmall => dot.size_1p5(),
|
||||
Size::Small | Size::Medium => dot.size_2(),
|
||||
Size::Large => dot.size_2p5(),
|
||||
};
|
||||
|
||||
dot.when_some(self.label, |dot, label| {
|
||||
dot.role(Role::Status).aria_label(label)
|
||||
})
|
||||
.refine_style(&self.style)
|
||||
.when_some(self.label, |dot, label| {
|
||||
dot.role(Role::Status).aria_label(label)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user