waft-widgets 0.15.0

Widget framework and widgets for Waft
Documentation
//! Flutter `widgets/title.dart`.

use waft_embedder::{ApplicationSwitcherDescription, Color};
use waft_foundation::{App, DiagnosticPropertiesBuilder, Handle, StringProperty};
use waft_painting::ColorProperty;
use waft_services::SystemChrome;

use crate::framework::{
    BuildContext, IntoWidget, KeyRef, State, StateData, StatefulWidget, WidgetRef,
};

/// A widget that describes this app in the operating system.
#[derive(Debug)]
pub struct Title {
    key: Option<KeyRef>,
    /// A one-line description of this app for use in the window manager.
    /// Must not be null.
    title: String,
    /// A color that the window manager should use to identify this app. Must be
    /// an opaque color (i.e. color.alpha must be 255 (0xFF)), and must not be
    /// null.
    color: Color,
    /// The widget below this widget in the tree.
    child: WidgetRef,
}

impl Title {
    /// Creates a widget that describes this app to the Android operating system.
    ///
    /// `color` must be an opaque color (i.e. color.alpha must be 255 (0xFF)).
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn new<K>(color: Color, child: impl IntoWidget<K>) -> Title {
        debug_assert!(alpha_byte(color) == 0xFF);
        Title {
            key: None,
            title: String::new(),
            color,
            child: child.into_widget(),
        }
    }

    /// Dart `Title(key:)`.
    pub fn key(mut self, key: KeyRef) -> Title {
        self.key = Some(key);
        self
    }

    /// Dart `Title(title:)`.
    pub fn title(mut self, title: impl Into<String>) -> Title {
        self.title = title.into();
        self
    }
}

fn alpha_byte(color: Color) -> i32 {
    ((color.a * 255.0).round() as i32).clamp(0, 255)
}

impl StatefulWidget for Title {
    type State = TitleState;

    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn create_state(&self) -> TitleState {
        TitleState {
            state: StateData::new(),
        }
    }
}

/// Dart's `_TitleState`.
pub struct TitleState {
    state: StateData<Title>,
}

impl TitleState {
    fn update_chrome(self: Handle<Self>, app: &mut App) {
        let widget = self.widget(app);
        let description = ApplicationSwitcherDescription {
            label: Some(widget.title.clone()),
            primary_color: Some(widget.color.to_argb32()),
        };
        SystemChrome::set_application_switcher_description(app, &description);
    }
}

impl State for TitleState {
    fn debug_fill_properties(
        self: Handle<Self>,
        app: &App,
        properties: &mut DiagnosticPropertiesBuilder,
    ) {
        let widget = self.widget(app);
        properties.add(
            StringProperty::new("title", Some(widget.title.clone()))
                .default_value(Some(String::new())),
        );
        properties.add(ColorProperty::new("color", widget.color).default_value(None));
    }

    type Widget = Title;
    crate::state_accessors!();

    fn init_state(self: Handle<Self>, app: &mut App) {
        self.update_chrome(app);
    }

    fn did_update_widget(self: Handle<Self>, app: &mut App, old_widget: &Title) {
        let widget = self.widget(app);
        if old_widget.title != widget.title || old_widget.color != widget.color {
            self.update_chrome(app);
        }
    }

    fn build(self: Handle<Self>, app: &mut App, _context: BuildContext) -> WidgetRef {
        self.widget(app).child.clone()
    }
}

#[cfg(test)]
mod tests {
    use std::cell::RefCell;
    use std::rc::Rc;
    use waft_foundation::AppCell;

    use waft_embedder::SystemChrome;
    use waft_test::TestPlatform;

    use super::*;
    use crate::test_harness::Harness;
    use crate::widgets::basic::SizedBox;

    /// A host that records the descriptions the app gives of itself.
    #[derive(Default)]
    struct RecordingChrome {
        descriptions: RefCell<Vec<ApplicationSwitcherDescription>>,
    }

    impl SystemChrome for RecordingChrome {
        fn set_application_switcher_description(
            &self,
            description: &ApplicationSwitcherDescription,
        ) {
            self.descriptions.borrow_mut().push(description.clone());
        }
    }

    #[test]
    fn a_title_describes_the_app_to_the_platform_and_again_when_it_changes() {
        let chrome = Rc::new(RecordingChrome::default());
        let platform = TestPlatform::new().with_system_chrome(chrome.clone());
        let cell = AppCell::with_platform(Rc::new(platform));
        let mut app = cell.borrow_mut();
        let titled = |title: &str| {
            Title::new(Color::new(0xFF112233), SizedBox::shrink())
                .title(title)
                .into_widget()
        };
        let harness = Harness::mount(&mut app, titled("One"));
        harness.pump(&mut app);
        harness.set_child(&mut app, titled("One"));
        harness.pump(&mut app);
        harness.set_child(&mut app, titled("Two"));
        harness.pump(&mut app);
        let descriptions = chrome.descriptions.borrow();
        assert_eq!(
            descriptions.as_slice(),
            [
                ApplicationSwitcherDescription {
                    label: Some(String::from("One")),
                    primary_color: Some(0xFF112233),
                },
                ApplicationSwitcherDescription {
                    label: Some(String::from("Two")),
                    primary_color: Some(0xFF112233),
                },
            ],
            "an unchanged title does not report again"
        );
    }
}