waft-winui 0.18.2

WinUI 3 controls and Fluent styling for the Waft UI framework
Documentation
//! XAML `Page` (`dxaml/xcp/dxaml/lib/Page_Partial.cpp`): the root of an app's content, whose background covers all of it while its content stays clear of what the system draws over the window.
//!
//! WinUI's `CalculateUpdatedBounds` arranges `Content` inside the window's layout bounds, the window less the system's bars, when the page is the window's size. Here the bars are the ambient [`MediaQuery`]'s `padding`, and the keyboard its `view_insets.bottom`, as Flutter's `Scaffold` takes it with `resizeToAvoidBottomInset`. The content's `MediaQuery` has both removed, so a page or `SafeArea` inside this one does not keep the content clear of them a second time: a later editor who forwards the ambient data unchanged pads nested pages twice.

use crate::{Brush, ControlBorder};
use waft_foundation::App;
use waft_painting::{EdgeInsets, EdgeInsetsGeometry};
use waft_widgets::*;

/// XAML `Page`: content on a background, clear of the system's bars and keyboard.
///
/// `FluentPage`, not `Page`: Flutter's widgets library already has a `Page` (a route's
/// settings), and a kit's `*` globs beside `waft::*`.
#[derive(Clone, Debug, Default)]
pub struct FluentPage {
    /// `Content`: what the page shows, inside the area the system's bars and the keyboard leave free.
    pub content: Option<WidgetRef>,

    /// `Background`: painted over the page's whole area, under the system's bars too. `None` paints nothing, as XAML's null brush.
    pub background: Option<Brush>,

    /// Whether the content reaches under the system's bars, which the page then leaves in the content's `MediaQuery` for the content to keep its controls clear of, as a `NavigationView` does while `is_title_bar_auto_padding_enabled`: WinUI's `Window.ExtendsContentIntoTitleBar`, widened from the title bar to the system's bars, and off by default as that is. The keyboard is kept clear of either way.
    pub extends_content_into_system_bars: bool,

    /// Identity retained across parent rebuilds.
    pub key: Option<KeyRef>,
}

impl FluentPage {
    /// Creates a page with no content and no background.
    pub fn new() -> Self {
        Self::default()
    }

    /// Sets the XAML `Content`.
    #[cfg_attr(debug_assertions, track_caller)]
    pub fn content<K>(mut self, content: impl IntoWidget<K>) -> Self {
        self.content = Some(content.into_widget());
        self
    }

    /// Sets the XAML `Background`.
    pub fn background(mut self, background: Brush) -> Self {
        self.background = Some(background);
        self
    }

    /// Sets `extends_content_into_system_bars`.
    pub fn extends_content_into_system_bars(mut self, value: bool) -> Self {
        self.extends_content_into_system_bars = value;
        self
    }

    /// Sets the identity retained across rebuilds.
    pub fn key(mut self, key: KeyRef) -> Self {
        self.key = Some(key);
        self
    }
}

impl StatelessWidget for FluentPage {
    fn key(&self) -> Option<&KeyRef> {
        self.key.as_ref()
    }

    fn build(&self, app: &mut App, context: BuildContext) -> WidgetRef {
        let page = ControlBorder::new_optional(self.background, None).border_thickness(0.0);
        match &self.content {
            Some(content) => page.child(updated_bounds(
                app,
                context,
                content.clone(),
                self.extends_content_into_system_bars,
            )),
            None => page,
        }
        .into_widget()
    }
}

/// `Page::CalculateUpdatedBounds`: the content inset by the keyboard and, unless it `extends` into them, by the system's bars, under a [`MediaQuery`] that no longer reports what the page kept it clear of.
fn updated_bounds(
    app: &mut App,
    context: BuildContext,
    content: WidgetRef,
    extends: bool,
) -> WidgetRef {
    let media = MediaQuery::of(app, context);
    let bars = if extends {
        EdgeInsets::ZERO
    } else {
        media.padding
    };
    let keyboard = media.view_insets.bottom;
    let consumed = if extends {
        media.clone()
    } else {
        media.remove_padding(true, true, true, true)
    }
    .remove_view_insets(false, false, false, true);
    MediaQuery::new(
        consumed,
        Padding::new(EdgeInsetsGeometry::from_ltrb(
            bars.left,
            bars.top,
            bars.right,
            bars.bottom + keyboard,
        ))
        .child(content),
    )
    .into_widget()
}