1
0
Fork 0
mirror of https://github.com/Zedfrigg/ironbar.git synced 2025-04-19 19:34:24 +02:00

fix: only update relevant parts of state upon property change

This commit is contained in:
Reinout Meliesie 2024-06-19 20:57:26 +02:00
parent d56f76f6a8
commit 465d16800a
Signed by: zedfrigg
GPG key ID: 3AFCC06481308BC6

View file

@ -35,7 +35,32 @@ struct ClientInner<'l> {
dbus_connection: Connection,
}
macro_rules! initialise_path_map {
impl Client {
fn new() -> Result<Client> {
let state = Mutable::new(State {
wired: WiredState::Unknown,
wifi: WifiState::Unknown,
cellular: CellularState::Unknown,
vpn: VpnState::Unknown,
});
let dbus_connection = Connection::system()?;
let root_object = {
let root_object = DbusProxyBlocking::new(&dbus_connection)?;
// Workaround for the fact that zbus (unnecessarily) requires a static lifetime here
Box::leak(Box::new(root_object))
};
Ok(Client(Arc::new(ClientInner {
state,
root_object,
active_connections: RwLock::new(HashMap::new()),
devices: RwLock::new(HashMap::new()),
dbus_connection,
})))
}
fn run(&self) -> Result<()> {
macro_rules! initialise_path_map {
(
$client:expr,
$path_map:ident,
@ -56,9 +81,9 @@ macro_rules! initialise_path_map {
}
*write_lock!($client.$path_map) = path_map;
};
}
}
macro_rules! spawn_path_list_watcher {
macro_rules! spawn_path_list_watcher {
(
$client:expr,
$property:ident,
@ -105,14 +130,15 @@ macro_rules! spawn_path_list_watcher {
Ok(())
});
}
}
}
macro_rules! spawn_property_watcher {
macro_rules! spawn_property_watcher {
(
$client:expr,
$path:expr,
$property_changes:ident,
$containing_list:ident
$containing_list:ident,
|$inner_client:ident| $state_update:expr
) => {
let client = $client.clone();
let path = $path.clone();
@ -125,51 +151,30 @@ macro_rules! spawn_property_watcher {
if !read_lock!(client.$containing_list).contains_key(&path) {
break;
}
client.state.set(State {
// TODO: Investigate if there's a sane way to do only the relevant updates
wired: determine_wired_state(&read_lock!(client.devices))?,
wifi: determine_wifi_state(&read_lock!(client.devices))?,
cellular: determine_cellular_state(&read_lock!(client.devices))?,
vpn: determine_vpn_state(&read_lock!(client.active_connections))?,
});
{
let $inner_client = &client;
$state_update;
}
}
Ok(())
});
};
}
impl Client {
fn new() -> Result<Client> {
let state = Mutable::new(State {
wired: WiredState::Unknown,
wifi: WifiState::Unknown,
cellular: CellularState::Unknown,
vpn: VpnState::Unknown,
});
let dbus_connection = Connection::system()?;
let root_object = {
let root_object = DbusProxyBlocking::new(&dbus_connection)?;
// Workaround for the fact that zbus (unnecessarily) requires a static lifetime here
Box::leak(Box::new(root_object))
};
Ok(Client(Arc::new(ClientInner {
state,
root_object,
active_connections: RwLock::new(HashMap::new()),
devices: RwLock::new(HashMap::new()),
dbus_connection,
})))
}
fn run(&self) -> Result<()> {
initialise_path_map!(
self.0,
active_connections,
ActiveConnectionDbusProxyBlocking
);
initialise_path_map!(self.0, devices, DeviceDbusProxyBlocking, |path| {
spawn_property_watcher!(self.0, path, receive_state_changed, devices);
spawn_property_watcher!(self.0, path, receive_state_changed, devices, |client| {
client.state.set(State {
wired: determine_wired_state(&read_lock!(client.devices))?,
wifi: determine_wifi_state(&read_lock!(client.devices))?,
cellular: determine_cellular_state(&read_lock!(client.devices))?,
vpn: client.state.get_cloned().vpn,
});
});
});
self.0.state.set(State {
wired: determine_wired_state(&read_lock!(self.0.devices))?,
@ -190,7 +195,14 @@ impl Client {
receive_devices_changed,
DeviceDbusProxyBlocking,
|client, path| {
spawn_property_watcher!(client, path, receive_state_changed, devices);
spawn_property_watcher!(client, path, receive_state_changed, devices, |client| {
client.state.set(State {
wired: determine_wired_state(&read_lock!(client.devices))?,
wifi: determine_wifi_state(&read_lock!(client.devices))?,
cellular: determine_cellular_state(&read_lock!(client.devices))?,
vpn: client.state.get_cloned().vpn,
});
});
}
);