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 {
|
||||
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
|
||||
.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(),
|
||||
})
|
||||
.when_some(self.icon, |badge, icon| {
|
||||
badge.child(icon.size(size.dense_icon_rems()).color(foreground))
|
||||
})
|
||||
|
||||
@@ -242,88 +242,6 @@ 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()
|
||||
.id(self.id.clone())
|
||||
.debug_selector({
|
||||
let id = self.id.clone();
|
||||
move || id.to_string()
|
||||
})
|
||||
.justify_center()
|
||||
.h_full()
|
||||
.when(icon_only, |zone| zone.w_full())
|
||||
.gap_2()
|
||||
.line_height(relative(1.0))
|
||||
.control_text(self.size)
|
||||
.text_color(foreground)
|
||||
.role(Role::Button)
|
||||
.when_some(a11y_label, |zone, label| zone.aria_label(label))
|
||||
.aria_expanded(self.open)
|
||||
.when(!icon_only, |zone| zone.control_padding_x(self.size))
|
||||
.when(split, |zone| zone.rounded_l(radius))
|
||||
.when(!split, |zone| {
|
||||
zone.rounded(radius)
|
||||
.when(self.open, |zone| zone.bg(pressed))
|
||||
})
|
||||
.when(!icon_only && !split, |zone| {
|
||||
zone.child(
|
||||
icon(IconRole::ChevronDown)
|
||||
.size(self.size.dense_icon_rems())
|
||||
.color(foreground),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
h_flex()
|
||||
.gap_2()
|
||||
.when(self.loading, |content| content.invisible())
|
||||
.when_some(self.icon, |content, icon| {
|
||||
content.child(icon.size(self.size.icon_rems()).color(foreground))
|
||||
})
|
||||
.when_some(self.label, |content, label| content.child(label)),
|
||||
)
|
||||
.when(self.loading, |zone| {
|
||||
zone.child(
|
||||
h_flex().absolute().inset_0().justify_center().child(
|
||||
Spinner::new((self.id.clone(), "spinner"))
|
||||
.with_size(self.size)
|
||||
.color(foreground),
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
let main = if inert {
|
||||
main
|
||||
} else {
|
||||
main.track_focus(&focus)
|
||||
.cursor_pointer()
|
||||
.hover(move |style| style.bg(hover))
|
||||
.active(move |style| style.bg(pressed))
|
||||
.on_click({
|
||||
let on_click = self.on_click;
|
||||
let on_open = on_open.clone();
|
||||
let keyboard_open = keyboard_open.clone();
|
||||
let open = self.open;
|
||||
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);
|
||||
on_open(!open, window, cx);
|
||||
}
|
||||
(None, _) => {}
|
||||
}
|
||||
})
|
||||
.on_key_down({
|
||||
let on_open = on_open.clone();
|
||||
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);
|
||||
on_open(true, window, cx);
|
||||
cx.stop_propagation();
|
||||
}
|
||||
}
|
||||
})
|
||||
};
|
||||
|
||||
let trigger = h_flex()
|
||||
.debug_selector({
|
||||
let id = self.id.clone();
|
||||
@@ -341,49 +259,129 @@ impl RenderOnce for DropdownMenu {
|
||||
.track_focus(&focus)
|
||||
.focus_visible(move |style| style.shadow(focus_ring(ring)))
|
||||
})
|
||||
.child(main);
|
||||
|
||||
let trigger = if split {
|
||||
trigger.child(
|
||||
.child(
|
||||
h_flex()
|
||||
.id((self.id.clone(), "chevron"))
|
||||
.id(self.id.clone())
|
||||
.debug_selector({
|
||||
let id = self.id.clone();
|
||||
move || format!("{id}-chevron")
|
||||
move || id.to_string()
|
||||
})
|
||||
.relative()
|
||||
.justify_center()
|
||||
.h_full()
|
||||
.control_dense_padding_x(self.size)
|
||||
.rounded_r(radius)
|
||||
.rounded_l_none()
|
||||
.border_l_1()
|
||||
.border_color(border)
|
||||
.when(self.open, |zone| zone.bg(pressed))
|
||||
.when(!inert, |zone| {
|
||||
zone.cursor_pointer()
|
||||
.when(icon_only, |zone| zone.w_full())
|
||||
.gap_2()
|
||||
.line_height(relative(1.0))
|
||||
.control_text(self.size)
|
||||
.text_color(foreground)
|
||||
.role(Role::Button)
|
||||
.when_some(a11y_label, |zone, label| zone.aria_label(label))
|
||||
.aria_expanded(self.open)
|
||||
.when(!icon_only, |zone| zone.control_padding_x(self.size))
|
||||
.when(split, |zone| zone.rounded_l(radius))
|
||||
.when(!split, |zone| {
|
||||
zone.rounded(radius)
|
||||
.when(self.open, |zone| zone.bg(pressed))
|
||||
})
|
||||
.when(!icon_only && !split, |zone| {
|
||||
zone.child(
|
||||
icon(IconRole::ChevronDown)
|
||||
.size(self.size.dense_icon_rems())
|
||||
.color(foreground),
|
||||
)
|
||||
})
|
||||
.child(
|
||||
h_flex()
|
||||
.gap_2()
|
||||
.when(self.loading, |content| content.invisible())
|
||||
.when_some(self.icon, |content, icon| {
|
||||
content.child(icon.size(self.size.icon_rems()).color(foreground))
|
||||
})
|
||||
.when_some(self.label, |content, label| content.child(label)),
|
||||
)
|
||||
.when(self.loading, |zone| {
|
||||
zone.child(
|
||||
h_flex().absolute().inset_0().justify_center().child(
|
||||
Spinner::new((self.id.clone(), "spinner"))
|
||||
.with_size(self.size)
|
||||
.color(foreground),
|
||||
),
|
||||
)
|
||||
})
|
||||
.when(!inert, |main| {
|
||||
main.track_focus(&focus)
|
||||
.cursor_pointer()
|
||||
.hover(move |style| style.bg(hover))
|
||||
.active(move |style| style.bg(pressed))
|
||||
})
|
||||
.child(
|
||||
icon(IconRole::ChevronDown)
|
||||
.size(self.size.dense_icon_rems())
|
||||
.color(foreground),
|
||||
)
|
||||
.child(
|
||||
canvas(
|
||||
{
|
||||
let cell = chevron_bounds.clone();
|
||||
move |bounds, _window, _cx| *cell.borrow_mut() = Some(bounds)
|
||||
},
|
||||
|_, _, _, _| {},
|
||||
)
|
||||
.absolute()
|
||||
.inset_0(),
|
||||
),
|
||||
.on_click({
|
||||
let on_click = self.on_click;
|
||||
let on_open = on_open.clone();
|
||||
let keyboard_open = keyboard_open.clone();
|
||||
let open = self.open;
|
||||
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;
|
||||
});
|
||||
on_open(!open, window, cx);
|
||||
}
|
||||
(None, _) => {}
|
||||
}
|
||||
})
|
||||
.on_key_down({
|
||||
let on_open = on_open.clone();
|
||||
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;
|
||||
});
|
||||
on_open(true, window, cx);
|
||||
cx.stop_propagation();
|
||||
}
|
||||
}
|
||||
})
|
||||
}),
|
||||
)
|
||||
} else {
|
||||
trigger
|
||||
};
|
||||
.when(split, |trigger| {
|
||||
trigger.child(
|
||||
h_flex()
|
||||
.id((self.id.clone(), "chevron"))
|
||||
.debug_selector({
|
||||
let id = self.id.clone();
|
||||
move || format!("{id}-chevron")
|
||||
})
|
||||
.relative()
|
||||
.h_full()
|
||||
.control_dense_padding_x(self.size)
|
||||
.rounded_r(radius)
|
||||
.rounded_l_none()
|
||||
.border_l_1()
|
||||
.border_color(border)
|
||||
.when(self.open, |zone| zone.bg(pressed))
|
||||
.when(!inert, |zone| {
|
||||
zone.cursor_pointer()
|
||||
.hover(move |style| style.bg(hover))
|
||||
.active(move |style| style.bg(pressed))
|
||||
})
|
||||
.child(
|
||||
icon(IconRole::ChevronDown)
|
||||
.size(self.size.dense_icon_rems())
|
||||
.color(foreground),
|
||||
)
|
||||
.child(
|
||||
canvas(
|
||||
{
|
||||
let cell = chevron_bounds.clone();
|
||||
move |bounds, _window, _cx| *cell.borrow_mut() = Some(bounds)
|
||||
},
|
||||
|_, _, _, _| {},
|
||||
)
|
||||
.absolute()
|
||||
.inset_0(),
|
||||
),
|
||||
)
|
||||
});
|
||||
|
||||
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 {
|
||||
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)),
|
||||
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()
|
||||
}
|
||||
})
|
||||
.child(text_child(spans, ctx.block_id, ctx.piece_context(0), cx)),
|
||||
)
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
@@ -732,28 +732,24 @@ 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.)));
|
||||
|
||||
// AnyElement lacks `Styled`, so the opacity animation rides a wrapper.
|
||||
let panel = match fade {
|
||||
Some((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))
|
||||
.with_priority(OVERLAY_LAYER)
|
||||
.into_any_element()
|
||||
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.
|
||||
.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()
|
||||
})),
|
||||
)
|
||||
.with_priority(OVERLAY_LAYER)
|
||||
.into_any_element()
|
||||
}
|
||||
|
||||
/// Canvas that grabs focus when `should_grab` holds.
|
||||
|
||||
@@ -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 {
|
||||
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)
|
||||
.bg(color)
|
||||
.map(|dot| match size {
|
||||
Size::XSmall => dot.size_1p5(),
|
||||
Size::Small | Size::Medium => dot.size_2(),
|
||||
Size::Large => dot.size_2p5(),
|
||||
})
|
||||
.refine_style(&self.style)
|
||||
.when_some(self.label, |dot, label| {
|
||||
dot.role(Role::Status).aria_label(label)
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user