Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 80 additions & 3 deletions benches/frame.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! What a frame costs: build, layout and draw of plain trees at 100, 1,000
//! and 5,000 nodes (#133), a page of buttons styled on each icon or by one
//! rule on its container (#443), a virtual list's frame at 1,000 to 100,000 rows,
//! rule on its container, or by a sheet (#443), a virtual list's frame at 1,000 to 100,000 rows,
//! and the js-framework-benchmark operations on a virtual list (#54).
//!
//! `cargo bench --bench frame`; one group with `cargo bench --bench frame --
Expand All @@ -11,10 +11,13 @@
use std::alloc::{GlobalAlloc, Layout, System};
use std::cell::{Cell, RefCell};
use std::hint::black_box;
use std::sync::Arc;
use std::sync::atomic::{AtomicUsize, Ordering};

use criterion::{BatchSize, BenchmarkId, Criterion, criterion_group, criterion_main};
use eggshell_ui::{Builder, Icon, LayoutState, Match, icons, tw};
use eggshell_ui::contract::Themed;
use eggshell_ui::sheet::{Part, Sheet, When};
use eggshell_ui::{Builder, Icon, LayoutState, Match, Style, icons, tw};
use egui::{Context, Id, RawInput, Rect, pos2, vec2};

/// Counts allocations, for the allocations-per-frame figures.
Expand Down Expand Up @@ -243,6 +246,80 @@ fn rules(c: &mut Criterion) {
group.finish();
}

#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
struct Button;

impl Part for Button {
const SOURCE: &'static str = "button.tsx";

fn slot(self) -> &'static str {
"button"
}
}

fn button_classes() -> eggshell_ui::Tw {
tw().flex().items_center().gap(1.0).px(2.0)
}

/// Where a page of buttons keeps what every button shares.
#[derive(Clone, Copy, Debug)]
enum Keeping {
/// Built into each button's own classes.
Own,
/// In the style's sheet, resolved for each button.
Sheet,
}

impl Keeping {
fn name(self) -> &'static str {
match self {
Self::Own => "own classes",
Self::Sheet => "sheet part",
}
}

fn style(self) -> Themed<Arc<Style>> {
let mut style = Style::default();
let mut sheet = Sheet::defaults();
sheet.rule(Button, When::always(), |_| button_classes());
style.sheet = sheet;
let style = Arc::new(style);
Themed {
light: style.clone(),
dark: style,
}
}

fn build(self, b: &mut Builder<'_, '_, ()>, buttons: usize) {
b.div(tw().flex().flex_wrap().gap(1.0).p(2.0), |b| {
for at in 0..buttons {
let classes = match self {
Self::Own => button_classes(),
Self::Sheet => b.part(Button, &[]).merge(tw()),
};
b.div(classes, |b| b.text(tw(), WORDS[at % WORDS.len()]));
}
});
}
}

fn sheets(c: &mut Criterion) {
let ctx = context();
let mut group = c.benchmark_group("sheet");
group.sample_size(30);
for keeping in [Keeping::Own, Keeping::Sheet] {
for buttons in [100, 1_000] {
let mut state = LayoutState::new().with_style(keeping.style());
let view = |b: &mut Builder<'_, '_, ()>, _: &()| keeping.build(b, buttons);
frame(&ctx, &mut state, &mut (), Phase::Layout, view);
group.bench_function(BenchmarkId::new(keeping.name(), buttons), |bench| {
bench.iter(|| frame(&ctx, &mut state, &mut (), Phase::Layout, view));
});
}
}
group.finish();
}

#[derive(Clone, Copy, Debug, Hash, PartialEq, Eq)]
struct RowId(u32);

Expand Down Expand Up @@ -429,5 +506,5 @@ fn operations(c: &mut Criterion) {
group.finish();
}

criterion_group!(benches, trees, rules, list, operations);
criterion_group!(benches, trees, rules, sheets, list, operations);
criterion_main!(benches);
1 change: 1 addition & 0 deletions crates/eggshell-ui-shadcn/src/preset.rs
Original file line number Diff line number Diff line change
Expand Up @@ -892,6 +892,7 @@ impl Shadcn {
easing: Easing::InOut,
};
style.tokens = self.core_tokens();
style.sheet.as_preset();
style
}

Expand Down
26 changes: 25 additions & 1 deletion src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,26 @@ impl<'b, 'f, A> Builder<'b, 'f, A> {
self.style
}

/// TODO(prose): the classes the style's sheet gives `part` for a node with `keys`: merge the call site's over them, and the rules of the containers above come between. A variant or size the sheet leaves out is reported as [`Problem::MissingStyleEntry`](crate::Problem::MissingStyleEntry).
#[track_caller]
pub fn part<P: crate::sheet::Part>(&mut self, part: P, keys: &[crate::sheet::Key]) -> Tw {
let sheet = &self.style.sheet;
let (classes, fresh) = self.st.sheets.resolve(sheet, part, keys);
if fresh && let Some(missing) = sheet.unnamed(part, keys) {
crate::report::report(
self.frame.ctx,
crate::Problem::MissingStyleEntry,
Some(Id::NULL.with(("style-entry", format!("{part:?} {missing:?}")))),
|| {
format!(
"the style's sheet names no {missing:?} for {part:?}; drawing without it"
)
},
);
}
Tw::over_sheet(classes)
}

/// TODO(prose): builds `body` in place with a style of its own: the style
/// here, changed by `edit`. Every node `body` builds, and every control
/// among them, is built, measured and drawn with it; nothing outside
Expand All @@ -287,6 +307,7 @@ impl<'b, 'f, A> Builder<'b, 'f, A> {
body: impl FnOnce(&mut Builder<'_, 'f, A>),
) {
let mut style = crate::Style::clone(self.style);
style.sheet.as_scope();
edit(&mut style);
self.with_style(&Arc::new(style), body);
}
Expand Down Expand Up @@ -1694,7 +1715,10 @@ impl<'b, 'f, A> Builder<'b, 'f, A> {
/// Gives `tw` the classes the rules of the containers above make it.
#[inline(always)]
fn apply_rules(&self, tw: &mut Tw, kind: NodeKind) {
if !self.ambient.rules.is_empty() {
if tw.is_layered() {
let own = std::mem::replace(tw, Tw::blank());
*tw = self.ambient.rules.apply_layered(&self.st.links, own, kind);
} else if !self.ambient.rules.is_empty() {
*tw = self.ambient.rules.apply(&self.st.links, tw, kind);
}
}
Expand Down
3 changes: 3 additions & 0 deletions src/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1571,6 +1571,8 @@ pub struct Style {
pub tones: Tones,
/// TODO(prose): the design system's named values, from which egui's own look is set.
pub tokens: Tokens,
/// TODO(prose): the classes of every widget's parts, by layer.
pub sheet: crate::sheet::Sheet,
/// The widgets' own looks, beside the core-painted fields.
extensions: Extensions,
}
Expand Down Expand Up @@ -2181,6 +2183,7 @@ impl Style {
motion: MotionStyle::INSTANT,
tones: Tones::from_egui(v),
tokens: Tokens::from_egui(egui),
sheet: crate::sheet::Sheet::defaults(),
extensions: Extensions::default(),
}
}
Expand Down
7 changes: 5 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ mod roving;
pub mod scroll;
mod scroll_keys;
mod shadow_corners;
pub mod sheet;
mod shell;
mod sizing;
mod spans;
Expand Down Expand Up @@ -442,7 +443,9 @@ pub use text::{
};
#[doc(hidden)]
pub use transition::{cycle, tracked_layout_nodes};
#[cfg(debug_assertions)]
pub use tw::Property;
pub use tw::{
Angle, Aria, BoxPaint, Divide, Easing, Match, Mirror, Opacity, Paint, Progress, Reserve, Ring,
RingPlacement, Sides, Track, Tw, tw,
Angle, Aria, BoxPaint, Divide, Easing, Match, Mirror, Opacity, Origin, Paint, Progress,
Reserve, Ring, RingPlacement, Sides, Source, Track, Tw, tw,
};
4 changes: 4 additions & 0 deletions src/pipeline.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ pub(crate) struct BuildState {
pub observed: std::collections::HashMap<NodeId, crate::tw::Observed>,
/// The rules containers declared this frame, by the index `Rules` holds.
pub links: Vec<crate::tw::Link>,
/// What the style's sheet gave the parts built so far.
pub sheets: crate::sheet::Memo,
pub names: ReadablePath,
pub pass: Pass,
}
Expand Down Expand Up @@ -348,6 +350,7 @@ impl BuildState {
seen: Seen::default(),
observed: std::collections::HashMap::new(),
links: Vec::new(),
sheets: crate::sheet::Memo::default(),
names: ReadablePath::default(),
pass: Pass::Scratch,
}
Expand Down Expand Up @@ -741,6 +744,7 @@ impl LayoutState {
seen: Seen::default(),
observed: std::collections::HashMap::new(),
links: Vec::new(),
sheets: crate::sheet::Memo::default(),
names: ReadablePath::default(),
pass: Pass::Live,
},
Expand Down
Loading
Loading