Add Resize::id_source and ScrollArea::id_source

This commit is contained in:
Emil Ernerfeldt 2020-12-10 10:28:30 +01:00
parent 6de93cb0ec
commit 6e8d5c87a0
4 changed files with 27 additions and 2 deletions

View File

@ -9,6 +9,9 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/).
### Added ⭐
* `SelectableLabel` (`ui.selectable_label` and `ui.selectable_value`): a text-button that can be selected
* Add `Resize::id_source` and `ScrollArea::id_source` to let the user avoid Id clashes.
### Changed 🔧
* Changed default font to [Ubuntu-Light](https://fonts.google.com/specimen/Ubuntu).

View File

@ -20,6 +20,7 @@ pub(crate) struct State {
#[derive(Clone, Copy, Debug)]
pub struct Resize {
id: Option<Id>,
id_source: Option<Id>,
/// If false, we are no enabled
resizable: bool,
@ -36,6 +37,7 @@ impl Default for Resize {
fn default() -> Self {
Self {
id: None,
id_source: None,
resizable: true,
min_size: Vec2::splat(16.0),
max_size: Vec2::splat(f32::INFINITY),
@ -52,6 +54,12 @@ impl Resize {
self
}
/// A source for the unique `Id`, e.g. `.id_source("second_resize_area")` or `.id_source(loop_index)`.
pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
self.id_source = Some(Id::new(id_source));
self
}
/// Preferred / suggested width. Actual width will depend on contents.
///
/// Examples:
@ -146,7 +154,10 @@ struct Prepared {
impl Resize {
fn begin(&mut self, ui: &mut Ui) -> Prepared {
let position = ui.available().min;
let id = self.id.unwrap_or_else(|| ui.make_persistent_id("resize"));
let id = self.id.unwrap_or_else(|| {
let id_source = self.id_source.unwrap_or_else(|| Id::new("resize"));
ui.make_persistent_id(id_source)
});
let mut state = ui.memory().resize.get(&id).cloned().unwrap_or_else(|| {
ui.ctx().request_repaint(); // counter frame delay

View File

@ -30,6 +30,7 @@ impl Default for State {
pub struct ScrollArea {
max_height: f32,
always_show_scroll: bool,
id_source: Option<Id>,
}
impl ScrollArea {
@ -43,6 +44,7 @@ impl ScrollArea {
Self {
max_height,
always_show_scroll: false,
id_source: None,
}
}
@ -52,6 +54,12 @@ impl ScrollArea {
self.always_show_scroll = always_show_scroll;
self
}
/// A source for the unique `Id`, e.g. `.id_source("second_scroll_area")` or `.id_source(loop_index)`.
pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
self.id_source = Some(Id::new(id_source));
self
}
}
struct Prepared {
@ -68,11 +76,13 @@ impl ScrollArea {
let Self {
max_height,
always_show_scroll,
id_source,
} = self;
let ctx = ui.ctx().clone();
let id = ui.make_persistent_id("scroll_area");
let id_source = id_source.unwrap_or_else(|| Id::new("scroll_area"));
let id = ui.make_persistent_id(id_source);
let state = ctx
.memory()
.scroll_areas

View File

@ -169,6 +169,7 @@ impl<'t> TextEdit<'t> {
self
}
/// A source for the unique `Id`, e.g. `.id_source("second_text_edit_field")` or `.id_source(loop_index)`.
pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
self.id_source = Some(Id::new(id_source));
self