From aed11c9bd490e03d316422e503f99fa363aea17e Mon Sep 17 00:00:00 2001 From: cnmcdee Date: Sun, 29 Nov 2026 14:56:13 +0000 Subject: [PATCH] UI Theme editor: wire View -> Color Theme picker, Dracula preset - View -> Color Theme now opens the real picker from lightgui-support (was a stub note); the designer depends on lightgui-support. - Fifth built-in preset: Dracula (#282a36 bg, #ff79c6 keyword), same colours as the C designer. - New theme_ui module: Apply repaints the code view (the plain TextView the designer actually uses) and the canvas; the canvas follows the theme when the project has no window background (project background still wins); the saved theme is applied to the canvas at startup. Clearing the project background reveals the theme background (bulk.rs). - Theme tests updated to 5 built-ins with Dracula checks. - Verified: cargo test --workspace 176 passed / 0 failed; all five presets + a custom colour applied live under Xvfb and persisted across a restart. --- Cargo.lock | 1 + crates/lightgui-designer/Cargo.toml | 1 + crates/lightgui-designer/src/bulk.rs | 13 +++++--- crates/lightgui-designer/src/codeview.rs | 22 +++++++++++++ crates/lightgui-designer/src/main.rs | 16 ++++++++- crates/lightgui-designer/src/theme_ui.rs | 34 ++++++++++++++++++++ crates/lightgui-support/src/theme.rs | 25 ++++++++++++-- crates/lightgui-support/tests/theme_tests.rs | 15 +++++++-- 8 files changed, 116 insertions(+), 11 deletions(-) create mode 100644 crates/lightgui-designer/src/theme_ui.rs diff --git a/Cargo.lock b/Cargo.lock index 08678ef..f1b54e6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -436,6 +436,7 @@ dependencies = [ "gtk", "lightgui-core", "lightgui-gen", + "lightgui-support", "pango", "serde_json", ] diff --git a/crates/lightgui-designer/Cargo.toml b/crates/lightgui-designer/Cargo.toml index 2bf97c1..68d3b98 100644 --- a/crates/lightgui-designer/Cargo.toml +++ b/crates/lightgui-designer/Cargo.toml @@ -12,6 +12,7 @@ path = "src/main.rs" [dependencies] lightgui-core = { path = "../lightgui-core" } lightgui-gen = { path = "../lightgui-gen" } +lightgui-support = { path = "../lightgui-support" } gtk = { workspace = true } gdk = { workspace = true } gdk-pixbuf = "0.18" diff --git a/crates/lightgui-designer/src/bulk.rs b/crates/lightgui-designer/src/bulk.rs index 5855e7d..57a30fa 100644 --- a/crates/lightgui-designer/src/bulk.rs +++ b/crates/lightgui-designer/src/bulk.rs @@ -121,13 +121,16 @@ pub fn set_project_background(app: &Shared, raw: &str) { if !raw.is_empty() && gdk::RGBA::parse(raw).is_err() { return; } - let css = if raw.is_empty() { - String::new() - } else { - format!("* {{ background: {raw}; }}") - }; let mut a = app.borrow_mut(); a.project.background = raw.to_string(); + // Clearing the project background reveals the UI theme background + // (mirrors C++ apply_canvas_background()). + let bg = if raw.is_empty() { + lightgui_support::theme::theme_current().background + } else { + raw.to_string() + }; + let css = format!("* {{ background: {bg}; }}"); let _ = a.canvas_css.load_from_data(css.as_bytes()); } diff --git a/crates/lightgui-designer/src/codeview.rs b/crates/lightgui-designer/src/codeview.rs index 73852d7..2eae6b7 100644 --- a/crates/lightgui-designer/src/codeview.rs +++ b/crates/lightgui-designer/src/codeview.rs @@ -20,6 +20,9 @@ use crate::state::Shared; use gtk::prelude::*; +use lightgui_support::theme::{ + theme_apply_to_view, theme_current, theme_highlight_buffer, +}; /// Primary monitor geometry `(x, y, width, height)`, falling back to /// 1920x1080 when no monitor is reported. @@ -33,6 +36,22 @@ pub fn monitor_geometry() -> (i32, i32, i32, i32) { (0, 0, 1920, 1080) } +/// Apply the current UI theme to the code view (background/foreground +/// CSS + syntax highlighting of the shown text). Called on every +/// refresh (hence on open, which refreshes) and when the theme changes +/// (see theme_ui::apply_theme). +pub fn apply_code_theme(app: &Shared) { + let view = app.borrow().code_view.clone(); + let Some(view) = view else { + return; + }; + let theme = theme_current(); + theme_apply_to_view(view.upcast_ref(), &theme); + if let Some(buf) = view.buffer() { + theme_highlight_buffer(&buf, &theme); + } +} + /// Regenerate the project into the scratch dir and show it in the code /// window: file list first, then the main window source. /// @@ -110,6 +129,9 @@ pub fn refresh_code_view(app: &Shared) { ); } } + // Re-apply the UI theme: the new text needs fresh highlighting and + // the view colours follow the saved theme. + apply_code_theme(app); } /// Save the code view's current contents back to the generated file diff --git a/crates/lightgui-designer/src/main.rs b/crates/lightgui-designer/src/main.rs index 6e4dea0..0d8b2c2 100644 --- a/crates/lightgui-designer/src/main.rs +++ b/crates/lightgui-designer/src/main.rs @@ -41,6 +41,7 @@ mod inspector; mod palette_ui; mod selftest; mod state; +mod theme_ui; use canvas::{add_widget, load_into_canvas}; use codeview::{ @@ -290,7 +291,17 @@ fn main() { } if let Some(mi) = builder.object::("mi_view_theme") { let app2 = app.clone(); - mi.connect_activate(move |_| unowned_note(&app2, "Theme picker lives in lightgui-support")); + mi.connect_activate(move |_| { + // Clone the window out of the RefCell first: the picker is + // modal and its on_apply callback borrows the app, so no + // borrow of `app` may be held across the call. + let main_win = app2.borrow().main_window.clone(); + let app3 = app2.clone(); + lightgui_support::theme_picker_open( + Some(&main_win), + Some(Box::new(move |t| theme_ui::apply_theme(&app3, t))), + ); + }); } if let Some(mi) = builder.object::("mi_view_palette") { app.borrow_mut().palette_menu_item = Some(mi.clone()); @@ -375,6 +386,9 @@ fn main() { }); } + // The canvas background follows the saved UI theme until the + // project sets its own window background. + theme_ui::apply_theme_to_canvas(&app, &lightgui_support::theme::theme_current()); refresh_props(&app); // Bulk parameter + colour palette sections, packed into the // inspector box below the property grid (mirrors C++ main()). diff --git a/crates/lightgui-designer/src/theme_ui.rs b/crates/lightgui-designer/src/theme_ui.rs new file mode 100644 index 0000000..4c39561 --- /dev/null +++ b/crates/lightgui-designer/src/theme_ui.rs @@ -0,0 +1,34 @@ +//! Apply the UI theme (lightgui-support `CodeTheme`) to the designer: +//! the code view's colours/highlighting and the canvas background. +//! +//! The theme is the one saved in `~/.config/lightgui/theme.conf` and +//! edited from View -> Color Theme (the picker in lightgui-support). +//! The canvas follows the theme only when the project has no window +//! background of its own; a project background always wins (mirrors the +//! C++ `apply_canvas_background()` in src/main.cpp). + +use crate::codeview::apply_code_theme; +use crate::state::Shared; +use gtk::prelude::*; +use lightgui_support::theme::CodeTheme; + +/// Apply `theme` to the open code view (if any) and the canvas. +pub fn apply_theme(app: &Shared, theme: &CodeTheme) { + apply_code_theme(app); + apply_theme_to_canvas(app, theme); +} + +/// Recolour the canvas background from `theme`, unless the project has +/// its own window background (which wins). +pub fn apply_theme_to_canvas(app: &Shared, theme: &CodeTheme) { + let (bg, css) = { + let a = app.borrow(); + let bg = if a.project.background.is_empty() { + theme.background.clone() + } else { + a.project.background.clone() + }; + (bg, a.canvas_css.clone()) + }; + let _ = css.load_from_data(format!("* {{ background: {bg}; }}").as_bytes()); +} diff --git a/crates/lightgui-support/src/theme.rs b/crates/lightgui-support/src/theme.rs index 1fbf879..94334c9 100644 --- a/crates/lightgui-support/src/theme.rs +++ b/crates/lightgui-support/src/theme.rs @@ -1,5 +1,5 @@ // LightGUI code-view color themes — port of src/theme.cpp / src/theme.h. -// Four built-ins: Light, Dark, Monokai, Solarized Dark. Each theme +// Five built-ins: Light, Dark, Monokai, Solarized Dark, Dracula. Each theme // colours keywords, types, strings, comments, numbers, preprocessor // lines, plus the view background/foreground and the // current-execution-line background. @@ -72,6 +72,7 @@ impl CodeTheme { "Dark".to_string(), "Monokai".to_string(), "Solarized Dark".to_string(), + "Dracula".to_string(), ] } @@ -113,6 +114,18 @@ impl CodeTheme { preprocessor: "#cb4b16".to_string(), current_line: "#073642".to_string(), }, + "Dracula" => CodeTheme { + name: "Dracula".to_string(), + background: "#282a36".to_string(), + foreground: "#f8f8f2".to_string(), + keyword: "#ff79c6".to_string(), + typ: "#8be9fd".to_string(), + string: "#f1fa8c".to_string(), + comment: "#6272a4".to_string(), + number: "#bd93f9".to_string(), + preprocessor: "#ffb86c".to_string(), + current_line: "#44475a".to_string(), + }, _ => CodeTheme::default(), // Light, incl. unknown names } } @@ -853,14 +866,22 @@ mod tests { #[test] fn builtins() { let names = CodeTheme::builtin_names(); - assert_eq!(names, vec!["Light", "Dark", "Monokai", "Solarized Dark"]); + assert_eq!( + names, + vec!["Light", "Dark", "Monokai", "Solarized Dark", "Dracula"] + ); let light = CodeTheme::builtin("Light"); let dark = CodeTheme::builtin("Dark"); let mono = CodeTheme::builtin("Monokai"); let solar = CodeTheme::builtin("Solarized Dark"); + let drac = CodeTheme::builtin("Dracula"); assert!(light.keyword != dark.keyword); assert!(dark.keyword != mono.keyword); assert!(mono.keyword != solar.keyword); + assert!(solar.keyword != drac.keyword); + assert_eq!(drac.background, "#282a36"); + assert_eq!(drac.keyword, "#ff79c6"); + assert_eq!(drac.foreground, "#f8f8f2"); assert!(light.background != dark.background); assert!(dark.background != mono.background); assert_eq!(CodeTheme::builtin("Nope").name, "Light"); diff --git a/crates/lightgui-support/tests/theme_tests.rs b/crates/lightgui-support/tests/theme_tests.rs index 491262a..27e6dc5 100644 --- a/crates/lightgui-support/tests/theme_tests.rs +++ b/crates/lightgui-support/tests/theme_tests.rs @@ -85,24 +85,33 @@ fn theme_suite() { // 1. Built-in themes. let names = CodeTheme::builtin_names(); - ck.check(names.len() == 4, "builtins-count-4"); + ck.check(names.len() == 5, "builtins-count-5"); ck.check( names[0] == "Light" && names[1] == "Dark" && names[2] == "Monokai" - && names[3] == "Solarized Dark", + && names[3] == "Solarized Dark" + && names[4] == "Dracula", "builtins-names", ); let light = CodeTheme::builtin("Light"); let dark = CodeTheme::builtin("Dark"); let mono = CodeTheme::builtin("Monokai"); let solar = CodeTheme::builtin("Solarized Dark"); + let drac = CodeTheme::builtin("Dracula"); ck.check( light.keyword != dark.keyword && dark.keyword != mono.keyword - && mono.keyword != solar.keyword, + && mono.keyword != solar.keyword + && solar.keyword != drac.keyword, "builtins-distinct-keywords", ); + ck.check( + drac.background == "#282a36" + && drac.keyword == "#ff79c6" + && drac.foreground == "#f8f8f2", + "dracula-colours", + ); ck.check( light.background != dark.background && dark.background != mono.background, "builtins-distinct-backgrounds",