Skip to content

Commit 95f9e52

Browse files
committed
Refresh adaptive TUI theme on focus
1 parent d3b0449 commit 95f9e52

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

‎codex-rs/tui/src/render/highlight.rs‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,29 @@ pub(crate) fn set_theme_override(
100100
warning
101101
}
102102

103+
/// Refresh the runtime syntax theme from the terminal palette when no explicit
104+
/// theme override is configured.
105+
///
106+
/// This lets the TUI follow terminals that update their reported default
107+
/// background color after focus regain or a system light/dark appearance
108+
/// change. Explicit user-configured themes continue to win.
109+
pub(crate) fn refresh_runtime_theme_from_terminal_if_needed() {
110+
let override_name = THEME_OVERRIDE.get().and_then(|name| name.as_deref());
111+
let codex_home = CODEX_HOME
112+
.get()
113+
.and_then(|codex_home| codex_home.as_deref());
114+
115+
if let Some(name) = override_name
116+
&& resolve_theme_by_name(name, codex_home).is_some()
117+
{
118+
return;
119+
}
120+
121+
if let Some(theme) = resolve_theme_by_name(adaptive_default_theme_name(), codex_home) {
122+
set_syntax_theme(theme);
123+
}
124+
}
125+
103126
/// Check whether a theme name resolves to a bundled theme or a custom
104127
/// `.tmTheme` file. Returns a user-facing warning when it does not.
105128
pub(crate) fn validate_theme_name(name: Option<&str>, codex_home: Option<&Path>) -> Option<String> {

‎codex-rs/tui/src/tui/event_stream.rs‎

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,6 +249,7 @@ impl<S: EventSource + Default + Unpin> TuiEventStream<S> {
249249
Event::FocusGained => {
250250
self.terminal_focused.store(true, Ordering::Relaxed);
251251
crate::terminal_palette::requery_default_colors();
252+
crate::render::highlight::refresh_runtime_theme_from_terminal_if_needed();
252253
Some(TuiEvent::Draw)
253254
}
254255
Event::FocusLost => {
@@ -408,6 +409,19 @@ mod tests {
408409
}
409410
}
410411

412+
#[tokio::test(flavor = "current_thread")]
413+
async fn focus_gained_maps_to_draw_and_marks_terminal_focused() {
414+
let (broker, handle, _draw_tx, draw_rx, terminal_focused) = setup();
415+
terminal_focused.store(false, Ordering::Relaxed);
416+
let mut stream = make_stream(broker, draw_rx, terminal_focused.clone());
417+
418+
handle.send(Ok(Event::FocusGained));
419+
420+
let next = stream.next().await;
421+
assert!(matches!(next, Some(TuiEvent::Draw)));
422+
assert!(terminal_focused.load(Ordering::Relaxed));
423+
}
424+
411425
#[tokio::test(flavor = "current_thread")]
412426
async fn draw_and_key_events_yield_both() {
413427
let (broker, handle, draw_tx, draw_rx, terminal_focused) = setup();

0 commit comments

Comments
 (0)