Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ oneshot = { version = "0", default-features = false, features = ["std"] }
softbuffer = "0"
channel-protocol = "0.3"
joy-vector = { git = "https://github.com/sub07/rust-utils", version = "0.2.1" }
joy-error = { git = "https://github.com/sub07/rust-utils", version = "0.6.0", features = ["log-crate", "anyhow-crate"] }
itertools = "0.14.0"
keyboard-types = { version = "0.8.3", features = ["std", "serde"] }
dirs = "6"
Expand Down
7 changes: 7 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ impl Winri {
"Opened windows: {:#?}",
get_process_names(&windows_snapshot)
);

self.tiler.handle_window_snapshot(&windows_snapshot);

if let Some(focused_window) = self.tiler.current_window()
Expand Down Expand Up @@ -232,9 +233,15 @@ impl Winri {
}
}
TilerAction::MoveFocusNext => {
if !self.tiler.has_focus() {
self.tiler.focus_first();
}
self.tiler.focus_right();
}
TilerAction::MoveFocusPrevious => {
if !self.tiler.has_focus() {
self.tiler.focus_first();
}
self.tiler.focus_left();
}
TilerAction::SwapWithNext => {
Expand Down
58 changes: 37 additions & 21 deletions src/tiler.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::{collections::HashSet, ops::Sub};
use std::{collections::HashSet, ops::Sub, time::Duration};

use log::{debug, error, warn};
use anyhow::Context;
use joy_error::log::ResultLogExt;
use log::{debug, warn};

use crate::{
cast,
Expand Down Expand Up @@ -45,6 +47,14 @@ impl ScrollTiler {
.position(|item| item.inner.is_focused().unwrap_or(false))
}

fn logged_focus_index(&self) -> Option<usize> {
self.focus_index()
.context("Focused window is not tiled")
.info()
.log_err()
.ok()
}

pub fn windows(&self) -> impl Iterator<Item = &WindowItem> {
self.windows.iter()
}
Expand All @@ -57,8 +67,24 @@ impl ScrollTiler {
self.swap_current(1);
}

pub fn has_focus(&self) -> bool {
self.focus_index().is_some()
}

pub fn focus_first(&self) {
if !self.windows.is_empty() {
let window = self.windows[0].inner;
let _ = window
.focus_and_wait(Duration::from_secs(1))
.context(window.get_formatted_extensive_info())
.context("focus first")
.error()
.log_err();
}
}

fn swap_current(&mut self, direction: i32) {
if let Some(focus_index) = self.focus_index() {
if let Some(focus_index) = self.logged_focus_index() {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
Expand All @@ -68,11 +94,6 @@ impl ScrollTiler {
let other_swap_index =
(focus_index as i32 + direction).clamp(0, self.windows.len() as i32 - 1) as usize;
self.windows.swap(focus_index, other_swap_index);
} else {
warn!(
"Could not find focused window in tiler. Focused window is {:?}",
Window::focused()
);
}
}

Expand All @@ -85,7 +106,7 @@ impl ScrollTiler {
}

fn focus(&self, direction: i32) {
if let Some(focus_index) = self.focus_index() {
if let Some(focus_index) = self.logged_focus_index() {
#[allow(
clippy::cast_possible_truncation,
clippy::cast_sign_loss,
Expand All @@ -95,18 +116,13 @@ impl ScrollTiler {
let new_focus_index =
(focus_index as i32 + direction).clamp(0, self.windows.len() as i32 - 1) as usize;
let window = self.windows[new_focus_index].inner;
if let Err(err) = window.focus() {
error!(
"Failed to focus window ({}): {}",
err,
window.get_formatted_extensive_info(),
);
}
} else {
warn!(
"Could not find focused window in tiler. Focused window is {:?}",
Window::focused()
);

let _ = window
.focus()
.context(window.get_formatted_extensive_info())
.context("Changing tiler focused window")
.error()
.log_err();
}
}

Expand Down
13 changes: 13 additions & 0 deletions src/window/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -370,6 +370,19 @@ impl Window {
Ok(())
}

pub fn focus_and_wait(self, timeout: Duration) -> anyhow::Result<bool> {
ensure_valid!(self);
self.focus()?;
let start = std::time::Instant::now();
while start.elapsed() < timeout {
if self.is_focused()? {
return Ok(true);
}
thread::sleep(Duration::from_millis(20));
}
Ok(false)
}

#[must_use]
pub fn get_formatted_extensive_info(self) -> String {
use std::fmt::Write as _;
Expand Down