parent
e93a833401
commit
045e8e7411
9 changed files with 333 additions and 160 deletions
@ -1,122 +0,0 @@ |
||||
// Copyright (C) 2021 Tassilo Horn <tsdh@gnu.org>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU General Public License as published by the Free
|
||||
// Software Foundation, either version 3 of the License, or (at your option)
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
// more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Extensions of swayipc types and IPC structs.
|
||||
|
||||
use clap::Clap; |
||||
use serde::{Deserialize, Serialize}; |
||||
use swayipc as s; |
||||
|
||||
/// Immutable Node Iterator
|
||||
///
|
||||
/// Iterates nodes in depth-first order, tiled nodes before floating nodes.
|
||||
pub struct NodeIter<'a> { |
||||
stack: Vec<&'a s::Node>, |
||||
} |
||||
|
||||
impl<'a> NodeIter<'a> { |
||||
pub fn new(node: &'a s::Node) -> NodeIter { |
||||
NodeIter { stack: vec![node] } |
||||
} |
||||
} |
||||
|
||||
impl<'a> Iterator for NodeIter<'a> { |
||||
type Item = &'a s::Node; |
||||
|
||||
fn next(&mut self) -> Option<Self::Item> { |
||||
if let Some(node) = self.stack.pop() { |
||||
for n in &node.floating_nodes { |
||||
self.stack.push(&n); |
||||
} |
||||
for n in &node.nodes { |
||||
self.stack.push(&n); |
||||
} |
||||
Some(node) |
||||
} else { |
||||
None |
||||
} |
||||
} |
||||
} |
||||
|
||||
/// Extension methods for [`swayipc::reply::Node`].
|
||||
pub trait NodeMethods { |
||||
/// Returns an iterator for this [`swayipc::reply::Node`] and its childres.
|
||||
fn iter(&self) -> NodeIter; |
||||
|
||||
/// Returns all nodes being application windows.
|
||||
fn windows(&self) -> Vec<&s::Node>; |
||||
|
||||
/// Returns all nodes being workspaces.
|
||||
fn workspaces(&self) -> Vec<&s::Node>; |
||||
|
||||
fn is_scratchpad(&self) -> bool; |
||||
} |
||||
|
||||
impl NodeMethods for s::Node { |
||||
fn iter(&self) -> NodeIter { |
||||
NodeIter::new(self) |
||||
} |
||||
|
||||
fn windows(&self) -> Vec<&s::Node> { |
||||
self.iter() |
||||
.filter(|n| { |
||||
(n.node_type == s::NodeType::Con |
||||
|| n.node_type == s::NodeType::FloatingCon) |
||||
&& n.name.is_some() |
||||
}) |
||||
.collect() |
||||
} |
||||
|
||||
fn workspaces(&self) -> Vec<&s::Node> { |
||||
self.iter() |
||||
.filter(|n| n.node_type == s::NodeType::Workspace) |
||||
.collect() |
||||
} |
||||
|
||||
fn is_scratchpad(&self) -> bool { |
||||
self.name.is_some() && self.name.as_ref().unwrap().eq("__i3_scratch") |
||||
} |
||||
} |
||||
|
||||
#[derive(Clap, Debug, Deserialize, Serialize)] |
||||
pub enum SwayrCommand { |
||||
/// Switch to next urgent window (if any) or to last recently used window.
|
||||
SwitchToUrgentOrLRUWindow, |
||||
/// Focus the selected window
|
||||
SwitchWindow, |
||||
/// Focus the next window.
|
||||
NextWindow, |
||||
/// Focus the previous window.
|
||||
PrevWindow, |
||||
/// Quit the selected window
|
||||
QuitWindow, |
||||
/// Switch to the selected workspace
|
||||
SwitchWorkspace, |
||||
/// Switch to the selected workspace or focus the selected window
|
||||
SwitchWorkspaceOrWindow, |
||||
/// Quit all windows of selected workspace or the selected window
|
||||
QuitWorkspaceOrWindow, |
||||
/// Select and execute a swaymsg command
|
||||
ExecuteSwaymsgCommand, |
||||
/// Select and execute a swayr command
|
||||
ExecuteSwayrCommand, |
||||
} |
||||
|
||||
/// Extra properties gathered by swayrd for windows and workspaces.
|
||||
#[derive(Clone, Debug, Deserialize, Serialize)] |
||||
pub struct ExtraProps { |
||||
/// Milliseconds since UNIX epoch.
|
||||
pub last_focus_time: u128, |
||||
} |
@ -0,0 +1,104 @@ |
||||
// Copyright (C) 2021 Tassilo Horn <tsdh@gnu.org>
|
||||
//
|
||||
// This program is free software: you can redistribute it and/or modify it
|
||||
// under the terms of the GNU General Public License as published by the Free
|
||||
// Software Foundation, either version 3 of the License, or (at your option)
|
||||
// any later version.
|
||||
//
|
||||
// This program is distributed in the hope that it will be useful, but WITHOUT
|
||||
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
||||
// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
|
||||
// more details.
|
||||
//
|
||||
// You should have received a copy of the GNU General Public License along with
|
||||
// this program. If not, see <https://www.gnu.org/licenses/>.
|
||||
|
||||
//! Functions and data structures of the swayrd demon.
|
||||
|
||||
use crate::con; |
||||
use crate::con::NodeMethods; |
||||
use crate::config; |
||||
use swayipc as s; |
||||
|
||||
pub fn auto_tile(layout: &config::Layout) { |
||||
if let Ok(mut con) = s::Connection::new() { |
||||
if let Ok(tree) = con.get_tree() { |
||||
let config_map = |
||||
layout.auto_tile_min_window_width_per_output_width_as_map(); |
||||
let default_map = config::Layout::default() |
||||
.auto_tile_min_window_width_per_output_width_as_map() |
||||
.unwrap(); |
||||
for output in &tree.nodes { |
||||
println!("output: {:?}", output.name); |
||||
let output_width = output.rect.width; |
||||
let min_window_width = &config_map |
||||
.as_ref() |
||||
.unwrap_or(&default_map) |
||||
.get(&output_width); |
||||
|
||||
if let Some(min_window_width) = min_window_width { |
||||
for container in |
||||
con::NodeIter::new(output).filter(|n| n.is_container()) |
||||
{ |
||||
println!( |
||||
" container: {:?}, {} nodes", |
||||
container.node_type, |
||||
container.nodes.len() |
||||
); |
||||
for child_win in |
||||
container.nodes.iter().filter(|n| n.is_window()) |
||||
{ |
||||
println!(" child_win: {:?}", child_win.app_id); |
||||
// Width if we'd split once more.
|
||||
let estimated_width = |
||||
child_win.rect.width as f32 / 2.0; |
||||
println!("estimated_width = {}", estimated_width); |
||||
let split = if container.layout |
||||
== s::NodeLayout::SplitH |
||||
&& estimated_width <= **min_window_width as f32 |
||||
{ |
||||
Some("splitv") |
||||
} else if container.layout == s::NodeLayout::SplitV |
||||
&& estimated_width > **min_window_width as f32 |
||||
{ |
||||
Some("splith") |
||||
} else { |
||||
None |
||||
}; |
||||
|
||||
if let Some(split) = split { |
||||
println!( |
||||
"Auto-tiling performing {} on window {} \ |
||||
because estimated width after another \ |
||||
split is {} and the minimum window width \ |
||||
is {} on this output.", |
||||
split, |
||||
child_win.id, |
||||
estimated_width, |
||||
min_window_width |
||||
); |
||||
match con.run_command(format!( |
||||
"[con_id={}] {}", |
||||
child_win.id, split |
||||
)) { |
||||
Ok(_) => (), |
||||
Err(e) => eprintln!( |
||||
"Couldn't set {} on con {}: {:?}", |
||||
split, child_win.id, e |
||||
), |
||||
} |
||||
} |
||||
} |
||||
} |
||||
} else { |
||||
eprintln!("No layout.auto_tile_min_window_width_per_output_width \ |
||||
setting for output_width {}", output_width); |
||||
} |
||||
} |
||||
} else { |
||||
eprintln!("Couldn't call get_tree during auto_tile."); |
||||
} |
||||
} else { |
||||
eprintln!("Couldn't get connection for auto_tile"); |
||||
} |
||||
} |
Loading…
Reference in new issue