Improve various documentation

This commit is contained in:
Emil Ernerfeldt 2021-05-20 22:09:35 +02:00
parent 5b462197fa
commit 085233f907
5 changed files with 33 additions and 6 deletions

View file

@ -28,12 +28,24 @@ pub use {button::*, drag_value::DragValue, image::Image, slider::*, text_edit::*
/// Anything implementing Widget can be added to a [`Ui`] with [`Ui::add`]. /// Anything implementing Widget can be added to a [`Ui`] with [`Ui::add`].
/// ///
/// Examples include `[Button]`, `[Label]` and [`Slider`]. /// `[Button]`, `[Label]`, [`Slider`], etc all implement the `Widget` trait.
/// ///
/// `|ui: &mut Ui| -> Response { … }` also implemented `Widget`. /// Note that the widgets (`Button`, `TextEdit` etc) are
/// [builders](https://doc.rust-lang.org/1.0.0/style/ownership/builders.html),
/// and not objects that hold state.
///
/// Tip: you can `impl Widget for &mut YourThing { }`.
///
/// `|ui: &mut Ui| -> Response { … }` also implements `Widget`.
#[must_use = "You should put this widget in an ui with `ui.add(widget);`"] #[must_use = "You should put this widget in an ui with `ui.add(widget);`"]
pub trait Widget { pub trait Widget {
/// Allocate space, interact, paint, and return a [`Response`]. /// Allocate space, interact, paint, and return a [`Response`].
///
/// Note that this consumes `self`.
/// This is because most widgets ([`Button`], [`TextEdit`] etc) are
/// [builders](https://doc.rust-lang.org/1.0.0/style/ownership/builders.html)
///
/// Tip: you can `impl Widget for &mut YourObject { }`.
fn ui(self, ui: &mut Ui) -> Response; fn ui(self, ui: &mut Ui) -> Response;
} }

View file

@ -54,6 +54,8 @@ pub enum Shape {
/// ## Constructors /// ## Constructors
impl Shape { impl Shape {
/// A line between two points.
/// More efficient than calling [`Self::line`].
pub fn line_segment(points: [Pos2; 2], stroke: impl Into<Stroke>) -> Self { pub fn line_segment(points: [Pos2; 2], stroke: impl Into<Stroke>) -> Self {
Self::LineSegment { Self::LineSegment {
points, points,
@ -61,6 +63,9 @@ impl Shape {
} }
} }
/// A line through many points.
///
/// Use [`Self::line_segment`] instead if your line only connect two points.
pub fn line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self { pub fn line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
Self::Path { Self::Path {
points, points,
@ -70,6 +75,7 @@ impl Shape {
} }
} }
/// A line that closes back to the start point again.
pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self { pub fn closed_line(points: Vec<Pos2>, stroke: impl Into<Stroke>) -> Self {
Self::Path { Self::Path {
points, points,

View file

@ -1,6 +1,8 @@
use super::*; use super::*;
/// Describes the width and color of a line. /// Describes the width and color of a line.
///
/// The default stroke is the same as [`Stroke::none`].
#[derive(Clone, Copy, Debug, Default, PartialEq)] #[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))] #[cfg_attr(feature = "persistence", derive(serde::Deserialize, serde::Serialize))]
pub struct Stroke { pub struct Stroke {
@ -9,6 +11,7 @@ pub struct Stroke {
} }
impl Stroke { impl Stroke {
/// Same as [`Stroke::default`].
pub fn none() -> Self { pub fn none() -> Self {
Self::new(0.0, Color32::TRANSPARENT) Self::new(0.0, Color32::TRANSPARENT)
} }

View file

@ -341,20 +341,23 @@ impl Font {
self.finalize_galley(galley) self.finalize_galley(galley)
} }
/// Always returns at least one row.
/// Will line break at `\n`. /// Will line break at `\n`.
///
/// Always returns at least one row.
pub fn layout_no_wrap(&self, text: String) -> Galley { pub fn layout_no_wrap(&self, text: String) -> Galley {
self.layout_multiline(text, f32::INFINITY) self.layout_multiline(text, f32::INFINITY)
} }
/// Will wrap text at the given width and line break at `\n`.
///
/// Always returns at least one row. /// Always returns at least one row.
/// Will wrap text at the given width.
pub fn layout_multiline(&self, text: String, max_width_in_points: f32) -> Galley { pub fn layout_multiline(&self, text: String, max_width_in_points: f32) -> Galley {
self.layout_multiline_with_indentation_and_max_width(text, 0.0, max_width_in_points) self.layout_multiline_with_indentation_and_max_width(text, 0.0, max_width_in_points)
} }
/// * `first_row_indentation`: extra space before the very first character (in points). /// * `first_row_indentation`: extra space before the very first character (in points).
/// * `max_width_in_points`: wrapping width. /// * `max_width_in_points`: wrapping width.
///
/// Always returns at least one row. /// Always returns at least one row.
pub fn layout_multiline_with_indentation_and_max_width( pub fn layout_multiline_with_indentation_and_max_width(
&self, &self,

View file

@ -315,8 +315,9 @@ impl Fonts {
self.fonts[&text_style].row_height() self.fonts[&text_style].row_height()
} }
/// Always returns at least one row.
/// Will line break at `\n`. /// Will line break at `\n`.
///
/// Always returns at least one row.
pub fn layout_no_wrap(&self, text_style: TextStyle, text: String) -> Arc<Galley> { pub fn layout_no_wrap(&self, text_style: TextStyle, text: String) -> Arc<Galley> {
self.layout_multiline(text_style, text, f32::INFINITY) self.layout_multiline(text_style, text, f32::INFINITY)
} }
@ -338,8 +339,9 @@ impl Fonts {
) )
} }
/// Will wrap text at the given width and line break at `\n`.
///
/// Always returns at least one row. /// Always returns at least one row.
/// Will wrap text at the given width.
pub fn layout_multiline( pub fn layout_multiline(
&self, &self,
text_style: TextStyle, text_style: TextStyle,
@ -356,6 +358,7 @@ impl Fonts {
/// * `first_row_indentation`: extra space before the very first character (in points). /// * `first_row_indentation`: extra space before the very first character (in points).
/// * `max_width_in_points`: wrapping width. /// * `max_width_in_points`: wrapping width.
///
/// Always returns at least one row. /// Always returns at least one row.
pub fn layout_multiline_with_indentation_and_max_width( pub fn layout_multiline_with_indentation_and_max_width(
&self, &self,