Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Status Bar

Group: Chrome

Code

/// Build the status bar for either state: `dirty == true` pads the mode
/// segment wide enough, at this demo's width, to force a low-priority
/// right segment to drop under priority-drop truncation.
fn status_bar(dirty: bool) -> StatusBar {
    let mode_text = if dirty {
        format!("NORMAL{}", " [+]".repeat(60))
    } else {
        "NORMAL".to_string()
    };
    let plain = |text: &str| StatusBarSegment {
        text: text.to_string(),
        fg: Color::rgb(220, 220, 220),
        bg: Color::rgb(40, 80, 120),
        bold: false,
        action_id: None,
    };
    StatusBar {
        id: WidgetId::new("gallery:status-bar"),
        left_segments: vec![StatusBarSegment {
            text: mode_text,
            fg: Color::rgb(255, 255, 255),
            bg: Color::rgb(40, 80, 120),
            bold: true,
            action_id: None,
        }],
        // Least-important first, cursor-position segment last — the
        // documented convention that keeps it visible under
        // priority-drop.
        right_segments: vec![
            plain("Spaces: 4"),
            plain("UTF-8"),
            plain("LF"),
            plain("Ln 1, Col 1"),
        ],
    }
}

pub struct StatusBarDemo;

impl StatusBarDemo {
    pub fn new() -> Self {
        Self
    }
}

impl Default for StatusBarDemo {
    fn default() -> Self {
        Self::new()
    }
}

impl Demo for StatusBarDemo {
    fn name(&self) -> &'static str {
        "Status Bar"
    }

    fn group(&self) -> &'static str {
        "Chrome"
    }

    fn variants(&self) -> &'static [&'static str] {
        &["Clean", "Dirty (priority drop)"]
    }

    fn render(&self, variant: usize, backend: &mut dyn Backend, area: Rect) {
        let lh = backend.line_height();
        let rect = Rect::new(area.x, area.y, area.width, lh);
        let bar = status_bar(variant == 1);
        let _ = backend.draw_status_bar_interactive(rect, &bar, &InteractionState::new());
    }

    fn handle(
        &mut self,
        _variant: usize,
        _event: &UiEvent,
        _backend: &mut dyn Backend,
        _area: Rect,
    ) -> Reaction {
        Reaction::Continue
    }

    fn source(&self) -> &'static str {
        extract_region(SOURCE)
    }

    fn data(&self, variant: usize) -> serde_json::Value {
        serde_json::to_value(status_bar(variant == 1)).unwrap_or(serde_json::Value::Null)
    }

    fn caps_note(&self, _variant: usize, _caps: &BackendCaps) -> Option<String> {
        None
    }
}

Backends

VariantTUIGTKmacOSWindows
Clean
Dirty (priority drop)