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 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))
|
||||||
})
|
})
|
||||||
|
|||||||
@@ -242,88 +242,6 @@ 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()
|
|
||||||
.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()
|
let trigger = h_flex()
|
||||||
.debug_selector({
|
.debug_selector({
|
||||||
let id = self.id.clone();
|
let id = self.id.clone();
|
||||||
@@ -341,49 +259,129 @@ impl RenderOnce for DropdownMenu {
|
|||||||
.track_focus(&focus)
|
.track_focus(&focus)
|
||||||
.focus_visible(move |style| style.shadow(focus_ring(ring)))
|
.focus_visible(move |style| style.shadow(focus_ring(ring)))
|
||||||
})
|
})
|
||||||
.child(main);
|
.child(
|
||||||
|
|
||||||
let trigger = if split {
|
|
||||||
trigger.child(
|
|
||||||
h_flex()
|
h_flex()
|
||||||
.id((self.id.clone(), "chevron"))
|
.id(self.id.clone())
|
||||||
.debug_selector({
|
.debug_selector({
|
||||||
let id = self.id.clone();
|
let id = self.id.clone();
|
||||||
move || format!("{id}-chevron")
|
move || id.to_string()
|
||||||
})
|
})
|
||||||
.relative()
|
.justify_center()
|
||||||
.h_full()
|
.h_full()
|
||||||
.control_dense_padding_x(self.size)
|
.when(icon_only, |zone| zone.w_full())
|
||||||
.rounded_r(radius)
|
.gap_2()
|
||||||
.rounded_l_none()
|
.line_height(relative(1.0))
|
||||||
.border_l_1()
|
.control_text(self.size)
|
||||||
.border_color(border)
|
.text_color(foreground)
|
||||||
.when(self.open, |zone| zone.bg(pressed))
|
.role(Role::Button)
|
||||||
.when(!inert, |zone| {
|
.when_some(a11y_label, |zone, label| zone.aria_label(label))
|
||||||
zone.cursor_pointer()
|
.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))
|
.hover(move |style| style.bg(hover))
|
||||||
.active(move |style| style.bg(pressed))
|
.active(move |style| style.bg(pressed))
|
||||||
})
|
.on_click({
|
||||||
.child(
|
let on_click = self.on_click;
|
||||||
icon(IconRole::ChevronDown)
|
let on_open = on_open.clone();
|
||||||
.size(self.size.dense_icon_rems())
|
let keyboard_open = keyboard_open.clone();
|
||||||
.color(foreground),
|
let open = self.open;
|
||||||
)
|
move |event, window, cx| match (on_click.as_ref(), event) {
|
||||||
.child(
|
(Some(on_click), _) => on_click(event, window, cx),
|
||||||
canvas(
|
(None, ClickEvent::Keyboard(_)) => {
|
||||||
{
|
keyboard_open.update(cx, |keyboard_open, _| {
|
||||||
let cell = chevron_bounds.clone();
|
*keyboard_open = true;
|
||||||
move |bounds, _window, _cx| *cell.borrow_mut() = Some(bounds)
|
});
|
||||||
},
|
on_open(!open, window, cx);
|
||||||
|_, _, _, _| {},
|
}
|
||||||
)
|
(None, _) => {}
|
||||||
.absolute()
|
}
|
||||||
.inset_0(),
|
})
|
||||||
),
|
.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 {
|
.when(split, |trigger| {
|
||||||
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 {
|
let items = if self.open {
|
||||||
self.items
|
self.items
|
||||||
|
|||||||
@@ -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);
|
|
||||||
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(
|
indented_frame(
|
||||||
indent,
|
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()
|
.into_any_element()
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -732,28 +732,24 @@ 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.
|
||||||
|
.child(panel.when_some(fade, |panel, (id, duration)| {
|
||||||
// AnyElement lacks `Styled`, so the opacity animation rides a wrapper.
|
div()
|
||||||
let panel = match fade {
|
.child(panel)
|
||||||
Some((id, duration)) => div()
|
.with_animation(
|
||||||
.child(panel)
|
id,
|
||||||
.with_animation(
|
Animation::new(duration).with_easing(ease_out_quint()),
|
||||||
id,
|
|panel, delta| panel.opacity(delta),
|
||||||
Animation::new(duration).with_easing(ease_out_quint()),
|
)
|
||||||
|panel, delta| panel.opacity(delta),
|
.into_any_element()
|
||||||
)
|
})),
|
||||||
.into_any_element(),
|
)
|
||||||
None => panel,
|
.with_priority(OVERLAY_LAYER)
|
||||||
};
|
.into_any_element()
|
||||||
|
|
||||||
deferred(anchored.child(panel))
|
|
||||||
.with_priority(OVERLAY_LAYER)
|
|
||||||
.into_any_element()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Canvas that grabs focus when `should_grab` holds.
|
/// Canvas that grabs focus when `should_grab` holds.
|
||||||
|
|||||||
@@ -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(),
|
||||||
};
|
})
|
||||||
|
.refine_style(&self.style)
|
||||||
dot.when_some(self.label, |dot, label| {
|
.when_some(self.label, |dot, label| {
|
||||||
dot.role(Role::Status).aria_label(label)
|
dot.role(Role::Status).aria_label(label)
|
||||||
})
|
})
|
||||||
.refine_style(&self.style)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user