Skip to content

DesignCraft: An open-source page and layout design editor written by scratch in pure Rust

refactor(ui): extract helpers from conditions, book, credits and page control panels (Sonar S3776) (#1721)

Dylan M. Taylore651c637f4d5
diff --git a/crates/ui-egui/src/chrome.rs b/crates/ui-egui/src/chrome.rs
index b0fed5799..2a1448719 100644
--- a/crates/ui-egui/src/chrome.rs
+++ b/crates/ui-egui/src/chrome.rs
@@ -477,6 +477,32 @@ pub fn control_bar(app: &mut DesignApp, ui: &mut egui::Ui) {
});
}
+/// Page-size preset menu and the portrait / landscape toggles of the Control panel.
+fn page_preset_and_orientation(app: &mut DesignApp, ui: &mut egui::Ui, lang: &str, size: (f64, f64), n: usize, anchor: u8) {
+ let landscape = size.0 > size.1;
+ ui.vertical(|ui| {
+ let cur = panels::properties::preset_name(size.0, size.1);
+ egui::ComboBox::from_id_salt("ctl_page_preset").selected_text(crate::rtl::widget(ui, crate::i18n::tr(lang, cur))).width(130.0).show_ui(
+ ui,
+ |ui| {
+ for (name, w, h) in panels::properties::PAGE_PRESETS {
+ if ui.selectable_label(cur == *name, crate::rtl::widget(ui, crate::i18n::tr(lang, name))).clicked() {
+ let (w, h) = if landscape { (h, w) } else { (w, h) };
+ let _ = app.run("layout.pageSize", json!({"pages": [n], "width": w, "height": h, "anchor": anchor}));
+ }
+ }
+ },
+ );
+ ui.horizontal(|ui| {
+ for (icon, tip, want) in [("orient-portrait", "Portrait", false), ("orient-landscape", "Landscape", true)] {
+ if crate::widgets::icon_toggle(ui, icon, landscape == want, crate::i18n::tr(lang, tip)).clicked() && landscape != want {
+ let _ = app.run("layout.pageSize", json!({"pages": [n], "width": size.1, "height": size.0, "anchor": anchor}));
+ }
+ }
+ });
+ });
+}
+
/// The Control panel with the Page tool, as InDesign's: reference point, X/Y, W/H, page-size
/// preset, orientation, Liquid Page Rule and Show Parent Page Overlay, for
/// the page the Page tool selected (else the page in view). W/H keep the reference point in place.
@@ -518,28 +544,7 @@ fn control_page(app: &mut DesignApp, ui: &mut egui::Ui) {
});
vsep(ui);
// Page-size preset and orientation.
- let landscape = pg.width > pg.height;
- ui.vertical(|ui| {
- let cur = panels::properties::preset_name(pg.width, pg.height);
- egui::ComboBox::from_id_salt("ctl_page_preset").selected_text(crate::rtl::widget(ui, crate::i18n::tr(&lang, cur))).width(130.0).show_ui(
- ui,
- |ui| {
- for (name, w, h) in panels::properties::PAGE_PRESETS {
- if ui.selectable_label(cur == *name, crate::rtl::widget(ui, crate::i18n::tr(&lang, name))).clicked() {
- let (w, h) = if landscape { (h, w) } else { (w, h) };
- let _ = app.run("layout.pageSize", json!({"pages": [n], "width": w, "height": h, "anchor": anchor}));
- }
- }
- },
- );
- ui.horizontal(|ui| {
- for (icon, tip, want) in [("orient-portrait", "Portrait", false), ("orient-landscape", "Landscape", true)] {
- if crate::widgets::icon_toggle(ui, icon, landscape == want, crate::i18n::tr(&lang, tip)).clicked() && landscape != want {
- let _ = app.run("layout.pageSize", json!({"pages": [n], "width": pg.height, "height": pg.width, "anchor": anchor}));
- }
- }
- });
- });
+ page_preset_and_orientation(app, ui, &lang, (pg.width, pg.height), n, anchor);
vsep(ui);
ui.vertical(|ui| {
caption(ui, crate::i18n::tr(&lang, "Liquid Page Rule:"));
diff --git a/crates/ui-egui/src/credits.rs b/crates/ui-egui/src/credits.rs
index b9efa035a..d4828b1f4 100644
--- a/crates/ui-egui/src/credits.rs
+++ b/crates/ui-egui/src/credits.rs
@@ -212,10 +212,8 @@ impl Default for View {
}
}
-/// About ▸ Contributors: a name toggle, a sort, and the list as a grab bag or a table.
-pub fn contributors_ui(ui: &mut egui::Ui, lang: &str) {
- let id = egui::Id::new("credits_view");
- let mut v = ui.data_mut(|d| d.get_temp::<View>(id)).unwrap_or_default();
+/// The name toggle, sort and layout controls above the list.
+fn controls(ui: &mut egui::Ui, v: &mut View, lang: &str) {
ui.horizontal_wrapped(|ui| {
ui.label(tr(lang, "Show"));
for m in NameMode::ALL {
@@ -244,6 +242,13 @@ pub fn contributors_ui(ui: &mut egui::Ui, lang: &str) {
v.table = true;
}
});
+}
+
+/// About ▸ Contributors: a name toggle, a sort, and the list as a grab bag or a table.
+pub fn contributors_ui(ui: &mut egui::Ui, lang: &str) {
+ let id = egui::Id::new("credits_view");
+ let mut v = ui.data_mut(|d| d.get_temp::<View>(id)).unwrap_or_default();
+ controls(ui, &mut v, lang);
let list = sorted(CONTRIBUTORS, v.names, v.key, v.ascending);
let totals = tr(lang, "{contributors} contributors · {commits} commits")
.replace("{contributors}", &list.len().to_string())
diff --git a/crates/ui-egui/src/panels/conditions.rs b/crates/ui-egui/src/panels/conditions.rs
index 45eddec20..a855b4820 100644
--- a/crates/ui-egui/src/panels/conditions.rs
+++ b/crates/ui-egui/src/panels/conditions.rs
@@ -7,6 +7,18 @@ use serde_json::json;
use crate::DesignApp;
use crate::theme::Tokens;
+/// Click on a condition name: toggle it on the selected text, or tell the user to select some.
+fn apply_condition(app: &mut DesignApp, name: &str, has_text: bool, applied: &[String]) {
+ if has_text {
+ let on = !applied.iter().any(|a| a == name);
+ if let Err(e) = app.run("condition.apply", json!({"name": name, "on": on})) {
+ app.status_error("Conditional Text", e);
+ }
+ } else {
+ app.status(crate::i18n::tr(&app.ui.language, "Select text to apply a condition"));
+ }
+}
+
pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
let t = Tokens::get(ui.ctx());
let Some(st) = app.session.active() else { return };
@@ -55,14 +67,7 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
let chip = egui::Rect::from_min_size(egui::pos2(row.max.x - 20.0, row.min.y + 5.0), vec2(14.0, 12.0));
ui.painter().rect_filled(chip, 2.0, egui::Color32::from_rgb(c.color[0], c.color[1], c.color[2]));
if nr.clicked() {
- if has_text {
- let on = !applied.contains(&c.name);
- if let Err(e) = app.run("condition.apply", json!({"name": c.name, "on": on})) {
- app.status_error("Conditional Text", e);
- }
- } else {
- app.status(crate::i18n::tr(&app.ui.language, "Select text to apply a condition"));
- }
+ apply_condition(app, &c.name, has_text, &applied);
}
nr.context_menu(|ui| {
if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Delete Condition"))).clicked() {
diff --git a/crates/ui-egui/src/panels/library.rs b/crates/ui-egui/src/panels/library.rs
index 65fb16862..2199e571e 100644
--- a/crates/ui-egui/src/panels/library.rs
+++ b/crates/ui-egui/src/panels/library.rs
@@ -84,10 +84,7 @@ pub fn show(app: &mut DesignApp, ui: &mut egui::Ui) {
}
}
-/// Book panel: the open book's documents with their page ranges; numbering, synchronising and
-/// exporting the whole book.
-pub fn book(app: &mut DesignApp, ui: &mut egui::Ui) {
- let t = Tokens::get(ui.ctx());
+fn book_open_buttons(app: &mut DesignApp, ui: &mut egui::Ui) {
ui.horizontal(|ui| {
if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "New Book…"))).clicked()
&& let Some(path) = app.pick_save("Book.dcbook")
@@ -102,6 +99,36 @@ pub fn book(app: &mut DesignApp, ui: &mut egui::Ui) {
app.status_error("Book", e);
}
});
+}
+
+fn book_action_buttons(app: &mut DesignApp, ui: &mut egui::Ui) {
+ ui.horizontal_wrapped(|ui| {
+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Add Document…"))).clicked()
+ && let Some(path) = app.services.pick_open.as_mut().and_then(|f| f("open"))
+ && let Err(e) = app.run("book.add", json!({"path": path}))
+ {
+ app.status_error("Book", e);
+ }
+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Update Numbering"))).clicked() {
+ let _ = app.run("book.paginate", json!({}));
+ }
+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Synchronize"))).clicked() {
+ let _ = app.run("book.syncStyles", json!({}));
+ }
+ if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Export PDF…"))).clicked()
+ && let Some(path) = app.pick_save("Book.pdf")
+ && let Err(e) = app.run("book.exportPdf", json!({"path": path}))
+ {
+ app.status_error("Book", e);
+ }
+ });
+}
+
+/// Book panel: the open book's documents with their page ranges; numbering, synchronising and
+/// exporting the whole book.
+pub fn book(app: &mut DesignApp, ui: &mut egui::Ui) {
+ let t = Tokens::get(ui.ctx());
+ book_open_buttons(app, ui);
let Ok(info) = app.session.execute("book.list", &json!({})) else {
crate::rtl::label(ui, egui::RichText::new(crate::i18n::tr(&app.ui.language, "No book open.")).size(11.0).color(t.text_dim));
return;
@@ -140,26 +167,7 @@ pub fn book(app: &mut DesignApp, ui: &mut egui::Ui) {
});
}
ui.separator();
- ui.horizontal_wrapped(|ui| {
- if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Add Document…"))).clicked()
- && let Some(path) = app.services.pick_open.as_mut().and_then(|f| f("open"))
- && let Err(e) = app.run("book.add", json!({"path": path}))
- {
- app.status_error("Book", e);
- }
- if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Update Numbering"))).clicked() {
- let _ = app.run("book.paginate", json!({}));
- }
- if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Synchronize"))).clicked() {
- let _ = app.run("book.syncStyles", json!({}));
- }
- if ui.button(crate::rtl::widget(ui, crate::i18n::tr(&app.ui.language, "Export PDF…"))).clicked()
- && let Some(path) = app.pick_save("Book.pdf")
- && let Err(e) = app.run("book.exportPdf", json!({"path": path}))
- {
- app.status_error("Book", e);
- }
- });
+ book_action_buttons(app, ui);
}
/// Scripts panel: saved command scripts — run (one undo step), edit, add, delete, load.