diff --git a/Makefile b/Makefile index 0877ca2..870f5e2 100644 --- a/Makefile +++ b/Makefile @@ -13,23 +13,57 @@ LIGHTGUI_SRC ?= /opt/lightgui PY ?= python3 -.PHONY: regen check check-registry check-palette check-props check-events check-indicators install +.PHONY: regen check check-registry check-palette check-props check-events check-indicators install designer # Install the XFrame widget headers (header-only) so generated apps # build on any machine where LightGUI is installed. The headers live # in the C repo ($(LIGHTGUI_SRC)/widgets); this just copies them to # $(PREFIX)/include/lightgui. The Rust generator emits the same -# default include path as the C generator. +# default include path as the C generator. Also installs the designer +# binary ($(BINARY), default target/debug/lightgui-designer), the +# palette icons, and a desktop entry + icon. Run from the designer via +# Tools -> Install Libraries…, which prompts for the root password +# (pkexec, sudo fallback). `make designer` builds the binary first +# (as the user, so no root-owned build artefacts land in the checkout). PREFIX ?= /usr/local INCLUDEDIR ?= $(PREFIX)/include/lightgui +BINDIR ?= $(PREFIX)/bin +SHAREDIR ?= $(PREFIX)/share/lightgui +BINARY ?= target/debug/lightgui-designer + +designer: + cargo build -p lightgui-designer install: @echo "Installing LightGUI widget headers to $(INCLUDEDIR)" - @mkdir -p $(INCLUDEDIR) - @cd $(LIGHTGUI_SRC)/widgets && find common core components -name '*.h' | while read f; do \ - install -D -m 644 "$$f" "$(INCLUDEDIR)/$$f"; \ - done - @echo "Installed $$(find $(LIGHTGUI_SRC)/widgets/common $(LIGHTGUI_SRC)/widgets/core $(LIGHTGUI_SRC)/widgets/components -name '*.h' | wc -l) headers to $(INCLUDEDIR)" + @if [ -d "$(LIGHTGUI_SRC)/widgets" ]; then \ + mkdir -p $(INCLUDEDIR); \ + cd $(LIGHTGUI_SRC)/widgets && find common core components -name '*.h' | while read f; do \ + install -D -m 644 "$$f" "$(INCLUDEDIR)/$$f"; \ + done; \ + echo "Installed $$(find $(LIGHTGUI_SRC)/widgets/common $(LIGHTGUI_SRC)/widgets/core $(LIGHTGUI_SRC)/widgets/components -name '*.h' | wc -l) headers to $(INCLUDEDIR)"; \ + else \ + echo "NOTE: no C sources at $(LIGHTGUI_SRC) — skipping headers (install them from the C designer instead)"; \ + fi + @echo "Installing designer binary to $(BINDIR)" + @mkdir -p $(BINDIR) + @install -m 755 $(BINARY) $(BINDIR)/lightgui-designer + @if [ -d "$(LIGHTGUI_SRC)/widgets/icons" ]; then \ + mkdir -p $(SHAREDIR)/icons; \ + cd $(LIGHTGUI_SRC)/widgets/icons && find . -name '*.ico' | while read f; do \ + install -D -m 644 "$$f" "$(SHAREDIR)/icons/$$f"; \ + done; \ + echo "Installed $$(find $(LIGHTGUI_SRC)/widgets/icons -name '*.ico' | wc -l) palette icons to $(SHAREDIR)/icons"; \ + else \ + echo "NOTE: no palette icons at $(LIGHTGUI_SRC)/widgets/icons — skipping"; \ + fi + @echo "Installing desktop entry + icon" + @mkdir -p $(PREFIX)/share/applications $(PREFIX)/share/icons/hicolor/512x512/apps + @install -D -m 644 lightgui-designer.desktop $(PREFIX)/share/applications/lightgui-designer.desktop + @install -D -m 644 flatpak/icon.png $(PREFIX)/share/icons/hicolor/512x512/apps/lightgui-designer.png + @-update-desktop-database $(PREFIX)/share/applications 2>/dev/null; true + @-gtk-update-icon-cache -q $(PREFIX)/share/icons/hicolor 2>/dev/null; true + @echo "Installed LightGUI (Rust): binary $(BINDIR)/lightgui-designer, headers $(INCLUDEDIR), data $(SHAREDIR)" regen: LIGHTGUI_SRC=$(LIGHTGUI_SRC) $(PY) tools/extract_all.py diff --git a/crates/lightgui-designer/src/main.rs b/crates/lightgui-designer/src/main.rs index 60e7fb2..86f7cc0 100644 --- a/crates/lightgui-designer/src/main.rs +++ b/crates/lightgui-designer/src/main.rs @@ -420,6 +420,10 @@ fn main() { let app2 = app.clone(); mi.connect_activate(move |_| on_cmake_open(&app2)); } + if let Some(mi) = builder.object::("mi_install_libraries") { + let app2 = app.clone(); + mi.connect_activate(move |_| on_install_libraries(&app2)); + } if let Some(mi) = builder.object::("mi_view_code") { let app2 = app.clone(); mi.connect_activate(move |_| toggle_code_view(&app2)); @@ -698,13 +702,11 @@ fn on_generate(app: &Shared) { // 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); +// Generic scrollable log dialog: the log area is packed expanding so +// it fills the dialog (see the CMake console fix), long lines wrap, +// and the view starts scrolled to the end of the log. Used by the +// CMake build result and by Install Libraries. +fn show_log_dialog(app: &Shared, title: &str, head: &str, log: &str) { let parent = app.borrow().main_window.clone(); let dlg = gtk::Dialog::with_buttons( Some(title), @@ -713,19 +715,14 @@ fn show_build_result(app: &Shared, title: &str, r: &lightgui_support::CMakeBuild &[("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. + // Wrap long 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)); + buf.set_text(&format!("{head}{log}")); } let scroll = gtk::ScrolledWindow::new(None::<>k::Adjustment>, None::<>k::Adjustment>); scroll.add(&view); @@ -747,7 +744,26 @@ fn show_build_result(app: &Shared, title: &str, r: &lightgui_support::CMakeBuild } }); dlg.run(); - dlg.close(); + // hide(), not close(): close() leaves the dialog mapped in this + // gtk-rs build (verified live on the Install Libraries confirm). + dlg.hide(); +} + +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 head = if r.ok { + format!("Build succeeded.\nExecutable: {}\n\n", r.exe_path) + } else { + "Build FAILED.\n\n".to_string() + }; + // Show the full log: the text view scrolls, so a long build log is + // fully visible instead of only its tail. + show_log_dialog(app, title, &head, &r.output); } /// Configure + build `dir` with the saved CMake settings and report @@ -830,6 +846,331 @@ fn on_cmake_open(app: &Shared) { cmake_build_dir(app, &dir); } +// ------------------------------------------------------- Install Libraries +// Tools → Install Libraries… : make LightGUI globally available by +// running `make install` in the checkout as root. The install target +// (Makefile) copies the widget headers to /usr/local/include/lightgui +// (so generated apps build), the designer binary to +// /usr/local/bin/lightgui-designer, the palette icons to +// /usr/local/share/lightgui, and a desktop entry + icon so the app +// menu lists it. There are no shared libraries to install (the +// widgets are header-only), so no ldconfig step is needed. Mirrors +// C++ `on_install_libraries` in src/main.cpp. +// +// The install runs as root via pkexec when available (graphical root +// password prompt) or, when pkexec is missing, via sudo -S with the +// password typed into a dialog (the password is piped to sudo's stdin +// and wiped from memory immediately after; it is never written to disk +// or passed on a command line). The build step runs first as the user +// so no root-owned build artefacts land in the checkout. + +/// Run a command and capture its combined stdout+stderr; `stdin_text` +/// (empty = none) is written to the child's stdin first. Returns +/// (exit code, combined output). +fn run_command_capture( + prog: &str, + args: &[&str], + stdin_text: &str, + dir: Option<&str>, +) -> (i32, String) { + use std::io::Write; + use std::process::{Command, Stdio}; + let mut cmd = Command::new(prog); + cmd.args(args) + .stdin(Stdio::piped()) + .stdout(Stdio::piped()) + .stderr(Stdio::piped()); + if let Some(d) = dir { + cmd.current_dir(d); + } + let mut child = match cmd.spawn() { + Ok(c) => c, + Err(e) => return (-1, format!("cannot run {prog}: {e}\n")), + }; + if !stdin_text.is_empty() { + if let Some(mut stdin) = child.stdin.take() { + let _ = stdin.write_all(stdin_text.as_bytes()); + } + } + match child.wait_with_output() { + Ok(out) => { + let mut combined = String::from_utf8_lossy(&out.stdout).to_string(); + combined.push_str(&String::from_utf8_lossy(&out.stderr)); + (out.status.code().unwrap_or(-1), combined) + } + Err(e) => (-1, format!("cannot run {prog}: {e}\n")), + } +} + +/// Absolute path of a helper program, or "" when it is not on PATH. +fn find_program(name: &str) -> String { + let (rc, out) = run_command_capture("which", &[name], "", None); + if rc == 0 { + out.trim().to_string() + } else { + String::new() + } +} + +/// Absolute path of `cargo` (it lives in ~/.cargo/bin, which is not +/// always on a GUI process's PATH). +fn find_cargo() -> String { + let from_path = find_program("cargo"); + if !from_path.is_empty() { + return from_path; + } + if let Ok(home) = std::env::var("HOME") { + for cand in [ + format!("{home}/.cargo/bin/cargo"), + "/root/.cargo/bin/cargo".to_string(), + "/usr/local/bin/cargo".to_string(), + "/usr/local/cargo/bin/cargo".to_string(), + ] { + if std::path::Path::new(&cand).is_file() { + return cand; + } + } + } + "cargo".to_string() +} + +/// The Rust checkout the designer is running from: the directory +/// holding the running binary's ancestors, or the current directory, +/// when it looks like a checkout (Cargo.toml + crates/ +/// lightgui-designer). "" when neither does. +fn find_checkout_dir() -> String { + fn looks_like_checkout(d: &std::path::Path) -> bool { + d.join("Cargo.toml").is_file() && d.join("crates/lightgui-designer").is_dir() + } + if let Ok(exe) = std::env::current_exe() { + let mut dir = exe.parent().map(|p| p.to_path_buf()); + while let Some(d) = dir { + if looks_like_checkout(&d) { + return d.to_string_lossy().to_string(); + } + dir = d.parent().map(|p| p.to_path_buf()); + } + } + if let Ok(cwd) = std::env::current_dir() { + if looks_like_checkout(&cwd) { + return cwd.to_string_lossy().to_string(); + } + } + String::new() +} + +/// The C repo (for the widget headers + palette icons the Rust +/// install copies): the `lightgui` sibling of the Rust checkout. +/// "" when there is none (the Makefile then skips those parts). +fn find_cmake_src_dir(rust_repo: &str) -> String { + let sib = std::path::Path::new(rust_repo).join("../lightgui"); + if sib.join("widgets/icons").is_dir() { + if let Ok(c) = std::fs::canonicalize(&sib) { + return c.to_string_lossy().to_string(); + } + return sib.to_string_lossy().to_string(); + } + String::new() +} + +/// Ask for the sudo password (used only when pkexec is missing). +/// Returns None when the user cancels. +fn ask_sudo_password(app: &Shared) -> Option { + let parent = app.borrow().main_window.clone(); + let dlg = gtk::Dialog::with_buttons( + Some("Install Libraries — sudo password"), + Some(&parent), + gtk::DialogFlags::MODAL, + &[ + ("Cancel", gtk::ResponseType::Cancel), + ("Install", gtk::ResponseType::Accept), + ], + ); + // Enter in the password entry must submit (activates_default needs + // a default response set on the dialog). + dlg.set_default_response(gtk::ResponseType::Accept); + let label = gtk::Label::new(Some( + "pkexec is not installed, so the install will run with sudo.\n\ + Enter your login password (the one sudo asks for):", + )); + label.set_selectable(true); + let entry = gtk::Entry::new(); + entry.set_visibility(false); + entry.set_activates_default(true); + // Enter in the entry must submit even if the default-widget chain + // does not fire (verified live: Return alone did not submit, so + // wire the entry's activate signal directly to Accept). + { + let d = dlg.clone(); + entry.connect_activate(move |_| { + d.response(gtk::ResponseType::Accept); + }); + } + let vbox = gtk::Box::new(gtk::Orientation::Vertical, 8); + vbox.set_border_width(12); + vbox.pack_start(&label, false, false, 0); + vbox.pack_start(&entry, false, false, 0); + dlg.content_area().pack_start(&vbox, false, false, 0); + dlg.show_all(); + let ok = dlg.run() == gtk::ResponseType::Accept; + let pw = if ok { + entry.text().to_string() + } else { + String::new() + }; + // hide(), not close(): close() leaves the dialog mapped in this + // gtk-rs build (verified live — the dialog stayed visible). + dlg.hide(); + if ok { + Some(pw) + } else { + None + } +} + +fn on_install_libraries(app: &Shared) { + let repo = find_checkout_dir(); + if repo.is_empty() { + app.borrow().status("Install Libraries needs a LightGUI checkout"); + let parent = app.borrow().main_window.clone(); + let msg = gtk::MessageDialog::new( + Some(&parent), + gtk::DialogFlags::MODAL, + gtk::MessageType::Error, + gtk::ButtonsType::Ok, + "Install Libraries needs a LightGUI checkout.\n\n\ + Run the designer from the checkout (cargo run -p \ + lightgui-designer) and try again.", + ); + msg.run(); + msg.close(); + return; + } + let parent = app.borrow().main_window.clone(); + let confirm = gtk::Dialog::with_buttons( + Some("Install Libraries"), + Some(&parent), + gtk::DialogFlags::MODAL, + &[ + ("Cancel", gtk::ResponseType::Cancel), + ("Install", gtk::ResponseType::Accept), + ], + ); + confirm.set_default_response(gtk::ResponseType::Accept); + let label = gtk::Label::new(Some(&format!( + "Install LightGUI (Rust) system-wide from:\n{repo}\n\n\ + \u{2022} Widget headers \u{2192} /usr/local/include/lightgui\n\ + \u{2003}(what generated apps include and link against)\n\ + \u{2022} Designer binary \u{2192} /usr/local/bin/lightgui-designer\n\ + \u{2022} Palette icons \u{2192} /usr/local/share/lightgui/icons\n\ + \u{2022} Desktop entry + icon \u{2192} the app menu\n\n\ + The widgets are header-only, so there are no shared\n\ + libraries and no ldconfig step.\n\n\ + This runs as root: a password prompt will appear." + ))); + label.set_selectable(true); + confirm.content_area().pack_start(&label, false, false, 0); + confirm.show_all(); + let go = confirm.run() == gtk::ResponseType::Accept; + // hide(), not close(): close() leaves the dialog mapped in this + // gtk-rs build (verified live — the dialog stayed visible). + confirm.hide(); + if !go { + return; + } + + // Step 1: make sure the binary is freshly built, as the user, so + // the privileged install never rebuilds (and never leaves + // root-owned build artefacts in the checkout). + app.borrow() + .status("Install Libraries: building designer…"); + let cargo = find_cargo(); + let (rc, out) = run_command_capture( + &cargo, + &["build", "-p", "lightgui-designer"], + "", + Some(&repo), + ); + if rc != 0 { + show_log_dialog( + app, + "Install Libraries", + "Install FAILED — the designer did not build.\n\n\ + Rebuild it first (cargo build -p lightgui-designer) \ + and try again.\n\n", + &out, + ); + app.borrow() + .status("Install Libraries FAILED — build failed"); + return; + } + // Step 2: the install itself, as root. + app.borrow() + .status("Install Libraries: installing as root…"); + let make = { + let m = find_program("make"); + if m.is_empty() { + "/usr/bin/make".to_string() + } else { + m + } + }; + let c_src = find_cmake_src_dir(&repo); + let src_arg = if c_src.is_empty() { + String::new() + } else { + format!("LIGHTGUI_SRC={c_src}") + }; + let mut install_args: Vec<&str> = + vec![make.as_str(), "-C", &repo, "install"]; + if !src_arg.is_empty() { + install_args.push(src_arg.as_str()); + } + let pkexec = find_program("pkexec"); + let (rc, out) = if !pkexec.is_empty() { + let mut args: Vec<&str> = vec![pkexec.as_str()]; + args.extend_from_slice(&install_args); + run_command_capture(&args[0], &args[1..], "", None) + } else { + let Some(mut pw) = ask_sudo_password(app) else { + app.borrow().status("Install Libraries cancelled"); + return; + }; + pw.push('\n'); + let mut args: Vec<&str> = vec!["sudo", "-S"]; + args.extend_from_slice(&install_args); + let res = run_command_capture(&args[0], &args[1..], &pw, None); + // Wipe the password from memory as soon as sudo has it. + // (as_bytes_mut is unsafe: it can break String's UTF-8 + // invariant, but writing zeros is safe here.) + unsafe { + for b in pw.as_bytes_mut() { + *b = 0; + } + } + res + }; + if rc == 0 { + show_log_dialog( + app, + "Install Libraries", + "Install succeeded.\n\n\ + LightGUI (Rust) is now available system-wide:\n\ + \u{2022} run `lightgui-designer` from anywhere\n\ + \u{2022} generated apps build against\n\ + \u{2003}/usr/local/include/lightgui\n\ + \u{2022} LightGUI (Rust) is in the app menu\n\n", + &out, + ); + app.borrow() + .status("Install Libraries: installed to /usr/local"); + } else { + show_log_dialog(app, "Install Libraries", "Install FAILED.\n\n", &out); + app.borrow() + .status("Install Libraries FAILED — see log"); + } +} + fn on_open(app: &Shared) { let parent = app.borrow().main_window.clone(); let dlg = gtk::FileChooserDialog::builder() diff --git a/crates/lightgui-designer/src/palette_ui.rs b/crates/lightgui-designer/src/palette_ui.rs index 41f9fc2..65e1a14 100644 --- a/crates/lightgui-designer/src/palette_ui.rs +++ b/crates/lightgui-designer/src/palette_ui.rs @@ -23,6 +23,7 @@ pub fn xframe_icons_dir() -> String { for cand in [ "widgets/icons", "../widgets/icons", + "/usr/local/share/lightgui/icons", "/opt/lightgui/widgets/icons", ] { if std::path::Path::new(&format!("{cand}/GtkButton.ico")).is_file() { diff --git a/crates/lightgui-designer/ui/designer.ui b/crates/lightgui-designer/ui/designer.ui index 8178f3f..caf3286 100644 --- a/crates/lightgui-designer/ui/designer.ui +++ b/crates/lightgui-designer/ui/designer.ui @@ -217,6 +217,23 @@ + + + Tools + True + + + True + + + Install Libraries… + True + + + + + + Help diff --git a/lightgui-designer.desktop b/lightgui-designer.desktop new file mode 100644 index 0000000..68bf54c --- /dev/null +++ b/lightgui-designer.desktop @@ -0,0 +1,10 @@ +[Desktop Entry] +Type=Application +Name=LightGUI (Rust) +GenericName=GUI Designer +Comment=Visual GUI designer for GTK 3, rewritten in Rust +Exec=lightgui-designer +Icon=lightgui-designer +Terminal=false +Categories=Development;IDE;GUIDesigner; +Keywords=GTK;GUI;designer;widgets;IDE;Rust;