Add CMake build console dialog to the Rust designer

The Rust designer had no CMake build UI at all: File -> Build with
CMake, Open CMake Project and CMake Settings all just posted an
unowned_note (CMake build lives in lightgui-support). Wire the first
two to lightgui-support now, mirroring the C++ designer:

- show_build_result(): modal 700x420 dialog with the full build log
  in a read-only monospace TextView. The log area is packed expanding
  (pack_start expand=true) so it fills the dialog instead of sitting
  at its ~2-line natural height at the top — the C console bug, fixed
  here by construction. Long lines wrap; the view starts scrolled to
  the end of the log (idle callback, so the scroll happens after
  allocation and lands on the true last line).
- cmake_build_dir(): error dialog when there is no CMakeLists.txt,
  otherwise configure + build with the saved settings and show the
  result dialog.
- on_cmake_build(): rebuilds the last generated project, or asks for
  a folder when there is none yet.
- on_cmake_open(): choose any CMakeLists.txt folder and build it.
- App.last_cmake_dir records the last generated/opened project
  (set by Generate too), mirroring C++ g_last_generated_dir.

CMake Settings… stays an unowned note: no settings dialog, out of
scope for this change.
This commit is contained in:
cnmcdee committed 2026-12-01 01:08:12 +00:00
1 parent 54509f32ba
commit 0c127d4355
2 files changed
+161 -1

No files matched your search

+156 -1
View File
@@ -213,6 +213,7 @@ fn main() {
palette_selftest_rc: 1,
backend: canvas::canvas_backend(),
gen_dir,
last_cmake_dir: String::new(),
}));
// XFrame-only palette: the 547 XFrame entries in their sections,
@@ -403,7 +404,7 @@ fn main() {
let app2 = app.clone();
mi.connect_activate(move |_| unowned_note(&app2, "Preferences live in lightgui-support"));
}
for id in ["mi_cmake_build", "mi_cmake_open", "mi_cmake_settings"] {
for id in ["mi_cmake_settings"] {
if let Some(mi) = builder.object::<gtk::MenuItem>(id) {
let app2 = app.clone();
mi.connect_activate(move |_| {
@@ -411,6 +412,14 @@ fn main() {
});
}
}
if let Some(mi) = builder.object::<gtk::MenuItem>("mi_cmake_build") {
let app2 = app.clone();
mi.connect_activate(move |_| on_cmake_build(&app2));
}
if let Some(mi) = builder.object::<gtk::MenuItem>("mi_cmake_open") {
let app2 = app.clone();
mi.connect_activate(move |_| on_cmake_open(&app2));
}
if let Some(mi) = builder.object::<gtk::MenuItem>("mi_view_code") {
let app2 = app.clone();
mi.connect_activate(move |_| toggle_code_view(&app2));
@@ -660,6 +669,9 @@ fn on_generate(app: &Shared) {
let project = current_project(app);
let res = lightgui_gen::generate_project(&project, &project.widgets, &dir);
if res.ok {
// Remember the folder so File → Build with CMake…
// rebuilds it without asking again (mirrors C++).
app.borrow_mut().last_cmake_dir = dir.to_string_lossy().to_string();
app.borrow().status(&format!(
"Generated {} files in {}",
res.files.len(),
@@ -675,6 +687,149 @@ fn on_generate(app: &Shared) {
dlg.close();
}
// ------------------------------------------------------- CMake
// (lightgui-support::cmake) Builds a project directory with CMake
// instead of the plain g++ / make command. Settings (generator, build
// type, build directory) live in ~/.config/lightgui/cmake.conf.
// Mirrors C++ `show_build_result` / `cmake_build_dir` /
// `on_cmake_build` / `on_cmake_open` in src/main.cpp, including the
// console-dialog fix: the log area is packed expanding so it fills
// the dialog (gtk-rs `pack_start` with expand=true — the C bug was
// packing with expand=FALSE), the full log is shown (no truncation),
// long lines wrap, and the view starts scrolled to the end of the
// log, where build failures are.
fn show_build_result(app: &Shared, title: &str, r: &lightgui_support::CMakeBuildResult) {
let msg = if r.ok {
format!("CMake build OK — {}", r.exe_path)
} else {
"CMake build FAILED — see log".to_string()
};
app.borrow().status(&msg);
let parent = app.borrow().main_window.clone();
let dlg = gtk::Dialog::with_buttons(
Some(title),
Some(&parent),
gtk::DialogFlags::MODAL,
&[("Close", gtk::ResponseType::Close)],
);
dlg.set_default_size(700, 420);
let head = if r.ok {
format!("Build succeeded.\nExecutable: {}\n\n", r.exe_path)
} else {
"Build FAILED.\n\n".to_string()
};
let view = gtk::TextView::new();
view.set_editable(false);
view.set_monospace(true);
// Wrap long lines (cmake command lines) so the whole log is
// readable without horizontal scrolling.
view.set_wrap_mode(gtk::WrapMode::Char);
if let Some(buf) = view.buffer() {
buf.set_text(&format!("{head}{}", r.output));
}
let scroll = gtk::ScrolledWindow::new(None::<&gtk::Adjustment>, None::<&gtk::Adjustment>);
scroll.add(&view);
// Pack expanding so the log fills the dialog (see note above).
dlg.content_area().pack_start(&scroll, true, true, 0);
dlg.show_all();
// Failures land at the end of the log: start scrolled there so the
// error is visible immediately; the user can scroll up for the
// rest. Do it in an idle callback, not right after show_all:
// scrolling before the dialog is realized/allocated computes
// against the not-yet-final size and lands a few lines short of
// the true end. The idle runs inside the dialog's loop, after
// allocation.
let view2 = view.clone();
glib::idle_add_local_once(move || {
if let Some(buf) = view2.buffer() {
let mut end = buf.end_iter();
view2.scroll_to_iter(&mut end, 0.0, false, 0.0, 0.0);
}
});
dlg.run();
dlg.close();
}
/// Configure + build `dir` with the saved CMake settings and report
/// the result. Returns false when `dir` holds no CMakeLists.txt.
fn cmake_build_dir(app: &Shared, dir: &str) -> bool {
if !lightgui_support::cmake_project_exists(dir) {
app.borrow()
.status(&format!("No CMakeLists.txt in {dir}"));
let parent = app.borrow().main_window.clone();
let msg = gtk::MessageDialog::new(
Some(&parent),
gtk::DialogFlags::MODAL,
gtk::MessageType::Error,
gtk::ButtonsType::Ok,
&format!("No CMakeLists.txt in:\n{dir}"),
);
msg.run();
msg.close();
return false;
}
let mut settings = lightgui_support::CMakeSettings::default();
settings.load("");
app.borrow()
.status(&format!("Building with CMake in {dir} …"));
let r = lightgui_support::cmake_build_project(dir, &settings);
show_build_result(app, &format!("CMake build — {dir}"), &r);
r.ok
}
/// File → Build with CMake… : rebuilds the last generated project, or
/// asks for a project folder when there is none yet.
fn on_cmake_build(app: &Shared) {
let mut dir = app.borrow().last_cmake_dir.clone();
if dir.is_empty() || !lightgui_support::cmake_project_exists(&dir) {
let parent = app.borrow().main_window.clone();
let dlg = gtk::FileChooserDialog::builder()
.title("Build with CMake — choose a project folder")
.transient_for(&parent)
.action(gtk::FileChooserAction::SelectFolder)
.build();
dlg.add_button("Cancel", gtk::ResponseType::Cancel);
dlg.add_button("Build", gtk::ResponseType::Accept);
if dlg.run() != gtk::ResponseType::Accept {
dlg.close();
return;
}
let Some(folder) = dlg.filename() else {
dlg.close();
return;
};
dlg.close();
dir = folder.to_string_lossy().to_string();
}
cmake_build_dir(app, &dir);
}
/// File → Open CMake Project… : point the designer at any directory
/// holding a CMakeLists.txt (generated or hand-written) and build it.
fn on_cmake_open(app: &Shared) {
let parent = app.borrow().main_window.clone();
let dlg = gtk::FileChooserDialog::builder()
.title("Open CMake Project — choose a folder with a CMakeLists.txt")
.transient_for(&parent)
.action(gtk::FileChooserAction::SelectFolder)
.build();
dlg.add_button("Cancel", gtk::ResponseType::Cancel);
dlg.add_button("Open & Build", gtk::ResponseType::Accept);
if dlg.run() != gtk::ResponseType::Accept {
dlg.close();
return;
}
let Some(folder) = dlg.filename() else {
dlg.close();
return;
};
dlg.close();
let dir = folder.to_string_lossy().to_string();
// So Build with CMake… rebuilds it (mirrors C++).
app.borrow_mut().last_cmake_dir = dir.clone();
cmake_build_dir(app, &dir);
}
fn on_open(app: &Shared) {
let parent = app.borrow().main_window.clone();
let dlg = gtk::FileChooserDialog::builder()
+5
View File
@@ -77,6 +77,11 @@ pub struct App {
pub backend: CanvasBackend,
/// Scratch dir for generated code shown in the code window.
pub gen_dir: std::path::PathBuf,
/// Folder of the last generated project (set by Generate and by
/// Open CMake Project), so File → Build with CMake… rebuilds it
/// without asking again. Mirrors C++ `g_last_generated_dir`.
/// Empty = none yet.
pub last_cmake_dir: String,
}
pub type Shared = Rc<RefCell<App>>;