|
| 1 | +//! The top-left project status bar: it shows where the current project lives |
| 2 | +//! and exposes the always-available actions the launcher used to own (Exit, |
| 3 | +//! open the project folder, open the write-data dir, upload the log). |
| 4 | +//! |
| 5 | +//! Modularity: the higher level owns a single `#project-status-root` div in the |
| 6 | +//! panel document; everything else — markup, wiring, and the effects — lives |
| 7 | +//! here. The panel manager only renders the caption each frame and forwards any |
| 8 | +//! queued actions, so this feature stays self-contained. |
| 9 | +
|
| 10 | +mod upload; |
| 11 | + |
| 12 | +use std::cell::RefCell; |
| 13 | +use std::path::{Path, PathBuf}; |
| 14 | +use std::rc::Rc; |
| 15 | + |
| 16 | +use spring_native::prelude::{Error, NativeInterfaceRef}; |
| 17 | + |
| 18 | +use crate::sbc::command_system::command::Command; |
| 19 | +use crate::sbc::command_system::model::Models; |
| 20 | +use crate::sbc::notifications::NotificationManager; |
| 21 | +use crate::sbc::project::ProjectManager; |
| 22 | +use crate::sbc::rml::{element_by_id, escape_rml}; |
| 23 | + |
| 24 | +pub(crate) use upload::UploadLogCommand; |
| 25 | + |
| 26 | +const ROOT_ID: &str = "project-status-root"; |
| 27 | +const LABEL_ID: &str = "project-status-label"; |
| 28 | + |
| 29 | +#[derive(Clone, Copy)] |
| 30 | +enum StatusBarAction { |
| 31 | + Exit, |
| 32 | + OpenProject, |
| 33 | + DataDir, |
| 34 | + UploadLog, |
| 35 | +} |
| 36 | + |
| 37 | +/// One button in the bar: its element id and the action it queues. |
| 38 | +const BUTTONS: [(&str, &str, StatusBarAction); 4] = [ |
| 39 | + ( |
| 40 | + "project-status-open", |
| 41 | + "Open Project", |
| 42 | + StatusBarAction::OpenProject, |
| 43 | + ), |
| 44 | + ("project-status-data", "Data Dir", StatusBarAction::DataDir), |
| 45 | + ( |
| 46 | + "project-status-upload", |
| 47 | + "Upload Log", |
| 48 | + StatusBarAction::UploadLog, |
| 49 | + ), |
| 50 | + ("project-status-exit", "Exit", StatusBarAction::Exit), |
| 51 | +]; |
| 52 | + |
| 53 | +#[derive(Default)] |
| 54 | +pub(crate) struct ProjectStatusBar { |
| 55 | + actions: Rc<RefCell<Vec<StatusBarAction>>>, |
| 56 | + /// Set once the markup and listeners are installed in the live document. |
| 57 | + /// Cleared by `forget` when a reload throws that document away. |
| 58 | + bound: bool, |
| 59 | + last_caption: Option<String>, |
| 60 | +} |
| 61 | + |
| 62 | +impl ProjectStatusBar { |
| 63 | + /// The document was destroyed (reload): drop every cached bit of state so |
| 64 | + /// the next `render` rebuilds against the fresh document. |
| 65 | + pub(crate) fn forget(&mut self) { |
| 66 | + self.bound = false; |
| 67 | + self.last_caption = None; |
| 68 | + self.actions.borrow_mut().clear(); |
| 69 | + } |
| 70 | + |
| 71 | + /// Install the bar the first time, then keep its caption in sync. |
| 72 | + pub(crate) fn render( |
| 73 | + &mut self, |
| 74 | + interface: &NativeInterfaceRef, |
| 75 | + document: u64, |
| 76 | + models: &mut Models, |
| 77 | + ) -> Result<(), Error> { |
| 78 | + if !self.bound { |
| 79 | + self.build(interface, document)?; |
| 80 | + self.bound = true; |
| 81 | + } |
| 82 | + let caption = caption(models); |
| 83 | + if self.last_caption.as_deref() != Some(caption.as_str()) { |
| 84 | + if let Some(label) = element_by_id(interface, document, LABEL_ID) { |
| 85 | + interface |
| 86 | + .rml_ui() |
| 87 | + .element_set_inner_rml(label, &escape_rml(&caption))?; |
| 88 | + } |
| 89 | + self.last_caption = Some(caption); |
| 90 | + } |
| 91 | + Ok(()) |
| 92 | + } |
| 93 | + |
| 94 | + /// Run whatever the user clicked. Synchronous effects happen here; the log |
| 95 | + /// upload is returned as a command so it can queue background IO. |
| 96 | + pub(crate) fn process( |
| 97 | + &mut self, |
| 98 | + interface: &NativeInterfaceRef, |
| 99 | + models: &mut Models, |
| 100 | + ) -> Vec<Box<dyn Command>> { |
| 101 | + let mut commands: Vec<Box<dyn Command>> = Vec::new(); |
| 102 | + for action in self.actions.borrow_mut().drain(..) { |
| 103 | + match action { |
| 104 | + StatusBarAction::Exit => { |
| 105 | + let _ = interface.messages().send_commands("quitforce", ""); |
| 106 | + } |
| 107 | + StatusBarAction::OpenProject => match models.get::<ProjectManager>().path() { |
| 108 | + Some(path) => open_in_file_manager(Path::new(path)), |
| 109 | + None => models.get::<NotificationManager>().warn( |
| 110 | + "project-folder", |
| 111 | + "Save the project first to open its folder", |
| 112 | + ), |
| 113 | + }, |
| 114 | + StatusBarAction::DataDir => open_in_file_manager(&write_dir()), |
| 115 | + StatusBarAction::UploadLog => { |
| 116 | + models.get::<NotificationManager>().progress( |
| 117 | + "upload-log", |
| 118 | + 0.1, |
| 119 | + "Uploading log...", |
| 120 | + ); |
| 121 | + commands.push(Box::new(UploadLogCommand::new(log_path()))); |
| 122 | + } |
| 123 | + } |
| 124 | + } |
| 125 | + commands |
| 126 | + } |
| 127 | + |
| 128 | + fn build(&self, interface: &NativeInterfaceRef, document: u64) -> Result<(), Error> { |
| 129 | + let Some(root) = element_by_id(interface, document, ROOT_ID) else { |
| 130 | + return Ok(()); |
| 131 | + }; |
| 132 | + let mut html = format!(r#"<div class="project-status-label" id="{LABEL_ID}"></div>"#); |
| 133 | + html.push_str(r#"<div class="project-status-buttons">"#); |
| 134 | + for (id, caption, _) in BUTTONS { |
| 135 | + let danger = if id == "project-status-exit" { |
| 136 | + " danger" |
| 137 | + } else { |
| 138 | + "" |
| 139 | + }; |
| 140 | + html.push_str(&format!( |
| 141 | + r#"<button id="{id}" class="project-status-btn{danger}">{caption}</button>"# |
| 142 | + )); |
| 143 | + } |
| 144 | + html.push_str("</div>"); |
| 145 | + interface.rml_ui().element_set_inner_rml(root, &html)?; |
| 146 | + |
| 147 | + for (id, _, action) in BUTTONS { |
| 148 | + let Some(button) = element_by_id(interface, document, id) else { |
| 149 | + continue; |
| 150 | + }; |
| 151 | + let queue = self.actions.clone(); |
| 152 | + interface |
| 153 | + .rml_ui() |
| 154 | + .element_add_event_listener(button, "click", false, move || { |
| 155 | + queue.borrow_mut().push(action); |
| 156 | + })?; |
| 157 | + } |
| 158 | + Ok(()) |
| 159 | + } |
| 160 | +} |
| 161 | + |
| 162 | +/// The engine's write-data dir, taken from its own `--write-dir` argv (the |
| 163 | +/// plugin shares the engine process). Falls back to the working directory. |
| 164 | +pub(crate) fn write_dir() -> PathBuf { |
| 165 | + let mut args = std::env::args(); |
| 166 | + while let Some(arg) = args.next() { |
| 167 | + if let Some(rest) = arg.strip_prefix("--write-dir=") { |
| 168 | + return PathBuf::from(rest); |
| 169 | + } |
| 170 | + if arg == "--write-dir" { |
| 171 | + if let Some(next) = args.next() { |
| 172 | + return PathBuf::from(next); |
| 173 | + } |
| 174 | + } |
| 175 | + } |
| 176 | + std::env::current_dir().unwrap_or_default() |
| 177 | +} |
| 178 | + |
| 179 | +fn caption(models: &mut Models) -> String { |
| 180 | + match models.get::<ProjectManager>().path() { |
| 181 | + Some(path) => format!("Project: {path}"), |
| 182 | + None => "Project not saved".to_string(), |
| 183 | + } |
| 184 | +} |
| 185 | + |
| 186 | +fn log_path() -> PathBuf { |
| 187 | + write_dir().join("infolog.txt") |
| 188 | +} |
| 189 | + |
| 190 | +fn open_in_file_manager(path: &Path) { |
| 191 | + let opener = if cfg!(target_os = "macos") { |
| 192 | + "open" |
| 193 | + } else if cfg!(target_os = "windows") { |
| 194 | + "explorer" |
| 195 | + } else { |
| 196 | + "xdg-open" |
| 197 | + }; |
| 198 | + if let Err(err) = std::process::Command::new(opener).arg(path).spawn() { |
| 199 | + log::warn!("could not open {}: {err}", path.display()); |
| 200 | + } |
| 201 | +} |
0 commit comments