summaryrefslogtreecommitdiffstats
path: root/examples/progress_indicators/src/main.rs
diff options
context:
space:
mode:
authorLibravatar Nick Senger <dev@nsenger.com>2023-06-06 21:12:41 -0700
committerLibravatar Nick Senger <dev@nsenger.com>2023-06-07 11:57:49 -0700
commitcdfb8b30680de164280b8b90fbc08a1638e597e2 (patch)
tree372a7262b9f7cda79bf02fcd6bb94bb8590dd25a /examples/progress_indicators/src/main.rs
parentc15f1b5f6575792cc89bb5fba2e613428397e46a (diff)
downloadiced-cdfb8b30680de164280b8b90fbc08a1638e597e2.tar.gz
iced-cdfb8b30680de164280b8b90fbc08a1638e597e2.tar.bz2
iced-cdfb8b30680de164280b8b90fbc08a1638e597e2.zip
feat: add progress indicators example
Diffstat (limited to 'examples/progress_indicators/src/main.rs')
-rw-r--r--examples/progress_indicators/src/main.rs104
1 files changed, 104 insertions, 0 deletions
diff --git a/examples/progress_indicators/src/main.rs b/examples/progress_indicators/src/main.rs
new file mode 100644
index 00000000..136b8d8c
--- /dev/null
+++ b/examples/progress_indicators/src/main.rs
@@ -0,0 +1,104 @@
+use iced::executor;
+use iced::widget::{column, container, row, slider, text};
+use iced::{Application, Command, Element, Length, Settings, Theme};
+
+use std::time::Duration;
+
+mod circular;
+mod easing;
+mod linear;
+
+use circular::Circular;
+use linear::Linear;
+
+pub fn main() -> iced::Result {
+ ProgressIndicators::run(Settings {
+ antialiasing: true,
+ ..Default::default()
+ })
+}
+
+struct ProgressIndicators {
+ cycle_duration: f32,
+}
+
+impl Default for ProgressIndicators {
+ fn default() -> Self {
+ Self {
+ cycle_duration: 2.0,
+ }
+ }
+}
+
+#[derive(Debug, Clone, Copy)]
+enum Message {
+ CycleDurationChanged(f32),
+}
+
+impl Application for ProgressIndicators {
+ type Message = Message;
+ type Flags = ();
+ type Executor = executor::Default;
+ type Theme = Theme;
+
+ fn new(_flags: Self::Flags) -> (Self, Command<Message>) {
+ (Self::default(), Command::none())
+ }
+
+ fn title(&self) -> String {
+ String::from("Progress Indicators - Iced")
+ }
+
+ fn update(&mut self, message: Message) -> Command<Message> {
+ match message {
+ Message::CycleDurationChanged(duration) => {
+ self.cycle_duration = duration;
+ }
+ }
+
+ Command::none()
+ }
+
+ fn view(&self) -> Element<Message> {
+ let column = easing::EXAMPLES
+ .iter()
+ .zip(["Decelerating:", "Accelerating:", "Standard:"])
+ .fold(column![], |column, (easing, label)| {
+ column.push(
+ row![
+ text(label).width(150),
+ Linear::new().easing(easing).cycle_duration(
+ Duration::from_secs_f32(self.cycle_duration)
+ ),
+ Circular::new().easing(easing).cycle_duration(
+ Duration::from_secs_f32(self.cycle_duration)
+ )
+ ]
+ .align_items(iced::Alignment::Center)
+ .spacing(20.0),
+ )
+ })
+ .spacing(20);
+
+ container(
+ column.push(
+ row(vec![
+ text("Cycle duration:").into(),
+ slider(1.0..=1000.0, self.cycle_duration * 100.0, |x| {
+ Message::CycleDurationChanged(x / 100.0)
+ })
+ .width(150.0)
+ .into(),
+ text(format!("{:.2}s", self.cycle_duration)).into(),
+ ])
+ .align_items(iced::Alignment::Center)
+ .spacing(20.0),
+ ),
+ )
+ .width(Length::Fill)
+ .height(Length::Fill)
+ .center_x()
+ .center_y()
+ .into()
+ }
+}