Skip to content

Commit

Permalink
add spinner
Browse files Browse the repository at this point in the history
  • Loading branch information
futurepaul committed May 15, 2024
1 parent e1ae790 commit a0c623e
Show file tree
Hide file tree
Showing 11 changed files with 572 additions and 29 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 4 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ vendored = ["rusqlite/bundled-sqlcipher-vendored-openssl"]
anyhow = "1"
log = "0.4"
pretty_env_logger = "0.5" # todo swap to a file logger
iced = { git = "https://github.com/iced-rs/iced", rev = "b30d34f", features = ["debug", "tokio", "svg", "qr_code"] }
iced = { git = "https://github.com/iced-rs/iced", rev = "b30d34f", features = ["debug", "tokio", "svg", "qr_code", "advanced"] }
lyon_algorithms = "1.0"
once_cell = "1.0"
tokio = { version = "1", features = ["full"] }
palette = "0.7"
config = "0.14.0"
Expand All @@ -33,3 +35,4 @@ fedimint-mint-client = "0.3.1"
fedimint-ln-client = "0.3.1"
fedimint-bip39 = "0.3.1"
fedimint-ln-common = "0.3.1"

28 changes: 14 additions & 14 deletions src/components/button.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,26 +3,26 @@ use iced::{
button::{self, Status},
center, horizontal_space, row, text, Button, Svg,
},
Border, Color, Length, Shadow, Theme,
Border, Color, Element, Length, Shadow, Theme,
};

use crate::{Message, Route};

use super::{darken, lighten, map_icon, SvgIcon};
use super::{darken, lighten, map_icon, the_spinner, SvgIcon};

pub fn h_button(text_str: &str, icon: SvgIcon) -> Button<'_, Message, Theme> {
pub fn h_button(text_str: &str, icon: SvgIcon, loading: bool) -> Button<'_, Message, Theme> {
let spinner: Element<'static, Message, Theme> = the_spinner();
let svg: Svg<'_, Theme> = map_icon(icon);
let content = row!(
svg.width(Length::Fixed(24.)).height(Length::Fixed(24.)),
text(text_str).size(24.) // .font(Font {
// family: iced::font::Family::default(),
// weight: iced::font::Weight::Bold,
// stretch: iced::font::Stretch::Normal,
// style: iced::font::Style::Normal,
// })
)
.align_items(iced::Alignment::Center)
.spacing(16);
let content = if loading {
row![spinner].align_items(iced::Alignment::Center)
} else {
row![
svg.width(Length::Fixed(24.)).height(Length::Fixed(24.)),
text(text_str).size(24.)
]
.align_items(iced::Alignment::Center)
.spacing(16)
};

Button::new(center(content))
.style(|theme, status| {
Expand Down
122 changes: 122 additions & 0 deletions src/components/easing.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
use iced::Point;

use lyon_algorithms::measure::PathMeasurements;
use lyon_algorithms::path::{builder::NoAttributes, path::BuilderImpl, Path};
use once_cell::sync::Lazy;

pub static EMPHASIZED: Lazy<Easing> = Lazy::new(|| {
Easing::builder()
.cubic_bezier_to([0.05, 0.0], [0.133333, 0.06], [0.166666, 0.4])
.cubic_bezier_to([0.208333, 0.82], [0.25, 1.0], [1.0, 1.0])
.build()
});

pub static EMPHASIZED_DECELERATE: Lazy<Easing> = Lazy::new(|| {
Easing::builder()
.cubic_bezier_to([0.05, 0.7], [0.1, 1.0], [1.0, 1.0])
.build()
});

pub static EMPHASIZED_ACCELERATE: Lazy<Easing> = Lazy::new(|| {
Easing::builder()
.cubic_bezier_to([0.3, 0.0], [0.8, 0.15], [1.0, 1.0])
.build()
});

pub static STANDARD: Lazy<Easing> = Lazy::new(|| {
Easing::builder()
.cubic_bezier_to([0.2, 0.0], [0.0, 1.0], [1.0, 1.0])
.build()
});

pub static STANDARD_DECELERATE: Lazy<Easing> = Lazy::new(|| {
Easing::builder()
.cubic_bezier_to([0.0, 0.0], [0.0, 1.0], [1.0, 1.0])
.build()
});

pub static STANDARD_ACCELERATE: Lazy<Easing> = Lazy::new(|| {
Easing::builder()
.cubic_bezier_to([0.3, 0.0], [1.0, 1.0], [1.0, 1.0])
.build()
});

pub struct Easing {
path: Path,
measurements: PathMeasurements,
}

impl Easing {
pub fn builder() -> Builder {
Builder::new()
}

pub fn y_at_x(&self, x: f32) -> f32 {
let mut sampler = self
.measurements
.create_sampler(&self.path, lyon_algorithms::measure::SampleType::Normalized);
let sample = sampler.sample(x);

sample.position().y
}
}

pub struct Builder(NoAttributes<BuilderImpl>);

impl Builder {
pub fn new() -> Self {
let mut builder = Path::builder();
builder.begin(lyon_algorithms::geom::point(0.0, 0.0));

Self(builder)
}

/// Adds a line segment. Points must be between 0,0 and 1,1
pub fn line_to(mut self, to: impl Into<Point>) -> Self {
self.0.line_to(Self::point(to));

self
}

/// Adds a quadratic bézier curve. Points must be between 0,0 and 1,1
pub fn quadratic_bezier_to(mut self, ctrl: impl Into<Point>, to: impl Into<Point>) -> Self {
self.0
.quadratic_bezier_to(Self::point(ctrl), Self::point(to));

self
}

/// Adds a cubic bézier curve. Points must be between 0,0 and 1,1
pub fn cubic_bezier_to(
mut self,
ctrl1: impl Into<Point>,
ctrl2: impl Into<Point>,
to: impl Into<Point>,
) -> Self {
self.0
.cubic_bezier_to(Self::point(ctrl1), Self::point(ctrl2), Self::point(to));

self
}

pub fn build(mut self) -> Easing {
self.0.line_to(lyon_algorithms::geom::point(1.0, 1.0));
self.0.end(false);

let path = self.0.build();
let measurements = PathMeasurements::from_path(&path, 0.0);

Easing { path, measurements }
}

fn point(p: impl Into<Point>) -> lyon_algorithms::geom::Point<f32> {
let p: Point = p.into();
lyon_algorithms::geom::point(p.x.min(1.0).max(0.0), p.y.min(1.0).max(0.0))
}
}

impl Default for Builder {
fn default() -> Self {
Self::new()
}
}
6 changes: 6 additions & 0 deletions src/components/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,9 @@ pub use input::*;

mod header;
pub use header::*;

mod easing;
pub use easing::*;

mod spinner;
pub use spinner::*;
Loading

0 comments on commit a0c623e

Please sign in to comment.