Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
# Startup window size follows `config.window.dimensions`

## Status

Implemented for issue #312 (GPUI shell, `dimensions` only).

## Context

The GPUI shell is the default product shell (`default = ["gpui-shell"]`, and
the legacy shell is a separate compile-time feature, so `config::load` is not
even in the shipped binary). Its only touch with the user's config file was
`gpui_shell::config::find_config_file()`, which resolves
`config::source::discover_toml()` and merges imports with its own TOML reader —
TOML only, never Lua, and only for the keys the settings page wants. So
`config.window.*` had no reader in the shell at all, while `pebrel config
check` — which uses `config::source::discover` + `config::load_source` —
accepted and validated keys the running app ignored. The reporter's
`config.window.dimensions = { columns = 155, lines = 43 }` on Windows 11 at
125% scaling fell into that gap: the window always opened at the shell's fixed
startup geometry.

Startup size had a second, independent cause. `workspace_window_options` pins a
1080×720 logical centered window with a 760×540 minimum, and the real grid came
from `prepare_initial_grid`, which multiplied the hard-coded
`TerminalView::DEFAULT_GRID_COLUMNS`/`DEFAULT_GRID_LINES` (116×30) by the
measured startup cell size. The 116×30 pair was itself the legacy shell's
default canvas, not a user value.

## Evidence

- `config::source::discover` / `config::load_source` are the same entry points
`config_cli.rs` uses for `pebrel config check`, so a key accepted there must
mean the same thing here.
- `WindowConfig::dimensions()` already carries the legacy contract "both
nonzero, else unset"; `display::window_size` clamps each axis to
`term::MIN_COLUMNS` / `term::MIN_SCREEN_LINES`.
- gpui rev `7c66238`: `WindowOptions::window_bounds` and `Window::resize` take
logical `Pixels` (scaled per window by the platform layer), `App` exposes no
scale factor, and `Window` has no `set_position`/`move_window`.
- Local `cargo check -p nebula` cannot complete on this machine: the
`gpui_macos` build script needs the Xcode `metal` tool
(`xcrun: error: unable to find utility "metal"`), so compile-level
verification runs in CI. See Validation.

## Decision

`gpui_shell::config::StartupWindow` holds the startup grid and is loaded once in
`init()` through `config::source::discover(config_file)` + `config::load_source` —
the pair `pebrel config check` uses, so a key accepted there means the same thing
here, and Lua, TOML and YAML share one parser. `main` forwards the selected
`--config-file` path through `run_shell`; without that option, normal environment
and file discovery still applies. Because the value is a grid (columns × lines)
and not pixels,
`prepare_initial_grid` multiplies it by the cell it measures in the window's own
scale domain, so the result is correct at 100%, 125% and any other DPI without
the shell knowing the scale factor. Unset config keeps the 116×30 built-in
canvas; an oversized grid keeps the existing 95%-of-visible-display clamp, which
shrinks the grid rather than pushing the window off screen.

## Rejected alternatives

- Implementing `config.window.position` in the same pass. gpui only accepts
placement through `WindowOptions::window_bounds`, in logical pixels, with no
programmatic move and no `App`-level scale factor; the legacy shell hands winit
a `PhysicalPosition`. Feeding the user's raw numbers as logical would place the
window ~25% off at the reporter's 125% scaling, so `position` is left
unwired rather than shipped wrong. `startup_mode`, `decorations` and
`dynamic_title` are similarly untouched: they are not the reported symptom.
- Folding the field into `Settings`. `Settings` reloads on every settings-page
write, and a Lua `require`/eval on that path would re-execute user code per
save; the startup grid is by definition read once. Separate global, separate
lifecycle.
- Reading `config.window.dimensions` directly in `workspace_window_options` as
pixel math. That duplicates the chrome budget already owned by
`prepare_initial_grid` and cannot measure the cell before a window exists, so
the two paths would drift.
- Parsing Lua/TOML again inside `gpui_shell`. A second parser would diverge from
`pebrel config check` in precedence and error handling; reuse keeps one
authority.
- Extending the shell's existing TOML reader (`find_config_file` +
`load_merged_toml`) with a `window.dimensions` lookup. It cannot see a Lua
file, which is the format the reported config uses, and it deserializes into
`toml::Value` rather than `UiConfig`, so one key would need a third spelling
of the same config.
- Fixing only the legacy shell, or documenting the key as legacy-only. The
default product shell is the one users hit.

## Consequences

- The GPUI shell now depends on the app-level config loader (`config::source`,
`config::load_source`, and therefore mlua for Lua sources) during bootstrap.
Discovery order stays `pebrel.lua` → `pebrel.toml` → YAML, so a Lua file
shadows a TOML file for startup size exactly as it does for `config check`.
- `init()` performs one synchronous config read plus possible Lua evaluation on
the main thread before the first window opens. The explicit CLI path reaches
this one-time load; settings-page writes do not re-evaluate it.
- A config file that fails to parse prints `[pebrel:gpui] failed to read window
config …` to stderr and falls back to the built-in canvas; the shell still
opens.
- Issue #312 is only half closed: window size now follows config, position still
does not.

## Validation

Local: `cargo fmt --all -- --check`,
`python3 scripts/check_architecture.py --base 7b0cedb` and
`python3 scripts/check_platform_cfg.py` (budget unchanged at 481) pass; no line
budget was raised, and `workspace.rs` is net −1 line because the grid math moved
to `config.rs`.

`lua_window_dimensions_reach_the_startup_grid` fails before the change, because
`StartupWindow` did not exist; it pins the Lua source → `dimensions()` link.
`startup_grid_follows_window_dimensions` pins the three shell-side verdicts:
unset → built-in canvas, configured → exact grid, below-minimum → clamped.
`startup_window_uses_the_config_file_selected_by_cli` covers CLI parsing through
startup loading. Follow-up builds and tests run through the upstream PR Actions.

## Supersedes

None.

## Revisit when

gpui exposes a window-position API or an app-level scale factor, and
`config.window.position` can be honoured with the same physical-pixel contract
the legacy shell has. Also when the GPUI shell moves onto a shared config
loader with the rest of the app; at that point `StartupWindow` should read from
it rather than re-discovering.
135 changes: 133 additions & 2 deletions nebula_app/src/gpui_shell/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -470,6 +470,63 @@ fn default_font_family() -> &'static str {
crate::font_install::REQUIRED_FONT_FAMILY
}

/// `config.window.dimensions`:启动窗口的网格行列。
///
/// 单独驻留而不是并入 [`Settings`],因为生命周期不同:`Settings` 随设置页写入
/// 反复重载,启动网格按定义只在开窗时生效一次,而 Lua 配置求值不该出现在每次
/// 保存设置的路径上。显式 `--config-file` 优先;否则复用主应用发现顺序(`pebrel.lua`
/// 优先于 `pebrel.toml`,YAML 同路)和 `pebrel config check` 的同一解析器。
#[derive(Default)]
pub(crate) struct StartupWindow {
pub dimensions: Option<crate::config::window::Dimensions>,
}

impl Global for StartupWindow {}

impl StartupWindow {
pub(crate) fn load(config_file: Option<PathBuf>) -> Self {
let Ok(Some(source)) = crate::config::source::discover(config_file) else {
return Self::default();
};
match crate::config::load_source(&source) {
Ok(loaded) => Self::from_config(&loaded.config),
Err(error) => {
super::try_write_stderr(format_args!(
"[pebrel:gpui] failed to read window config {}: {error}",
source.primary_path.display()
));
Self::default()
},
}
}

/// `dimensions()` 自带「行列必须同时非零」的旧壳合同。
fn from_config(config: &crate::config::UiConfig) -> Self {
Self { dimensions: config.window.dimensions() }
}
}

/// 启动网格行列(浮点,供 cell 宽度直接相乘)。
///
/// 配置了 `config.window.dimensions` 就按它定形,与旧壳 `window_size` 同合同
/// (行列各钳到终端最小可用值);未配置时用 `TerminalView` 的内建默认画布。
pub(crate) fn startup_grid(cx: &App) -> (f32, f32) {
let builtin = || {
(
f32::from(super::terminal::view::TerminalView::DEFAULT_GRID_COLUMNS),
f32::from(super::terminal::view::TerminalView::DEFAULT_GRID_LINES),
)
};
let Some(dimensions) = cx.try_global::<StartupWindow>().and_then(|window| window.dimensions)
else {
return builtin();
};
(
dimensions.columns.max(nebula_terminal::term::MIN_COLUMNS) as f32,
dimensions.lines.max(nebula_terminal::term::MIN_SCREEN_LINES) as f32,
)
}

/// 查找配置文件;顺序与 `nebula_app::config::installed_config` 一致。
/// `NEBULA_GPUI_CONFIG` 用于隔离测试:绝不能往用户真实配置目录写测试文件,
/// 正式版 Nebula 正在监视它做热重载。
Expand Down Expand Up @@ -784,10 +841,11 @@ fn build_palette(raw: &RawColors) -> Palette {
#[cfg(test)]
mod tests {
use super::{
RawColors, Settings, apply_resolved_theme, apply_theme, build_palette,
RawColors, Settings, StartupWindow, apply_resolved_theme, apply_theme, build_palette,
effective_font_sizes, effective_theme_line_height, resolve_ui_language, rgba8,
runtime_background,
runtime_background, startup_grid,
};
use crate::config::window::Dimensions;
use crate::display::UiLanguage;
use crate::gpui_shell::terminal::colors::Palette;
use nebula_settings::{
Expand All @@ -805,6 +863,79 @@ mod tests {
assert_eq!(resolve_ui_language(LanguagePref::EnUs), UiLanguage::EnUs);
}

#[test]
fn startup_window_uses_the_config_file_selected_by_cli() {
use clap::Parser as _;

let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("selected.lua");
std::fs::write(
&path,
"local pebrel = require 'pebrel'\n\
local config = pebrel.config_builder()\n\
config.window = { dimensions = { columns = 123, lines = 37 } }\n\
return config\n",
)
.unwrap();
let options = crate::cli::Options::try_parse_from([
"pebrel",
"--config-file",
path.to_str().unwrap(),
])
.unwrap();

assert_eq!(
StartupWindow::load(options.config_file).dimensions,
Some(Dimensions { columns: 123, lines: 37 })
);
}

/// 用户写在 `pebrel.lua` 里的 `config.window.dimensions` 必须真的决定启动
/// 网格;过去它只被 `pebrel config check` 的同一套解析认下,壳从不读取。
#[test]
fn lua_window_dimensions_reach_the_startup_grid() {
use crate::config::lua::runtime::ReloadSignal;

let temp = tempfile::tempdir().unwrap();
let path = temp.path().join("pebrel.lua");
std::fs::write(
&path,
"local pebrel = require 'pebrel'\n\
local config = pebrel.config_builder()\n\
config.window = { dimensions = { columns = 155, lines = 43 } }\n\
return config\n",
)
.unwrap();
let loaded = crate::config::lua::load_lua_file(&path, ReloadSignal::default()).unwrap();

assert_eq!(
StartupWindow::from_config(&loaded.config).dimensions,
Some(Dimensions { columns: 155, lines: 43 })
);
}

#[cfg(feature = "gpui-test-support")]
#[gpui::test]
fn startup_grid_follows_window_dimensions(cx: &mut gpui::TestAppContext) {
use crate::gpui_shell::terminal::view::TerminalView;

let builtin = (
f32::from(TerminalView::DEFAULT_GRID_COLUMNS),
f32::from(TerminalView::DEFAULT_GRID_LINES),
);
let configured = |columns: usize, lines: usize| StartupWindow {
dimensions: Some(Dimensions { columns, lines }),
};
cx.update(|cx| {
assert_eq!(startup_grid(cx), builtin);
cx.set_global(configured(155, 43));
assert_eq!(startup_grid(cx), (155.0, 43.0));
// 与旧壳 `window_size` 同合同:行列各钳到终端最小可用值。
cx.set_global(configured(1, 1));
assert_eq!(startup_grid(cx), (2.0, 1.0));
});
}

#[cfg(feature = "gpui-test-support")]
#[gpui::test]
fn render_preferences_read_current_settings(cx: &mut gpui::TestAppContext) {
Expand Down
6 changes: 4 additions & 2 deletions nebula_app/src/gpui_shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ pub fn run_shell(
initial_cwd: Option<std::path::PathBuf>,
initial_command: Option<crate::config::ui_config::Program>,
shell_id: Option<String>,
config_file: Option<std::path::PathBuf>,
) {
if crate::platform::CAPABILITIES.self_update_install {
match crate::update_download::handoff::installation_in_progress() {
Expand Down Expand Up @@ -179,7 +180,7 @@ pub fn run_shell(
let ai_events = crate::ai_hook::spawn_gpui_server();
#[cfg(windows)]
crate::ai_hook::spawn_config_guard();
init(cx);
init(cx, config_file);
if initial_cwd.is_some()
|| !crate::platform::startup::start_hidden(&nebula_settings::RuntimeSettings::load())
{
Expand All @@ -191,7 +192,7 @@ pub fn run_shell(
}

/// 组件库/主题/快捷键/用户配置的一次性初始化。
fn init(cx: &mut App) {
fn init(cx: &mut App, config_file: Option<std::path::PathBuf>) {
crate::platform::acrylic::init(cx);
// 三端都注册内嵌 Maple:Linux/macOS 的系统等宽字体没有 NF 图标码点,
// 侧栏与提示符会出方框;字形同源也是跨平台截图能互相比对的前提。
Expand All @@ -218,6 +219,7 @@ fn init(cx: &mut App) {
let settings = config::Settings::load_with_runtime(theme, runtime);
gpui_component::set_locale(settings.ui_language.gpui_component_locale());
cx.set_global(settings);
cx.set_global(config::StartupWindow::load(config_file));
theme::apply_chrome_theme(cx);
terminal::init(cx);
workspace::init(cx);
Expand Down
2 changes: 1 addition & 1 deletion nebula_app/src/gpui_shell/workspace/terminal_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ mod tests {
) {
cx.update(|cx| {
gpui_component::init(cx);
crate::gpui_shell::init(cx);
crate::gpui_shell::init(cx, None);
windowing::initialize(cx, crate::runtime_api::RuntimeHub::new());
cx.set_reduce_motion(true);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ fn git_list_repaints_visible_rows_and_clicks_the_scrolled_path(cx: &mut TestAppC
gpui_component::init(cx);
crate::gpui_shell::math_view::register(cx);
crate::gpui_shell::file_editor::init(cx);
crate::gpui_shell::init(cx);
crate::gpui_shell::init(cx, None);
cx.set_reduce_motion(true);
windowing::initialize(cx, crate::runtime_api::RuntimeHub::new());
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@ fn chrome_size(sidebar_width: f32) -> Size<Pixels> {

fn default_size(
metrics: (Pixels, Pixels),
grid: (f32, f32),
sidebar_width: f32,
display_limit: Option<Size<Pixels>>,
) -> Size<Pixels> {
let chrome = chrome_size(sidebar_width);
let preferred = size(
metrics.0 * f32::from(TerminalView::DEFAULT_GRID_COLUMNS) + chrome.width,
metrics.1 * f32::from(TerminalView::DEFAULT_GRID_LINES) + chrome.height,
);
let preferred = size(metrics.0 * grid.0 + chrome.width, metrics.1 * grid.1 + chrome.height);
display_limit.map_or(preferred, |limit| preferred.min(&limit))
}

Expand All @@ -49,6 +47,7 @@ pub(super) fn preferred_size(cx: &App, sidebar_width: f32) -> Option<Size<Pixels
Some(fit_preflight_size(
default_size(
TerminalView::startup_cell_metrics_at_scale(scale, cx),
crate::gpui_shell::config::startup_grid(cx),
sidebar_width,
display_limit(cx),
),
Expand All @@ -74,6 +73,7 @@ pub(in crate::gpui_shell::workspace) fn prepare_initial_grid(
let target = if fit_window_to_default_grid {
let requested = default_size(
TerminalView::startup_cell_metrics(window, cx),
crate::gpui_shell::config::startup_grid(cx),
sidebar_width,
display_limit(cx),
);
Expand Down Expand Up @@ -112,12 +112,16 @@ mod tests {
#[test]
fn initial_window_keeps_the_base_font_grid_and_fits_the_display() {
let metrics = (px(8.0), px(20.0));
assert_eq!(default_size(metrics, 230.0, None), size(px(1200.0), px(668.0)));
let grid = (
f32::from(TerminalView::DEFAULT_GRID_COLUMNS),
f32::from(TerminalView::DEFAULT_GRID_LINES),
);
assert_eq!(default_size(metrics, grid, 230.0, None), size(px(1200.0), px(668.0)));
assert_eq!(
default_size(metrics, 230.0, Some(size(px(1000.0), px(600.0)))),
default_size(metrics, grid, 230.0, Some(size(px(1000.0), px(600.0)))),
size(px(1000.0), px(600.0)),
);
assert_eq!(default_size(metrics, 300.0, None), size(px(1270.0), px(668.0)),);
assert_eq!(default_size(metrics, grid, 300.0, None), size(px(1270.0), px(668.0)),);
}

#[test]
Expand Down
Loading
Loading