Clean up clippy warnings; cargo fmt core+gen

This commit is contained in:
cnmcdee committed 2026-11-29 06:02:19 +00:00
1 parent 122ae8bd45
commit 5bafafa8b0
10 files changed
+1777 -288

No files matched your search

+26 -6
View File
@@ -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);
}
+16 -4
View File
@@ -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"),
+224 -33
View File
@@ -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().
_ => &[],
+252 -36
View File
@@ -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.
+29 -10
View File
@@ -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<Project, String> {
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<Project, String> {
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");
}
File diff suppressed because it is too large. Load diff
+28 -15
View File
@@ -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");
+1 -1
View File
@@ -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)
}
}
+134 -32
View File
@@ -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<MenuDef> {
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<String> {
} 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;
+145 -47
View File
@@ -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(),