diff --git a/crates/eww/src/app.rs b/crates/eww/src/app.rs index 98d748c..fe01bac 100644 --- a/crates/eww/src/app.rs +++ b/crates/eww/src/app.rs @@ -71,16 +71,20 @@ pub enum DaemonCommand { PrintWindows(DaemonResponseSender), } +/// An opened window. #[derive(Debug)] pub struct EwwWindow { pub name: String, - pub definition: yuck::config::window_definition::WindowDefinition, pub scope_index: ScopeIndex, pub gtk_window: gtk::Window, pub destroy_event_handler_id: Option, } impl EwwWindow { + /// Close the GTK window and disconnect the destroy event-handler. + /// + /// You need to make sure that the scope get's properly cleaned from the state graph + /// and that script-vars get cleaned up properly pub fn close(self) { log::info!("Closing gtk window {}", self.name); self.gtk_window.close(); @@ -93,12 +97,14 @@ impl EwwWindow { pub struct App { pub scope_graph: Rc>, pub eww_config: config::EwwConfig, + /// Map of all currently open windows pub open_windows: HashMap, /// Window names that are supposed to be open, but failed. /// When reloading the config, these should be opened again. pub failed_windows: HashSet, pub css_provider: gtk::CssProvider, + /// Sender to send [`DaemonCommand`]s pub app_evt_send: UnboundedSender, pub script_var_handler: ScriptVarHandlerHandle, @@ -118,7 +124,7 @@ impl std::fmt::Debug for App { } impl App { - /// Handle a [DaemonCommand] event. + /// Handle a [`DaemonCommand`] event. pub fn handle_command(&mut self, event: DaemonCommand) { log::debug!("Handling event: {:?}", &event); let result: Result<_> = try { @@ -188,8 +194,7 @@ impl App { let scope_graph = self.scope_graph.borrow(); let used_globals_names = scope_graph.currently_used_globals(); let output = scope_graph - .scope_at(scope_graph.root_index) - .expect("No global scope in scopegraph") + .global_scope() .data .iter() .filter(|(key, _)| all || used_globals_names.contains(*key)) @@ -199,7 +204,7 @@ impl App { } DaemonCommand::GetVar { name, sender } => { let scope_graph = &*self.scope_graph.borrow(); - let vars = &scope_graph.scope_at(scope_graph.root_index).expect("No root scope in graph").data; + let vars = &scope_graph.global_scope().data; match vars.get(name.as_str()) { Some(x) => sender.send_success(x.to_string())?, None => sender.send_failure(format!("Variable not found \"{}\"", name))?, @@ -275,6 +280,7 @@ impl App { } } + /// Close a window and do all the required cleanups in the scope_graph and script_var_handler fn close_window(&mut self, window_name: &str) -> Result<()> { let eww_window = self .open_windows @@ -446,13 +452,7 @@ fn initialize_window( window.show_all(); - Ok(EwwWindow { - name: window_def.name.clone(), - definition: window_def, - gtk_window: window, - scope_index: window_scope, - destroy_event_handler_id: None, - }) + Ok(EwwWindow { name: window_def.name.clone(), gtk_window: window, scope_index: window_scope, destroy_event_handler_id: None }) } /// Apply the provided window-positioning rules to the window. diff --git a/crates/eww/src/paths.rs b/crates/eww/src/paths.rs index fc35d80..376e9a6 100644 --- a/crates/eww/src/paths.rs +++ b/crates/eww/src/paths.rs @@ -6,6 +6,7 @@ use std::{ use anyhow::{bail, Result}; +/// Stores references to all the paths relevant to eww, and abstracts access to these files and directories #[derive(Debug, Clone)] pub struct EwwPaths { pub log_file: PathBuf, diff --git a/crates/eww/src/state/scope_graph.rs b/crates/eww/src/state/scope_graph.rs index 052a1c4..43dc0f5 100644 --- a/crates/eww/src/state/scope_graph.rs +++ b/crates/eww/src/state/scope_graph.rs @@ -124,14 +124,17 @@ impl ScopeGraph { pub fn currently_unused_globals(&self) -> HashSet { let used_variables = self.currently_used_globals(); - let global_scope = self.graph.scope_at(self.root_index).expect("No root scope in graph"); - global_scope.data.keys().cloned().collect::>().difference(&used_variables).cloned().collect() + self.global_scope().data.keys().cloned().collect::>().difference(&used_variables).cloned().collect() } pub fn scope_at(&self, index: ScopeIndex) -> Option<&Scope> { self.graph.scope_at(index) } + pub fn global_scope(&self) -> &Scope { + self.graph.scope_at(self.root_index).expect("No root scope in graph") + } + /// Evaluate a [SimplExpr] in a given scope. This will return `Err` if any referenced variables /// are not available in the scope. If evaluation fails for other reasons (bad types, etc) /// this will print a warning and return an empty string instead.