Add some comments

This commit is contained in:
elkowar 2022-09-03 21:06:50 +02:00
parent d3b94cc76e
commit 58907eac4b
No known key found for this signature in database
GPG key ID: E321AD71B1D1F27F
3 changed files with 18 additions and 14 deletions

View file

@ -71,16 +71,20 @@ pub enum DaemonCommand {
PrintWindows(DaemonResponseSender), PrintWindows(DaemonResponseSender),
} }
/// An opened window.
#[derive(Debug)] #[derive(Debug)]
pub struct EwwWindow { pub struct EwwWindow {
pub name: String, pub name: String,
pub definition: yuck::config::window_definition::WindowDefinition,
pub scope_index: ScopeIndex, pub scope_index: ScopeIndex,
pub gtk_window: gtk::Window, pub gtk_window: gtk::Window,
pub destroy_event_handler_id: Option<glib::SignalHandlerId>, pub destroy_event_handler_id: Option<glib::SignalHandlerId>,
} }
impl EwwWindow { 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) { pub fn close(self) {
log::info!("Closing gtk window {}", self.name); log::info!("Closing gtk window {}", self.name);
self.gtk_window.close(); self.gtk_window.close();
@ -93,12 +97,14 @@ impl EwwWindow {
pub struct App { pub struct App {
pub scope_graph: Rc<RefCell<ScopeGraph>>, pub scope_graph: Rc<RefCell<ScopeGraph>>,
pub eww_config: config::EwwConfig, pub eww_config: config::EwwConfig,
/// Map of all currently open windows
pub open_windows: HashMap<String, EwwWindow>, pub open_windows: HashMap<String, EwwWindow>,
/// Window names that are supposed to be open, but failed. /// Window names that are supposed to be open, but failed.
/// When reloading the config, these should be opened again. /// When reloading the config, these should be opened again.
pub failed_windows: HashSet<String>, pub failed_windows: HashSet<String>,
pub css_provider: gtk::CssProvider, pub css_provider: gtk::CssProvider,
/// Sender to send [`DaemonCommand`]s
pub app_evt_send: UnboundedSender<DaemonCommand>, pub app_evt_send: UnboundedSender<DaemonCommand>,
pub script_var_handler: ScriptVarHandlerHandle, pub script_var_handler: ScriptVarHandlerHandle,
@ -118,7 +124,7 @@ impl std::fmt::Debug for App {
} }
impl App { impl App {
/// Handle a [DaemonCommand] event. /// Handle a [`DaemonCommand`] event.
pub fn handle_command(&mut self, event: DaemonCommand) { pub fn handle_command(&mut self, event: DaemonCommand) {
log::debug!("Handling event: {:?}", &event); log::debug!("Handling event: {:?}", &event);
let result: Result<_> = try { let result: Result<_> = try {
@ -188,8 +194,7 @@ impl App {
let scope_graph = self.scope_graph.borrow(); let scope_graph = self.scope_graph.borrow();
let used_globals_names = scope_graph.currently_used_globals(); let used_globals_names = scope_graph.currently_used_globals();
let output = scope_graph let output = scope_graph
.scope_at(scope_graph.root_index) .global_scope()
.expect("No global scope in scopegraph")
.data .data
.iter() .iter()
.filter(|(key, _)| all || used_globals_names.contains(*key)) .filter(|(key, _)| all || used_globals_names.contains(*key))
@ -199,7 +204,7 @@ impl App {
} }
DaemonCommand::GetVar { name, sender } => { DaemonCommand::GetVar { name, sender } => {
let scope_graph = &*self.scope_graph.borrow(); 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()) { match vars.get(name.as_str()) {
Some(x) => sender.send_success(x.to_string())?, Some(x) => sender.send_success(x.to_string())?,
None => sender.send_failure(format!("Variable not found \"{}\"", name))?, 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<()> { fn close_window(&mut self, window_name: &str) -> Result<()> {
let eww_window = self let eww_window = self
.open_windows .open_windows
@ -446,13 +452,7 @@ fn initialize_window(
window.show_all(); window.show_all();
Ok(EwwWindow { Ok(EwwWindow { name: window_def.name.clone(), gtk_window: window, scope_index: window_scope, destroy_event_handler_id: None })
name: window_def.name.clone(),
definition: window_def,
gtk_window: window,
scope_index: window_scope,
destroy_event_handler_id: None,
})
} }
/// Apply the provided window-positioning rules to the window. /// Apply the provided window-positioning rules to the window.

View file

@ -6,6 +6,7 @@ use std::{
use anyhow::{bail, Result}; use anyhow::{bail, Result};
/// Stores references to all the paths relevant to eww, and abstracts access to these files and directories
#[derive(Debug, Clone)] #[derive(Debug, Clone)]
pub struct EwwPaths { pub struct EwwPaths {
pub log_file: PathBuf, pub log_file: PathBuf,

View file

@ -124,14 +124,17 @@ impl ScopeGraph {
pub fn currently_unused_globals(&self) -> HashSet<VarName> { pub fn currently_unused_globals(&self) -> HashSet<VarName> {
let used_variables = self.currently_used_globals(); let used_variables = self.currently_used_globals();
let global_scope = self.graph.scope_at(self.root_index).expect("No root scope in graph"); self.global_scope().data.keys().cloned().collect::<HashSet<_>>().difference(&used_variables).cloned().collect()
global_scope.data.keys().cloned().collect::<HashSet<_>>().difference(&used_variables).cloned().collect()
} }
pub fn scope_at(&self, index: ScopeIndex) -> Option<&Scope> { pub fn scope_at(&self, index: ScopeIndex) -> Option<&Scope> {
self.graph.scope_at(index) 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 /// 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) /// 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. /// this will print a warning and return an empty string instead.