Native UI Book · 01 Layout
布局系统与响应式断点 (Layout & Responsive)
使用 GPUI 链式 Flexbox / Grid 构建桌面与移动端自适应界面,并通过 ResponsiveContext 自动切换布局形态。
Flexbox 链式布局与自适应骨架 (`v_flex` & `h_flex`)
通过 use gpux::kit::{h_flex, v_flex, ActiveTheme, StyledExt};,你可以结合 ResponsiveContext 构建自动适配 7 端窗口尺寸与安全区的自适应工作台骨架:
rust
use gpux::prelude::*;
use gpux::kit::{h_flex, v_flex, ActiveTheme, StyledExt};
pub fn render_adaptive_shell(
rc: &ResponsiveContext,
cx: &mut Context<Self>,
) -> impl IntoElement {
let pad = rc.recommended_padding();
let cols = rc.recommended_columns();
v_flex()
.size_full()
.pt(px(rc.safe_area.top))
.pb(px(rc.safe_area.bottom))
.bg(cx.theme().background)
.text_color(cx.theme().foreground)
.child(
h_flex()
.flex_1()
.p(px(pad))
.gap_4()
.when(rc.is_expanded(), |el| {
el.child(
v_flex()
.w(px(240.0))
.p_3()
.rounded_lg()
.bg(cx.theme().sidebar)
.child("Desktop Navigation Rail"),
)
})
.child(
v_flex()
.flex_1()
.gap_3()
.child(format!("Grid Columns: {cols} · Breakpoint: {:?}", rc.breakpoint())),
),
)
}
三档响应式断点 (`Breakpoint`)
| 断点枚举 | 视口宽度范围 | 典型设备形态 (8 Targets) | recommended_columns() | 推荐导航模式 |
|---|---|---|---|---|
Breakpoint::Compact | < 600px | iOS / Android / 鸿蒙手机竖屏、移动端 Web | 1 列 | 底部 TabBar + 顶部安全区避让 |
Breakpoint::Medium | 600px .. 1024px | 折叠屏展开态 / iPad 竖屏 / 紧凑浏览器窗口 | 2 列 | 可折叠侧边栏 NavigationRail |
Breakpoint::Expanded | >= 1024px | macOS / Linux / Windows 桌面大屏与 Web 宽屏 | 3 列 | 固定左侧边栏 + 多栏 SplitPane |