small changes/fixes
This commit is contained in:
parent
08a0c6b089
commit
c6fc328702
268
src/clients.rs
268
src/clients.rs
|
@ -1,7 +1,7 @@
|
||||||
use std::{borrow::Borrow, cell::RefCell, collections::HashMap, rc::Rc};
|
use std::{borrow::Borrow, cell::RefCell, collections::HashMap, rc::Rc};
|
||||||
use std::{
|
use std::{
|
||||||
hash::{Hash, Hasher},
|
hash::{Hash, Hasher},
|
||||||
rc::Weak,
|
num::NonZeroI32,
|
||||||
};
|
};
|
||||||
|
|
||||||
use x11::xlib::Window;
|
use x11::xlib::Window;
|
||||||
|
@ -89,30 +89,95 @@ mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn client_lists_test() {}
|
fn client_lists_test() {
|
||||||
|
let mut clients = ClientState::default();
|
||||||
|
|
||||||
|
clients.insert(Client {
|
||||||
|
window: 1,
|
||||||
|
size: (1, 1),
|
||||||
|
position: (1, 1),
|
||||||
|
floating: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
clients.insert(Client {
|
||||||
|
window: 2,
|
||||||
|
size: (1, 1),
|
||||||
|
position: (1, 1),
|
||||||
|
floating: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
clients.stack_unstacked();
|
||||||
|
clients.refresh_virtual_screen();
|
||||||
|
clients.arange_virtual_screen(600, 400);
|
||||||
|
|
||||||
|
println!("{:#?}", clients);
|
||||||
|
|
||||||
|
clients.remove(&1u64);
|
||||||
|
|
||||||
|
clients.stack_unstacked();
|
||||||
|
clients.refresh_virtual_screen();
|
||||||
|
clients.arange_virtual_screen(600, 400);
|
||||||
|
|
||||||
|
println!("{:#?}", clients);
|
||||||
|
|
||||||
|
clients.virtual_screens.rotate_right(1);
|
||||||
|
|
||||||
|
clients.insert(Client {
|
||||||
|
window: 3,
|
||||||
|
size: (1, 1),
|
||||||
|
position: (1, 1),
|
||||||
|
floating: false,
|
||||||
|
});
|
||||||
|
|
||||||
|
clients.stack_unstacked();
|
||||||
|
clients.refresh_virtual_screen();
|
||||||
|
clients.arange_virtual_screen(600, 400);
|
||||||
|
|
||||||
|
println!("{:#?}", clients);
|
||||||
|
|
||||||
|
clients.toggle_floating(&2u64);
|
||||||
|
|
||||||
|
clients.virtual_screens.rotate_left(1);
|
||||||
|
|
||||||
|
clients.stack_unstacked();
|
||||||
|
clients.refresh_virtual_screen();
|
||||||
|
clients.arange_virtual_screen(600, 400);
|
||||||
|
|
||||||
|
println!("{:#?}", clients);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
mod no_refcell {
|
|
||||||
use std::{collections::VecDeque, iter::repeat};
|
use std::{collections::VecDeque, iter::repeat};
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
type ClientsWrapped = Rc<RefCell<Clients>>;
|
|
||||||
type Clients = HashMap<Window, Client, BuildIdentityHasher>;
|
type Clients = HashMap<Window, Client, BuildIdentityHasher>;
|
||||||
type ClientRef = u64;
|
type ClientRef = u64;
|
||||||
type ClientRefs = Vec<ClientRef>;
|
type ClientRefs = Vec<ClientRef>;
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
struct ClientState {
|
struct ClientState {
|
||||||
clients: Clients,
|
clients: Clients,
|
||||||
virtual_screens: VecDeque<VirtualScreen>,
|
virtual_screens: VecDeque<VirtualScreen>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Debug, Clone)]
|
||||||
struct VirtualScreen {
|
struct VirtualScreen {
|
||||||
master: ClientRefs,
|
master: ClientRefs,
|
||||||
aux: ClientRefs,
|
aux: ClientRefs,
|
||||||
focused: Option<ClientRef>,
|
focused: Option<ClientRef>,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl Default for ClientState {
|
||||||
|
fn default() -> Self {
|
||||||
|
let mut vss = VecDeque::<VirtualScreen>::new();
|
||||||
|
vss.resize_with(10, Default::default);
|
||||||
|
|
||||||
|
Self {
|
||||||
|
clients: Default::default(),
|
||||||
|
virtual_screens: vss,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
impl ClientState {
|
impl ClientState {
|
||||||
fn insert(&mut self, client: Client) {
|
fn insert(&mut self, client: Client) {
|
||||||
let key = client.key();
|
let key = client.key();
|
||||||
|
@ -120,6 +185,17 @@ mod no_refcell {
|
||||||
self.clients.insert(key, client);
|
self.clients.insert(key, client);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn remove<K>(&mut self, key: &K)
|
||||||
|
where
|
||||||
|
K: ClientKey,
|
||||||
|
{
|
||||||
|
self.virtual_screens
|
||||||
|
.iter_mut()
|
||||||
|
.for_each(|vs| vs.remove(key));
|
||||||
|
|
||||||
|
self.clients.remove(&key.key());
|
||||||
|
}
|
||||||
|
|
||||||
fn get<K>(&self, key: &K) -> Option<&Client>
|
fn get<K>(&self, key: &K) -> Option<&Client>
|
||||||
where
|
where
|
||||||
K: ClientKey,
|
K: ClientKey,
|
||||||
|
@ -151,15 +227,9 @@ mod no_refcell {
|
||||||
where
|
where
|
||||||
K: ClientKey,
|
K: ClientKey,
|
||||||
{
|
{
|
||||||
self.virtual_screens.iter().find_map(
|
self.virtual_screens
|
||||||
|vs| {
|
.iter()
|
||||||
if vs.contains(key) {
|
.find_map(|vs| if vs.contains(key) { Some(vs) } else { None })
|
||||||
Some(vs)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_mut_virtualscreen_for_client<K>(&mut self, key: &K) -> Option<&mut VirtualScreen>
|
fn get_mut_virtualscreen_for_client<K>(&mut self, key: &K) -> Option<&mut VirtualScreen>
|
||||||
|
@ -193,14 +263,13 @@ mod no_refcell {
|
||||||
.clients
|
.clients
|
||||||
.iter()
|
.iter()
|
||||||
.filter(|&(key, client)| {
|
.filter(|&(key, client)| {
|
||||||
!client.floating && self.get_virtualscreen_for_client(key).is_some()
|
!client.floating && self.get_virtualscreen_for_client(key).is_none()
|
||||||
})
|
})
|
||||||
.map(|(key, _)| key)
|
.map(|(key, _)| key)
|
||||||
.collect::<Vec<_>>();
|
.collect::<Vec<_>>();
|
||||||
|
|
||||||
match self.virtual_screens.front_mut() {
|
if let Some(vs) = self.virtual_screens.front_mut() {
|
||||||
Some(vs) => vs.aux.extend(unstacked.into_iter()),
|
vs.aux.extend(unstacked.into_iter())
|
||||||
None => {}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -250,7 +319,20 @@ mod no_refcell {
|
||||||
|
|
||||||
if let Some(vs) = self.virtual_screens.front_mut() {
|
if let Some(vs) = self.virtual_screens.front_mut() {
|
||||||
// if aux is empty -> width : width / 2
|
// if aux is empty -> width : width / 2
|
||||||
let width = width / 1 + i32::from(!vs.aux.is_empty());
|
let width = width / (1 + i32::from(!vs.aux.is_empty() && !vs.master.is_empty()));
|
||||||
|
|
||||||
|
// make sure we dont devide by 0
|
||||||
|
let master_height = height
|
||||||
|
/ match NonZeroI32::new(vs.master.len() as i32) {
|
||||||
|
Some(i) => i.get(),
|
||||||
|
None => 1,
|
||||||
|
};
|
||||||
|
|
||||||
|
let aux_height = height
|
||||||
|
/ match NonZeroI32::new(vs.aux.len() as i32) {
|
||||||
|
Some(i) => i.get(),
|
||||||
|
None => 1,
|
||||||
|
};
|
||||||
|
|
||||||
// chaining master and aux together with `Zip`s for height and x reduces duplicate code
|
// chaining master and aux together with `Zip`s for height and x reduces duplicate code
|
||||||
for ((i, key), (height, x)) in vs
|
for ((i, key), (height, x)) in vs
|
||||||
|
@ -258,12 +340,12 @@ mod no_refcell {
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
// add repeating height for each window and x pos for each window
|
// add repeating height for each window and x pos for each window
|
||||||
.zip(repeat(height / vs.master.len() as i32).zip(repeat(0i32)))
|
.zip(repeat(master_height).zip(repeat(0i32)))
|
||||||
.chain(
|
.chain(
|
||||||
vs.aux
|
vs.aux
|
||||||
.iter()
|
.iter()
|
||||||
.enumerate()
|
.enumerate()
|
||||||
.zip(repeat(height / vs.aux.len() as i32).zip(repeat(width))),
|
.zip(repeat(aux_height).zip(repeat(width))),
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
let size = (width, height);
|
let size = (width, height);
|
||||||
|
@ -283,135 +365,16 @@ mod no_refcell {
|
||||||
// Should have xlib send those changes back to the x server after this function
|
// Should have xlib send those changes back to the x server after this function
|
||||||
}
|
}
|
||||||
|
|
||||||
impl VirtualScreen {
|
impl Default for VirtualScreen {
|
||||||
fn contains<K>(&self, key: &K) -> bool
|
fn default() -> Self {
|
||||||
where
|
Self {
|
||||||
K: ClientKey,
|
master: Default::default(),
|
||||||
{
|
aux: Default::default(),
|
||||||
self.master.contains(&key.key()) || self.aux.contains(&key.key())
|
focused: None,
|
||||||
}
|
|
||||||
|
|
||||||
fn focus<K>(&mut self, key: &K)
|
|
||||||
where
|
|
||||||
K: ClientKey,
|
|
||||||
{
|
|
||||||
self.focused = Some(key.key());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
|
||||||
mod refcell {
|
|
||||||
use std::collections::VecDeque;
|
|
||||||
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
type ClientsWrapped = Rc<RefCell<Clients>>;
|
|
||||||
type Clients = HashMap<Window, Rc<RefCell<Client>>, BuildIdentityHasher>;
|
|
||||||
type ClientRef = Weak<RefCell<Client>>;
|
|
||||||
type ClientRefs = Vec<ClientRef>;
|
|
||||||
|
|
||||||
struct ClientState {
|
|
||||||
clients: Clients,
|
|
||||||
virtual_screens: VecDeque<VirtualScreen>,
|
|
||||||
}
|
|
||||||
|
|
||||||
struct VirtualScreen {
|
|
||||||
master: ClientRefs,
|
|
||||||
aux: ClientRefs,
|
|
||||||
focused: Option<ClientRef>,
|
|
||||||
}
|
|
||||||
|
|
||||||
impl ClientState {
|
|
||||||
fn insert(&mut self, client: Client) {
|
|
||||||
let key = client.key();
|
|
||||||
|
|
||||||
self.clients.insert(key, Rc::new(RefCell::new(client)));
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get<K>(&self, key: &K) -> Option<&Rc<RefCell<Client>>>
|
|
||||||
where
|
|
||||||
K: ClientKey,
|
|
||||||
{
|
|
||||||
self.clients.get(&key.key())
|
|
||||||
}
|
|
||||||
|
|
||||||
fn toggle_floating<K>(&mut self, key: &K) -> Option<bool>
|
|
||||||
where
|
|
||||||
K: ClientKey,
|
|
||||||
{
|
|
||||||
match self.get(key) {
|
|
||||||
Some(client) => {
|
|
||||||
let client = client.borrow_mut();
|
|
||||||
client.floating = !client.floating;
|
|
||||||
|
|
||||||
Some(client.floating)
|
|
||||||
}
|
|
||||||
None => None,
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_virtualscreen_for_client<K>(&self, key: &K) -> Option<&VirtualScreen>
|
|
||||||
where
|
|
||||||
K: ClientKey,
|
|
||||||
{
|
|
||||||
self.virtual_screens.iter().find_map(
|
|
||||||
|vs| {
|
|
||||||
if vs.contains(key) {
|
|
||||||
Some(vs)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
fn get_mut_virtualscreen_for_client<K>(&mut self, key: &K) -> Option<&mut VirtualScreen>
|
|
||||||
where
|
|
||||||
K: ClientKey,
|
|
||||||
{
|
|
||||||
self.virtual_screens.iter_mut().find_map(
|
|
||||||
|vs| {
|
|
||||||
if vs.contains(key) {
|
|
||||||
Some(vs)
|
|
||||||
} else {
|
|
||||||
None
|
|
||||||
}
|
|
||||||
},
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
/// focuses client `key` on current virtual screen
|
|
||||||
fn focus_client<K>(&mut self, key: &K)
|
|
||||||
where
|
|
||||||
K: ClientKey,
|
|
||||||
{
|
|
||||||
match self.virtual_screens.front_mut() {
|
|
||||||
Some(vs) => vs.focus(key),
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn stack_unstacked(&mut self) {
|
|
||||||
let unstacked = self
|
|
||||||
.clients
|
|
||||||
.iter()
|
|
||||||
.filter(|&(key, client)| {
|
|
||||||
!client.as_ref().borrow().floating
|
|
||||||
&& self.get_virtualscreen_for_client(key).is_some()
|
|
||||||
})
|
|
||||||
.map(|(key, _)| key)
|
|
||||||
.collect::<Vec<_>>();
|
|
||||||
|
|
||||||
match self.virtual_screens.front_mut() {
|
|
||||||
Some(vs) => vs.aux.extend(unstacked.into_iter()),
|
|
||||||
None => {}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn arrange(&mut self) {}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl VirtualScreen {
|
impl VirtualScreen {
|
||||||
fn contains<K>(&self, key: &K) -> bool
|
fn contains<K>(&self, key: &K) -> bool
|
||||||
where
|
where
|
||||||
|
@ -420,6 +383,15 @@ mod refcell {
|
||||||
self.master.contains(&key.key()) || self.aux.contains(&key.key())
|
self.master.contains(&key.key()) || self.aux.contains(&key.key())
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn remove<K>(&mut self, key: &K)
|
||||||
|
where
|
||||||
|
K: ClientKey,
|
||||||
|
{
|
||||||
|
let key = key.key();
|
||||||
|
self.master.retain(|k| *k != key);
|
||||||
|
self.aux.retain(|k| *k != key);
|
||||||
|
}
|
||||||
|
|
||||||
fn focus<K>(&mut self, key: &K)
|
fn focus<K>(&mut self, key: &K)
|
||||||
where
|
where
|
||||||
K: ClientKey,
|
K: ClientKey,
|
||||||
|
@ -427,5 +399,3 @@ mod refcell {
|
||||||
self.focused = Some(key.key());
|
self.focused = Some(key.key());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
*/
|
|
||||||
|
|
Loading…
Reference in a new issue