From 1264f0f1301e709799426d3d95755fd1babe4a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?John=20K=C3=A5re=20Alsaker?= Date: Sat, 4 Nov 2023 21:23:37 +0100 Subject: [PATCH] Add style property transitions --- examples/themes/src/main.rs | 25 +++- src/context.rs | 33 ++++- src/inspector.rs | 109 +++++++++------ src/style.rs | 255 ++++++++++++++++++++++++++++++------ src/view.rs | 43 ++++-- src/views/scroll.rs | 9 +- src/window_handle.rs | 12 +- 7 files changed, 376 insertions(+), 110 deletions(-) diff --git a/examples/themes/src/main.rs b/examples/themes/src/main.rs index 31f78872..8b67a4db 100644 --- a/examples/themes/src/main.rs +++ b/examples/themes/src/main.rs @@ -3,7 +3,7 @@ use floem::{ keyboard::{Key, NamedKey}, peniko::Color, reactive::create_signal, - style::Style, + style::{Background, BorderColor, Style, TextColor, Transition}, style_class, view::View, views::{label, stack, text, Decorators}, @@ -20,6 +20,9 @@ fn app_view() -> impl View { .border(1.0) .border_color(Color::rgb8(109, 121, 135)) .hover(|s| s.background(Color::rgb8(170, 175, 187))) + .transition(TextColor, Transition::linear(0.06)) + .transition(BorderColor, Transition::linear(0.06)) + .transition(Background, Transition::linear(0.06)) .disabled(|s| { s.background(Color::DARK_GRAY.with_alpha_factor(0.1)) .border_color(Color::BLACK.with_alpha_factor(0.2)) @@ -30,9 +33,13 @@ fn app_view() -> impl View { .border_radius(5.0); let blue_theme = Style::new() .background(Color::rgb8(95, 102, 118)) + .transition(Background, Transition::linear(0.1)) + .transition(TextColor, Transition::linear(0.1)) .color(Color::WHITE) .class(Button, move |_| blue_button) - .class(Label, |s| s.margin(4.0)) + .class(Label, |s| { + s.margin(4.0).transition(TextColor, Transition::linear(0.1)) + }) .font_size(12.0); let green_button = Style::new() @@ -40,10 +47,14 @@ fn app_view() -> impl View { .disabled(|s| { s.background(Color::rgb8(180, 188, 175).with_alpha_factor(0.3)) .border_color(Color::rgb8(131, 145, 123).with_alpha_factor(0.3)) + .color(Color::GRAY) }) .active(|s| s.background(Color::rgb8(95, 105, 88)).color(Color::WHITE)) .color(Color::BLACK.with_alpha_factor(0.7)) .border(2.0) + .transition(TextColor, Transition::linear(0.3)) + .transition(BorderColor, Transition::linear(0.3)) + .transition(Background, Transition::linear(0.3)) .border_color(Color::rgb8(131, 145, 123)) .hover(|s| s.background(Color::rgb8(204, 209, 201))) .padding(8.0) @@ -51,8 +62,11 @@ fn app_view() -> impl View { .margin(6.0); let green_theme = Style::new() .background(Color::rgb8(227, 231, 226)) + .transition(Background, Transition::linear(0.5)) .class(Button, move |_| green_button) - .class(Label, |s| s.margin(4.0)) + .class(Label, |s| { + s.margin(4.0).transition(TextColor, Transition::linear(0.5)) + }) .class(Frame, |s| { s.border(2.0) .border_color(Color::rgb8(131, 145, 123).with_alpha_factor(0.2)) @@ -64,7 +78,7 @@ fn app_view() -> impl View { .font_size(16.0); let (counter, set_counter) = create_signal(0); - let (theme, set_theme) = create_signal(true); + let (theme, set_theme) = create_signal(false); let view = stack((stack(( text("Toggle Theme") .class(Button) @@ -120,7 +134,8 @@ fn app_view() -> impl View { .flex_col() .items_center() .justify_center() - }); + }) + .window_title(|| "Themes Example".to_string()); let id = view.id(); view.on_event(EventListener::KeyUp, move |e| { diff --git a/src/context.rs b/src/context.rs index 929a5d59..3d8f5ab3 100644 --- a/src/context.rs +++ b/src/context.rs @@ -2,7 +2,7 @@ use std::{ collections::{HashMap, HashSet}, ops::{Deref, DerefMut}, rc::Rc, - time::Duration, + time::{Duration, Instant}, }; use floem_renderer::{ @@ -24,8 +24,10 @@ use crate::{ inspector::CaptureState, menu::Menu, pointer::PointerInputEvent, + prop_extracter, responsive::{GridBreakpoints, ScreenSizeBp}, style::{ + Background, BorderBottom, BorderColor, BorderLeft, BorderRadius, BorderRight, BorderTop, BuiltinStyle, CursorStyle, DisplayProp, LayoutProps, Style, StyleClassRef, StyleProp, StyleSelector, StyleSelectors, }, @@ -47,6 +49,19 @@ pub(crate) struct MoveListener { pub(crate) callback: Box, } +prop_extracter! { + pub(crate) ViewStyleProps { + pub border_left: BorderLeft, + pub border_top: BorderTop, + pub border_right: BorderRight, + pub border_bottom: BorderBottom, + pub border_radius: BorderRadius, + + pub border_color: BorderColor, + pub background: Background, + } +} + pub struct ViewState { pub(crate) node: Node, pub(crate) children_nodes: Vec, @@ -58,6 +73,7 @@ pub struct ViewState { pub(crate) viewport: Option, pub(crate) layout_rect: Rect, pub(crate) layout_props: LayoutProps, + pub(crate) view_style_props: ViewStyleProps, pub(crate) animation: Option, pub(crate) base_style: Option