feat: added better dynamic update

This commit is contained in:
Byson94
2025-08-05 17:39:11 +05:30
parent 3a98e9c9cc
commit 9a8109232f
6 changed files with 45 additions and 39 deletions

View File

@@ -276,13 +276,13 @@ impl<B: DisplayBackend> App<B> {
if let Some(old_abort_send) = self.window_close_timer_abort_senders.remove(instance_id) {
_ = old_abort_send.send(());
}
let eww_window = self
let ewwii_window = self
.open_windows
.remove(instance_id)
.with_context(|| format!("Tried to close window with id '{instance_id}', but no such window was open"))?;
// let scope_index = eww_window.scope_index;
eww_window.close();
// let scope_index = ewwii_window.scope_index;
ewwii_window.close();
if auto_reopen {
self.failed_windows.insert(instance_id.to_string());
@@ -329,15 +329,15 @@ impl<B: DisplayBackend> App<B> {
// )?;
let root_widget = build_gtk_widget(WidgetInput::Window(window_def))?;
crate::updates::handle_state_changes(self.ewwii_config.get_root_node()?);
// crate::updates::handle_state_changes(self.ewwii_config.get_root_node()?, self.paths.get_rhai_path());
root_widget.style_context().add_class(window_name);
let monitor = get_gdk_monitor(initiator.monitor.clone())?;
let mut eww_window = initialize_window::<B>(&initiator, monitor, root_widget)?;
eww_window.gtk_window.style_context().add_class(window_name);
let mut ewwii_window = initialize_window::<B>(&initiator, monitor, root_widget)?;
ewwii_window.gtk_window.style_context().add_class(window_name);
eww_window.destroy_event_handler_id = Some(eww_window.gtk_window.connect_destroy({
ewwii_window.destroy_event_handler_id = Some(ewwii_window.gtk_window.connect_destroy({
let app_evt_sender = self.app_evt_send.clone();
let instance_id = instance_id.to_string();
move |_| {
@@ -388,7 +388,7 @@ impl<B: DisplayBackend> App<B> {
}
}
self.open_windows.insert(instance_id.to_string(), eww_window);
self.open_windows.insert(instance_id.to_string(), ewwii_window);
Ok(())
})();

View File

@@ -12,7 +12,6 @@ pub struct EwwPaths {
pub log_file: PathBuf,
pub log_dir: PathBuf,
pub ipc_socket_file: PathBuf,
pub iirhai_ipc_socket_file: PathBuf,
pub config_dir: PathBuf,
}
@@ -41,20 +40,11 @@ impl EwwPaths {
.unwrap_or_else(|_| std::path::PathBuf::from("/tmp"))
.join(format!("ewwii-server_{}", daemon_id));
let iirhai_ipc_socket_file = std::env::var("XDG_RUNTIME_DIR")
.map(std::path::PathBuf::from)
.unwrap_or_else(|_| std::path::PathBuf::from("/tmp"))
.join(format!("ewwii_iirhai-server_{}", daemon_id));
// 100 as the limit isn't quite 108 everywhere (i.e 104 on BSD or mac)
if format!("{}", ipc_socket_file.display()).len() > 100 {
log::warn!("The IPC socket file's absolute path exceeds 100 bytes, the socket may fail to create.");
}
if format!("{}", iirhai_ipc_socket_file.display()).len() > 100 {
log::warn!("The iirhai IPC socket file's absolute path exceeds 100 bytes, the socket may fail to create.");
}
let log_dir = std::env::var("XDG_CACHE_HOME")
.map(PathBuf::from)
.unwrap_or_else(|_| PathBuf::from(std::env::var("HOME").unwrap()).join(".cache"))
@@ -70,7 +60,6 @@ impl EwwPaths {
log_file: log_dir.join(format!("eww_{}.log", daemon_id)),
log_dir,
ipc_socket_file,
iirhai_ipc_socket_file,
})
}
@@ -95,10 +84,6 @@ impl EwwPaths {
self.ipc_socket_file.as_path()
}
pub fn get_iirhai_ipc_socket_file(&self) -> &Path {
self.iirhai_ipc_socket_file.as_path()
}
pub fn get_config_dir(&self) -> &Path {
self.config_dir.as_path()
}

View File

@@ -18,16 +18,22 @@ mod listen;
mod poll;
use iirhai::widgetnode::WidgetNode;
use iirhai::parser::ParseConfig;
use crate::widgets::build_widget::{build_gtk_widget, WidgetInput};
use listen::handle_listen;
use poll::handle_poll;
use rhai::{Dynamic, Scope};
use std::collections::HashMap;
use std::sync::Arc;
use std::sync::RwLock;
use std::{
collections::HashMap,
sync::Arc,
sync::RwLock,
path::{PathBuf, Path},
};
use anyhow::{bail, Result};
pub type ReactiveVarStore = Arc<RwLock<HashMap<String, String>>>;
pub fn handle_state_changes(enter_node: WidgetNode) {
pub fn handle_state_changes(enter_node: WidgetNode, code_path: PathBuf) {
/// Enter node is the WidgetNode of Enter()
/// it is the very root of every config.
let store: ReactiveVarStore = Arc::new(RwLock::new(HashMap::new()));
@@ -52,18 +58,24 @@ pub fn handle_state_changes(enter_node: WidgetNode) {
let store_clone = store.clone();
tokio::spawn(async move {
while let Some(var_name) = rx.recv().await {
log::debug!("Reactive var changed: {}", var_name);
let vars = store_clone.read().unwrap().clone();
re_eval_widgets(&vars).await;
reeval_and_update(&vars, &code_path).await;
}
});
}
pub async fn re_eval_widgets(all_vars: &HashMap<String, String>) {
pub async fn reeval_and_update(all_vars: &HashMap<String, String>, code_path: &Path) -> Result<()> {
let mut scope = Scope::new();
for (name, val) in all_vars {
scope.set_value(name.clone(), Dynamic::from(val.clone()));
}
// TODO: added re-eval here later;
if !code_path.exists() {
bail!("The configuration file `{}` does not exist", code_path.display());
}
let mut reeval_parser = ParseConfig::new();
let new_config_tree = reeval_parser.parse_widget_file_from_scope(code_path, scope);
Ok(())
}

View File

@@ -29,7 +29,7 @@ impl ObjectSubclass for WindowPriv {
type ParentType = gtk::Window;
type Type = Window;
const NAME: &'static str = "WindowEww";
const NAME: &'static str = "WindowEwwii";
}
impl Default for Window {

View File

@@ -42,4 +42,13 @@ impl ParseConfig {
let code = fs::read_to_string(&file_path).map_err(|e| anyhow!("Failed to read {:?}: {}", file_path.as_ref(), e))?;
self.parse_widget_code(&code)
}
pub fn parse_widget_code_from_scope(&mut self, code: &str, mut scope: Scope) -> Result<WidgetNode> {
self.engine.eval_with_scope::<WidgetNode>(&mut scope, code).map_err(|e| anyhow!(format_rhai_error(&e, code)))
}
pub fn parse_widget_file_from_scope<P: AsRef<Path>>(&mut self, file_path: P, scope: Scope) -> Result<WidgetNode> {
let code = fs::read_to_string(&file_path).map_err(|e| anyhow!("Failed to read {:?}: {}", file_path.as_ref(), e))?;
self.parse_widget_code_from_scope(&code, scope)
}
}

View File

@@ -29,7 +29,7 @@
mkRustToolchain = pkgs: pkgs.rust-bin.fromRustupToolchainFile ./rust-toolchain.toml;
in
{
overlays.default = final: prev: { inherit (self.packages.${prev.system}) eww eww-wayland; };
overlays.default = final: prev: { inherit (self.packages.${prev.system}) ewwii ewwii-wayland; };
packages = nixpkgs.lib.genAttrs targetSystems (
system:
@@ -40,10 +40,10 @@
cargo = rust;
rustc = rust;
};
version = (builtins.fromTOML (builtins.readFile ./crates/eww/Cargo.toml)).package.version;
version = (builtins.fromTOML (builtins.readFile ./crates/ewwii/Cargo.toml)).package.version;
in
rec {
eww = rustPlatform.buildRustPackage {
ewwii = rustPlatform.buildRustPackage {
version = "${version}-dirty";
pname = "ewwii";
@@ -66,8 +66,8 @@
];
};
eww-wayland = nixpkgs.lib.warn "`eww-wayland` is deprecated due to eww building with both X11 and wayland support by default. Use `eww` instead." eww;
default = eww;
ewwii-wayland = nixpkgs.lib.warn "`ewwii-wayland` is deprecated due to ewwii building with both X11 and wayland support by default. Use `ewwii` instead." ewwii;
default = ewwii;
}
);
@@ -79,7 +79,7 @@
in
{
default = pkgs.mkShell {
inputsFrom = [ self.packages.${system}.eww ];
inputsFrom = [ self.packages.${system}.ewwii ];
packages = with pkgs; [
deno
mdbook