Skip to content

Commit

Permalink
Rename id_source to id_salt (#5025)
Browse files Browse the repository at this point in the history
* Closes <#5020 >
* [x] I have followed the instructions in the PR template
  • Loading branch information
bircni authored Sep 2, 2024
1 parent edea5a4 commit be944f0
Show file tree
Hide file tree
Showing 19 changed files with 169 additions and 121 deletions.
25 changes: 17 additions & 8 deletions crates/egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -372,7 +372,7 @@ pub struct CollapsingHeader {
text: WidgetText,
default_open: bool,
open: Option<bool>,
id_source: Id,
id_salt: Id,
enabled: bool,
selectable: bool,
selected: bool,
Expand All @@ -386,15 +386,15 @@ impl CollapsingHeader {
/// The label is used as an [`Id`] source.
/// If the label is unique and static this is fine,
/// but if it changes or there are several [`CollapsingHeader`] with the same title
/// you need to provide a unique id source with [`Self::id_source`].
/// you need to provide a unique id source with [`Self::id_salt`].
pub fn new(text: impl Into<WidgetText>) -> Self {
let text = text.into();
let id_source = Id::new(text.text());
let id_salt = Id::new(text.text());
Self {
text,
default_open: false,
open: None,
id_source,
id_salt,
enabled: true,
selectable: false,
selected: false,
Expand Down Expand Up @@ -425,8 +425,17 @@ impl CollapsingHeader {
/// Explicitly set the source of the [`Id`] of this widget, instead of using title label.
/// This is useful if the title label is dynamic or not unique.
#[inline]
pub fn id_source(mut self, id_source: impl Hash) -> Self {
self.id_source = Id::new(id_source);
pub fn id_salt(mut self, id_salt: impl Hash) -> Self {
self.id_salt = Id::new(id_salt);
self
}

/// Explicitly set the source of the [`Id`] of this widget, instead of using title label.
/// This is useful if the title label is dynamic or not unique.
#[deprecated = "Renamed id_salt"]
#[inline]
pub fn id_source(mut self, id_salt: impl Hash) -> Self {
self.id_salt = Id::new(id_salt);
self
}

Expand Down Expand Up @@ -494,7 +503,7 @@ impl CollapsingHeader {
text,
default_open,
open,
id_source,
id_salt,
enabled: _,
selectable,
selected,
Expand All @@ -503,7 +512,7 @@ impl CollapsingHeader {

// TODO(emilk): horizontal layout, with icon and text as labels. Insert background behind using Frame.

let id = ui.make_persistent_id(id_source);
let id = ui.make_persistent_id(id_salt);
let button_padding = ui.spacing().button_padding;

let available = ui.available_rect_before_wrap();
Expand Down
24 changes: 15 additions & 9 deletions crates/egui/src/containers/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub type IconPainter = Box<dyn FnOnce(&Ui, Rect, &WidgetVisuals, bool, AboveOrBe
/// ```
#[must_use = "You should call .show*"]
pub struct ComboBox {
id_source: Id,
id_salt: Id,
label: Option<WidgetText>,
selected_text: WidgetText,
width: Option<f32>,
Expand All @@ -49,9 +49,9 @@ pub struct ComboBox {

impl ComboBox {
/// Create new [`ComboBox`] with id and label
pub fn new(id_source: impl std::hash::Hash, label: impl Into<WidgetText>) -> Self {
pub fn new(id_salt: impl std::hash::Hash, label: impl Into<WidgetText>) -> Self {
Self {
id_source: Id::new(id_source),
id_salt: Id::new(id_salt),
label: Some(label.into()),
selected_text: Default::default(),
width: None,
Expand All @@ -65,7 +65,7 @@ impl ComboBox {
pub fn from_label(label: impl Into<WidgetText>) -> Self {
let label = label.into();
Self {
id_source: Id::new(label.text()),
id_salt: Id::new(label.text()),
label: Some(label),
selected_text: Default::default(),
width: None,
Expand All @@ -76,9 +76,9 @@ impl ComboBox {
}

/// Without label.
pub fn from_id_source(id_source: impl std::hash::Hash) -> Self {
pub fn from_id_salt(id_salt: impl std::hash::Hash) -> Self {
Self {
id_source: Id::new(id_source),
id_salt: Id::new(id_salt),
label: Default::default(),
selected_text: Default::default(),
width: None,
Expand All @@ -88,6 +88,12 @@ impl ComboBox {
}
}

/// Without label.
#[deprecated = "Renamed id_salt"]
pub fn from_id_source(id_salt: impl std::hash::Hash) -> Self {
Self::from_id_salt(id_salt)
}

/// Set the outer width of the button and menu.
///
/// Default is [`Spacing::combo_width`].
Expand Down Expand Up @@ -138,7 +144,7 @@ impl ComboBox {
/// ));
/// }
///
/// egui::ComboBox::from_id_source("my-combobox")
/// egui::ComboBox::from_id_salt("my-combobox")
/// .selected_text(text)
/// .icon(filled_triangle)
/// .show_ui(ui, |_ui| {});
Expand Down Expand Up @@ -195,7 +201,7 @@ impl ComboBox {
menu_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
) -> InnerResponse<Option<R>> {
let Self {
id_source,
id_salt,
label,
selected_text,
width,
Expand All @@ -204,7 +210,7 @@ impl ComboBox {
wrap_mode,
} = self;

let button_id = ui.make_persistent_id(id_source);
let button_id = ui.make_persistent_id(id_salt);

ui.horizontal(|ui| {
let mut ir = combo_box_dyn(
Expand Down
4 changes: 2 additions & 2 deletions crates/egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,7 +266,7 @@ impl SidePanel {

let mut panel_ui = ui.new_child(
UiBuilder::new()
.id_source(id)
.id_salt(id)
.ui_stack_info(UiStackInfo::new(match side {
Side::Left => UiKind::LeftPanel,
Side::Right => UiKind::RightPanel,
Expand Down Expand Up @@ -761,7 +761,7 @@ impl TopBottomPanel {

let mut panel_ui = ui.new_child(
UiBuilder::new()
.id_source(id)
.id_salt(id)
.ui_stack_info(UiStackInfo::new(match side {
TopBottomSide::Top => UiKind::TopPanel,
TopBottomSide::Bottom => UiKind::BottomPanel,
Expand Down
19 changes: 13 additions & 6 deletions crates/egui/src/containers/resize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ impl State {
#[must_use = "You should call .show()"]
pub struct Resize {
id: Option<Id>,
id_source: Option<Id>,
id_salt: Option<Id>,

/// If false, we are no enabled
resizable: Vec2b,
Expand All @@ -51,7 +51,7 @@ impl Default for Resize {
fn default() -> Self {
Self {
id: None,
id_source: None,
id_salt: None,
resizable: Vec2b::TRUE,
min_size: Vec2::splat(16.0),
max_size: Vec2::splat(f32::INFINITY),
Expand All @@ -71,8 +71,15 @@ impl Resize {

/// A source for the unique [`Id`], e.g. `.id_source("second_resize_area")` or `.id_source(loop_index)`.
#[inline]
pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
self.id_source = Some(Id::new(id_source));
#[deprecated = "Renamed id_salt"]
pub fn id_source(self, id_salt: impl std::hash::Hash) -> Self {
self.id_salt(id_salt)
}

/// A source for the unique [`Id`], e.g. `.id_salt("second_resize_area")` or `.id_salt(loop_index)`.
#[inline]
pub fn id_salt(mut self, id_salt: impl std::hash::Hash) -> Self {
self.id_salt = Some(Id::new(id_salt));
self
}

Expand Down Expand Up @@ -201,8 +208,8 @@ impl Resize {
fn begin(&mut self, ui: &mut Ui) -> Prepared {
let position = ui.available_rect_before_wrap().min;
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 id_salt = self.id_salt.unwrap_or_else(|| Id::new("resize"));
ui.make_persistent_id(id_salt)
});

let mut state = State::load(ui.ctx(), id).unwrap_or_else(|| {
Expand Down
21 changes: 14 additions & 7 deletions crates/egui/src/containers/scroll_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,7 @@ pub struct ScrollArea {
max_size: Vec2,
min_scrolled_size: Vec2,
scroll_bar_visibility: ScrollBarVisibility,
id_source: Option<Id>,
id_salt: Option<Id>,
offset_x: Option<f32>,
offset_y: Option<f32>,

Expand Down Expand Up @@ -223,7 +223,7 @@ impl ScrollArea {
max_size: Vec2::INFINITY,
min_scrolled_size: Vec2::splat(64.0),
scroll_bar_visibility: Default::default(),
id_source: None,
id_salt: None,
offset_x: None,
offset_y: None,
scrolling_enabled: true,
Expand Down Expand Up @@ -290,8 +290,15 @@ impl ScrollArea {

/// A source for the unique [`Id`], e.g. `.id_source("second_scroll_area")` or `.id_source(loop_index)`.
#[inline]
pub fn id_source(mut self, id_source: impl std::hash::Hash) -> Self {
self.id_source = Some(Id::new(id_source));
#[deprecated = "Renamed id_salt"]
pub fn id_source(self, id_salt: impl std::hash::Hash) -> Self {
self.id_salt(id_salt)
}

/// A source for the unique [`Id`], e.g. `.id_salt("second_scroll_area")` or `.id_salt(loop_index)`.
#[inline]
pub fn id_salt(mut self, id_salt: impl std::hash::Hash) -> Self {
self.id_salt = Some(Id::new(id_salt));
self
}

Expand Down Expand Up @@ -490,7 +497,7 @@ impl ScrollArea {
max_size,
min_scrolled_size,
scroll_bar_visibility,
id_source,
id_salt,
offset_x,
offset_y,
scrolling_enabled,
Expand All @@ -502,8 +509,8 @@ impl ScrollArea {
let ctx = ui.ctx().clone();
let scrolling_enabled = scrolling_enabled && ui.is_enabled();

let id_source = id_source.unwrap_or_else(|| Id::new("scroll_area"));
let id = ui.make_persistent_id(id_source);
let id_salt = id_salt.unwrap_or_else(|| Id::new("scroll_area"));
let id = ui.make_persistent_id(id_salt);
ctx.check_for_id_clash(
id,
Rect::from_min_size(ui.available_rect_before_wrap().min, Vec2::ZERO),
Expand Down
2 changes: 1 addition & 1 deletion crates/egui/src/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1022,7 +1022,7 @@ impl Context {
tooltip_pos,
format!("Widget is {} this text.\n\n\
ID clashes happens when things like Windows or CollapsingHeaders share names,\n\
or when things like Plot and Grid:s aren't given unique id_source:s.\n\n\
or when things like Plot and Grid:s aren't given unique id_salt:s.\n\n\
Sometimes the solution is to use ui.push_id.",
if below { "above" } else { "below" })
);
Expand Down
10 changes: 5 additions & 5 deletions crates/egui/src/grid.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl GridLayout {
/// ```
#[must_use = "You should call .show()"]
pub struct Grid {
id_source: Id,
id_salt: Id,
num_columns: Option<usize>,
min_col_width: Option<f32>,
min_row_height: Option<f32>,
Expand All @@ -311,9 +311,9 @@ pub struct Grid {

impl Grid {
/// Create a new [`Grid`] with a locally unique identifier.
pub fn new(id_source: impl std::hash::Hash) -> Self {
pub fn new(id_salt: impl std::hash::Hash) -> Self {
Self {
id_source: Id::new(id_source),
id_salt: Id::new(id_salt),
num_columns: None,
min_col_width: None,
min_row_height: None,
Expand Down Expand Up @@ -407,7 +407,7 @@ impl Grid {
add_contents: Box<dyn FnOnce(&mut Ui) -> R + 'c>,
) -> InnerResponse<R> {
let Self {
id_source,
id_salt,
num_columns,
min_col_width,
min_row_height,
Expand All @@ -423,7 +423,7 @@ impl Grid {
color_picker = Some(Box::new(striped_row_color));
}

let id = ui.make_persistent_id(id_source);
let id = ui.make_persistent_id(id_salt);
let prev_state = State::load(ui.ctx(), id);

// Each grid cell is aligned LEFT_CENTER.
Expand Down
6 changes: 3 additions & 3 deletions crates/egui/src/style.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1537,7 +1537,7 @@ impl Style {
ui.end_row();

ui.label("Override text style");
crate::ComboBox::from_id_source("Override text style")
crate::ComboBox::from_id_salt("Override text style")
.selected_text(match override_text_style {
None => "None".to_owned(),
Some(override_text_style) => override_text_style.to_string(),
Expand All @@ -1554,7 +1554,7 @@ impl Style {
ui.end_row();

ui.label("Text style of DragValue");
crate::ComboBox::from_id_source("drag_value_text_style")
crate::ComboBox::from_id_salt("drag_value_text_style")
.selected_text(drag_value_text_style.to_string())
.show_ui(ui, |ui| {
let all_text_styles = ui.style().text_styles();
Expand All @@ -1567,7 +1567,7 @@ impl Style {
ui.end_row();

ui.label("Text Wrap Mode");
crate::ComboBox::from_id_source("text_wrap_mode")
crate::ComboBox::from_id_salt("text_wrap_mode")
.selected_text(format!("{wrap_mode:?}"))
.show_ui(ui, |ui| {
let all_wrap_mode: Vec<Option<TextWrapMode>> = vec![
Expand Down
Loading

0 comments on commit be944f0

Please sign in to comment.