Modifiers
SwiftUI’s modifiers on any element: a badge, a help tooltip, an edge fade while more lies beyond, and asking for the next page near the end.
Badge
if ui
.add(Button::new("Inbox"))
.badge(ui, state.unread)
.clicked()
{
state.unread = 0;
}
ui.add(Button::new("Archive")).badge(ui, 120);
ui.add(Button::icon_only(glyph(ui.ctx(), "bell")))
.badge_dot(ui);
Help
ui.add(Button::new("Shuffle"))
.help("Plays the queue in any order");
ui.add(Button::icon_only(glyph(ui.ctx(), "star")))
.help("Adds it to your favourites")
.badge_dot(ui);
Edge fade
ui.vertical(|ui| {
ui.set_width(360.0);
Scrollable::horizontal()
.id_salt("chips")
.mode(ScrollbarMode::Hidden)
.edge_fade()
.wheel_scrolls_only_direction()
.show(ui, |ui| {
ui.horizontal(|ui| {
for (index, chip) in CHIPS.iter().enumerate() {
let tag = if index == state.chip {
Tag::primary(*chip)
} else {
Tag::new(*chip)
};
let response = ui.add(tag).interact(egui::Sense::click());
if response.clicked() {
response.scroll_to_me(None);
state.chip = index;
}
}
});
});
ui.add_space(12.0);
egui::Frame::new()
.fill(tokens(ui.ctx()).muted)
.corner_radius(tokens(ui.ctx()).shape.radius_lg)
.inner_margin(8)
.show(ui, |ui| {
Scrollable::vertical()
.id_salt("column")
.edge_fade_size(28.0)
.max_height(180.0)
.auto_shrink([false, true])
.show(ui, |ui| {
for song in SONGS {
ui.add_sized([ui.available_width(), 28.0], egui::Label::new(song));
}
});
});
});
Load more
ui.vertical(|ui| {
ui.set_width(360.0);
ui.label(format!("{} of {TOTAL} songs loaded", state.loaded));
let loaded = state.loaded;
let loading = state.loading_since.is_some();
Scrollable::vertical()
.id_salt("songs")
.max_height(240.0)
.auto_shrink([false, true])
.show(ui, |ui| {
for index in 0..loaded {
ui.label(format!("{}. {}", index + 1, SONGS[index % SONGS.len()]));
}
if loading {
ui.horizontal(|ui| {
ui.add(Progress::new());
ui.label("Loading...");
});
}
})
.load_more_within(240.0, || {
if !loading && loaded < TOTAL {
state.loading_since = Some(now);
}
});
});
The story above is gallery/src/stories/modifiers.rs, running in your browser. Every snippet on this page is cut from it.