From 7b0714172201abe2cca46e6940ce3ab50de1c76a Mon Sep 17 00:00:00 2001 From: Tassilo Horn Date: Sat, 29 Jan 2022 14:08:50 +0100 Subject: [PATCH] Replace last_focus_time with last_focus_tick Fixes: https://todo.sr.ht/~tsdh/swayr/12 --- src/cmds.rs | 6 +++--- src/demon.rs | 31 ++++++++++++++++--------------- src/tree.rs | 17 ++++++++--------- 3 files changed, 27 insertions(+), 27 deletions(-) diff --git a/src/cmds.rs b/src/cmds.rs index 9c6c012..9144250 100644 --- a/src/cmds.rs +++ b/src/cmds.rs @@ -238,7 +238,7 @@ pub fn exec_swayr_cmd(args: ExecSwayrCmdArgs) { if !before { let mut map = props.write().unwrap(); for val in map.values_mut() { - val.last_focus_time_for_next_prev_seq = val.last_focus_time; + val.last_focus_tick_for_next_prev_seq = val.last_focus_tick; } } } else { @@ -806,8 +806,8 @@ pub fn focus_window_in_direction( } wins.sort_by(|a, b| { - let lru_a = tree.last_focus_time_for_next_prev_seq(a.node.id); - let lru_b = tree.last_focus_time_for_next_prev_seq(b.node.id); + let lru_a = tree.last_focus_tick_for_next_prev_seq(a.node.id); + let lru_b = tree.last_focus_tick_for_next_prev_seq(b.node.id); lru_a.cmp(&lru_b).reverse() }); diff --git a/src/demon.rs b/src/demon.rs index 10fb7a0..4b1b769 100644 --- a/src/demon.rs +++ b/src/demon.rs @@ -26,7 +26,6 @@ use std::os::unix::net::{UnixListener, UnixStream}; use std::sync::Arc; use std::sync::RwLock; use std::thread; -use std::time::{SystemTime, UNIX_EPOCH}; use swayipc as s; pub fn run_demon() { @@ -53,6 +52,7 @@ pub fn monitor_sway_events( extra_props: Arc>>, ) { let config = config::load_config(); + let mut focus_counter = 0; let mut resets = 0; let max_resets = 10; @@ -76,17 +76,21 @@ pub fn monitor_sway_events( Ok(ev) => match ev { s::Event::Window(win_ev) => { let extra_props_clone = extra_props.clone(); + focus_counter += 1; show_extra_props_state = handle_window_event( win_ev, extra_props_clone, &config, + focus_counter, ); } s::Event::Workspace(ws_ev) => { let extra_props_clone = extra_props.clone(); + focus_counter += 1; show_extra_props_state = handle_workspace_event( ws_ev, extra_props_clone, + focus_counter, ); } s::Event::Shutdown(sd_ev) => { @@ -124,6 +128,7 @@ fn handle_window_event( ev: Box, extra_props: Arc>>, config: &config::Config, + focus_val: u64, ) -> bool { let s::WindowEvent { change, container, .. @@ -131,13 +136,13 @@ fn handle_window_event( match change { s::WindowChange::Focus => { layout::maybe_auto_tile(config); - update_last_focus_time(container.id, extra_props); + update_last_focus_tick(container.id, extra_props, focus_val); println!("Handled window event type {:?}", change); true } s::WindowChange::New => { layout::maybe_auto_tile(config); - update_last_focus_time(container.id, extra_props); + update_last_focus_tick(container.id, extra_props, focus_val); println!("Handled window event type {:?}", change); true } @@ -162,6 +167,7 @@ fn handle_window_event( fn handle_workspace_event( ev: Box, extra_props: Arc>>, + focus_val: u64, ) -> bool { let s::WorkspaceEvent { change, @@ -171,11 +177,12 @@ fn handle_workspace_event( } = *ev; match change { s::WorkspaceChange::Init | s::WorkspaceChange::Focus => { - update_last_focus_time( + update_last_focus_tick( current .expect("No current in Init or Focus workspace event") .id, extra_props, + focus_val, ); println!("Handled workspace event type {:?}", change); true @@ -192,19 +199,20 @@ fn handle_workspace_event( } } -fn update_last_focus_time( +fn update_last_focus_tick( id: i64, extra_props: Arc>>, + focus_val: u64, ) { let mut write_lock = extra_props.write().unwrap(); if let Some(wp) = write_lock.get_mut(&id) { - wp.last_focus_time = get_epoch_time_as_millis(); + wp.last_focus_tick = focus_val; } else { write_lock.insert( id, t::ExtraProps { - last_focus_time: get_epoch_time_as_millis(), - last_focus_time_for_next_prev_seq: 0, + last_focus_tick: focus_val, + last_focus_tick_for_next_prev_seq: 0, }, ); } @@ -217,13 +225,6 @@ fn remove_extra_props( extra_props.write().unwrap().remove(&id); } -fn get_epoch_time_as_millis() -> u128 { - SystemTime::now() - .duration_since(UNIX_EPOCH) - .expect("Couldn't get epoch time!") - .as_millis() -} - pub fn serve_client_requests( extra_props: Arc>>, ) { diff --git a/src/tree.rs b/src/tree.rs index 02b7f49..3b293f8 100644 --- a/src/tree.rs +++ b/src/tree.rs @@ -160,9 +160,8 @@ impl NodeMethods for s::Node { /// Extra properties gathered by swayrd for windows and workspaces. #[derive(Copy, Clone, Debug, Deserialize, Serialize)] pub struct ExtraProps { - /// Milliseconds since UNIX epoch. - pub last_focus_time: u128, - pub last_focus_time_for_next_prev_seq: u128, + pub last_focus_tick: u64, + pub last_focus_tick_for_next_prev_seq: u64, } pub struct Tree<'a> { @@ -211,14 +210,14 @@ impl<'a> Tree<'a> { } } - pub fn last_focus_time(&self, id: i64) -> u128 { - self.extra_props.get(&id).map_or(0, |wp| wp.last_focus_time) + pub fn last_focus_tick(&self, id: i64) -> u64 { + self.extra_props.get(&id).map_or(0, |wp| wp.last_focus_tick) } - pub fn last_focus_time_for_next_prev_seq(&self, id: i64) -> u128 { + pub fn last_focus_tick_for_next_prev_seq(&self, id: i64) -> u64 { self.extra_props .get(&id) - .map_or(0, |wp| wp.last_focus_time_for_next_prev_seq) + .map_or(0, |wp| wp.last_focus_tick_for_next_prev_seq) } fn sorted_nodes_of_type_1( @@ -321,8 +320,8 @@ impl<'a> Tree<'a> { } else if !a.urgent && b.urgent { cmp::Ordering::Greater } else { - let lru_a = self.last_focus_time(a.id); - let lru_b = self.last_focus_time(b.id); + let lru_a = self.last_focus_tick(a.id); + let lru_b = self.last_focus_tick(b.id); lru_a.cmp(&lru_b).reverse() } });