summaryrefslogtreecommitdiffstats
path: root/native/src
diff options
context:
space:
mode:
authorLibravatar Songtronix <contact@songtronix.com>2020-01-01 17:27:14 +0100
committerLibravatar Songtronix <contact@songtronix.com>2020-01-02 11:11:08 +0100
commitd60f3b89a75f5b2ad8e6fb17827f5574a0a44bd1 (patch)
tree2fb1abb3717e8170df4e7946675b550b07d5eab9 /native/src
parent26de688e68347e1f6e388d01014eac89cea71afa (diff)
downloadiced-d60f3b89a75f5b2ad8e6fb17827f5574a0a44bd1.tar.gz
iced-d60f3b89a75f5b2ad8e6fb17827f5574a0a44bd1.tar.bz2
iced-d60f3b89a75f5b2ad8e6fb17827f5574a0a44bd1.zip
add(widget): primitive progressbar widget
Diffstat (limited to 'native/src')
-rw-r--r--native/src/widget.rs3
-rw-r--r--native/src/widget/progressbar.rs135
-rw-r--r--native/src/widget/text_input.rs3
3 files changed, 140 insertions, 1 deletions
diff --git a/native/src/widget.rs b/native/src/widget.rs
index dcc99645..d899da58 100644
--- a/native/src/widget.rs
+++ b/native/src/widget.rs
@@ -25,6 +25,7 @@ pub mod checkbox;
pub mod column;
pub mod container;
pub mod image;
+pub mod progressbar;
pub mod radio;
pub mod row;
pub mod scrollable;
@@ -45,6 +46,8 @@ pub use container::Container;
#[doc(no_inline)]
pub use image::Image;
#[doc(no_inline)]
+pub use progressbar::Progressbar;
+#[doc(no_inline)]
pub use radio::Radio;
#[doc(no_inline)]
pub use row::Row;
diff --git a/native/src/widget/progressbar.rs b/native/src/widget/progressbar.rs
new file mode 100644
index 00000000..a6725f2c
--- /dev/null
+++ b/native/src/widget/progressbar.rs
@@ -0,0 +1,135 @@
+//! Display a progressbar
+//!
+//!
+//! [`Progressbar`]: struct.Progressbar.html
+use crate::{
+ layout, Element, Hasher, Layout, Length, Point, Rectangle, Size, Widget,
+};
+
+use std::{hash::Hash, ops::RangeInclusive};
+
+/// A progressbar
+///
+/// # Example
+///
+/// ```
+/// # use iced_native::Progressbar;
+///
+/// Progressbar::new(0.0..=100.0, value);
+/// ```
+///
+/// TODO: Get a progressbar render
+/// ![Slider drawn by Coffee's renderer](https://github.com/hecrj/coffee/blob/bda9818f823dfcb8a7ad0ff4940b4d4b387b5208/images/ui/slider.png?raw=true)
+#[allow(missing_debug_implementations)]
+pub struct Progressbar {
+ range: RangeInclusive<f32>,
+ value: f32,
+ width: Length,
+}
+
+impl Progressbar {
+ /// Creates a new [`Progressbar`].
+ ///
+ /// It expects:
+ /// * an inclusive range of possible values
+ /// * the current value of the [`Progressbar`]
+ ///
+ /// [`Progressbar`]: struct.Progressbar.html
+ pub fn new(range: RangeInclusive<f32>, value: f32) -> Self {
+ Progressbar {
+ value: value.max(*range.start()).min(*range.end()),
+ range,
+ width: Length::Fill,
+ }
+ }
+
+ /// Sets the width of the [`Progressbar`].
+ ///
+ /// [`Progressbar`]: struct.Progressbar.html
+ pub fn width(mut self, width: Length) -> Self {
+ self.width = width;
+ self
+ }
+}
+
+impl<Message, Renderer> Widget<Message, Renderer> for Progressbar
+where
+ Renderer: self::Renderer,
+{
+ fn width(&self) -> Length {
+ self.width
+ }
+
+ fn height(&self) -> Length {
+ Length::Shrink
+ }
+
+ fn layout(
+ &self,
+ renderer: &Renderer,
+ limits: &layout::Limits,
+ ) -> layout::Node {
+ let limits = limits
+ .width(self.width)
+ .height(Length::Units(renderer.height() as u16));
+
+ let size = limits.resolve(Size::ZERO);
+
+ layout::Node::new(size)
+ }
+
+ fn draw(
+ &self,
+ renderer: &mut Renderer,
+ layout: Layout<'_>,
+ _cursor_position: Point,
+ ) -> Renderer::Output {
+ renderer.draw(layout.bounds(), self.range.clone(), self.value)
+ }
+
+ fn hash_layout(&self, state: &mut Hasher) {
+ self.width.hash(state);
+ }
+}
+
+/// The renderer of a [`Progressbar`].
+///
+/// Your [renderer] will need to implement this trait before being
+/// able to use a [`Progressbar`] in your user interface.
+///
+/// [`Progressbar`]: struct.Progressbar.html
+/// [renderer]: ../../renderer/index.html
+pub trait Renderer: crate::Renderer {
+ /// Returns the height of the [`Progressbar`].
+ ///
+ /// [`Progressbar`]: struct.Progressbar.html
+ fn height(&self) -> u32;
+
+ /// Draws a [`Progressbar`].
+ ///
+ /// It receives:
+ /// * the local state of the [`Progressbar`]
+ /// * the bounds of the [`Progressbar`]
+ /// * the range of values of the [`Progressbar`]
+ /// * the current value of the [`Progressbar`]
+ ///
+ /// [`Progressbar`]: struct.Progressbar.html
+ /// [`State`]: struct.State.html
+ /// [`Class`]: enum.Class.html
+ fn draw(
+ &self,
+ bounds: Rectangle,
+ range: RangeInclusive<f32>,
+ value: f32,
+ ) -> Self::Output;
+}
+
+impl<'a, Message, Renderer> From<Progressbar> for Element<'a, Message, Renderer>
+where
+ Renderer: self::Renderer,
+ Message: 'static,
+{
+ fn from(progressbar: Progressbar) -> Element<'a, Message, Renderer> {
+ Element::new(progressbar)
+ }
+}
diff --git a/native/src/widget/text_input.rs b/native/src/widget/text_input.rs
index 1d1c32a2..c994b7ba 100644
--- a/native/src/widget/text_input.rs
+++ b/native/src/widget/text_input.rs
@@ -633,7 +633,8 @@ impl Value {
.unwrap_or(self.len())
}
- /// Returns a new [`Value`] containing the graphemes until the given `index`.
+ /// Returns a new [`Value`] containing the graphemes until the given
+ /// `index`.
///
/// [`Value`]: struct.Value.html
pub fn until(&self, index: usize) -> Self {