Replace last_focus_time with last_focus_tick

Fixes: https://todo.sr.ht/~tsdh/swayr/12
timeout_old
Tassilo Horn 2 years ago
parent b167acac88
commit 7b07141722
  1. 6
      src/cmds.rs
  2. 31
      src/demon.rs
  3. 17
      src/tree.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()
});

@ -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<RwLock<HashMap<i64, t::ExtraProps>>>,
) {
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<s::WindowEvent>,
extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>,
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<s::WorkspaceEvent>,
extra_props: Arc<RwLock<HashMap<i64, t::ExtraProps>>>,
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<RwLock<HashMap<i64, t::ExtraProps>>>,
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<RwLock<HashMap<i64, t::ExtraProps>>>,
) {

@ -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()
}
});

Loading…
Cancel
Save