Skip to content

Commit

Permalink
xilem: docs update for label, button, sized_box.
Browse files Browse the repository at this point in the history
  • Loading branch information
Artyom Sinyugin committed Jan 15, 2025
1 parent 9abc35d commit df1e9e0
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 1 deletion.
2 changes: 1 addition & 1 deletion xilem/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ The primitives your [Xilem] app’s view tree will generally be constructed from

## Minimum supported Rust Version (MSRV)

This version of Xilem has been verified to compile with **Rust 1.81** and later.
This version of Xilem has been verified to compile with **Rust 1.82** and later.

Future versions of Xilem might increase the Rust version requirement.
It will not be treated as a breaking change and as such can even happen with small patch releases.
Expand Down
45 changes: 45 additions & 0 deletions xilem/src/view/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,48 @@ use crate::{Affine, MessageResult, Pod, ViewCtx, ViewId};
use super::Transformable;

/// A button which calls `callback` when the primary mouse button (normally left) is pressed.
///
/// # Examples
/// To use button provide it with a button text and a closure.
/// ```ignore
/// use xilem::view::button;
///
/// struct State {
/// int: i32,
/// }
///
/// impl State {
/// fn increase(&mut self) {
/// self.int += 1;
/// }
/// }
///
/// button("Button", |state: &mut State| {
/// state.increase();
/// })
/// ```
///
/// Create a `button` with a custom `label`.
///
/// ```ignore
/// use xilem::view::{button, label};
///
/// struct State {
/// int: i32,
/// }
///
/// impl State {
/// fn increase(&mut self) {
/// self.int += 1;
/// }
/// }
///
/// let label = label("Button").weight(FontWeight::BOLD);
///
/// button(label, |state: &mut State| {
/// state.increase();
/// })
/// ```
pub fn button<State, Action>(
label: impl Into<Label>,
callback: impl Fn(&mut State) -> Action + Send + 'static,
Expand Down Expand Up @@ -40,6 +82,9 @@ pub fn button_any_pointer<State, Action>(
}
}

/// The [`View`] created by [`button`].
///
/// See `button` documentation for more context.
#[must_use = "View values do nothing unless provided to Xilem."]
pub struct Button<F> {
label: Label,
Expand Down
23 changes: 23 additions & 0 deletions xilem/src/view/label.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,22 @@ use crate::{Affine, Color, MessageResult, Pod, TextAlignment, View, ViewCtx, Vie

use super::Transformable;

/// A non-interactive text element.
/// # Example
///
/// ```ignore
/// use xilem::palette;
/// use xilem::view::label;
/// use masonry::TextAlignment;
/// use masonry::parley::fontique;
///
/// label("Text example.")
/// .brush(palette::css::RED)
/// .alignment(TextAlignment::Middle)
/// .text_size(24.0)
/// .weight(FontWeight::BOLD)
/// .with_font(fontique::GenericFamily::Serif)
/// ```
pub fn label(label: impl Into<ArcStr>) -> Label {
Label {
label: label.into(),
Expand All @@ -23,6 +39,9 @@ pub fn label(label: impl Into<ArcStr>) -> Label {
}
}

/// The [`View`] created by [`label`].
///
/// See `label` documentation for more context.
#[must_use = "View values do nothing unless provided to Xilem."]
pub struct Label {
// Public for button and variable_label as a semi-interim state.
Expand All @@ -36,23 +55,27 @@ pub struct Label {
}

impl Label {
/// In most cases brush sets text color, but gradients and images are also supported.
#[doc(alias = "color")]
pub fn brush(mut self, brush: impl Into<Brush>) -> Self {
self.text_brush = brush.into();
self
}

/// Sets text alignment: `Start`, `Middle`, `End` or `Justified`.
pub fn alignment(mut self, alignment: TextAlignment) -> Self {
self.alignment = alignment;
self
}

/// Sets text size.
#[doc(alias = "font_size")]
pub fn text_size(mut self, text_size: f32) -> Self {
self.text_size = text_size;
self
}

/// Sets font weight.
pub fn weight(mut self, weight: FontWeight) -> Self {
self.weight = weight;
self
Expand Down
19 changes: 19 additions & 0 deletions xilem/src/view/sized_box.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,22 @@ use super::Transformable;
/// This widget forces its child to have a specific width and/or height (assuming values are permitted by
/// this widget's parent). If either the width or height is not set, this widget will size itself
/// to match the child's size in that dimension.
///
/// # Example
/// See more methods for `sized_box` on [`SizedBox`] page.
/// ```ignore
/// use xilem::view::{sized_box, button};
/// use xilem::palette;
/// use vello::kurbo::RoundedRectRadii;
/// use masonry::widget::Padding;
///
/// sized_box(button("Button", |data: &mut i32| *data+=1))
/// .expand()
/// .background(palette::css::RED)
/// .border(palette::css::YELLOW, 20.)
/// .rounded(RoundedRectRadii::from_single_radius(5.))
/// .padding(Padding::from(5.))
/// ```
pub fn sized_box<State, Action, V>(inner: V) -> SizedBox<V, State, Action>
where
V: WidgetView<State, Action>,
Expand All @@ -35,6 +51,9 @@ where
}
}

/// The [`View`] created by [`sized_box`].
///
/// See `sized_box` documentation for more context.
#[must_use = "View values do nothing unless provided to Xilem."]
pub struct SizedBox<V, State, Action = ()> {
inner: V,
Expand Down

0 comments on commit df1e9e0

Please sign in to comment.