Show name of current Theme in clock example
This commit is contained in:
parent
348e00e11c
commit
bad3b1ac47
5 changed files with 67 additions and 14 deletions
|
|
@ -7,6 +7,18 @@ use std::ops::{Add, AddAssign, Div, Mul, RangeInclusive, Sub, SubAssign};
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
|
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
|
||||||
pub struct Degrees(pub f32);
|
pub struct Degrees(pub f32);
|
||||||
|
|
||||||
|
impl PartialEq<f32> for Degrees {
|
||||||
|
fn eq(&self, other: &f32) -> bool {
|
||||||
|
self.0.eq(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd<f32> for Degrees {
|
||||||
|
fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
|
||||||
|
self.0.partial_cmp(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// Radians
|
/// Radians
|
||||||
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
|
#[derive(Debug, Copy, Clone, PartialEq, PartialOrd)]
|
||||||
pub struct Radians(pub f32);
|
pub struct Radians(pub f32);
|
||||||
|
|
@ -140,3 +152,15 @@ impl Div for Radians {
|
||||||
Self(self.0 / rhs.0)
|
Self(self.0 / rhs.0)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl PartialEq<f32> for Radians {
|
||||||
|
fn eq(&self, other: &f32) -> bool {
|
||||||
|
self.0.eq(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PartialOrd<f32> for Radians {
|
||||||
|
fn partial_cmp(&self, other: &f32) -> Option<std::cmp::Ordering> {
|
||||||
|
self.0.partial_cmp(other)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,8 +1,10 @@
|
||||||
|
use iced::alignment;
|
||||||
use iced::mouse;
|
use iced::mouse;
|
||||||
use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke};
|
use iced::widget::canvas::{stroke, Cache, Geometry, LineCap, Path, Stroke};
|
||||||
use iced::widget::{canvas, container};
|
use iced::widget::{canvas, container};
|
||||||
use iced::{
|
use iced::{
|
||||||
Element, Length, Point, Rectangle, Renderer, Subscription, Theme, Vector,
|
Degrees, Element, Font, Length, Point, Rectangle, Renderer, Subscription,
|
||||||
|
Theme, Vector,
|
||||||
};
|
};
|
||||||
|
|
||||||
pub fn main() -> iced::Result {
|
pub fn main() -> iced::Result {
|
||||||
|
|
@ -133,8 +135,31 @@ impl<Message> canvas::Program<Message> for Clock {
|
||||||
});
|
});
|
||||||
|
|
||||||
frame.with_save(|frame| {
|
frame.with_save(|frame| {
|
||||||
frame.rotate(hand_rotation(self.now.second(), 60));
|
let rotation = hand_rotation(self.now.second(), 60);
|
||||||
|
|
||||||
|
frame.rotate(rotation);
|
||||||
frame.stroke(&long_hand, thin_stroke());
|
frame.stroke(&long_hand, thin_stroke());
|
||||||
|
|
||||||
|
let rotate_factor = if rotation < 180.0 { 1.0 } else { -1.0 };
|
||||||
|
|
||||||
|
frame.rotate(Degrees(-90.0 * rotate_factor));
|
||||||
|
frame.fill_text(canvas::Text {
|
||||||
|
content: theme.to_string(),
|
||||||
|
size: 15.into(),
|
||||||
|
position: Point::new(
|
||||||
|
(0.8 * radius - 8.0) * rotate_factor,
|
||||||
|
-8.0,
|
||||||
|
),
|
||||||
|
color: palette.primary.weak.text,
|
||||||
|
horizontal_alignment: if rotate_factor > 0.0 {
|
||||||
|
alignment::Horizontal::Right
|
||||||
|
} else {
|
||||||
|
alignment::Horizontal::Left
|
||||||
|
},
|
||||||
|
vertical_alignment: alignment::Vertical::Bottom,
|
||||||
|
font: Font::MONOSPACE,
|
||||||
|
..canvas::Text::default()
|
||||||
|
});
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
@ -142,8 +167,8 @@ impl<Message> canvas::Program<Message> for Clock {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn hand_rotation(n: u8, total: u8) -> f32 {
|
fn hand_rotation(n: u8, total: u8) -> Degrees {
|
||||||
let turns = n as f32 / total as f32;
|
let turns = n as f32 / total as f32;
|
||||||
|
|
||||||
2.0 * std::f32::consts::PI * turns
|
Degrees(360.0 * turns)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -2,7 +2,7 @@ mod cache;
|
||||||
|
|
||||||
pub use cache::Cache;
|
pub use cache::Cache;
|
||||||
|
|
||||||
use crate::core::{Point, Rectangle, Size, Transformation, Vector};
|
use crate::core::{Point, Radians, Rectangle, Size, Transformation, Vector};
|
||||||
use crate::graphics::geometry::{Fill, Path, Stroke, Text};
|
use crate::graphics::geometry::{Fill, Path, Stroke, Text};
|
||||||
use crate::Renderer;
|
use crate::Renderer;
|
||||||
|
|
||||||
|
|
@ -184,7 +184,7 @@ impl Frame {
|
||||||
|
|
||||||
/// Applies a rotation in radians to the current transform of the [`Frame`].
|
/// Applies a rotation in radians to the current transform of the [`Frame`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn rotate(&mut self, angle: f32) {
|
pub fn rotate(&mut self, angle: impl Into<Radians>) {
|
||||||
delegate!(self, frame, frame.rotate(angle));
|
delegate!(self, frame, frame.rotate(angle));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,7 @@
|
||||||
use crate::core::text::LineHeight;
|
use crate::core::text::LineHeight;
|
||||||
use crate::core::{Pixels, Point, Rectangle, Size, Transformation, Vector};
|
use crate::core::{
|
||||||
|
Pixels, Point, Radians, Rectangle, Size, Transformation, Vector,
|
||||||
|
};
|
||||||
use crate::graphics::geometry::fill::{self, Fill};
|
use crate::graphics::geometry::fill::{self, Fill};
|
||||||
use crate::graphics::geometry::stroke::{self, Stroke};
|
use crate::graphics::geometry::stroke::{self, Stroke};
|
||||||
use crate::graphics::geometry::{Path, Style, Text};
|
use crate::graphics::geometry::{Path, Style, Text};
|
||||||
|
|
@ -192,10 +194,10 @@ impl Frame {
|
||||||
self.transform.pre_translate(translation.x, translation.y);
|
self.transform.pre_translate(translation.x, translation.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn rotate(&mut self, angle: f32) {
|
pub fn rotate(&mut self, angle: impl Into<Radians>) {
|
||||||
self.transform = self
|
self.transform = self.transform.pre_concat(
|
||||||
.transform
|
tiny_skia::Transform::from_rotate(angle.into().0.to_degrees()),
|
||||||
.pre_concat(tiny_skia::Transform::from_rotate(angle.to_degrees()));
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn scale(&mut self, scale: impl Into<f32>) {
|
pub fn scale(&mut self, scale: impl Into<f32>) {
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,8 @@
|
||||||
//! Build and draw geometry.
|
//! Build and draw geometry.
|
||||||
use crate::core::text::LineHeight;
|
use crate::core::text::LineHeight;
|
||||||
use crate::core::{Pixels, Point, Rectangle, Size, Transformation, Vector};
|
use crate::core::{
|
||||||
|
Pixels, Point, Radians, Rectangle, Size, Transformation, Vector,
|
||||||
|
};
|
||||||
use crate::graphics::color;
|
use crate::graphics::color;
|
||||||
use crate::graphics::geometry::fill::{self, Fill};
|
use crate::graphics::geometry::fill::{self, Fill};
|
||||||
use crate::graphics::geometry::{
|
use crate::graphics::geometry::{
|
||||||
|
|
@ -475,12 +477,12 @@ impl Frame {
|
||||||
|
|
||||||
/// Applies a rotation in radians to the current transform of the [`Frame`].
|
/// Applies a rotation in radians to the current transform of the [`Frame`].
|
||||||
#[inline]
|
#[inline]
|
||||||
pub fn rotate(&mut self, angle: f32) {
|
pub fn rotate(&mut self, angle: impl Into<Radians>) {
|
||||||
self.transforms.current.0 = self
|
self.transforms.current.0 = self
|
||||||
.transforms
|
.transforms
|
||||||
.current
|
.current
|
||||||
.0
|
.0
|
||||||
.pre_rotate(lyon::math::Angle::radians(angle));
|
.pre_rotate(lyon::math::Angle::radians(angle.into().0));
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Applies a uniform scaling to the current transform of the [`Frame`].
|
/// Applies a uniform scaling to the current transform of the [`Frame`].
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue