Lines
91.26 %
Functions
32.7 %
Branches
100 %
//! Microsoft Office-style backstage view widget (the full-window "FILE"
//! screen, the Office-2013-era look look by default).
//!
//! Models the component hierarchy of the Office backstage:
//! ```text
//! Backstage ─ nav column (dark accent, full height)
//! │ ├─ back button (white ring + left arrow)
//! │ └─ nav items ("Info", "New", "Open", …, "Account", "Options")
//! └─ right side
//! ├─ title strip (optional, app-provided: window title/buttons)
//! └─ content pane (app-provided Dom for the active item)
//! ```
//! The widget owns the CHROME: nav column, back button, item highlight and
//! the content host. The per-item pane content ("Open" recent list, "Info"
//! properties, …) is application composition, injected through
//! [`Backstage::content`] - the backstage does not model document state.
//! The back button expands to the existing [`super::button::Button`] widget
//! with backstage part styles injected (the ribbon's composition rule), and
//! its arrow uses `Dom::create_icon("arrow_back")` so glyphs resolve through
//! the registered icon provider (Material Icons by default).
//! All visual parts are exposed on [`BackstageStyle`] (defaults = the Office-2013-era look
//! look, [`BackstageStyle::office_2013`]); replace any field to re-theme
//! without touching widget code. [`BackstageBehavior`] holds the
//! interactions the backstage performs by itself (currently: Escape invokes
//! the back callback, like classic office suites).
use azul_core::{
callbacks::{CoreCallback, CoreCallbackData, Update},
dom::{
Dom, DomVec, EventFilter, HoverEventFilter, IdOrClass, IdOrClass::Class, IdOrClassVec,
WindowEventFilter,
},
refany::RefAny,
window::VirtualKeyCode,
};
#[allow(clippy::wildcard_imports)]
// widget/render module pulls in the css property/value types it builds with
use azul_css::{
dynamic_selector::{
CssPropertyWithConditions as Cond, CssPropertyWithConditionsVec,
OptionCssPropertyWithConditionsVec,
props::{
basic::{
color::ColorU,
font::{StyleFontFamily, StyleFontFamilyVec},
*,
layout::*,
property::CssProperty as P,
style::*,
impl_option, impl_vec, impl_vec_clone, impl_vec_debug, impl_vec_mut, system::SystemStyle,
use super::{
button::{Button, ButtonOnClick, OptionButtonOnClick},
themes::flat,
use crate::callbacks::CallbackInfo;
// -- Callbacks --
/// Callback signature invoked when a nav item is clicked (receives the item
/// index).
pub type BackstageOnNavSelectCallbackType = extern "C" fn(RefAny, CallbackInfo, usize) -> Update;
impl_widget_callback!(
BackstageOnNavSelect,
OptionBackstageOnNavSelect,
BackstageOnNavSelectCallback,
BackstageOnNavSelectCallbackType
);
azul_core::impl_managed_callback! {
wrapper: BackstageOnNavSelectCallback,
info_ty: CallbackInfo,
return_ty: Update,
default_ret: Update::DoNothing,
invoker_static: BACKSTAGE_ON_NAV_SELECT_INVOKER,
invoker_ty: AzBackstageOnNavSelectCallbackInvoker,
thunk_fn: az_backstage_on_nav_select_callback_thunk,
setter_fn: AzApp_setBackstageOnNavSelectCallbackInvoker,
from_handle_fn: AzBackstageOnNavSelectCallback_createFromHostHandle,
extra_args: [ item_index: usize ],
}
// -- Font --
const SYSTEM_UI_STR: AzString = AzString::from_const_str("system:ui");
const SYSTEM_UI_FAMILIES: &[StyleFontFamily] = &[StyleFontFamily::System(SYSTEM_UI_STR)];
const SYSTEM_UI_FAMILY: StyleFontFamilyVec =
StyleFontFamilyVec::from_const_slice(SYSTEM_UI_FAMILIES);
// -- the Office-2013-era look palette (seeds BackstageTheme::office_2013) --
const WHITE: ColorU = ColorU {
r: 255,
g: 255,
b: 255,
a: 255,
const TRANSPARENT: ColorU = ColorU {
r: 0,
g: 0,
b: 0,
a: 0,
/// Office 2013 accent blue (#2B579A): the nav column fill.
const W13_BLUE: ColorU = ColorU {
r: 43,
g: 87,
b: 154,
/// Hover fill on nav items (#3465AC).
const W13_NAV_HOVER: ColorU = ColorU {
r: 52,
g: 101,
b: 172,
/// Active nav item fill (#3E6DB5).
const W13_NAV_ACTIVE: ColorU = ColorU {
r: 62,
g: 109,
b: 181,
// -- Metrics (the Office-2013-era look, logical px) --
/// Nav column width.
const NAV_WIDTH: isize = 126;
/// Height of one nav item.
const NAV_ITEM_H: isize = 38;
/// Nav item text size.
const NAV_TEXT_PX: isize = 13;
/// Extra gap above a `gap_before` item (office-2013: before "Account").
const NAV_GAP_H: isize = 22;
/// Back button ring diameter.
const BACK_D: isize = 38;
// -- Theme --
/// Color palette from which a full [`BackstageStyle`] is derived via
/// [`BackstageStyle::from_theme`]. All fields are plain colors, so themes
/// are trivially constructible over FFI. Preset:
/// [`BackstageTheme::office_2013`] (the default).
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(C)]
pub struct BackstageTheme {
/// Nav column fill (office-2013: accent blue).
pub nav_bg: ColorU,
/// Nav item text and back-arrow color.
pub nav_text: ColorU,
/// Hover fill on nav items.
pub nav_hover_bg: ColorU,
/// Fill of the active nav item.
pub nav_active_bg: ColorU,
/// Content pane fill.
pub content_bg: ColorU,
/// Back button ring color.
pub back_ring: ColorU,
impl BackstageTheme {
/// The the Office-2013-era look palette: #2B579A nav, white text, lighter-blue
/// highlights, white content.
#[must_use]
pub const fn office_2013() -> Self {
Self {
nav_bg: W13_BLUE,
nav_text: WHITE,
nav_hover_bg: W13_NAV_HOVER,
nav_active_bg: W13_NAV_ACTIVE,
content_bg: WHITE,
back_ring: WHITE,
/// Extracts a backstage palette from the OS theme.
///
/// The nav column is an ACCENT-FILLED band and the pane behind it is the
/// window surface, so those two system colours carry the whole screen. A
/// colour the platform does not report falls back to that field's own
/// Office-2013 value, never to another derived value; same discipline as
/// [`super::ribbon::RibbonTheme::from_system`], no colour arithmetic.
/// Takes the style by value (FFI constructor convention).
pub fn from_system(style: SystemStyle) -> Self {
let d = Self::office_2013();
let c = &style.colors;
let on_accent = c.accent_text.into_option();
nav_bg: c.accent.into_option().unwrap_or(d.nav_bg),
nav_text: on_accent.unwrap_or(d.nav_text),
nav_hover_bg: c
.selection_background
.into_option()
.unwrap_or(d.nav_hover_bg),
nav_active_bg: c
.selection_background_inactive
.unwrap_or(d.nav_active_bg),
content_bg: c.window_background.into_option().unwrap_or(d.content_bg),
back_ring: on_accent.unwrap_or(d.back_ring),
impl Default for BackstageTheme {
fn default() -> Self {
Self::office_2013()
// -- Theme -> property-list builders --
fn bg_vec(c: ColorU) -> StyleBackgroundContentVec {
StyleBackgroundContentVec::from_vec(vec![StyleBackgroundContent::Color(c)])
fn cond_bg(c: ColorU) -> Cond {
Cond::simple(P::const_background_content(bg_vec(c)))
const fn cond_text_color(c: ColorU) -> Cond {
Cond::simple(P::const_text_color(StyleTextColor { inner: c }))
const fn cond_border_box() -> Cond {
Cond::simple(P::const_box_sizing(LayoutBoxSizing::BorderBox))
fn push_ring_border(v: &mut Vec<Cond>, c: ColorU, width: isize, radius: isize) {
v.push(Cond::simple(P::const_border_top_width(
LayoutBorderTopWidth::const_px(width),
)));
v.push(Cond::simple(P::const_border_left_width(
LayoutBorderLeftWidth::const_px(width),
v.push(Cond::simple(P::const_border_right_width(
LayoutBorderRightWidth::const_px(width),
v.push(Cond::simple(P::const_border_bottom_width(
LayoutBorderBottomWidth::const_px(width),
v.push(Cond::simple(P::const_border_top_style(
StyleBorderTopStyle {
inner: BorderStyle::Solid,
v.push(Cond::simple(P::const_border_left_style(
StyleBorderLeftStyle {
v.push(Cond::simple(P::const_border_right_style(
StyleBorderRightStyle {
v.push(Cond::simple(P::const_border_bottom_style(
StyleBorderBottomStyle {
v.push(Cond::simple(P::const_border_top_color(
StyleBorderTopColor { inner: c },
v.push(Cond::simple(P::const_border_left_color(
StyleBorderLeftColor { inner: c },
v.push(Cond::simple(P::const_border_right_color(
StyleBorderRightColor { inner: c },
v.push(Cond::simple(P::const_border_bottom_color(
StyleBorderBottomColor { inner: c },
v.push(Cond::simple(P::const_border_top_left_radius(
StyleBorderTopLeftRadius::const_px(radius),
v.push(Cond::simple(P::const_border_top_right_radius(
StyleBorderTopRightRadius::const_px(radius),
v.push(Cond::simple(P::const_border_bottom_left_radius(
StyleBorderBottomLeftRadius::const_px(radius),
v.push(Cond::simple(P::const_border_bottom_right_radius(
StyleBorderBottomRightRadius::const_px(radius),
/// The hover fill of the two controls on the nav column (back button, nav
/// item), light AND dark.
/// Built by the theme module rather than declared here, so the pair cannot be
/// split: the dark half needs a palette this file cannot see, and a rule
/// written here could only ever name the light colour — which is how every
/// interactive state in this toolkit came to paint its light fill onto a dark
/// surface.
/// The dark twin is the SAME colour, on purpose. The nav column is an ACCENT
/// fill in either mode (`theme_nav` paints `nav_bg` with no dark variant;
/// `from_system` reads it from the desktop's accent), and `nav_hover_bg` is a
/// shade of that accent. The theme's neutral `DARK_HT` on a blue band would be
/// worse than today's light-only rule — see `themes::flat::hover_bg_both` for
/// the rule, and `flat::button_states` for the same call on a Primary button.
fn push_nav_hover(v: &mut Vec<Cond>, t: &BackstageTheme) {
v.extend(flat::hover_bg_both(t.nav_hover_bg, t.nav_hover_bg));
fn theme_root(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
CssPropertyWithConditionsVec::from_vec(vec![
cond_border_box(),
Cond::simple(P::const_display(LayoutDisplay::Flex)),
Cond::simple(P::const_flex_direction(LayoutFlexDirection::Row)),
Cond::simple(P::const_flex_grow(LayoutFlexGrow::const_new(1))),
Cond::simple(P::const_font_family(SYSTEM_UI_FAMILY)),
Cond::simple(P::const_font_size(StyleFontSize::const_px(NAV_TEXT_PX))),
cond_bg(t.content_bg),
])
fn theme_nav(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
Cond::simple(P::const_flex_direction(LayoutFlexDirection::Column)),
Cond::simple(P::const_flex_grow(LayoutFlexGrow::const_new(0))),
Cond::simple(P::const_flex_shrink(LayoutFlexShrink {
inner: FloatValue::const_new(0),
})),
Cond::simple(P::const_width(LayoutWidth::const_px(NAV_WIDTH))),
cond_bg(t.nav_bg),
/// The circled back arrow. office-2013: a 2px white ring, transparent fill,
/// hover fills like a nav item.
fn theme_back_button(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
let mut v = vec![
Cond::simple(P::const_align_items(LayoutAlignItems::Center)),
Cond::simple(P::const_justify_content(LayoutJustifyContent::Center)),
Cond::simple(P::const_width(LayoutWidth::const_px(BACK_D))),
Cond::simple(P::const_height(LayoutHeight::const_px(BACK_D))),
Cond::simple(P::const_margin_top(LayoutMarginTop::const_px(16))),
Cond::simple(P::const_margin_left(LayoutMarginLeft::const_px(20))),
Cond::simple(P::const_margin_bottom(LayoutMarginBottom::const_px(18))),
Cond::simple(P::const_cursor(StyleCursor::Pointer)),
Cond::simple(P::user_select(StyleUserSelect::None)),
cond_bg(TRANSPARENT),
];
push_nav_hover(&mut v, t);
push_ring_border(&mut v, t.back_ring, 2, BACK_D / 2);
CssPropertyWithConditionsVec::from_vec(v)
fn theme_back_icon(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
Cond::simple(P::const_font_size(StyleFontSize::const_px(20))),
cond_text_color(t.nav_text),
fn theme_nav_item(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
Cond::simple(P::const_height(LayoutHeight::const_px(NAV_ITEM_H))),
Cond::simple(P::const_padding_left(LayoutPaddingLeft::const_px(24))),
/// APPENDED to the active nav item.
fn theme_nav_item_active(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
CssPropertyWithConditionsVec::from_vec(vec![cond_bg(t.nav_active_bg)])
/// APPENDED to a `gap_before` nav item (office-2013: the gap before "Account").
fn theme_nav_item_gap(_t: &BackstageTheme) -> CssPropertyWithConditionsVec {
CssPropertyWithConditionsVec::from_vec(vec![Cond::simple(P::const_margin_top(
LayoutMarginTop::const_px(NAV_GAP_H),
))])
fn theme_right(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
fn theme_content(t: &BackstageTheme) -> CssPropertyWithConditionsVec {
// -- Style --
/// All part styles of the backstage. Every part defaults to the the Office-2013-era look
/// look; replace any field for finer control (the same override API as
/// [`super::ribbon::RibbonStyle`]).
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BackstageStyle {
/// The palette this style bundle was derived from. Kept for consumers
/// deriving matching custom parts.
pub theme: BackstageTheme,
/// Root container (horizontal: nav column beside the right side).
/// `None` means "no opinion": the part is derived from [`Self::theme`] at
/// render time. `Some` is an override the caller chose, and `Some(empty)` is
/// a real answer — "no properties at all" — which the pre-filled field could
/// not express.
pub root_style: OptionCssPropertyWithConditionsVec,
/// The nav column.
pub nav_style: OptionCssPropertyWithConditionsVec,
/// Container style injected into the back [`Button`] (the ring).
pub back_button_style: OptionCssPropertyWithConditionsVec,
/// Icon style injected into the back [`Button`] (the arrow).
pub back_icon_style: OptionCssPropertyWithConditionsVec,
/// One nav item.
pub nav_item_style: OptionCssPropertyWithConditionsVec,
pub nav_item_active_style: OptionCssPropertyWithConditionsVec,
/// APPENDED to a `gap_before` nav item.
pub nav_item_gap_style: OptionCssPropertyWithConditionsVec,
/// The right side (title strip over content).
pub right_style: OptionCssPropertyWithConditionsVec,
/// The content host for the active pane.
pub content_style: OptionCssPropertyWithConditionsVec,
impl BackstageStyle {
/// The the Office-2013-era look look (#2B579A nav, white content) - the default.
Self::from_theme(BackstageTheme::office_2013())
/// Every part style, derived from the OS theme - see
/// [`BackstageTheme::from_system`]. Takes the style by value (FFI
/// constructor convention).
Self::from_theme(BackstageTheme::from_system(style))
/// Derives every part style from the given palette.
pub const fn from_theme(theme: BackstageTheme) -> Self {
theme,
root_style: OptionCssPropertyWithConditionsVec::None,
nav_style: OptionCssPropertyWithConditionsVec::None,
back_button_style: OptionCssPropertyWithConditionsVec::None,
back_icon_style: OptionCssPropertyWithConditionsVec::None,
nav_item_style: OptionCssPropertyWithConditionsVec::None,
nav_item_active_style: OptionCssPropertyWithConditionsVec::None,
nav_item_gap_style: OptionCssPropertyWithConditionsVec::None,
right_style: OptionCssPropertyWithConditionsVec::None,
content_style: OptionCssPropertyWithConditionsVec::None,
/// The `root_style` this bundle renders with: the caller's override if there is
/// one, else derived from [`Self::theme`].
pub fn resolved_root_style(&self) -> CssPropertyWithConditionsVec {
self.root_style
.clone()
.unwrap_or_else(|| theme_root(&self.theme))
/// The `nav_style` this bundle renders with: the caller's override if there is
pub fn resolved_nav_style(&self) -> CssPropertyWithConditionsVec {
self.nav_style
.unwrap_or_else(|| theme_nav(&self.theme))
/// The `back_button_style` this bundle renders with: the caller's override if there is
pub fn resolved_back_button_style(&self) -> CssPropertyWithConditionsVec {
self.back_button_style
.unwrap_or_else(|| theme_back_button(&self.theme))
/// The `back_icon_style` this bundle renders with: the caller's override if there is
pub fn resolved_back_icon_style(&self) -> CssPropertyWithConditionsVec {
self.back_icon_style
.unwrap_or_else(|| theme_back_icon(&self.theme))
/// The `nav_item_style` this bundle renders with: the caller's override if there is
pub fn resolved_nav_item_style(&self) -> CssPropertyWithConditionsVec {
self.nav_item_style
.unwrap_or_else(|| theme_nav_item(&self.theme))
/// The `nav_item_active_style` this bundle renders with: the caller's override if there is
pub fn resolved_nav_item_active_style(&self) -> CssPropertyWithConditionsVec {
self.nav_item_active_style
.unwrap_or_else(|| theme_nav_item_active(&self.theme))
/// The `nav_item_gap_style` this bundle renders with: the caller's override if there is
pub fn resolved_nav_item_gap_style(&self) -> CssPropertyWithConditionsVec {
self.nav_item_gap_style
.unwrap_or_else(|| theme_nav_item_gap(&self.theme))
/// The `right_style` this bundle renders with: the caller's override if there is
pub fn resolved_right_style(&self) -> CssPropertyWithConditionsVec {
self.right_style
.unwrap_or_else(|| theme_right(&self.theme))
/// The `content_style` this bundle renders with: the caller's override if there is
pub fn resolved_content_style(&self) -> CssPropertyWithConditionsVec {
self.content_style
.unwrap_or_else(|| theme_content(&self.theme))
impl Default for BackstageStyle {
// -- Behavior --
/// The interactions the backstage performs BY ITSELF. Each is the classic
/// default and each can be turned off.
pub struct BackstageBehavior {
/// Pressing Escape invokes the back callback (office-2013: Esc leaves the
/// backstage). Attached as a window-level key handler on the root, so
/// it fires regardless of focus. Requires [`Backstage::on_back`].
pub close_on_escape: bool,
impl BackstageBehavior {
/// All classic office-suite behaviors enabled - the default.
close_on_escape: true,
/// Every self-driven behavior off.
pub const fn inert() -> Self {
close_on_escape: false,
impl Default for BackstageBehavior {
// -- Data model --
/// One backstage nav item ("Info", "Open", …).
pub struct BackstageNavItem {
/// The item label.
pub label: AzString,
/// Renders an extra gap above this item (office-2013: before "Account").
pub gap_before: bool,
impl BackstageNavItem {
/// Creates a nav item without a gap.
pub const fn new(label: AzString) -> Self {
label,
gap_before: false,
/// Builder method: marks this item as starting a new group.
pub const fn with_gap_before(mut self) -> Self {
self.gap_before = true;
self
impl_option!(
BackstageNavItem,
OptionBackstageNavItem,
copy = false,
[Debug, Clone, PartialEq]
impl_vec!(
BackstageNavItemVec,
BackstageNavItemVecDestructor,
BackstageNavItemVecDestructorType,
BackstageNavItemVecSlice,
OptionBackstageNavItem
impl_vec_clone!(
BackstageNavItemVecDestructor
impl_vec_debug!(BackstageNavItem, BackstageNavItemVec);
impl_vec_mut!(BackstageNavItem, BackstageNavItemVec);
/// Top-level backstage widget: nav column + app-provided content pane.
#[derive(Debug, Clone)]
pub struct Backstage {
/// Nav items, top to bottom.
pub nav_items: BackstageNavItemVec,
/// Index of the active (highlighted) nav item.
pub active_item: usize,
/// Optional callback fired when a nav item is clicked (receives the
/// item index).
pub on_nav_select: OptionBackstageOnNavSelect,
/// Optional callback fired by the back button (and by Escape, if
/// [`BackstageBehavior::close_on_escape`] is set).
pub on_back: OptionButtonOnClick,
/// Optional strip rendered above the content, right of the nav column
/// (office-2013: the white title bar area with the window buttons).
pub title_strip: azul_core::dom::OptionDom,
/// The active item's pane content (application composition).
pub content: azul_core::dom::OptionDom,
/// Which interactions the backstage handles by itself (defaults to
/// Word).
pub behavior: BackstageBehavior,
/// All part styles (defaults to the the Office-2013-era look look).
pub style: BackstageStyle,
// -- CSS classes --
static CLS_BACKSTAGE: &[IdOrClass] = &[Class(AzString::from_const_str("__azul-native-backstage"))];
static CLS_NAV: &[IdOrClass] = &[Class(AzString::from_const_str(
"__azul-native-backstage-nav",
))];
static CLS_NAV_ITEM: &[IdOrClass] = &[Class(AzString::from_const_str(
"__azul-native-backstage-nav-item",
static CLS_NAV_ITEM_ACTIVE: &[IdOrClass] = &[
Class(AzString::from_const_str("__azul-native-backstage-nav-item")),
Class(AzString::from_const_str(
"__azul-native-backstage-nav-item-active",
)),
static CLS_RIGHT: &[IdOrClass] = &[Class(AzString::from_const_str(
"__azul-native-backstage-right",
static CLS_CONTENT: &[IdOrClass] = &[Class(AzString::from_const_str(
"__azul-native-backstage-content",
/// The default the Office-2013-era look nav labels, in order.
pub const OFFICE_2013_NAV_LABELS: &[&str] = &[
"Info", "New", "Open", "Save", "Save As", "Print", "Share", "Export", "Close",
// -- Constructors / builders --
impl Backstage {
/// Creates a backstage with the given nav items, item 0 active, no
/// callbacks and no content, in the the Office-2013-era look style.
pub fn new(nav_items: BackstageNavItemVec) -> Self {
nav_items,
active_item: 0,
on_nav_select: None.into(),
on_back: None.into(),
title_strip: None.into(),
content: None.into(),
behavior: BackstageBehavior::office_2013(),
style: BackstageStyle::office_2013(),
/// The the Office-2013-era look nav: Info / New / Open / Save / Save As / Print /
/// Share / Export / Close, then a gap, then Account / Options.
pub fn office_2013() -> Self {
let mut items: Vec<BackstageNavItem> = OFFICE_2013_NAV_LABELS
.iter()
.map(|l| BackstageNavItem::new(AzString::from(*l)))
.collect();
items.push(BackstageNavItem::new(AzString::from_const_str("Account")).with_gap_before());
items.push(BackstageNavItem::new(AzString::from_const_str("Options")));
Self::new(BackstageNavItemVec::from_vec(items))
/// Sets the active nav item.
pub const fn set_active_item(&mut self, active_item: usize) {
self.active_item = active_item;
/// Builder method: sets the active nav item and returns `self`.
pub const fn with_active_item(mut self, active_item: usize) -> Self {
self.set_active_item(active_item);
/// Sets the pane content for the active item.
pub fn set_content(&mut self, content: Dom) {
self.content = Some(content).into();
/// Builder method: sets the pane content and returns `self`.
pub fn with_content(mut self, content: Dom) -> Self {
self.set_content(content);
/// Sets the title strip rendered above the content.
pub fn set_title_strip(&mut self, title_strip: Dom) {
self.title_strip = Some(title_strip).into();
/// Builder method: sets the title strip and returns `self`.
pub fn with_title_strip(mut self, title_strip: Dom) -> Self {
self.set_title_strip(title_strip);
/// Sets the nav-select callback.
pub fn set_on_nav_select<C: Into<BackstageOnNavSelectCallback>>(
&mut self,
data: RefAny,
on_nav_select: C,
) {
self.on_nav_select = Some(BackstageOnNavSelect {
refany: data,
callback: on_nav_select.into(),
})
.into();
/// Builder method: sets the nav-select callback and returns `self`.
pub fn with_on_nav_select<C: Into<BackstageOnNavSelectCallback>>(
mut self,
) -> Self {
self.set_on_nav_select(data, on_nav_select);
/// Sets the back callback (back button + Escape).
pub fn set_on_back<C: Into<super::button::ButtonOnClickCallback>>(
on_back: C,
self.on_back = Some(ButtonOnClick {
callback: on_back.into(),
/// Builder method: sets the back callback and returns `self`.
pub fn with_on_back<C: Into<super::button::ButtonOnClickCallback>>(
self.set_on_back(data, on_back);
/// Builder method: replaces the behavior set.
pub const fn with_behavior(mut self, behavior: BackstageBehavior) -> Self {
self.behavior = behavior;
/// Builder method: replaces the style bundle.
pub fn with_style(mut self, style: BackstageStyle) -> Self {
self.style = style;
/// Renders the backstage.
pub fn dom(self) -> Dom {
let Self {
active_item,
on_nav_select,
on_back,
title_strip,
content,
behavior,
style,
} = self;
// Every part resolved up front: the resolvers borrow `&style`, and
// `root_style` is moved out of it at the end of this function.
let part_root = style.resolved_root_style();
let part_nav = style.resolved_nav_style();
let part_back_button = style.resolved_back_button_style();
let part_back_icon = style.resolved_back_icon_style();
let part_nav_item = style.resolved_nav_item_style();
let part_nav_item_active = style.resolved_nav_item_active_style();
let part_nav_item_gap = style.resolved_nav_item_gap_style();
let part_right = style.resolved_right_style();
let part_content = style.resolved_content_style();
// -- nav column --
let mut nav_children: Vec<Dom> = Vec::with_capacity(nav_items.len() + 1);
{
let mut b = Button::create(AzString::from_const_str(""));
b.icon = AzString::from_const_str("arrow_back");
b.container_style = OptionCssPropertyWithConditionsVec::Some(part_back_button);
b.icon_style = OptionCssPropertyWithConditionsVec::Some(part_back_icon);
b.on_click = on_back.clone();
nav_children.push(b.dom());
for (idx, item) in nav_items.into_library_owned_vec().into_iter().enumerate() {
let (classes, mut part_style) = if idx == active_item {
(
CLS_NAV_ITEM_ACTIVE,
merged_style(&part_nav_item, &part_nav_item_active),
)
} else {
(CLS_NAV_ITEM, part_nav_item.clone())
if item.gap_before {
part_style = merged_style(&part_style, &part_nav_item_gap);
// The nav item div is display:flex — a raw text run cannot be a
// flex item (no anonymous-block wrapping in azul), so the label
// gets its `<p>` per the label convention. Caught by `dom_lint`
// on its very first run.
let mut d = Dom::create_div()
.with_ids_and_classes(IdOrClassVec::from_const_slice(classes))
.with_css_props(part_style)
.with_children(DomVec::from_vec(vec![crate::widgets::widget_p_with_text(
item.label,
)]));
if let Some(cb) = on_nav_select.as_ref() {
d = d.with_callbacks(
vec![CoreCallbackData {
event: EventFilter::Hover(HoverEventFilter::Click),
callback: CoreCallback {
cb: on_backstage_nav_click as usize,
ctx: azul_core::refany::OptionRefAny::None,
refany: RefAny::new(NavClickData {
item_idx: idx,
on_nav_select: cb.clone(),
}),
}]
.into(),
nav_children.push(d);
let nav = Dom::create_div()
.with_ids_and_classes(IdOrClassVec::from_const_slice(CLS_NAV))
.with_css_props(part_nav)
.with_children(DomVec::from_vec(nav_children));
// -- right side --
let mut right_children: Vec<Dom> = Vec::with_capacity(2);
if let Some(strip) = title_strip.into_option() {
right_children.push(strip);
let pane = match content.into_option() {
Some(c) => c,
None => Dom::create_div(),
right_children.push(
Dom::create_div()
.with_ids_and_classes(IdOrClassVec::from_const_slice(CLS_CONTENT))
.with_css_props(part_content)
.with_children(DomVec::from_vec(vec![pane])),
let right = Dom::create_div()
.with_ids_and_classes(IdOrClassVec::from_const_slice(CLS_RIGHT))
.with_css_props(part_right)
.with_children(DomVec::from_vec(right_children));
let mut root = Dom::create_div()
.with_ids_and_classes(IdOrClassVec::from_const_slice(CLS_BACKSTAGE))
.with_css_props(part_root)
.with_children(DomVec::from_vec(vec![nav, right]));
// Escape leaves the backstage (window-level, focus-independent).
if behavior.close_on_escape {
if let Some(back) = on_back.into_option() {
root = root.with_callbacks(
event: EventFilter::Window(WindowEventFilter::VirtualKeyDown),
cb: on_backstage_key_down as usize,
refany: RefAny::new(EscCloseData { on_back: back }),
root
impl Default for Backstage {
impl From<Backstage> for Dom {
fn from(b: Backstage) -> Self {
b.dom()
fn merged_style(
base: &CssPropertyWithConditionsVec,
extra: &CssPropertyWithConditionsVec,
) -> CssPropertyWithConditionsVec {
if extra.as_ref().is_empty() {
return base.clone();
let mut v: Vec<Cond> = base.as_ref().to_vec();
v.extend_from_slice(extra.as_ref());
// -- Nav-click / Escape plumbing --
/// Payload of one nav item: the item index plus the user's nav callback.
struct NavClickData {
item_idx: usize,
on_nav_select: BackstageOnNavSelect,
extern "C" fn on_backstage_nav_click(mut data: RefAny, info: CallbackInfo) -> Update {
let Some(payload) = data.downcast_ref::<NavClickData>() else {
return Update::DoNothing;
let idx = payload.item_idx;
let cb = payload.on_nav_select.callback.cb;
let refany = payload.on_nav_select.refany.clone();
drop(payload);
(cb)(refany, info, idx)
/// Payload of the window-level Escape handler: the user's back callback.
struct EscCloseData {
on_back: ButtonOnClick,
extern "C" fn on_backstage_key_down(mut data: RefAny, info: CallbackInfo) -> Update {
let Some(payload) = data.downcast_ref::<EscCloseData>() else {
let is_escape = matches!(
info.get_current_keyboard_state()
.current_virtual_keycode
.into_option(),
Some(VirtualKeyCode::Escape)
if !is_escape {
let cb = payload.on_back.callback.cb;
let refany = payload.on_back.refany.clone();
(cb)(refany, info)
#[cfg(test)]
mod tests {
use super::*;
extern "C" fn nav_cb(_: RefAny, _: CallbackInfo, _: usize) -> Update {
Update::DoNothing
extern "C" fn back_cb(_: RefAny, _: CallbackInfo) -> Update {
#[test]
fn from_system_with_no_reported_colors_falls_back_to_office_2013() {
// SystemStyle::default() may pre-fill platform colors; the fallback
// contract is about a system that reports NO colors at all.
let mut sys = azul_css::system::SystemStyle::default();
sys.colors = azul_css::system::SystemColors::default();
assert_eq!(
BackstageTheme::from_system(sys.clone()),
BackstageTheme::office_2013()
BackstageStyle::from_system(sys),
BackstageStyle::office_2013()
fn from_system_separates_the_accent_nav_band_from_the_window_surface() {
let accent = ColorU {
r: 61,
g: 174,
b: 233,
let surface = ColorU {
r: 35,
g: 38,
b: 41,
sys.colors.accent = Some(accent).into();
sys.colors.window_background = Some(surface).into();
let t = BackstageTheme::from_system(sys);
assert_eq!(t.nav_bg, accent, "the nav column is the accent band");
assert_eq!(t.content_bg, surface, "the pane is the window surface");
assert_ne!(
t.nav_bg, t.content_bg,
"a dark surface must not swallow the nav column"
t.nav_text,
BackstageTheme::office_2013().nav_text,
"an unreported colour falls back to its OWN office value"
// ------------------------------------------------------------------
// Constructors and invariants
fn backstage_office_2013_has_eleven_items_with_account_gapped() {
let b = Backstage::office_2013();
assert_eq!(b.nav_items.len(), 11);
assert_eq!(b.active_item, 0);
let items = b.nav_items.as_slice();
assert_eq!(items[0].label.as_str(), "Info");
assert_eq!(items[8].label.as_str(), "Close");
assert_eq!(items[9].label.as_str(), "Account");
assert!(items[9].gap_before);
assert_eq!(items[10].label.as_str(), "Options");
assert!(!items[10].gap_before);
fn backstage_style_default_is_office_2013() {
assert_eq!(BackstageStyle::default(), BackstageStyle::office_2013());
fn backstage_behavior_default_closes_on_escape() {
BackstageBehavior::default(),
BackstageBehavior::office_2013()
assert!(BackstageBehavior::office_2013().close_on_escape);
assert!(!BackstageBehavior::inert().close_on_escape);
// DOM shape
fn dom_renders_nav_and_right_side() {
let dom = Backstage::office_2013().dom();
assert_eq!(dom.children.as_ref().len(), 2);
// Nav: back button + 11 items.
let nav = &dom.children.as_ref()[0];
assert_eq!(nav.children.as_ref().len(), 12);
fn dom_places_the_title_strip_above_the_content() {
let strip = Dom::create_div();
let dom = Backstage::office_2013().with_title_strip(strip).dom();
let right = &dom.children.as_ref()[1];
assert_eq!(right.children.as_ref().len(), 2);
fn nav_items_get_click_callbacks_only_with_a_select_handler() {
// Without a handler: inert items.
for item in nav.children.as_ref().iter().skip(1) {
assert!(item.root.callbacks.as_ref().is_empty());
// With a handler: every item carries one.
let dom = Backstage::office_2013()
.with_on_nav_select(RefAny::new(()), nav_cb as BackstageOnNavSelectCallbackType)
.dom();
assert_eq!(item.root.callbacks.as_ref().len(), 1);
fn escape_handler_is_attached_only_with_behavior_and_back_callback() {
// Behavior on, no back callback: nothing to invoke, no handler.
assert!(dom.root.callbacks.as_ref().is_empty());
// Behavior on + back callback: window-level key handler on the root.
.with_on_back(
RefAny::new(()),
back_cb as super::super::button::ButtonOnClickCallbackType,
assert_eq!(dom.root.callbacks.as_ref().len(), 1);
dom.root.callbacks.as_ref()[0].event,
EventFilter::Window(WindowEventFilter::VirtualKeyDown)
// Behavior off: no handler even with a back callback.
.with_behavior(BackstageBehavior::inert())
fn active_item_gets_the_active_class() {
let dom = Backstage::office_2013().with_active_item(2).dom();
// Nav child 0 is the back button; item i is child i+1.
let active = &nav.children.as_ref()[3];
let classes = active.root.get_ids_and_classes();
assert!(classes.as_ref().iter().any(|c| match c {
Class(s) => s.as_str().contains("nav-item-active"),
IdOrClass::Id(_) => false,
}));