From dd7f2a3575a239dc025ac2fabb68abeb30bfe0cb Mon Sep 17 00:00:00 2001 From: i2cjak Date: Tue, 24 Nov 2026 01:14:46 -0500 Subject: [PATCH] gpui: fix Pop back in text color in detached KiCad dark window Closes #1654 --- native/gpui/src/keys.rs | 1 + native/gpui/src/screens/viewer.rs | 2 +- native/gpui/src/ui.rs | 1 + native/gpui/src/viewers/kicad_pane.rs | 45 ++++++++++++++++++++++++--- 4 files changed, 44 insertions(+), 5 deletions(-) diff --git a/native/gpui/src/keys.rs b/native/gpui/src/keys.rs index 0b683cf75b..3bf1c8e618 100644 --- a/native/gpui/src/keys.rs +++ b/native/gpui/src/keys.rs @@ -321,6 +321,7 @@ fn has_tool(data: &Value, action: &str) -> bool { data["thread"]["tools"].as_arr /// in the system's proportional one, unlike everything around it. The window says its font at each draw (`set_tip_font`). static TIP_FONT: std::sync::Mutex = std::sync::Mutex::new(String::new()); pub fn set_tip_font(font: &str) { let mut f = TIP_FONT.lock().unwrap(); if *f != font { *f = font.to_string() } } +pub fn tip_font() -> String { TIP_FONT.lock().unwrap().clone() } struct Tip(SharedString, bool); impl Render for Tip { fn render(&mut self, _: &mut Window, _: &mut Context) -> impl IntoElement { diff --git a/native/gpui/src/screens/viewer.rs b/native/gpui/src/screens/viewer.rs index 32f7be9b48..05f38456d0 100644 --- a/native/gpui/src/screens/viewer.rs +++ b/native/gpui/src/screens/viewer.rs @@ -1050,7 +1050,7 @@ pub fn page(cx: &mut Context, reg: &Reg, light: bool, files_ui: &supe let body = if popped { // the editor is in its own window: the pane says so and offers it back div().flex_1().min_h_0().flex().flex_col().items_center().justify_center().gap_2().text_color(theme::color(light, "faint")).child(trs("KiCad is open in its own window.")) - .child(div().id("kicad-popin").px_3().py_1().border_1().border_color(theme::color(light, "line")).bg(theme::color(light, "raise")).cursor_pointer().child(trs("Pop back in")) + .child(div().id("kicad-popin").px_3().py_1().border_1().border_color(theme::color(light, "line")).bg(theme::color(light, "raise")).text_color(theme::color(light, "text")).cursor_pointer().hover(|s| s.bg(theme::color(light, "panel"))).child(trs("Pop back in")) .on_click(cx.listener(|this, _, _, cx| { let h = this.screens.kicad_window; this.kicad_popin(cx); if let Some(h) = h { let _ = h.update(cx, |_, w, _| w.remove_window()); } }))) .into_any_element() } else if e.read(cx).handoff() && matches!(open, "board" | "schematic") { diff --git a/native/gpui/src/ui.rs b/native/gpui/src/ui.rs index 2f3efba850..8d2dc9fbfe 100644 --- a/native/gpui/src/ui.rs +++ b/native/gpui/src/ui.rs @@ -924,6 +924,7 @@ impl Render for Backplane { // whether the page holds the embedded editor this frame (popped out: its own window does), from this frame's state: macOS hides // KiCad's frame by it when the pane is not drawn (#1199: the viewer closed, the PCB Editor stayed on screen) if let Some(p) = self.screens.kicad.clone() { + if p.read(cx).light != light { p.update(cx, |p, cx| p.set_light(light, cx)); } let mine = self.kicad_for(thread, cx).is_some(); let in_page = self.screens.kicad_window.is_some() || (!docked && mine && crate::screens::viewer::holds_editor(thread)); // #1345: off screen because the person moved away (another thread or design, or the viewer closed or on another page): the hub diff --git a/native/gpui/src/viewers/kicad_pane.rs b/native/gpui/src/viewers/kicad_pane.rs index 6a0b2d62b4..fa79993a66 100644 --- a/native/gpui/src/viewers/kicad_pane.rs +++ b/native/gpui/src/viewers/kicad_pane.rs @@ -3427,6 +3427,31 @@ mod tests { assert_eq!(hub_open_step("schematic", &mut last, "3d", None), Some("3d")); assert_eq!(last, "3d"); } + #[test] fn detached_window_and_popin_button_have_contrasting_text_colors() { + fn luminance(c: gpui::Rgba) -> f32 { + let lin = |u: f32| if u <= 0.03928 { u / 12.92 } else { ((u + 0.055) / 1.055).powf(2.4) }; + 0.2126 * lin(c.r) + 0.7152 * lin(c.g) + 0.0722 * lin(c.b) + } + fn contrast(a: gpui::Rgba, b: gpui::Rgba) -> f32 { + let (la, lb) = (luminance(a), luminance(b)); + let (hi, lo) = if la > lb { (la, lb) } else { (lb, la) }; + (hi + 0.05) / (lo + 0.05) + } + for light in [false, true] { + let (win_bg, win_fg) = super::kicad_window_colors(light); + assert_eq!(win_bg, crate::theme::color(light, "bg")); + assert_eq!(win_fg, crate::theme::color(light, "text")); + assert!(contrast(win_fg, win_bg) >= 4.5, "light={light} window contrast too low"); + + let (btn_bg, btn_fg) = super::strip_button_colors(light); + assert_eq!(btn_bg, crate::theme::color(light, "raise")); + assert_eq!(btn_fg, crate::theme::color(light, "text")); + assert!(contrast(btn_fg, btn_bg) >= 4.5, "light={light} strip button contrast too low"); + if !light { + assert_ne!(btn_fg, gpui::rgb(0x000000).into(), "dark theme button text must not default to black"); + } + } + } } /// Whether this window can hold the thread's KiCad: the hub is on this machine (the editor runs beside it) and the thread is not another machine's. @@ -3837,7 +3862,7 @@ pub fn strip(cx: &mut Context, light: bool, pane: &Entity(cx: &mut Context, light: bool, pane: &Entity| { let act = act.clone(); + let (btn_bg, btn_fg) = strip_button_colors(light); // its rect under `kicad:` (kicad:full, kicad:screen, kicad:popout, kicad:popin): a driver clicks the button itself, never a place // measured once (real_use's fixed offset for Full size landed on Full screen once it was added beside it) - crate::ui::with_rect(div().id(id), move || format!("kicad:{a}")).when(narrow, |d| d.px_1()).when(!narrow, |d| d.px_2()).py(px(3.)).text_size(crate::scale::tpx(12.)).border_1().border_color(c("line")).bg(c("raise")).cursor_pointer().hover(|s| s.bg(c("panel"))) + crate::ui::with_rect(div().id(id), move || format!("kicad:{a}")).when(narrow, |d| d.px_1()).when(!narrow, |d| d.px_2()).py(px(3.)).text_size(crate::scale::tpx(12.)).border_1().border_color(c("line")).bg(btn_bg).text_color(btn_fg).cursor_pointer().hover(|s| s.bg(c("panel"))) .tooltip(crate::keys::tip(&tip, "", light)).child(label) .on_click(cx.listener(move |this, _, window, cx| act(this, a, window, cx))) }; @@ -3975,6 +4001,15 @@ pub fn strip(cx: &mut Context, light: bool, pane: &Entity (Rgba, Rgba) { + (crate::theme::color(light, "bg"), crate::theme::color(light, "text")) +} +/// Strip action button (background, text) colours (Full size, Full screen, Pop out, Pop back in). +fn strip_button_colors(light: bool) -> (Rgba, Rgba) { + (crate::theme::color(light, "raise"), crate::theme::color(light, "text")) +} + /// The editor in a window of its own (Pop out): the strip and the same pane (the same KiCad, nothing restarted). Closing it pops it back in. pub struct KicadWindow { checks: crate::screens::mgmt_checks::State, pub pane: Entity, pub app: WeakEntity, pub light: bool } impl Render for KicadWindow { @@ -3985,7 +4020,9 @@ impl Render for KicadWindow { let strip = strip(cx, light, &self.pane, &self.checks, true, false, false, |this: &mut KicadWindow, a, window, cx| { if a == "popin" { if let Some(app) = this.app.upgrade() { app.update(cx, |app, cx| app.kicad_popin(cx)); } window.remove_window(); } }); - div().size_full().flex().flex_col().bg(crate::theme::color(light, "bg")).child(strip).child(div().flex_1().min_h_0().child(self.pane.clone())) + let (win_bg, win_fg) = kicad_window_colors(light); + let font = crate::keys::tip_font(); + div().size_full().flex().flex_col().bg(win_bg).text_color(win_fg).text_size(crate::scale::tpx(13.)).line_height(crate::scale::tpx(20.)).when(!font.is_empty(), |d| d.font_family(font)).child(strip).child(div().flex_1().min_h_0().child(self.pane.clone())) } } @@ -4002,7 +4039,7 @@ impl crate::Backplane { match cx.open_window(opts, move |window, cx| { let app2 = app.clone(); window.on_window_should_close(cx, move |window, cx| { let popped = top_of(window).unwrap_or(0); if let Some(a) = app2.upgrade() { a.update(cx, |a, cx| a.kicad_popin_closing(popped, cx)); } true }); - cx.new(|_| KicadWindow { pane, app, light, checks }) + cx.new(|cx| { cx.observe(&pane, |_, _, cx| cx.notify()).detach(); KicadWindow { pane, app, light, checks } }) }) { // the popped-out window is the person's now: the editor in it gets the keys as on Edit (the window itself is shown // active by open_window)