Skip to content

Commit

Permalink
main: don't focus windows in scratchpad
Browse files Browse the repository at this point in the history
Signed-off-by: x1b6e6 <[email protected]>
  • Loading branch information
x1b6e6 committed Dec 11, 2021
1 parent af87ba7 commit 3a64db2
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 20 deletions.
56 changes: 37 additions & 19 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,28 +52,21 @@ impl SwayAlttab {
/// `key_alt` is key with Alt behavior
/// `key_sft` is key with Shift behavior
pub async fn new(key_tab: EV_KEY, key_alt: EV_KEY, key_sft: EV_KEY) -> Result<Self, Error> {
let mut conn = Connection::new().await?;
let mut stack_holder = StackHolder::new();

let tree = conn.get_tree().await?;

let nodes = SwayAlttab::nodes(&tree);
nodes.iter().for_each(|node| stack_holder.add(node.id));
if let Some(node) = nodes.iter().find(|node| node.focused) {
stack_holder.move_up(node.id);
}

Ok(Self {
let mut swayalttab = Self {
key_tab,
key_alt,
key_sft,

psd_alt: false,
psd_sft: false,

stack_holder,
stack_holder: StackHolder::new(),
ignore_move_up: None,
})
};

swayalttab.refresh_nodes().await?;

Ok(swayalttab)
}

/// Try create stream of [`EventStream`]
Expand All @@ -87,6 +80,7 @@ impl SwayAlttab {
/// Get vector of windows as [`Node`]
fn nodes(tree: &Node) -> Vec<&Node> {
match tree.node_type {
NodeType::Workspace if tree.name == Some("__i3_scratch".to_string()) => vec![],
NodeType::Con if tree.layout == NodeLayout::None => vec![tree],
NodeType::FloatingCon => vec![tree],
_ => tree
Expand All @@ -98,6 +92,24 @@ impl SwayAlttab {
}
}

async fn refresh_nodes(&mut self) -> Result<(), Error> {
let mut sway = Connection::new().await?;
let root = sway.get_tree().await?;
let nodes = Self::nodes(&root);
let size = self.stack_holder.depth();
nodes.iter().for_each(|node| self.stack_holder.add(node.id));
if size != self.stack_holder.depth() {
let size = self.stack_holder.depth();
if let Some(id) = self.stack_holder.get(size - 1) {
self.stack_holder.move_up(id);
}
}
if let Some(node) = nodes.iter().find(|node| node.focused) {
self.stack_holder.move_up(node.id);
}
Ok(())
}

/// Focus window in preview mode
async fn preview(&mut self, id: i64) -> Result<(), Error> {
let mut sway = Connection::new().await?;
Expand Down Expand Up @@ -137,7 +149,7 @@ impl SwayAlttab {
}

/// Process sway event [`Event`]
pub fn process_sway_event(&mut self, event: Event) {
pub async fn process_sway_event(&mut self, event: Event) -> Result<(), Error> {
if let Event::Window(window) = event {
let id = window.container.id;
match window.change {
Expand All @@ -150,9 +162,15 @@ impl SwayAlttab {
self.ignore_move_up = None;
}
}
WindowChange::Move => {
self.stack_holder.remove(id);
self.refresh_nodes().await?;
}
_ => {}
}
}

Ok(())
}
}

Expand Down Expand Up @@ -195,12 +213,12 @@ async fn main() -> Fallible<()> {
loop {
select! {
ev = kb.next() => {
let ev = ev.expect("keyboard stream error").unwrap();
swayalttab.process_keyboard_event(ev).await.unwrap();
let ev = ev.expect("keyboard stream error")?;
swayalttab.process_keyboard_event(ev).await?;
}
ev = sway.next() => {
let ev = ev.expect("sway events stream error").unwrap();
swayalttab.process_sway_event(ev);
let ev = ev.expect("sway events stream error")?;
swayalttab.process_sway_event(ev).await?;
}
};
}
Expand Down
10 changes: 9 additions & 1 deletion src/stack_holder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::window_stack::WindowStack;
/// Hold stack of windows
///
/// The main function is preview of window
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct StackHolder {
window_stack: WindowStack,
preview_depth: usize,
Expand Down Expand Up @@ -33,6 +33,14 @@ impl StackHolder {
self.window_stack.remove(id);
}

pub fn get(&self, depth: usize) -> Option<i64> {
self.window_stack.get(depth)
}

pub fn depth(&self) -> usize {
self.window_stack.depth()
}

/// Finish preview and move currently focused window to the up
pub fn preview_finish(&mut self) {
if let Some(id) = self.window_stack.get(self.preview_depth) {
Expand Down

0 comments on commit 3a64db2

Please sign in to comment.