Improve documentation of ui.add_sized

This commit is contained in:
Emil Ernerfeldt 2021-03-31 21:49:24 +02:00
parent 3450168e94
commit b393bdcb74
2 changed files with 25 additions and 3 deletions

View file

@ -201,6 +201,15 @@
//! } //! }
//! ``` //! ```
//! //!
//! ## Sizes
//! You can control the size of widgets using [`Ui::add_sized`].
//!
//! ```
//! # let ui = &mut egui::Ui::__test();
//! # let mut my_value = 0.0_f32;
//! ui.add_sized([40.0, 20.0], egui::DragValue::new(&mut my_value));
//! ```
//!
//! ## Code snippets //! ## Code snippets
//! //!
//! ``` //! ```

View file

@ -781,6 +781,8 @@ impl Ui {
/// The returned [`Response`] can be used to check for interactions, /// The returned [`Response`] can be used to check for interactions,
/// as well as adding tooltips using [`Response::on_hover_text`]. /// as well as adding tooltips using [`Response::on_hover_text`].
/// ///
/// See also [`Self::add_sized`] and [`Self::put`].
///
/// ``` /// ```
/// # let mut ui = egui::Ui::__test(); /// # let mut ui = egui::Ui::__test();
/// # let mut my_value = 42; /// # let mut my_value = 42;
@ -791,15 +793,26 @@ impl Ui {
widget.ui(self) widget.ui(self)
} }
/// Add a [`Widget`] to this `Ui` with a given max size. /// Add a [`Widget`] to this `Ui` with a given size.
pub fn add_sized(&mut self, max_size: Vec2, widget: impl Widget) -> Response { /// The widget will attempt to fit within the given size, but some widgets may overflow.
self.allocate_ui(max_size, |ui| { ///
/// See also [`Self::add`] and [`Self::put`].
///
/// ```
/// # let mut ui = egui::Ui::__test();
/// # let mut my_value = 42;
/// ui.add_sized([40.0, 20.0], egui::DragValue::new(&mut my_value));
/// ```
pub fn add_sized(&mut self, max_size: impl Into<Vec2>, widget: impl Widget) -> Response {
self.allocate_ui(max_size.into(), |ui| {
ui.centered_and_justified(|ui| ui.add(widget)).inner ui.centered_and_justified(|ui| ui.add(widget)).inner
}) })
.inner .inner
} }
/// Add a [`Widget`] to this `Ui` at a specific location (manual layout). /// Add a [`Widget`] to this `Ui` at a specific location (manual layout).
///
/// See also [`Self::add`] and [`Self::add_sized`].
pub fn put(&mut self, max_rect: Rect, widget: impl Widget) -> Response { pub fn put(&mut self, max_rect: Rect, widget: impl Widget) -> Response {
self.allocate_ui_at_rect(max_rect, |ui| { self.allocate_ui_at_rect(max_rect, |ui| {
ui.centered_and_justified(|ui| ui.add(widget)).inner ui.centered_and_justified(|ui| ui.add(widget)).inner