From 5bafafa8b0a6727074a8507e911d9e2974348241 Mon Sep 17 00:00:00 2001 From: cnmcdee Date: Sun, 29 Nov 2026 06:02:19 +0000 Subject: [PATCH] Clean up clippy warnings; cargo fmt core+gen --- crates/lightgui-core/src/canonicalize.rs | 32 +- crates/lightgui-core/src/cnum.rs | 20 +- crates/lightgui-core/src/events.rs | 257 ++++- crates/lightgui-core/src/model.rs | 288 ++++- crates/lightgui-core/src/project_io.rs | 39 +- crates/lightgui-core/src/props.rs | 1026 +++++++++++++++-- .../lightgui-core/tests/project_io_tests.rs | 43 +- crates/lightgui-gen/src/chart_template.rs | 2 +- crates/lightgui-gen/src/gen.rs | 166 ++- crates/lightgui-gen/tests/fixtures_test.rs | 192 ++- 10 files changed, 1777 insertions(+), 288 deletions(-) diff --git a/crates/lightgui-core/src/canonicalize.rs b/crates/lightgui-core/src/canonicalize.rs index 6662276..f8ad9a3 100644 --- a/crates/lightgui-core/src/canonicalize.rs +++ b/crates/lightgui-core/src/canonicalize.rs @@ -96,10 +96,18 @@ mod tests { fn str_and_bool() { assert_eq!(canon("Button", "label", " hi "), Some("hi".to_string())); for good in ["true", "True", "1", "yes", "ON", " on "] { - assert_eq!(canon("Button", "focus-on-click", good), Some("true".to_string()), "{good}"); + assert_eq!( + canon("Button", "focus-on-click", good), + Some("true".to_string()), + "{good}" + ); } for good in ["false", "0", "no", "off"] { - assert_eq!(canon("Button", "focus-on-click", good), Some("false".to_string()), "{good}"); + assert_eq!( + canon("Button", "focus-on-click", good), + Some("false".to_string()), + "{good}" + ); } assert_eq!(canon("Button", "focus-on-click", "maybe"), None); assert_eq!(canon("Button", "focus-on-click", ""), None); @@ -118,7 +126,10 @@ mod tests { assert_eq!(canon("Label", "xalign", "0.25"), Some("0.25".to_string())); assert_eq!(canon("Label", "xalign", "1.5"), None); assert_eq!(canon("Label", "xalign", "1"), Some("1".to_string())); - assert_eq!(canon("Label", "xalign", "0.00001"), Some("1e-05".to_string())); + assert_eq!( + canon("Label", "xalign", "0.00001"), + Some("1e-05".to_string()) + ); // No range carried (lo == hi == 0): any value passes. let def = &props_for("Button")[0]; assert_eq!(canonicalize(def, "anything"), Some("anything".to_string())); @@ -126,8 +137,14 @@ mod tests { #[test] fn color() { - assert_eq!(canon("Shape", "fill-color", "#336699"), Some("rgb(51,102,153)".to_string())); - assert_eq!(canon("Shape", "fill-color", "white"), Some("rgb(255,255,255)".to_string())); + assert_eq!( + canon("Shape", "fill-color", "#336699"), + Some("rgb(51,102,153)".to_string()) + ); + assert_eq!( + canon("Shape", "fill-color", "white"), + Some("rgb(255,255,255)".to_string()) + ); assert_eq!( canon("Shape", "fill-color", "rgb(53,132,228)"), Some("rgb(53,132,228)".to_string()) @@ -144,7 +161,10 @@ mod tests { // Labels map case-insensitively to table values. assert_eq!(canon("Button", "relief", "Half"), Some("1".to_string())); assert_eq!(canon("Button", "relief", "NONE"), Some("2".to_string())); - assert_eq!(canon("Frame", "shadow-type", "Etched out"), Some("4".to_string())); + assert_eq!( + canon("Frame", "shadow-type", "Etched out"), + Some("4".to_string()) + ); assert_eq!(canon("Button", "relief", "Nope"), None); assert_eq!(canon("Button", "relief", "-"), None); } diff --git a/crates/lightgui-core/src/cnum.rs b/crates/lightgui-core/src/cnum.rs index 26f7147..afaf717 100644 --- a/crates/lightgui-core/src/cnum.rs +++ b/crates/lightgui-core/src/cnum.rs @@ -11,11 +11,19 @@ pub fn fmt_g(v: f64) -> String { return "nan".to_string(); } if v.is_infinite() { - return if v > 0.0 { "inf".to_string() } else { "-inf".to_string() }; + return if v > 0.0 { + "inf".to_string() + } else { + "-inf".to_string() + }; } if v == 0.0 { // C prints "-0" for negative zero. - return if v.is_sign_negative() { "-0".to_string() } else { "0".to_string() }; + return if v.is_sign_negative() { + "-0".to_string() + } else { + "0".to_string() + }; } // Rust's {:.5e} rounds to 6 significant digits, exactly like %g's // default precision; parse that and re-lay it out per %g's rules. @@ -92,7 +100,11 @@ fn parse_c_float_prefix(s: &str) -> Option<(f64, usize)> { } let rest = &s[i..]; let lower = rest.to_ascii_lowercase(); - for (word, val) in [("infinity", f64::INFINITY), ("inf", f64::INFINITY), ("nan", f64::NAN)] { + for (word, val) in [ + ("infinity", f64::INFINITY), + ("inf", f64::INFINITY), + ("nan", f64::NAN), + ] { if lower.starts_with(word) { let mut n = i + word.len(); // C strtod accepts nan(...) — consume the parenthesised part. @@ -292,7 +304,7 @@ mod tests { (123456.0, "123456"), (1234567.8, "1.23457e+06"), (-42.5, "-42.5"), - (3.14159265358979, "3.14159"), + (12345.6789, "12345.7"), (1000.0, "1000"), (0.000123456, "0.000123456"), (f64::NAN, "nan"), diff --git a/crates/lightgui-core/src/events.rs b/crates/lightgui-core/src/events.rs index 7800963..f37fd71 100644 --- a/crates/lightgui-core/src/events.rs +++ b/crates/lightgui-core/src/events.rs @@ -53,57 +53,248 @@ pub struct EventDef { pub builtin: bool, } - use EventKind as K; /// The connectable events for a widget type (`&[]` when it has none). pub fn events_for(widget_type: &str) -> &'static [EventDef] { match widget_type { - "Button" => &[EventDef { signal: "clicked", handler: "on_clicked", label: "Clicked", kind: K::Simple, builtin: false }], + "Button" => &[EventDef { + signal: "clicked", + handler: "on_clicked", + label: "Clicked", + kind: K::Simple, + builtin: false, + }], "Entry" => &[ - EventDef { signal: "changed", handler: "on_changed", label: "Text changed", kind: K::Simple, builtin: false }, - EventDef { signal: "activate", handler: "on_activate", label: "Enter pressed (activate)", kind: K::Simple, builtin: false }, + EventDef { + signal: "changed", + handler: "on_changed", + label: "Text changed", + kind: K::Simple, + builtin: false, + }, + EventDef { + signal: "activate", + handler: "on_activate", + label: "Enter pressed (activate)", + kind: K::Simple, + builtin: false, + }, ], - "Checkbutton" => &[EventDef { signal: "toggled", handler: "on_toggled", label: "Toggled", kind: K::Simple, builtin: false }], - "Radiobutton" => &[EventDef { signal: "toggled", handler: "on_toggled", label: "Toggled", kind: K::Simple, builtin: false }], - "Combobox" => &[EventDef { signal: "changed", handler: "on_changed", label: "Selection changed", kind: K::Simple, builtin: false }], - "Listbox" => &[EventDef { signal: "row-selected", handler: "on_row_selected", label: "Row selected", kind: K::RowSelected, builtin: false }], - "Menubutton" => &[EventDef { signal: "clicked", handler: "on_clicked", label: "Clicked", kind: K::Simple, builtin: false }], - "Canvas" => &[EventDef { signal: "draw", handler: "on_draw", label: "Draw (redraw)", kind: K::Draw, builtin: false }], - "Scale" => &[EventDef { signal: "value-changed", handler: "on_value_changed", label: "Value changed", kind: K::Simple, builtin: false }], - "Scrollbar" => &[EventDef { signal: "value-changed", handler: "on_value_changed", label: "Value changed", kind: K::Simple, builtin: false }], - "Text" => &[EventDef { signal: "changed", handler: "on_changed", label: "Text changed", kind: K::BufferChanged, builtin: false }], - "Toplevel" => &[EventDef { signal: "delete-event", handler: "on_delete_event", label: "Window close (delete-event)", kind: K::DeleteEvent, builtin: false }], - "Spreadsheet" => &[EventDef { signal: "changed", handler: "on_cell_changed", label: "Cell text changed", kind: K::CellChanged, builtin: false }], + "Checkbutton" => &[EventDef { + signal: "toggled", + handler: "on_toggled", + label: "Toggled", + kind: K::Simple, + builtin: false, + }], + "Radiobutton" => &[EventDef { + signal: "toggled", + handler: "on_toggled", + label: "Toggled", + kind: K::Simple, + builtin: false, + }], + "Combobox" => &[EventDef { + signal: "changed", + handler: "on_changed", + label: "Selection changed", + kind: K::Simple, + builtin: false, + }], + "Listbox" => &[EventDef { + signal: "row-selected", + handler: "on_row_selected", + label: "Row selected", + kind: K::RowSelected, + builtin: false, + }], + "Menubutton" => &[EventDef { + signal: "clicked", + handler: "on_clicked", + label: "Clicked", + kind: K::Simple, + builtin: false, + }], + "Canvas" => &[EventDef { + signal: "draw", + handler: "on_draw", + label: "Draw (redraw)", + kind: K::Draw, + builtin: false, + }], + "Scale" => &[EventDef { + signal: "value-changed", + handler: "on_value_changed", + label: "Value changed", + kind: K::Simple, + builtin: false, + }], + "Scrollbar" => &[EventDef { + signal: "value-changed", + handler: "on_value_changed", + label: "Value changed", + kind: K::Simple, + builtin: false, + }], + "Text" => &[EventDef { + signal: "changed", + handler: "on_changed", + label: "Text changed", + kind: K::BufferChanged, + builtin: false, + }], + "Toplevel" => &[EventDef { + signal: "delete-event", + handler: "on_delete_event", + label: "Window close (delete-event)", + kind: K::DeleteEvent, + builtin: false, + }], + "Spreadsheet" => &[EventDef { + signal: "changed", + handler: "on_cell_changed", + label: "Cell text changed", + kind: K::CellChanged, + builtin: false, + }], // Draw, wheel zoom, drag pan and the drawing tools are already // wired by the generator (on_draw, on_chart_scroll, on_chart_press, // on_chart_motion, on_chart_release, on_chart_key). - "StockChart" => &[EventDef { signal: "draw", handler: "on_draw", label: "Draw (redraw)", kind: K::Draw, builtin: true }], - "Memo" => &[EventDef { signal: "changed", handler: "on_changed", label: "Text changed", kind: K::BufferChanged, builtin: false }], - "Shape" => &[EventDef { signal: "draw", handler: "on_draw", label: "Draw (redraw)", kind: K::Draw, builtin: true }], - "Notebook" => &[EventDef { signal: "switch-page", handler: "on_switch_page", label: "Page switched", kind: K::SwitchPage, builtin: false }], - "ToolBar" => &[EventDef { signal: "clicked", handler: "on_clicked", label: "Tool button clicked", kind: K::ItemClicked, builtin: false }], - "MenuBar" => &[EventDef { signal: "activate", handler: "on_activate", label: "Menu item activated", kind: K::ItemActivate, builtin: false }], - "SpinEdit" => &[EventDef { signal: "value-changed", handler: "on_value_changed", label: "Value changed", kind: K::Simple, builtin: false }], - "TrackBar" => &[EventDef { signal: "value-changed", handler: "on_value_changed", label: "Value changed", kind: K::Simple, builtin: false }], + "StockChart" => &[EventDef { + signal: "draw", + handler: "on_draw", + label: "Draw (redraw)", + kind: K::Draw, + builtin: true, + }], + "Memo" => &[EventDef { + signal: "changed", + handler: "on_changed", + label: "Text changed", + kind: K::BufferChanged, + builtin: false, + }], + "Shape" => &[EventDef { + signal: "draw", + handler: "on_draw", + label: "Draw (redraw)", + kind: K::Draw, + builtin: true, + }], + "Notebook" => &[EventDef { + signal: "switch-page", + handler: "on_switch_page", + label: "Page switched", + kind: K::SwitchPage, + builtin: false, + }], + "ToolBar" => &[EventDef { + signal: "clicked", + handler: "on_clicked", + label: "Tool button clicked", + kind: K::ItemClicked, + builtin: false, + }], + "MenuBar" => &[EventDef { + signal: "activate", + handler: "on_activate", + label: "Menu item activated", + kind: K::ItemActivate, + builtin: false, + }], + "SpinEdit" => &[EventDef { + signal: "value-changed", + handler: "on_value_changed", + label: "Value changed", + kind: K::Simple, + builtin: false, + }], + "TrackBar" => &[EventDef { + signal: "value-changed", + handler: "on_value_changed", + label: "Value changed", + kind: K::Simple, + builtin: false, + }], "DateTimePicker" => &[ - EventDef { signal: "day-selected", handler: "on_day_selected", label: "Day selected", kind: K::Simple, builtin: false }, - EventDef { signal: "month-changed", handler: "on_month_changed", label: "Month changed", kind: K::Simple, builtin: false }, + EventDef { + signal: "day-selected", + handler: "on_day_selected", + label: "Day selected", + kind: K::Simple, + builtin: false, + }, + EventDef { + signal: "month-changed", + handler: "on_month_changed", + label: "Month changed", + kind: K::Simple, + builtin: false, + }, ], - "ColorBox" => &[EventDef { signal: "color-set", handler: "on_color_set", label: "Colour chosen", kind: K::Simple, builtin: false }], + "ColorBox" => &[EventDef { + signal: "color-set", + handler: "on_color_set", + label: "Colour chosen", + kind: K::Simple, + builtin: false, + }], "ListView" => &[ - EventDef { signal: "row-activated", handler: "on_row_activated", label: "Row activated (double-click)", kind: K::RowActivated, builtin: false }, - EventDef { signal: "cursor-changed", handler: "on_cursor_changed", label: "Cursor moved", kind: K::Simple, builtin: false }, + EventDef { + signal: "row-activated", + handler: "on_row_activated", + label: "Row activated (double-click)", + kind: K::RowActivated, + builtin: false, + }, + EventDef { + signal: "cursor-changed", + handler: "on_cursor_changed", + label: "Cursor moved", + kind: K::Simple, + builtin: false, + }, ], "TreeView" => &[ - EventDef { signal: "row-activated", handler: "on_row_activated", label: "Row activated (double-click)", kind: K::RowActivated, builtin: false }, - EventDef { signal: "cursor-changed", handler: "on_cursor_changed", label: "Cursor moved", kind: K::Simple, builtin: false }, + EventDef { + signal: "row-activated", + handler: "on_row_activated", + label: "Row activated (double-click)", + kind: K::RowActivated, + builtin: false, + }, + EventDef { + signal: "cursor-changed", + handler: "on_cursor_changed", + label: "Cursor moved", + kind: K::Simple, + builtin: false, + }, ], "StringGrid" => &[ - EventDef { signal: "row-activated", handler: "on_row_activated", label: "Row activated (double-click)", kind: K::RowActivated, builtin: false }, - EventDef { signal: "cursor-changed", handler: "on_cursor_changed", label: "Cursor moved", kind: K::Simple, builtin: false }, + EventDef { + signal: "row-activated", + handler: "on_row_activated", + label: "Row activated (double-click)", + kind: K::RowActivated, + builtin: false, + }, + EventDef { + signal: "cursor-changed", + handler: "on_cursor_changed", + label: "Cursor moved", + kind: K::Simple, + builtin: false, + }, ], - "Plot" => &[EventDef { signal: "draw", handler: "on_draw", label: "Draw (redraw)", kind: K::Draw, builtin: true }], + "Plot" => &[EventDef { + signal: "draw", + handler: "on_draw", + label: "Draw (redraw)", + kind: K::Draw, + builtin: true, + }], // Label, Frame, GroupBox, Panel, Image, ScrollBox, Splitter, // StatusBar, ProgressBar — see no_events_note(). _ => &[], diff --git a/crates/lightgui-core/src/model.rs b/crates/lightgui-core/src/model.rs index bbc6e53..3100163 100644 --- a/crates/lightgui-core/src/model.rs +++ b/crates/lightgui-core/src/model.rs @@ -99,42 +99,258 @@ pub struct TypeInfo { /// The 36 palette types, in the C++ `kTypes` order. pub const KTYPES: &[TypeInfo] = &[ - TypeInfo { key: "Button", palette_id: "pal_button", def_width: 100, def_height: 30, phase1: true }, - TypeInfo { key: "Label", palette_id: "pal_label", def_width: 100, def_height: 24, phase1: true }, - TypeInfo { key: "Entry", palette_id: "pal_entry", def_width: 160, def_height: 28, phase1: true }, - TypeInfo { key: "Checkbutton", palette_id: "pal_checkbutton", def_width: 120, def_height: 26, phase1: true }, - TypeInfo { key: "Radiobutton", palette_id: "pal_radiobutton", def_width: 120, def_height: 26, phase1: true }, - TypeInfo { key: "Frame", palette_id: "pal_frame", def_width: 200, def_height: 120, phase1: true }, - TypeInfo { key: "Combobox", palette_id: "pal_combobox", def_width: 160, def_height: 28, phase1: true }, - TypeInfo { key: "Listbox", palette_id: "pal_listbox", def_width: 180, def_height: 110, phase1: true }, - TypeInfo { key: "Menubutton", palette_id: "pal_menubutton", def_width: 110, def_height: 30, phase1: true }, - TypeInfo { key: "Canvas", palette_id: "pal_canvas", def_width: 200, def_height: 140, phase1: true }, - TypeInfo { key: "Scale", palette_id: "pal_scale", def_width: 160, def_height: 28, phase1: true }, - TypeInfo { key: "Scrollbar", palette_id: "pal_scrollbar", def_width: 160, def_height: 18, phase1: true }, - TypeInfo { key: "Text", palette_id: "pal_text", def_width: 220, def_height: 120, phase1: true }, - TypeInfo { key: "Toplevel", palette_id: "pal_toplevel", def_width: 300, def_height: 200, phase1: true }, - TypeInfo { key: "Spreadsheet", palette_id: "pal_spreadsheet", def_width: 320, def_height: 180, phase1: true }, - TypeInfo { key: "StockChart", palette_id: "pal_stockchart", def_width: 320, def_height: 200, phase1: true }, - TypeInfo { key: "Memo", palette_id: "pal_memo", def_width: 220, def_height: 120, phase1: true }, - TypeInfo { key: "GroupBox", palette_id: "pal_groupbox", def_width: 200, def_height: 120, phase1: true }, - TypeInfo { key: "Panel", palette_id: "pal_panel", def_width: 200, def_height: 120, phase1: true }, - TypeInfo { key: "Image", palette_id: "pal_image", def_width: 160, def_height: 120, phase1: true }, - TypeInfo { key: "Shape", palette_id: "pal_shape", def_width: 160, def_height: 120, phase1: true }, - TypeInfo { key: "Notebook", palette_id: "pal_notebook", def_width: 320, def_height: 200, phase1: true }, - TypeInfo { key: "ScrollBox", palette_id: "pal_scrollbox", def_width: 240, def_height: 160, phase1: true }, - TypeInfo { key: "Splitter", palette_id: "pal_splitter", def_width: 280, def_height: 160, phase1: true }, - TypeInfo { key: "StatusBar", palette_id: "pal_statusbar", def_width: 320, def_height: 24, phase1: true }, - TypeInfo { key: "ToolBar", palette_id: "pal_toolbar", def_width: 320, def_height: 36, phase1: true }, - TypeInfo { key: "MenuBar", palette_id: "pal_menubar", def_width: 320, def_height: 28, phase1: true }, - TypeInfo { key: "SpinEdit", palette_id: "pal_spinedit", def_width: 120, def_height: 28, phase1: true }, - TypeInfo { key: "TrackBar", palette_id: "pal_trackbar", def_width: 160, def_height: 28, phase1: true }, - TypeInfo { key: "ProgressBar", palette_id: "pal_progressbar", def_width: 200, def_height: 24, phase1: true }, - TypeInfo { key: "DateTimePicker", palette_id: "pal_datetimepicker", def_width: 240, def_height: 200, phase1: true }, - TypeInfo { key: "ColorBox", palette_id: "pal_colorbox", def_width: 120, def_height: 28, phase1: true }, - TypeInfo { key: "ListView", palette_id: "pal_listview", def_width: 240, def_height: 140, phase1: true }, - TypeInfo { key: "TreeView", palette_id: "pal_treeview", def_width: 240, def_height: 140, phase1: true }, - TypeInfo { key: "StringGrid", palette_id: "pal_stringgrid", def_width: 280, def_height: 160, phase1: true }, - TypeInfo { key: "Plot", palette_id: "pal_plot", def_width: 280, def_height: 180, phase1: true }, + TypeInfo { + key: "Button", + palette_id: "pal_button", + def_width: 100, + def_height: 30, + phase1: true, + }, + TypeInfo { + key: "Label", + palette_id: "pal_label", + def_width: 100, + def_height: 24, + phase1: true, + }, + TypeInfo { + key: "Entry", + palette_id: "pal_entry", + def_width: 160, + def_height: 28, + phase1: true, + }, + TypeInfo { + key: "Checkbutton", + palette_id: "pal_checkbutton", + def_width: 120, + def_height: 26, + phase1: true, + }, + TypeInfo { + key: "Radiobutton", + palette_id: "pal_radiobutton", + def_width: 120, + def_height: 26, + phase1: true, + }, + TypeInfo { + key: "Frame", + palette_id: "pal_frame", + def_width: 200, + def_height: 120, + phase1: true, + }, + TypeInfo { + key: "Combobox", + palette_id: "pal_combobox", + def_width: 160, + def_height: 28, + phase1: true, + }, + TypeInfo { + key: "Listbox", + palette_id: "pal_listbox", + def_width: 180, + def_height: 110, + phase1: true, + }, + TypeInfo { + key: "Menubutton", + palette_id: "pal_menubutton", + def_width: 110, + def_height: 30, + phase1: true, + }, + TypeInfo { + key: "Canvas", + palette_id: "pal_canvas", + def_width: 200, + def_height: 140, + phase1: true, + }, + TypeInfo { + key: "Scale", + palette_id: "pal_scale", + def_width: 160, + def_height: 28, + phase1: true, + }, + TypeInfo { + key: "Scrollbar", + palette_id: "pal_scrollbar", + def_width: 160, + def_height: 18, + phase1: true, + }, + TypeInfo { + key: "Text", + palette_id: "pal_text", + def_width: 220, + def_height: 120, + phase1: true, + }, + TypeInfo { + key: "Toplevel", + palette_id: "pal_toplevel", + def_width: 300, + def_height: 200, + phase1: true, + }, + TypeInfo { + key: "Spreadsheet", + palette_id: "pal_spreadsheet", + def_width: 320, + def_height: 180, + phase1: true, + }, + TypeInfo { + key: "StockChart", + palette_id: "pal_stockchart", + def_width: 320, + def_height: 200, + phase1: true, + }, + TypeInfo { + key: "Memo", + palette_id: "pal_memo", + def_width: 220, + def_height: 120, + phase1: true, + }, + TypeInfo { + key: "GroupBox", + palette_id: "pal_groupbox", + def_width: 200, + def_height: 120, + phase1: true, + }, + TypeInfo { + key: "Panel", + palette_id: "pal_panel", + def_width: 200, + def_height: 120, + phase1: true, + }, + TypeInfo { + key: "Image", + palette_id: "pal_image", + def_width: 160, + def_height: 120, + phase1: true, + }, + TypeInfo { + key: "Shape", + palette_id: "pal_shape", + def_width: 160, + def_height: 120, + phase1: true, + }, + TypeInfo { + key: "Notebook", + palette_id: "pal_notebook", + def_width: 320, + def_height: 200, + phase1: true, + }, + TypeInfo { + key: "ScrollBox", + palette_id: "pal_scrollbox", + def_width: 240, + def_height: 160, + phase1: true, + }, + TypeInfo { + key: "Splitter", + palette_id: "pal_splitter", + def_width: 280, + def_height: 160, + phase1: true, + }, + TypeInfo { + key: "StatusBar", + palette_id: "pal_statusbar", + def_width: 320, + def_height: 24, + phase1: true, + }, + TypeInfo { + key: "ToolBar", + palette_id: "pal_toolbar", + def_width: 320, + def_height: 36, + phase1: true, + }, + TypeInfo { + key: "MenuBar", + palette_id: "pal_menubar", + def_width: 320, + def_height: 28, + phase1: true, + }, + TypeInfo { + key: "SpinEdit", + palette_id: "pal_spinedit", + def_width: 120, + def_height: 28, + phase1: true, + }, + TypeInfo { + key: "TrackBar", + palette_id: "pal_trackbar", + def_width: 160, + def_height: 28, + phase1: true, + }, + TypeInfo { + key: "ProgressBar", + palette_id: "pal_progressbar", + def_width: 200, + def_height: 24, + phase1: true, + }, + TypeInfo { + key: "DateTimePicker", + palette_id: "pal_datetimepicker", + def_width: 240, + def_height: 200, + phase1: true, + }, + TypeInfo { + key: "ColorBox", + palette_id: "pal_colorbox", + def_width: 120, + def_height: 28, + phase1: true, + }, + TypeInfo { + key: "ListView", + palette_id: "pal_listview", + def_width: 240, + def_height: 140, + phase1: true, + }, + TypeInfo { + key: "TreeView", + palette_id: "pal_treeview", + def_width: 240, + def_height: 140, + phase1: true, + }, + TypeInfo { + key: "StringGrid", + palette_id: "pal_stringgrid", + def_width: 280, + def_height: 160, + phase1: true, + }, + TypeInfo { + key: "Plot", + palette_id: "pal_plot", + def_width: 280, + def_height: 180, + phase1: true, + }, ]; /// Find a palette type by key, or `None` when unknown. diff --git a/crates/lightgui-core/src/project_io.rs b/crates/lightgui-core/src/project_io.rs index 9c5aa45..4b69118 100644 --- a/crates/lightgui-core/src/project_io.rs +++ b/crates/lightgui-core/src/project_io.rs @@ -52,18 +52,30 @@ pub fn save_to_string(p: &Project) -> String { o.push_str(" \"version\": 1,\n"); o.push_str(" \"window\": {\n"); o.push_str(&format!(" \"title\": \"{}\",\n", json_escape(&p.title))); - o.push_str(&format!(" \"class_name\": \"{}\",\n", json_escape(&p.class_name))); + o.push_str(&format!( + " \"class_name\": \"{}\",\n", + json_escape(&p.class_name) + )); o.push_str(&format!(" \"width\": {},\n", p.win_width)); o.push_str(&format!(" \"height\": {},\n", p.win_height)); - o.push_str(&format!(" \"background\": \"{}\",\n", json_escape(&p.background))); - o.push_str(&format!(" \"menubar\": \"{}\"\n", json_escape(&p.menubar))); + o.push_str(&format!( + " \"background\": \"{}\",\n", + json_escape(&p.background) + )); + o.push_str(&format!( + " \"menubar\": \"{}\"\n", + json_escape(&p.menubar) + )); o.push_str(" },\n"); o.push_str(" \"widgets\": ["); for (i, w) in p.widgets.iter().enumerate() { o.push_str(if i == 0 { "\n" } else { ",\n" }); o.push_str(" {\n"); o.push_str(&format!(" \"name\": \"{}\",\n", json_escape(&w.name))); - o.push_str(&format!(" \"type\": \"{}\",\n", json_escape(&w.widget_type))); + o.push_str(&format!( + " \"type\": \"{}\",\n", + json_escape(&w.widget_type) + )); o.push_str(&format!( " \"x\": {}, \"y\": {}, \"width\": {}, \"height\": {},\n", w.x, w.y, w.width, w.height @@ -72,7 +84,11 @@ pub fn save_to_string(p: &Project) -> String { let mut first = true; for (k, v) in &w.props { o.push_str(if first { "" } else { "," }); - o.push_str(&format!("\n \"{}\": \"{}\"", json_escape(k), json_escape(v))); + o.push_str(&format!( + "\n \"{}\": \"{}\"", + json_escape(k), + json_escape(v) + )); first = false; } o.push_str(if w.props.is_empty() { "" } else { "\n " }); @@ -111,8 +127,8 @@ fn jint(j: Option<&serde_json::Value>, dflt: i32) -> i32 { /// Parse `.fdj` text with the C++ loader's semantics. `path` is used /// only in error messages, mirroring the C++ strings. pub fn load_from_str(text: &str, path: &str) -> Result { - let root: serde_json::Value = serde_json::from_str(text) - .map_err(|e| format!("invalid JSON in {path}: {e}"))?; + let root: serde_json::Value = + serde_json::from_str(text).map_err(|e| format!("invalid JSON in {path}: {e}"))?; if jstr(root.get("format"), "") != "lightgui" { return Err(format!("{path} is not an lightgui project file")); } @@ -186,8 +202,8 @@ pub fn save_project_file(path: &Path, project: &Project) -> Result<(), String> { /// Load a project file. Mirrors `load_project_file()`. pub fn load_project_file(path: &Path) -> Result { - let text = std::fs::read_to_string(path) - .map_err(|_| format!("cannot open {}", path.display()))?; + let text = + std::fs::read_to_string(path).map_err(|_| format!("cannot open {}", path.display()))?; load_from_str(&text, &path.display().to_string()) } @@ -198,7 +214,10 @@ mod tests { #[test] fn escape_matches_cpp() { assert_eq!(json_escape("a\"b\\c\nd"), "a\\\"b\\\\c\\nd"); - assert_eq!(json_escape("\u{1}\u{8}\u{c}\u{1f}"), "\\u0001\\u0008\\u000c\\u001f"); + assert_eq!( + json_escape("\u{1}\u{8}\u{c}\u{1f}"), + "\\u0001\\u0008\\u000c\\u001f" + ); assert_eq!(json_escape("café — ☕"), "café — ☕"); assert_eq!(json_escape("\t\r"), "\\t\\r"); } diff --git a/crates/lightgui-core/src/props.rs b/crates/lightgui-core/src/props.rs index 2a49cc1..74e1db0 100644 --- a/crates/lightgui-core/src/props.rs +++ b/crates/lightgui-core/src/props.rs @@ -31,7 +31,6 @@ pub struct PropDef { pub enum_values: Option<&'static [i32]>, } - // Enum value tables (GTK 3 constants, written out so this module stays // independent of the GTK headers). pub const RELIEF_VALUES: &[i32] = &[0, 1, 2]; // NORMAL, HALF, NONE @@ -49,9 +48,33 @@ use PropKind as K; /// Properties every widget gets. pub fn common_props() -> &'static [PropDef] { &[ - PropDef { name: "background", kind: K::Color, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "foreground", kind: K::Color, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "tooltip-text", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "background", + kind: K::Color, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "foreground", + kind: K::Color, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "tooltip-text", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ] } @@ -60,163 +83,955 @@ pub fn common_props() -> &'static [PropDef] { pub fn props_for(widget_type: &str) -> &'static [PropDef] { match widget_type { "Button" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "relief", kind: K::Enum, def: "0", lo: 0.0, hi: 0.0, enum_labels: Some("Normal|Half|None"), enum_values: Some(RELIEF_VALUES) }, - PropDef { name: "focus-on-click", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "relief", + kind: K::Enum, + def: "0", + lo: 0.0, + hi: 0.0, + enum_labels: Some("Normal|Half|None"), + enum_values: Some(RELIEF_VALUES), + }, + PropDef { + name: "focus-on-click", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Label" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "xalign", kind: K::Double, def: "0.5", lo: 0.0, hi: 1.0, enum_labels: None, enum_values: None }, - PropDef { name: "selectable", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "wrap", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "xalign", + kind: K::Double, + def: "0.5", + lo: 0.0, + hi: 1.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "selectable", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "wrap", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Entry" => &[ - PropDef { name: "text", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "placeholder-text", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "max-length", kind: K::Int, def: "0", lo: 0.0, hi: 100000.0, enum_labels: None, enum_values: None }, - PropDef { name: "width-chars", kind: K::Int, def: "-1", lo: -1.0, hi: 1000.0, enum_labels: None, enum_values: None }, - PropDef { name: "editable", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "visibility", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "text", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "placeholder-text", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "max-length", + kind: K::Int, + def: "0", + lo: 0.0, + hi: 100000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "width-chars", + kind: K::Int, + def: "-1", + lo: -1.0, + hi: 1000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "editable", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "visibility", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Checkbutton" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "active", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "inconsistent", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "active", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "inconsistent", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Radiobutton" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "active", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "active", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Frame" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "shadow-type", kind: K::Enum, def: "3", lo: 0.0, hi: 0.0, enum_labels: Some("None|In|Out|Etched in|Etched out"), enum_values: Some(SHADOW_VALUES) }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "shadow-type", + kind: K::Enum, + def: "3", + lo: 0.0, + hi: 0.0, + enum_labels: Some("None|In|Out|Etched in|Etched out"), + enum_values: Some(SHADOW_VALUES), + }, ], - "Combobox" => &[PropDef { name: "active", kind: K::Int, def: "0", lo: -1.0, hi: 1000.0, enum_labels: None, enum_values: None }], - "Listbox" => &[PropDef { name: "selection-mode", kind: K::Enum, def: "1", lo: 0.0, hi: 0.0, enum_labels: Some("None|Single|Browse|Multiple"), enum_values: Some(SEL_MODE_VALUES) }], + "Combobox" => &[PropDef { + name: "active", + kind: K::Int, + def: "0", + lo: -1.0, + hi: 1000.0, + enum_labels: None, + enum_values: None, + }], + "Listbox" => &[PropDef { + name: "selection-mode", + kind: K::Enum, + def: "1", + lo: 0.0, + hi: 0.0, + enum_labels: Some("None|Single|Browse|Multiple"), + enum_values: Some(SEL_MODE_VALUES), + }], "Menubutton" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "direction", kind: K::Enum, def: "1", lo: 0.0, hi: 0.0, enum_labels: Some("Up|Down|Left|Right|None"), enum_values: Some(ARROW_VALUES) }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "direction", + kind: K::Enum, + def: "1", + lo: 0.0, + hi: 0.0, + enum_labels: Some("Up|Down|Left|Right|None"), + enum_values: Some(ARROW_VALUES), + }, ], "Scale" => &[ - PropDef { name: "value", kind: K::Double, def: "0", lo: 0.0, hi: 100.0, enum_labels: None, enum_values: None }, - PropDef { name: "digits", kind: K::Int, def: "1", lo: -1.0, hi: 10.0, enum_labels: None, enum_values: None }, - PropDef { name: "draw-value", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "inverted", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "value", + kind: K::Double, + def: "0", + lo: 0.0, + hi: 100.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "digits", + kind: K::Int, + def: "1", + lo: -1.0, + hi: 10.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "draw-value", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "inverted", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Scrollbar" => &[ - PropDef { name: "value", kind: K::Double, def: "0", lo: 0.0, hi: 100.0, enum_labels: None, enum_values: None }, - PropDef { name: "inverted", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "value", + kind: K::Double, + def: "0", + lo: 0.0, + hi: 100.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "inverted", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Text" => &[ - PropDef { name: "editable", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "cursor-visible", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "wrap-mode", kind: K::Enum, def: "0", lo: 0.0, hi: 0.0, enum_labels: Some("None|Char|Word|Word-char"), enum_values: Some(WRAP_VALUES) }, + PropDef { + name: "editable", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "cursor-visible", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "wrap-mode", + kind: K::Enum, + def: "0", + lo: 0.0, + hi: 0.0, + enum_labels: Some("None|Char|Word|Word-char"), + enum_values: Some(WRAP_VALUES), + }, ], "Memo" => &[ - PropDef { name: "text", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "editable", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "wrap-mode", kind: K::Enum, def: "0", lo: 0.0, hi: 0.0, enum_labels: Some("None|Char|Word|Word-char"), enum_values: Some(WRAP_VALUES) }, + PropDef { + name: "text", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "editable", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "wrap-mode", + kind: K::Enum, + def: "0", + lo: 0.0, + hi: 0.0, + enum_labels: Some("None|Char|Word|Word-char"), + enum_values: Some(WRAP_VALUES), + }, ], "GroupBox" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "shadow-type", kind: K::Enum, def: "3", lo: 0.0, hi: 0.0, enum_labels: Some("None|In|Out|Etched in|Etched out"), enum_values: Some(SHADOW_VALUES) }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "shadow-type", + kind: K::Enum, + def: "3", + lo: 0.0, + hi: 0.0, + enum_labels: Some("None|In|Out|Etched in|Etched out"), + enum_values: Some(SHADOW_VALUES), + }, ], "Panel" => &[ - PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "shadow-type", kind: K::Enum, def: "3", lo: 0.0, hi: 0.0, enum_labels: Some("None|In|Out|Etched in|Etched out"), enum_values: Some(SHADOW_VALUES) }, + PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "shadow-type", + kind: K::Enum, + def: "3", + lo: 0.0, + hi: 0.0, + enum_labels: Some("None|In|Out|Etched in|Etched out"), + enum_values: Some(SHADOW_VALUES), + }, ], "Image" => &[ - PropDef { name: "file", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "pixel-size", kind: K::Int, def: "-1", lo: -1.0, hi: 512.0, enum_labels: None, enum_values: None }, + PropDef { + name: "file", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "pixel-size", + kind: K::Int, + def: "-1", + lo: -1.0, + hi: 512.0, + enum_labels: None, + enum_values: None, + }, ], "Shape" => &[ - PropDef { name: "shape", kind: K::Enum, def: "0", lo: 0.0, hi: 0.0, enum_labels: Some("Rectangle|Ellipse|Line|Triangle"), enum_values: Some(SHAPE_VALUES) }, - PropDef { name: "fill-color", kind: K::Color, def: "rgb(53,132,228)", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "shape", + kind: K::Enum, + def: "0", + lo: 0.0, + hi: 0.0, + enum_labels: Some("Rectangle|Ellipse|Line|Triangle"), + enum_values: Some(SHAPE_VALUES), + }, + PropDef { + name: "fill-color", + kind: K::Color, + def: "rgb(53,132,228)", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Notebook" => &[ - PropDef { name: "pages", kind: K::Str, def: "Tab 1|Tab 2|Tab 3", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "page", kind: K::Int, def: "0", lo: 0.0, hi: 100.0, enum_labels: None, enum_values: None }, + PropDef { + name: "pages", + kind: K::Str, + def: "Tab 1|Tab 2|Tab 3", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "page", + kind: K::Int, + def: "0", + lo: 0.0, + hi: 100.0, + enum_labels: None, + enum_values: None, + }, ], - "ScrollBox" => &[PropDef { name: "label", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }], + "ScrollBox" => &[PropDef { + name: "label", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }], "Splitter" => &[ - PropDef { name: "position", kind: K::Int, def: "100", lo: 0.0, hi: 10000.0, enum_labels: None, enum_values: None }, - PropDef { name: "orientation", kind: K::Enum, def: "0", lo: 0.0, hi: 0.0, enum_labels: Some("Horizontal|Vertical"), enum_values: Some(ORIENTATION_VALUES) }, + PropDef { + name: "position", + kind: K::Int, + def: "100", + lo: 0.0, + hi: 10000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "orientation", + kind: K::Enum, + def: "0", + lo: 0.0, + hi: 0.0, + enum_labels: Some("Horizontal|Vertical"), + enum_values: Some(ORIENTATION_VALUES), + }, ], - "StatusBar" => &[PropDef { name: "text", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }], + "StatusBar" => &[PropDef { + name: "text", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }], "ToolBar" => &[ - PropDef { name: "items", kind: K::Str, def: "New|Open|Save", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "toolbar-style", kind: K::Enum, def: "2", lo: 0.0, hi: 0.0, enum_labels: Some("Icons|Text|Both|Both horizontal"), enum_values: Some(TOOLBAR_STYLE_VALUES) }, + PropDef { + name: "items", + kind: K::Str, + def: "New|Open|Save", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "toolbar-style", + kind: K::Enum, + def: "2", + lo: 0.0, + hi: 0.0, + enum_labels: Some("Icons|Text|Both|Both horizontal"), + enum_values: Some(TOOLBAR_STYLE_VALUES), + }, ], - "MenuBar" => &[PropDef { name: "menus", kind: K::Str, def: "File|Edit|Help", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }], + "MenuBar" => &[PropDef { + name: "menus", + kind: K::Str, + def: "File|Edit|Help", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }], "SpinEdit" => &[ - PropDef { name: "minimum", kind: K::Int, def: "0", lo: -1000000.0, hi: 1000000.0, enum_labels: None, enum_values: None }, - PropDef { name: "maximum", kind: K::Int, def: "100", lo: -1000000.0, hi: 1000000.0, enum_labels: None, enum_values: None }, - PropDef { name: "step", kind: K::Int, def: "1", lo: 1.0, hi: 1000.0, enum_labels: None, enum_values: None }, - PropDef { name: "value", kind: K::Double, def: "0", lo: -1000000.0, hi: 1000000.0, enum_labels: None, enum_values: None }, - PropDef { name: "digits", kind: K::Int, def: "0", lo: 0.0, hi: 10.0, enum_labels: None, enum_values: None }, - PropDef { name: "numeric", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "wrap", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "minimum", + kind: K::Int, + def: "0", + lo: -1000000.0, + hi: 1000000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "maximum", + kind: K::Int, + def: "100", + lo: -1000000.0, + hi: 1000000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "step", + kind: K::Int, + def: "1", + lo: 1.0, + hi: 1000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "value", + kind: K::Double, + def: "0", + lo: -1000000.0, + hi: 1000000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "digits", + kind: K::Int, + def: "0", + lo: 0.0, + hi: 10.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "numeric", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "wrap", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "TrackBar" => &[ - PropDef { name: "minimum", kind: K::Int, def: "0", lo: -1000000.0, hi: 1000000.0, enum_labels: None, enum_values: None }, - PropDef { name: "maximum", kind: K::Int, def: "100", lo: -1000000.0, hi: 1000000.0, enum_labels: None, enum_values: None }, - PropDef { name: "step", kind: K::Int, def: "1", lo: 1.0, hi: 1000.0, enum_labels: None, enum_values: None }, - PropDef { name: "value", kind: K::Double, def: "0", lo: 0.0, hi: 100.0, enum_labels: None, enum_values: None }, - PropDef { name: "digits", kind: K::Int, def: "1", lo: 0.0, hi: 10.0, enum_labels: None, enum_values: None }, - PropDef { name: "draw-value", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "inverted", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "orientation", kind: K::Enum, def: "0", lo: 0.0, hi: 0.0, enum_labels: Some("Horizontal|Vertical"), enum_values: Some(ORIENTATION_VALUES) }, + PropDef { + name: "minimum", + kind: K::Int, + def: "0", + lo: -1000000.0, + hi: 1000000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "maximum", + kind: K::Int, + def: "100", + lo: -1000000.0, + hi: 1000000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "step", + kind: K::Int, + def: "1", + lo: 1.0, + hi: 1000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "value", + kind: K::Double, + def: "0", + lo: 0.0, + hi: 100.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "digits", + kind: K::Int, + def: "1", + lo: 0.0, + hi: 10.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "draw-value", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "inverted", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "orientation", + kind: K::Enum, + def: "0", + lo: 0.0, + hi: 0.0, + enum_labels: Some("Horizontal|Vertical"), + enum_values: Some(ORIENTATION_VALUES), + }, ], "ProgressBar" => &[ - PropDef { name: "fraction", kind: K::Double, def: "0", lo: 0.0, hi: 1.0, enum_labels: None, enum_values: None }, - PropDef { name: "show-text", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "text", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "fraction", + kind: K::Double, + def: "0", + lo: 0.0, + hi: 1.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "show-text", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "text", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "DateTimePicker" => &[ - PropDef { name: "date", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "show-heading", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "show-day-names", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "date", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "show-heading", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "show-day-names", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "ColorBox" => &[ - PropDef { name: "color", kind: K::Color, def: "rgb(53,132,228)", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "use-alpha", kind: K::Bool, def: "false", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "title", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "color", + kind: K::Color, + def: "rgb(53,132,228)", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "use-alpha", + kind: K::Bool, + def: "false", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "title", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "ListView" => &[ - PropDef { name: "columns", kind: K::Str, def: "Name|Value", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "items", kind: K::Str, def: "Row 1,Value 1;Row 2,Value 2", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "headers-visible", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "columns", + kind: K::Str, + def: "Name|Value", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "items", + kind: K::Str, + def: "Row 1,Value 1;Row 2,Value 2", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "headers-visible", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "TreeView" => &[ - PropDef { name: "items", kind: K::Str, def: "Root/Child 1;Root/Child 2;Other", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "headers-visible", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "items", + kind: K::Str, + def: "Root/Child 1;Root/Child 2;Other", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "headers-visible", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "StringGrid" => &[ - PropDef { name: "rows", kind: K::Int, def: "5", lo: 1.0, hi: 50.0, enum_labels: None, enum_values: None }, - PropDef { name: "columns", kind: K::Int, def: "4", lo: 1.0, hi: 26.0, enum_labels: None, enum_values: None }, - PropDef { name: "headers-visible", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "rows", + kind: K::Int, + def: "5", + lo: 1.0, + hi: 50.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "columns", + kind: K::Int, + def: "4", + lo: 1.0, + hi: 26.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "headers-visible", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Plot" => &[ - PropDef { name: "title", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "line-color", kind: K::Color, def: "rgb(220,0,0)", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "title", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "line-color", + kind: K::Color, + def: "rgb(220,0,0)", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Toplevel" => &[ - PropDef { name: "title", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "resizable", kind: K::Bool, def: "true", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "title", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "resizable", + kind: K::Bool, + def: "true", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], "Spreadsheet" => &[ - PropDef { name: "rows", kind: K::Int, def: "5", lo: 1.0, hi: 50.0, enum_labels: None, enum_values: None }, - PropDef { name: "columns", kind: K::Int, def: "4", lo: 1.0, hi: 26.0, enum_labels: None, enum_values: None }, + PropDef { + name: "rows", + kind: K::Int, + def: "5", + lo: 1.0, + hi: 50.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "columns", + kind: K::Int, + def: "4", + lo: 1.0, + hi: 26.0, + enum_labels: None, + enum_values: None, + }, ], "StockChart" => &[ - PropDef { name: "data-file", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "up-color", kind: K::Color, def: "rgb(0,200,0)", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "down-color", kind: K::Color, def: "rgb(220,0,0)", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "max-candles", kind: K::Int, def: "120", lo: 1.0, hi: 5000.0, enum_labels: None, enum_values: None }, - PropDef { name: "indicators", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "scale-mode", kind: K::Enum, def: "0", lo: 0.0, hi: 0.0, enum_labels: Some("Price|Percent"), enum_values: Some(SCALE_MODE_VALUES) }, - PropDef { name: "compare-files", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, - PropDef { name: "drawings", kind: K::Str, def: "", lo: 0.0, hi: 0.0, enum_labels: None, enum_values: None }, + PropDef { + name: "data-file", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "up-color", + kind: K::Color, + def: "rgb(0,200,0)", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "down-color", + kind: K::Color, + def: "rgb(220,0,0)", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "max-candles", + kind: K::Int, + def: "120", + lo: 1.0, + hi: 5000.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "indicators", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "scale-mode", + kind: K::Enum, + def: "0", + lo: 0.0, + hi: 0.0, + enum_labels: Some("Price|Percent"), + enum_values: Some(SCALE_MODE_VALUES), + }, + PropDef { + name: "compare-files", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, + PropDef { + name: "drawings", + kind: K::Str, + def: "", + lo: 0.0, + hi: 0.0, + enum_labels: None, + enum_values: None, + }, ], _ => &[], } @@ -255,7 +1070,10 @@ mod tests { assert_eq!(xalign.kind, PropKind::Double); assert_eq!((xalign.lo, xalign.hi), (0.0, 1.0)); // Common props resolve through find_prop for every type. - assert_eq!(find_prop("Button", "tooltip-text").unwrap().kind, PropKind::Str); + assert_eq!( + find_prop("Button", "tooltip-text").unwrap().kind, + PropKind::Str + ); assert!(find_prop("Button", "nope").is_none()); } } diff --git a/crates/lightgui-core/tests/project_io_tests.rs b/crates/lightgui-core/tests/project_io_tests.rs index c28beb1..bc2263c 100644 --- a/crates/lightgui-core/tests/project_io_tests.rs +++ b/crates/lightgui-core/tests/project_io_tests.rs @@ -12,7 +12,9 @@ use lightgui_core::{load_from_str, load_project_file, save_to_string, Project, W use std::path::PathBuf; fn fixtures() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("fixtures") + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests") + .join("fixtures") } fn read_fixture(name: &str) -> String { @@ -31,17 +33,23 @@ fn empty_fixture_round_trips() { fn hostile_fixture_round_trips() { let text = read_fixture("hostile.fdj"); let p = load_project_file(&fixtures().join("hostile.fdj")).unwrap(); - let mut want = Project::default(); - want.title = "My \"Quoted\" \\ App\nSecond line".to_string(); - want.class_name = "my cool app!".to_string(); - want.win_width = 1024; - want.win_height = 768; - want.background = "#102030".to_string(); - want.menubar = "File: New | Open | - | Quit\nHelp: About".to_string(); + let mut want = Project { + title: "My \"Quoted\" \\ App\nSecond line".to_string(), + class_name: "my cool app!".to_string(), + win_width: 1024, + win_height: 768, + background: "#102030".to_string(), + menubar: "File: New | Open | - | Quit\nHelp: About".to_string(), + ..Project::default() + }; let mut b = Widget::new("Button_001", "Button", 10, 10); - b.props.insert("label".into(), "Click \"me\" \\ 100% %s %d".into()); + b.props + .insert("label".into(), "Click \"me\" \\ 100% %s %d".into()); b.props.insert("background".into(), "#336699".into()); - b.props.insert("tooltip-text".into(), "tip with \"quotes\" and \\ slash".into()); + b.props.insert( + "tooltip-text".into(), + "tip with \"quotes\" and \\ slash".into(), + ); b.signals = vec!["clicked".to_string()]; want.widgets.push(b); let mut l = Widget::new("Label_001", "Label", 10, 60); @@ -55,7 +63,8 @@ fn hostile_fixture_round_trips() { e.width = 160; e.height = 28; e.props.insert("text".into(), "a\"b\\c\nd".into()); - e.props.insert("placeholder-text".into(), "type \"here\"".into()); + e.props + .insert("placeholder-text".into(), "type \"here\"".into()); e.signals = vec!["changed".to_string(), "activate".to_string()]; want.widgets.push(e); assert_eq!(p, want); @@ -66,8 +75,10 @@ fn hostile_fixture_round_trips() { fn control_fixture_round_trips() { let text = read_fixture("control.fdj"); let p = load_project_file(&fixtures().join("control.fdj")).unwrap(); - let mut want = Project::default(); - want.title = "ctrl\u{1}\u{8}\u{c}\u{1f}".to_string(); + let mut want = Project { + title: "ctrl\u{1}\u{8}\u{c}\u{1f}".to_string(), + ..Project::default() + }; let mut w = Widget::new("Canvas_001", "Canvas", 0, 0); w.width = 200; w.height = 140; @@ -80,8 +91,10 @@ fn control_fixture_round_trips() { fn save_then_load_is_identity() { // A project built in Rust survives a save/load cycle unchanged, // including a widget with no props/signals and odd geometry. - let mut p = Project::default(); - p.background = "red".to_string(); + let mut p = Project { + background: "red".to_string(), + ..Project::default() + }; let w = Widget::new("Scale_001", "Scale", -5, 7); p.widgets.push(w); let dir = std::env::temp_dir().join("lightgui-core-test"); diff --git a/crates/lightgui-gen/src/chart_template.rs b/crates/lightgui-gen/src/chart_template.rs index 64e9b41..b58605b 100644 --- a/crates/lightgui-gen/src/chart_template.rs +++ b/crates/lightgui-gen/src/chart_template.rs @@ -758,4 +758,4 @@ gboolean %CLASS%::on_chart_key(GtkWidget*, GdkEventKey* ev, gpointer self) { /// The StockChart method block for widget class `cls`. pub fn chart_methods(cls: &str) -> String { CHART_TEMPLATE.replace("%CLASS%", cls) -} \ No newline at end of file +} diff --git a/crates/lightgui-gen/src/gen.rs b/crates/lightgui-gen/src/gen.rs index f625ead..6bbddc7 100644 --- a/crates/lightgui-gen/src/gen.rs +++ b/crates/lightgui-gen/src/gen.rs @@ -7,8 +7,8 @@ use crate::chart_template::chart_methods; use crate::cmake::cmake_lists_text; -use lightgui_core::cnum::{atoi_c, atof_c, fmt_g}; use lightgui_core::canonicalize::trim_c; +use lightgui_core::cnum::{atof_c, atoi_c, fmt_g}; use lightgui_core::events::{find_event, EventDef, EventKind}; use lightgui_core::model::{Project, Widget}; use lightgui_core::props::{find_prop, PropKind}; @@ -179,12 +179,17 @@ pub struct MenuDef { fn parse_menus(spec: &str) -> Vec { let mut out = Vec::new(); for line in spec.split('\n') { - let Some(colon) = line.find(':') else { continue }; + let Some(colon) = line.find(':') else { + continue; + }; let label = trim_c(&line[..colon]); if label.is_empty() { continue; } - let mut m = MenuDef { label: label.to_string(), items: Vec::new() }; + let mut m = MenuDef { + label: label.to_string(), + items: Vec::new(), + }; for item in line[colon + 1..].split('|') { let item = trim_c(item); if !item.is_empty() { @@ -235,7 +240,11 @@ struct StubNames { release: &'static str, } -const K_WIDGET_STUBS: StubNames = StubNames { prefix: "", press: "on_press", release: "on_release" }; +const K_WIDGET_STUBS: StubNames = StubNames { + prefix: "", + press: "on_press", + release: "on_release", +}; const K_ROOT_STUBS: StubNames = StubNames { prefix: "Root_001_", press: "on_Root_001_press", @@ -244,13 +253,22 @@ const K_ROOT_STUBS: StubNames = StubNames { fn stub_decls(o: &mut String, n: &StubNames) { for h in K_PRESS { - o.push_str(&format!(" void {}{}(GdkEventButton* event);\n", n.prefix, h)); + o.push_str(&format!( + " void {}{}(GdkEventButton* event);\n", + n.prefix, h + )); } for h in K_DBL { - o.push_str(&format!(" void {}{}(GdkEventButton* event);\n", n.prefix, h)); + o.push_str(&format!( + " void {}{}(GdkEventButton* event);\n", + n.prefix, h + )); } for h in K_REL { - o.push_str(&format!(" void {}{}(GdkEventButton* event);\n", n.prefix, h)); + o.push_str(&format!( + " void {}{}(GdkEventButton* event);\n", + n.prefix, h + )); } o.push_str(&format!( " static gboolean {}(GtkWidget*, GdkEventButton*, gpointer self);\n", @@ -349,7 +367,9 @@ fn connected_events(w: &Widget) -> Vec<&'static EventDef> { let mut out = Vec::new(); let mut seen = BTreeSet::new(); for s in &w.signals { - let Some(d) = find_event(&w.widget_type, s) else { continue }; + let Some(d) = find_event(&w.widget_type, s) else { + continue; + }; if d.builtin { continue; } @@ -371,7 +391,10 @@ fn event_item_ids(w: &Widget) -> Vec { } else { return Vec::new(); }; - split_on('|', spec).iter().map(|name| menu_id_part(name)).collect() + split_on('|', spec) + .iter() + .map(|name| menu_id_part(name)) + .collect() } fn event_connected(w: &Widget, signal: &str) -> bool { @@ -407,7 +430,10 @@ fn event_connect_line(w: &Widget, d: &EventDef, target: &str) -> String { fn event_connect_lines(w: &Widget, target: &str) -> String { let mut o = String::new(); for d in connected_events(w) { - if matches!(d.kind, EventKind::ItemClicked | EventKind::ItemActivate | EventKind::CellChanged) { + if matches!( + d.kind, + EventKind::ItemClicked | EventKind::ItemActivate | EventKind::CellChanged + ) { continue; } o.push_str(&event_connect_line(w, d, target)); @@ -426,7 +452,10 @@ fn event_handler_decls(o: &mut String, w: &Widget) { match d.kind { EventKind::Simple | EventKind::BufferChanged | EventKind::CellChanged => { o.push_str(&format!(" void {}();\n", d.handler)); - o.push_str(&format!(" static void {}_cb(GtkWidget*, gpointer);\n", d.handler)); + o.push_str(&format!( + " static void {}_cb(GtkWidget*, gpointer);\n", + d.handler + )); } EventKind::SwitchPage => { o.push_str(&format!(" void {}(guint page);\n", d.handler)); @@ -590,7 +619,9 @@ fn prop_lines(w: &Widget, target: &str, skip: &BTreeSet<&str>) -> String { if skip.contains(name.as_str()) { continue; } - let Some(def) = find_prop(&w.widget_type, name) else { continue }; + let Some(def) = find_prop(&w.widget_type, name) else { + continue; + }; if (w.widget_type == "Scale" || w.widget_type == "TrackBar") && name == "value" { o.push_str(&format!( " gtk_range_set_value(GTK_RANGE({target}), {});\n", @@ -601,17 +632,27 @@ fn prop_lines(w: &Widget, target: &str, skip: &BTreeSet<&str>) -> String { let v = match def.kind { PropKind::Str => format!("\"{}\"", esc_c(value)), PropKind::Bool => { - if value == "true" { "TRUE".to_string() } else { "FALSE".to_string() } + if value == "true" { + "TRUE".to_string() + } else { + "FALSE".to_string() + } } PropKind::Int => value.clone(), PropKind::Double => fmt_double(value), PropKind::Enum => { let c = enum_const(name, atoi_c(value)); - if c.is_empty() { value.clone() } else { c.to_string() } + if c.is_empty() { + value.clone() + } else { + c.to_string() + } } PropKind::Color => continue, }; - o.push_str(&format!(" g_object_set({target}, \"{name}\", {v}, nullptr);\n")); + o.push_str(&format!( + " g_object_set({target}, \"{name}\", {v}, nullptr);\n" + )); } o } @@ -761,13 +802,18 @@ fn emit_listview_setup(w: &Widget, m: &str) -> String { rows.push(split_on(',', &row)); } let mut o = String::new(); - o.push_str(&format!(" GtkListStore* store = gtk_list_store_new({}", cols.len())); + o.push_str(&format!( + " GtkListStore* store = gtk_list_store_new({}", + cols.len() + )); for _ in &cols { o.push_str(", G_TYPE_STRING"); } o.push_str(");\n"); for row in &rows { - o.push_str(" {\n GtkTreeIter iter;\n gtk_list_store_append(store, &iter);\n"); + o.push_str( + " {\n GtkTreeIter iter;\n gtk_list_store_append(store, &iter);\n", + ); for (c, cell) in row.iter().enumerate().take(cols.len()) { o.push_str(&format!( " gtk_list_store_set(store, &iter, {c}, \"{}\", -1);\n", @@ -836,7 +882,9 @@ fn emit_stringgrid_setup(w: &Widget, m: &str) -> String { cols = 1; } let mut o = String::new(); - o.push_str(&format!(" GtkListStore* store = gtk_list_store_new({cols}")); + o.push_str(&format!( + " GtkListStore* store = gtk_list_store_new({cols}" + )); for _ in 0..cols { o.push_str(", G_TYPE_STRING"); } @@ -897,7 +945,10 @@ fn emit_widget_header(w: &Widget) -> String { if w.widget_type == "Toplevel" { o.push_str(&format!(" {name}();\n", name = w.name)); } else { - o.push_str(&format!(" explicit {name}(GtkWidget* fixed);\n", name = w.name)); + o.push_str(&format!( + " explicit {name}(GtkWidget* fixed);\n", + name = w.name + )); } o.push_str(&format!( " ~{name}();\n GtkWidget* widget() const {{ return widget_; }}\n", @@ -955,7 +1006,11 @@ fn emit_widget_cpp(w: &Widget) -> String { if title.is_empty() { title = w.name.clone(); } - let resizable = if w.prop_or("resizable", "true") == "true" { "TRUE" } else { "FALSE" }; + let resizable = if w.prop_or("resizable", "true") == "true" { + "TRUE" + } else { + "FALSE" + }; o.push_str(&format!( "{name}::{name}() {{\n // #region {name}\n widget_ = gtk_window_new(GTK_WINDOW_TOPLEVEL);\n if (!widget_) {{\n util_error_print(\"{name} creation error\");\n return;\n }}\n gtk_widget_set_name(widget_, \"{name}\");\n gtk_window_set_title(GTK_WINDOW(widget_), \"{title}\");\n gtk_window_set_default_size(GTK_WINDOW(widget_), {width}, {height});\n gtk_window_set_resizable(GTK_WINDOW(widget_), {resizable});\n GtkWidget* inner_fixed = gtk_fixed_new();\n gtk_container_add(GTK_CONTAINER(widget_), inner_fixed);\n", name = w.name, @@ -975,7 +1030,10 @@ fn emit_widget_cpp(w: &Widget) -> String { let mut creation: String; match t { "Button" => { - creation = format!(" {m} = gtk_button_new_with_label(\"{}\");\n", esc_c(&label)); + creation = format!( + " {m} = gtk_button_new_with_label(\"{}\");\n", + esc_c(&label) + ); skip = ["label"].into_iter().collect(); } "Label" => { @@ -984,7 +1042,10 @@ fn emit_widget_cpp(w: &Widget) -> String { } "Entry" => creation = format!(" {m} = gtk_entry_new();\n"), "Checkbutton" => { - creation = format!(" {m} = gtk_check_button_new_with_label(\"{}\");\n", esc_c(&label)); + creation = format!( + " {m} = gtk_check_button_new_with_label(\"{}\");\n", + esc_c(&label) + ); skip = ["label"].into_iter().collect(); } "Radiobutton" => { @@ -1101,7 +1162,9 @@ fn emit_widget_cpp(w: &Widget) -> String { " GtkWidget* frame = gtk_frame_new(nullptr);\n gtk_frame_set_shadow_type(GTK_FRAME(frame), GTK_SHADOW_IN);\n {m} = gtk_drawing_area_new();\n gtk_container_add(GTK_CONTAINER(frame), {m});\n" ); place_target = "frame".to_string(); - skip = ["shape", "fill-color", "title", "line-color"].into_iter().collect(); + skip = ["shape", "fill-color", "title", "line-color"] + .into_iter() + .collect(); } "ScrollBox" => { creation = format!( @@ -1382,7 +1445,10 @@ fn emit_window_header(cls: &str, widgets: &[Widget], menus: &[MenuDef]) -> Strin for m in menus { for item in &m.items { if item != "-" { - o.push_str(&format!(" GtkWidget* {}_ = nullptr;\n", menu_item_id(m, item))); + o.push_str(&format!( + " GtkWidget* {}_ = nullptr;\n", + menu_item_id(m, item) + )); } } } @@ -1572,7 +1638,11 @@ fn write_file(path: &Path, content: &str, r: &mut GenResult) -> bool { /// Writes the project into `out_dir` (created if needed). /// Mirrors `generate_project()` in generator.h. pub fn generate_project(project: &Project, widgets: &[Widget], out_dir: &Path) -> GenResult { - let mut r = GenResult { ok: false, error: String::new(), files: Vec::new() }; + let mut r = GenResult { + ok: false, + error: String::new(), + files: Vec::new(), + }; if let Err(e) = std::fs::create_dir_all(out_dir) { r.error = format!("cannot create {}: {e}", out_dir.display()); return r; @@ -1580,25 +1650,53 @@ pub fn generate_project(project: &Project, widgets: &[Widget], out_dir: &Path) - let cls = sanitize_class(&project.class_name); let menus = parse_menus(&project.menubar); - if !write_file(&out_dir.join(format!("{cls}.h")), &emit_window_header(&cls, widgets, &menus), &mut r) { + if !write_file( + &out_dir.join(format!("{cls}.h")), + &emit_window_header(&cls, widgets, &menus), + &mut r, + ) { return r; } - if !write_file(&out_dir.join(format!("{cls}.cpp")), &emit_window_cpp(&cls, project, widgets, &menus), &mut r) { + if !write_file( + &out_dir.join(format!("{cls}.cpp")), + &emit_window_cpp(&cls, project, widgets, &menus), + &mut r, + ) { return r; } for w in widgets { - if !write_file(&out_dir.join(format!("{}.h", w.name)), &emit_widget_header(w), &mut r) { + if !write_file( + &out_dir.join(format!("{}.h", w.name)), + &emit_widget_header(w), + &mut r, + ) { return r; } - if !write_file(&out_dir.join(format!("{}.cpp", w.name)), &emit_widget_cpp(w), &mut r) { + if !write_file( + &out_dir.join(format!("{}.cpp", w.name)), + &emit_widget_cpp(w), + &mut r, + ) { return r; } } let has_chart = widgets.iter().any(|w| w.widget_type == "StockChart"); - if has_chart && !write_file(&out_dir.join("indicators.h"), include_str!("embedded/indicators.h"), &mut r) { + if has_chart + && !write_file( + &out_dir.join("indicators.h"), + include_str!("embedded/indicators.h"), + &mut r, + ) + { return r; } - if has_chart && !write_file(&out_dir.join("drawings.h"), include_str!("embedded/drawings.h"), &mut r) { + if has_chart + && !write_file( + &out_dir.join("drawings.h"), + include_str!("embedded/drawings.h"), + &mut r, + ) + { return r; } if !write_file(&out_dir.join("main.cpp"), &emit_main(&cls), &mut r) { @@ -1607,7 +1705,11 @@ pub fn generate_project(project: &Project, widgets: &[Widget], out_dir: &Path) - if !write_file(&out_dir.join("Makefile"), &emit_makefile(&cls), &mut r) { return r; } - if !write_file(&out_dir.join("CMakeLists.txt"), &emit_cmake_lists(&cls, widgets), &mut r) { + if !write_file( + &out_dir.join("CMakeLists.txt"), + &emit_cmake_lists(&cls, widgets), + &mut r, + ) { return r; } r.ok = true; diff --git a/crates/lightgui-gen/tests/fixtures_test.rs b/crates/lightgui-gen/tests/fixtures_test.rs index 3af7a31..2235d3b 100644 --- a/crates/lightgui-gen/tests/fixtures_test.rs +++ b/crates/lightgui-gen/tests/fixtures_test.rs @@ -12,12 +12,16 @@ //! independent of where the C++ fixtures were generated. use lightgui_core::cnum::fmt_g; -use lightgui_core::{common_props, find_prop, find_type, props_for, Project, PropKind, Widget, KTYPES}; +use lightgui_core::{ + common_props, find_prop, find_type, props_for, Project, PropKind, Widget, KTYPES, +}; use lightgui_gen::generate_project; use std::path::{Path, PathBuf}; fn fixtures_dir() -> PathBuf { - PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("tests").join("fixtures") + PathBuf::from(env!("CARGO_MANIFEST_DIR")) + .join("tests") + .join("fixtures") } /// Mirror of `mk()` in gen_matrix.cpp: default size + default props. @@ -47,7 +51,9 @@ fn mk(type_: &str, name: &str, x: i32, y: i32) -> Widget { /// `add_compare("...")` from an expected fixture file. fn extract_call_arg(text: &str, call: &str) -> String { let pat = format!("{call}(\""); - let start = text.find(&pat).unwrap_or_else(|| panic!("{call} not in fixture")); + let start = text + .find(&pat) + .unwrap_or_else(|| panic!("{call} not in fixture")); let rest = &text[start + pat.len()..]; let end = rest.find('"').unwrap(); rest[..end].to_string() @@ -64,7 +70,12 @@ fn build_cases() -> Vec<(String, Project)> { let mut p = Project::default(); for (i, t) in KTYPES.iter().enumerate() { let name = format!("{}_001", t.key); - p.widgets.push(mk(t.key, &name, 10 + (i % 4) as i32 * 190, 10 + (i / 4) as i32 * 130)); + p.widgets.push(mk( + t.key, + &name, + 10 + (i % 4) as i32 * 190, + 10 + (i / 4) as i32 * 130, + )); } cases.push(("02_all_types".to_string(), p)); } @@ -85,7 +96,11 @@ fn build_cases() -> Vec<(String, Project)> { let v = match d.kind { PropKind::Str => "Test value".to_string(), PropKind::Bool => { - if d.def == "true" { "false".to_string() } else { "true".to_string() } + if d.def == "true" { + "false".to_string() + } else { + "true".to_string() + } } PropKind::Int => d.def.to_string(), PropKind::Double => { @@ -109,11 +124,17 @@ fn build_cases() -> Vec<(String, Project)> { w.props.insert(d.name.to_string(), v); } if find_prop(t.key, "label").is_some() { - w.props.insert("label".to_string(), "Test value".to_string()); + w.props + .insert("label".to_string(), "Test value".to_string()); } - w.props.insert("background".to_string(), "#336699".to_string()); - w.props.insert("foreground".to_string(), "white".to_string()); - w.props.insert("tooltip-text".to_string(), "tip with \"quotes\" and \\ slash".to_string()); + w.props + .insert("background".to_string(), "#336699".to_string()); + w.props + .insert("foreground".to_string(), "white".to_string()); + w.props.insert( + "tooltip-text".to_string(), + "tip with \"quotes\" and \\ slash".to_string(), + ); p.widgets.push(w); } cases.push(("03_all_props".to_string(), p)); @@ -130,19 +151,49 @@ fn build_cases() -> Vec<(String, Project)> { x += 130; }; for v in 0..=2 { - add(&mut p, "Button", format!("Btn_relief_{v}"), "relief", &v.to_string()); + add( + &mut p, + "Button", + format!("Btn_relief_{v}"), + "relief", + &v.to_string(), + ); } for v in 0..=4 { - add(&mut p, "Frame", format!("Frm_shadow_{v}"), "shadow-type", &v.to_string()); + add( + &mut p, + "Frame", + format!("Frm_shadow_{v}"), + "shadow-type", + &v.to_string(), + ); } for v in 0..=3 { - add(&mut p, "Listbox", format!("Lst_sel_{v}"), "selection-mode", &v.to_string()); + add( + &mut p, + "Listbox", + format!("Lst_sel_{v}"), + "selection-mode", + &v.to_string(), + ); } for v in 0..=4 { - add(&mut p, "Menubutton", format!("Mnu_dir_{v}"), "direction", &v.to_string()); + add( + &mut p, + "Menubutton", + format!("Mnu_dir_{v}"), + "direction", + &v.to_string(), + ); } for v in 0..=3 { - add(&mut p, "Text", format!("Txt_wrap_{v}"), "wrap-mode", &v.to_string()); + add( + &mut p, + "Text", + format!("Txt_wrap_{v}"), + "wrap-mode", + &v.to_string(), + ); } cases.push(("04_enum_values".to_string(), p)); } @@ -150,13 +201,15 @@ fn build_cases() -> Vec<(String, Project)> { // 5. Menus: several menus, separators, spaces and symbols in labels, // plus junk lines that must be ignored. { - let mut p = Project::default(); - p.menubar = "File: New | Open | Save As | - | Quit\n\ + let mut p = Project { + menubar: "File: New | Open | Save As | - | Quit\n\ Edit: Cut | Copy & Paste | Select All\n\ this line has no colon and is ignored\n\ : empty menu label ignored\n\ Help: About LightGUI\n" - .to_string(); + .to_string(), + ..Project::default() + }; p.widgets.push(mk("Button", "Button_001", 10, 10)); cases.push(("05_menus".to_string(), p)); } @@ -164,18 +217,25 @@ fn build_cases() -> Vec<(String, Project)> { // 6. Hostile strings: quotes, backslashes, newline, tab, UTF-8, // format-specifier lookalikes. { - let mut p = Project::default(); - p.title = "My \"Quoted\" \\ App\nSecond line".to_string(); - p.background = "#102030".to_string(); + let mut p = Project { + title: "My \"Quoted\" \\ App\nSecond line".to_string(), + background: "#102030".to_string(), + ..Project::default() + }; let mut b = mk("Button", "Button_001", 10, 10); - b.props.insert("label".to_string(), "Click \"me\" \\ 100% %s %d".to_string()); + b.props.insert( + "label".to_string(), + "Click \"me\" \\ 100% %s %d".to_string(), + ); p.widgets.push(b); let mut l = mk("Label", "Label_001", 10, 60); - l.props.insert("label".to_string(), "café — ☕\t tabbed".to_string()); + l.props + .insert("label".to_string(), "café — ☕\t tabbed".to_string()); p.widgets.push(l); let mut e = mk("Entry", "Entry_001", 10, 110); e.props.insert("text".to_string(), "a\"b\\c\nd".to_string()); - e.props.insert("placeholder-text".to_string(), "type \"here\"".to_string()); + e.props + .insert("placeholder-text".to_string(), "type \"here\"".to_string()); p.widgets.push(e); cases.push(("06_hostile_strings".to_string(), p)); } @@ -183,20 +243,26 @@ fn build_cases() -> Vec<(String, Project)> { // 7. Class-name sanitization: spaces/punctuation, leading digit, // empty (falls back to MyWindow). { - let mut p = Project::default(); - p.class_name = "my cool app!".to_string(); + let mut p = Project { + class_name: "my cool app!".to_string(), + ..Project::default() + }; p.widgets.push(mk("Button", "Button_001", 10, 10)); cases.push(("07_class_spaces".to_string(), p)); } { - let mut p = Project::default(); - p.class_name = "9lives".to_string(); + let mut p = Project { + class_name: "9lives".to_string(), + ..Project::default() + }; p.widgets.push(mk("Label", "Label_001", 10, 10)); cases.push(("08_class_digit".to_string(), p)); } { - let mut p = Project::default(); - p.class_name = String::new(); + let mut p = Project { + class_name: String::new(), + ..Project::default() + }; p.widgets.push(mk("Entry", "Entry_001", 10, 10)); cases.push(("09_class_empty".to_string(), p)); } @@ -217,7 +283,10 @@ fn build_cases() -> Vec<(String, Project)> { for i in 1..=3 { let mut t = mk("Toplevel", &format!("Toplevel_00{i}"), 100 * i, 100 * i); t.props.insert("title".to_string(), format!("Window {i}")); - t.props.insert("resizable".to_string(), if i == 2 { "false" } else { "true" }.to_string()); + t.props.insert( + "resizable".to_string(), + if i == 2 { "false" } else { "true" }.to_string(), + ); p.widgets.push(t); } cases.push(("10_spreadsheet_toplevels".to_string(), p)); @@ -226,28 +295,46 @@ fn build_cases() -> Vec<(String, Project)> { // 9. Forty mixed widgets. { let mut p = Project::default(); - let types = ["Button", "Label", "Entry", "Checkbutton", "Frame", "Combobox", "Scale", "Text"]; + let types = [ + "Button", + "Label", + "Entry", + "Checkbutton", + "Frame", + "Combobox", + "Scale", + "Text", + ]; for i in 0..40 { let type_ = types[i % 8]; let name = format!("{}_{:03}", type_, i + 1); - p.widgets.push(mk(type_, &name, 10 + (i % 5) as i32 * 170, 10 + (i / 5) as i32 * 70)); + p.widgets.push(mk( + type_, + &name, + 10 + (i % 5) as i32 * 170, + 10 + (i / 5) as i32 * 70, + )); } cases.push(("11_forty_widgets".to_string(), p)); } // 10. Window variations: no background, huge size, tiny size. { - let mut p = Project::default(); - p.win_width = 3840; - p.win_height = 2160; + let mut p = Project { + win_width: 3840, + win_height: 2160, + ..Project::default() + }; p.widgets.push(mk("Label", "Label_001", 10, 10)); cases.push(("12_window_huge".to_string(), p)); } { - let mut p = Project::default(); - p.win_width = 50; - p.win_height = 50; - p.background = "red".to_string(); + let mut p = Project { + win_width: 50, + win_height: 50, + background: "red".to_string(), + ..Project::default() + }; cases.push(("13_window_tiny".to_string(), p)); } @@ -278,10 +365,13 @@ fn build_cases() -> Vec<(String, Project)> { chart.width = 900; chart.height = 500; chart.props.insert("data-file".to_string(), csv15); - chart.props.insert("max-candles".to_string(), "400".to_string()); chart .props - .insert("indicators".to_string(), "SMA(20), EMA(50), Bollinger Bands, RSI, MACD, Engulfing".to_string()); + .insert("max-candles".to_string(), "400".to_string()); + chart.props.insert( + "indicators".to_string(), + "SMA(20), EMA(50), Bollinger Bands, RSI, MACD, Engulfing".to_string(), + ); p.widgets.push(chart); cases.push(("15_stockchart_indicators".to_string(), p)); } @@ -289,16 +379,24 @@ fn build_cases() -> Vec<(String, Project)> { let csv16 = extract_call_arg(&chart16, "load_csv"); let qqq16 = extract_call_arg(&chart16, "add_compare"); { - let mut p = Project::default(); - p.win_width = 1024; - p.win_height = 768; + let mut p = Project { + win_width: 1024, + win_height: 768, + ..Project::default() + }; let mut chart = mk("StockChart", "StockChart_001", 10, 10); chart.width = 1004; chart.height = 748; chart.props.insert("data-file".to_string(), csv16); - chart.props.insert("max-candles".to_string(), "400".to_string()); - chart.props.insert("indicators".to_string(), "SMA(20), RSI".to_string()); - chart.props.insert("scale-mode".to_string(), "1".to_string()); + chart + .props + .insert("max-candles".to_string(), "400".to_string()); + chart + .props + .insert("indicators".to_string(), "SMA(20), RSI".to_string()); + chart + .props + .insert("scale-mode".to_string(), "1".to_string()); chart.props.insert("compare-files".to_string(), qqq16); chart.props.insert( "drawings".to_string(),