⎇ be3a0e78fc894587313cb5e23631daf89ce52976
crates/modules/ui_engine/widgets/app-header/src/lib.rs
Historia
Quién tocó qué
crudo
feat(yahweh-widget-app-header): promover header standard de explorers
1//! `yahweh-widget-app-header` — tira superior estándar de las apps
2//! del repo.
3//!
4//! Compone:
5//! - Label dinámico a la izquierda (flex_grow).
6//! - [`theme_switcher`] a la derecha.
7//! - bg = `theme.bg_panel`, text = `theme.fg_text`,
8//! border-bottom = `theme.border`.
9//! - Padding 16/12, text_size 14.
10//!
11//! Patrón emergente: `nakui-explorer`, `nouser-explorer`,
12//! `minga-explorer`, `brahman-broker-explorer` declaran headers
13//! idénticos sólo cambiando el label. Ahora es 1 línea.
14//!
15//! # Ejemplo
16//!
17//! ```ignore
18//! use yahweh_widget_app_header::app_header;
19//!
20//! let header = app_header(cx, format!("Log: {} · {} entries", path, n));
21//! div().child(header).child(body)
22//! ```
23
24#![forbid(unsafe_code)]
25
26use gpui::{div, prelude::*, px, App, IntoElement, SharedString};
27use yahweh_theme::Theme;
28use yahweh_widget_theme_switcher::theme_switcher;
29
30/// Construye el header standard. Lee `Theme::global(cx)` para los
31/// colors; falla si no hay theme instalado (panic propagado de
32/// `Theme::global`).
33///
34/// `label` es texto plano. Para labels más ricos (ej. icon + text,
35/// múltiples spans), usar [`app_header_with`] que acepta
36/// cualquier child element.
37pub fn app_header(cx: &mut App, label: impl Into<SharedString>) -> impl IntoElement {
38 let label: SharedString = label.into();
39 app_header_with(cx, div().child(label))
40}
41
42/// Variante de [`app_header`] que acepta cualquier `IntoElement`
43/// como contenido del lado izquierdo. El widget envuelve el child
44/// en un `div().flex_grow()` para que el switcher quede pegado a
45/// la derecha.
46pub fn app_header_with(cx: &mut App, label_child: impl IntoElement) -> impl IntoElement {
47 let theme = Theme::global(cx).clone();
48 div()
49 .flex()
50 .flex_row()
51 .items_center()
52 .px(px(16.))
53 .py(px(12.))
54 .bg(theme.bg_panel.clone())
55 .border_b_1()
56 .border_color(theme.border)
57 .text_color(theme.fg_text)
58 .text_size(px(14.))
59 .child(div().flex_grow().child(label_child))
60 .child(theme_switcher(cx))
61}
62
63#[cfg(test)]
64mod tests {
65 use super::*;
66 use gpui::TestAppContext;
67
68 #[gpui::test]
69 fn app_header_constructs_with_string_label(cx: &mut TestAppContext) {
70 cx.update(|cx| {
71 Theme::install_default(cx);
72 let _h = app_header(cx, "Test header");
73 });
74 }
75
76 #[gpui::test]
77 fn app_header_with_accepts_arbitrary_child(cx: &mut TestAppContext) {
78 cx.update(|cx| {
79 Theme::install_default(cx);
80 let _h = app_header_with(
81 cx,
82 div().child(SharedString::from("Custom child")),
83 );
84 });
85 }
86
87 #[gpui::test]
88 fn app_header_label_accepts_owned_or_borrowed(cx: &mut TestAppContext) {
89 cx.update(|cx| {
90 Theme::install_default(cx);
91 let _ = app_header(cx, "literal");
92 let _ = app_header(cx, "owned".to_string());
93 let _ = app_header(cx, format!("formatted {}", 42));
94 });
95 }
96}