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.
This commit is contained in:
1 parent
a2fb4fd701
commit
aed11c9bd4
8 files changed
+116
-11
No files matched your search
Generated
+1
@@ -436,6 +436,7 @@ dependencies = [
|
||||
"gtk",
|
||||
"lightgui-core",
|
||||
"lightgui-gen",
|
||||
"lightgui-support",
|
||||
"pango",
|
||||
"serde_json",
|
||||
]
|
||||
|
||||
@@ -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"
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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::<gtk::MenuItem>("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::<gtk::CheckMenuItem>("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()).
|
||||
|
||||
@@ -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());
|
||||
}
|
||||
@@ -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");
|
||||
|
||||
@@ -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",
|
||||
|
||||
Reference in new issue
Block a user