Skip to content

Commit

Permalink
Add #[inline] to all builder-pattern functions (#3557)
Browse files Browse the repository at this point in the history
Better performance and maybe code size
  • Loading branch information
emilk authored Nov 16, 2023
1 parent 4886c8c commit a243180
Show file tree
Hide file tree
Showing 32 changed files with 399 additions and 36 deletions.
13 changes: 13 additions & 0 deletions crates/egui/src/containers/area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ impl Area {
}
}

#[inline]
pub fn id(mut self, id: Id) -> Self {
self.id = id;
self
Expand All @@ -103,12 +104,14 @@ impl Area {
/// and widgets will be shown grayed out.
/// You won't be able to move the window.
/// Default: `true`.
#[inline]
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}

/// moveable by dragging the area?
#[inline]
pub fn movable(mut self, movable: bool) -> Self {
self.movable = movable;
self.interactable |= movable;
Expand All @@ -125,31 +128,36 @@ impl Area {

/// If false, clicks goes straight through to what is behind us.
/// Good for tooltips etc.
#[inline]
pub fn interactable(mut self, interactable: bool) -> Self {
self.interactable = interactable;
self.movable &= interactable;
self
}

/// `order(Order::Foreground)` for an Area that should always be on top
#[inline]
pub fn order(mut self, order: Order) -> Self {
self.order = order;
self
}

#[inline]
pub fn default_pos(mut self, default_pos: impl Into<Pos2>) -> Self {
self.default_pos = Some(default_pos.into());
self
}

/// Positions the window and prevents it from being moved
#[inline]
pub fn fixed_pos(mut self, fixed_pos: impl Into<Pos2>) -> Self {
self.new_pos = Some(fixed_pos.into());
self.movable = false;
self
}

/// Constrains this area to the screen bounds.
#[inline]
pub fn constrain(mut self, constrain: bool) -> Self {
self.constrain = constrain;
self
Expand All @@ -158,13 +166,15 @@ impl Area {
/// Constrain the movement of the window to the given rectangle.
///
/// For instance: `.constrain_to(ctx.screen_rect())`.
#[inline]
pub fn constrain_to(mut self, constrain_rect: Rect) -> Self {
self.constrain = true;
self.constrain_rect = Some(constrain_rect);
self
}

#[deprecated = "Use `constrain_to` instead"]
#[inline]
pub fn drag_bounds(mut self, constrain_rect: Rect) -> Self {
self.constrain_rect = Some(constrain_rect);
self
Expand All @@ -177,12 +187,14 @@ impl Area {
/// corner of the area.
///
/// Default: [`Align2::LEFT_TOP`].
#[inline]
pub fn pivot(mut self, pivot: Align2) -> Self {
self.pivot = pivot;
self
}

/// Positions the window but you can still move it.
#[inline]
pub fn current_pos(mut self, current_pos: impl Into<Pos2>) -> Self {
self.new_pos = Some(current_pos.into());
self
Expand All @@ -199,6 +211,7 @@ impl Area {
/// Anchoring also makes the window immovable.
///
/// It is an error to set both an anchor and a position.
#[inline]
pub fn anchor(mut self, align: Align2, offset: impl Into<Vec2>) -> Self {
self.anchor = Some((align, offset.into()));
self.movable(false)
Expand Down
8 changes: 8 additions & 0 deletions crates/egui/src/containers/collapsing_header.rs
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,7 @@ impl CollapsingHeader {

/// By default, the [`CollapsingHeader`] is collapsed.
/// Call `.default_open(true)` to change this.
#[inline]
pub fn default_open(mut self, open: bool) -> Self {
self.default_open = open;
self
Expand All @@ -400,13 +401,15 @@ impl CollapsingHeader {
/// Calling `.open(Some(false))` will make the collapsing header close this frame (or stay closed).
///
/// Calling `.open(None)` has no effect (default).
#[inline]
pub fn open(mut self, open: Option<bool>) -> Self {
self.open = open;
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.
#[inline]
pub fn id_source(mut self, id_source: impl Hash) -> Self {
self.id_source = Id::new(id_source);
self
Expand All @@ -415,13 +418,15 @@ impl CollapsingHeader {
/// If you set this to `false`, the [`CollapsingHeader`] will be grayed out and un-clickable.
///
/// This is a convenience for [`Ui::set_enabled`].
#[inline]
pub fn enabled(mut self, enabled: bool) -> Self {
self.enabled = enabled;
self
}

/// Can the [`CollapsingHeader`] be selected by clicking it? Default: `false`.
#[deprecated = "Use the more powerful egui::collapsing_header::CollapsingState::show_header"] // Deprecated in 2022-04-28, before egui 0.18
#[inline]
pub fn selectable(mut self, selectable: bool) -> Self {
self.selectable = selectable;
self
Expand All @@ -443,6 +448,7 @@ impl CollapsingHeader {
/// # });
/// ```
#[deprecated = "Use the more powerful egui::collapsing_header::CollapsingState::show_header"] // Deprecated in 2022-04-28, before egui 0.18
#[inline]
pub fn selected(mut self, selected: bool) -> Self {
self.selected = selected;
self
Expand All @@ -456,6 +462,7 @@ impl CollapsingHeader {
/// ui.visuals_mut().collapsing_header_frame = true;
/// # });
/// ```
#[inline]
pub fn show_background(mut self, show_background: bool) -> Self {
self.show_background = show_background;
self
Expand All @@ -478,6 +485,7 @@ impl CollapsingHeader {
/// .show(ui, |ui| { ui.label("Hi!"); });
/// # });
/// ```
#[inline]
pub fn icon(mut self, icon_fn: impl FnOnce(&mut Ui, f32, &Response) + 'static) -> Self {
self.icon = Some(Box::new(icon_fn));
self
Expand Down
3 changes: 3 additions & 0 deletions crates/egui/src/containers/combo_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,12 +78,14 @@ impl ComboBox {
}

/// Set the outer width of the button and menu.
#[inline]
pub fn width(mut self, width: f32) -> Self {
self.width = Some(width);
self
}

/// What we show as the currently selected value
#[inline]
pub fn selected_text(mut self, selected_text: impl Into<WidgetText>) -> Self {
self.selected_text = selected_text.into();
self
Expand Down Expand Up @@ -129,6 +131,7 @@ impl ComboBox {
}

/// Controls whether text wrap is used for the selected text
#[inline]
pub fn wrap(mut self, wrap: bool) -> Self {
self.wrap_enabled = wrap;
self
Expand Down
1 change: 1 addition & 0 deletions crates/egui/src/containers/frame.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,7 @@ impl Frame {
self
}

#[inline]
pub fn multiply_with_opacity(mut self, opacity: f32) -> Self {
self.fill = self.fill.linear_multiply(opacity);
self.stroke.color = self.stroke.color.linear_multiply(opacity);
Expand Down
17 changes: 17 additions & 0 deletions crates/egui/src/containers/panel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ impl SidePanel {
/// * A [`Separator`].
/// * A [`TextEdit`].
/// * …
#[inline]
pub fn resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
Expand All @@ -143,12 +144,14 @@ impl SidePanel {
/// Show a separator line, even when not interacting with it?
///
/// Default: `true`.
#[inline]
pub fn show_separator_line(mut self, show_separator_line: bool) -> Self {
self.show_separator_line = show_separator_line;
self
}

/// The initial wrapping width of the [`SidePanel`].
#[inline]
pub fn default_width(mut self, default_width: f32) -> Self {
self.default_width = default_width;
self.width_range = Rangef::new(
Expand All @@ -159,18 +162,21 @@ impl SidePanel {
}

/// Minimum width of the panel.
#[inline]
pub fn min_width(mut self, min_width: f32) -> Self {
self.width_range = Rangef::new(min_width, self.width_range.max.at_least(min_width));
self
}

/// Maximum width of the panel.
#[inline]
pub fn max_width(mut self, max_width: f32) -> Self {
self.width_range = Rangef::new(self.width_range.min.at_most(max_width), max_width);
self
}

/// The allowable width range for the panel.
#[inline]
pub fn width_range(mut self, width_range: impl Into<Rangef>) -> Self {
let width_range = width_range.into();
self.default_width = clamp_to_range(self.default_width, width_range);
Expand All @@ -179,13 +185,15 @@ impl SidePanel {
}

/// Enforce this exact width.
#[inline]
pub fn exact_width(mut self, width: f32) -> Self {
self.default_width = width;
self.width_range = Rangef::point(width);
self
}

/// Change the background color, margins, etc.
#[inline]
pub fn frame(mut self, frame: Frame) -> Self {
self.frame = Some(frame);
self
Expand Down Expand Up @@ -582,6 +590,7 @@ impl TopBottomPanel {
/// * A [`Separator`].
/// * A [`TextEdit`].
/// * …
#[inline]
pub fn resizable(mut self, resizable: bool) -> Self {
self.resizable = resizable;
self
Expand All @@ -590,13 +599,15 @@ impl TopBottomPanel {
/// Show a separator line, even when not interacting with it?
///
/// Default: `true`.
#[inline]
pub fn show_separator_line(mut self, show_separator_line: bool) -> Self {
self.show_separator_line = show_separator_line;
self
}

/// The initial height of the [`SidePanel`].
/// Defaults to [`style::Spacing::interact_size`].y.
#[inline]
pub fn default_height(mut self, default_height: f32) -> Self {
self.default_height = Some(default_height);
self.height_range = Rangef::new(
Expand All @@ -607,18 +618,21 @@ impl TopBottomPanel {
}

/// Minimum height of the panel.
#[inline]
pub fn min_height(mut self, min_height: f32) -> Self {
self.height_range = Rangef::new(min_height, self.height_range.max.at_least(min_height));
self
}

/// Maximum height of the panel.
#[inline]
pub fn max_height(mut self, max_height: f32) -> Self {
self.height_range = Rangef::new(self.height_range.min.at_most(max_height), max_height);
self
}

/// The allowable height range for the panel.
#[inline]
pub fn height_range(mut self, height_range: impl Into<Rangef>) -> Self {
let height_range = height_range.into();
self.default_height = self
Expand All @@ -629,13 +643,15 @@ impl TopBottomPanel {
}

/// Enforce this exact height.
#[inline]
pub fn exact_height(mut self, height: f32) -> Self {
self.default_height = Some(height);
self.height_range = Rangef::point(height);
self
}

/// Change the background color, margins, etc.
#[inline]
pub fn frame(mut self, frame: Frame) -> Self {
self.frame = Some(frame);
self
Expand Down Expand Up @@ -994,6 +1010,7 @@ pub struct CentralPanel {

impl CentralPanel {
/// Change the background color, margins, etc.
#[inline]
pub fn frame(mut self, frame: Frame) -> Self {
self.frame = Some(frame);
self
Expand Down
Loading

0 comments on commit a243180

Please sign in to comment.