From 0d3751a7cc4e320dbf55eeb9320013971ab5981e Mon Sep 17 00:00:00 2001 From: LGUG2Z Date: Fri, 13 Aug 2021 10:24:29 -0700 Subject: [PATCH] refactor(wm): reduce boilerplate with getset This commit introduces the getset crate to reduce a lot of the boilerplate, especially in workspace.rs, around different variations of getters. Hopefully this will make the codebase easier to navigate for contributors in the future. Also trying to avoid pinning to patch versions and minor versions wherever possible. --- Cargo.lock | 41 +++++++++------ komorebi-core/Cargo.toml | 6 +-- komorebi/Cargo.toml | 29 +++++----- komorebi/src/container.rs | 8 ++- komorebi/src/monitor.rs | 20 +++---- komorebi/src/window_manager.rs | 28 +++++----- komorebi/src/workspace.rs | 96 +++++++--------------------------- komorebic/Cargo.toml | 8 +-- 8 files changed, 89 insertions(+), 147 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 71163af2..e91e8fa9 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -67,9 +67,9 @@ dependencies = [ [[package]] name = "bitflags" -version = "1.2.1" +version = "1.3.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf1de2fe8c75bc145a2f577add951f8134889b4795d47466a54a5c846d691693" +checksum = "2da1976d75adbe5fbc88130ecd119529cf1cc6a93ae1546d8696ee66f0d21af1" [[package]] name = "cc" @@ -213,9 +213,9 @@ dependencies = [ [[package]] name = "ctrlc" -version = "3.1.9" +version = "3.2.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "232295399409a8b7ae41276757b5a1cc21032848d42bff2352261f958b3ca29a" +checksum = "377c9b002a72a0b2c1a18c62e2f3864bdfea4a015e3683a96e24aa45dd6c02d1" dependencies = [ "nix", "winapi", @@ -241,12 +241,6 @@ dependencies = [ "winapi", ] -[[package]] -name = "doc-comment" -version = "0.3.3" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fea41bba32d969b513997752735605054bc0dfa92b4c56bf1189f2e174be7a10" - [[package]] name = "either" version = "1.6.1" @@ -280,6 +274,18 @@ dependencies = [ "wasi", ] +[[package]] +name = "getset" +version = "0.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "24b328c01a4d71d2d8173daa93562a73ab0fe85616876f02500f53d82948c504" +dependencies = [ + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "gimli" version = "0.25.0" @@ -344,6 +350,7 @@ dependencies = [ "ctrlc", "dirs", "eyre", + "getset", "komorebi-core", "lazy_static", "nanoid", @@ -451,14 +458,15 @@ dependencies = [ [[package]] name = "nix" -version = "0.20.0" +version = "0.22.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "fa9b4819da1bc61c0ea48b63b7bc8604064dd43013e7cc325df098d49cd7c18a" +checksum = "cf1e25ee6b412c2a1e3fcb6a4499a5c1bfe7f43e014bdce9a6b6666e5aa2d187" dependencies = [ "bitflags", "cc", "cfg-if", "libc", + "memoffset", ] [[package]] @@ -540,9 +548,9 @@ checksum = "8d31d11c69a6b52a174b42bdc0c30e5e11670f90788b2c471c31c1d17d449443" [[package]] name = "powershell_script" -version = "0.1.5" +version = "0.2.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "232c43ca4a2aec888dbdcf0167ad05c228692624d5b75b0a775ef4065ca8b03e" +checksum = "36d62894f5590e88d99d0d82918742ba8e5bff1985af15d4906b6a65f635adb2" [[package]] name = "ppv-lite86" @@ -850,13 +858,12 @@ dependencies = [ [[package]] name = "sysinfo" -version = "0.19.2" +version = "0.20.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9e7de153d0438a648bb71e06e300e54fc641685e96af96d49b843f43172d341c" +checksum = "0af066e6272f2175c1783cfc2ebf3e2d8dfe2c182b00677fdeccbf8291af83fb" dependencies = [ "cfg-if", "core-foundation-sys", - "doc-comment", "libc", "ntapi", "once_cell", diff --git a/komorebi-core/Cargo.toml b/komorebi-core/Cargo.toml index a7343a6f..83210cc4 100644 --- a/komorebi-core/Cargo.toml +++ b/komorebi-core/Cargo.toml @@ -8,8 +8,8 @@ edition = "2018" [dependencies] bindings = { package = "bindings", path = "../bindings" } -color-eyre = "0.5.11" clap = "3.0.0-beta.2" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" +color-eyre = "0.5" +serde = { version = "1", features = ["derive"] } +serde_json = "1" strum = { version = "0.21", features = ["derive"] } diff --git a/komorebi/Cargo.toml b/komorebi/Cargo.toml index 4c9d8309..808099a8 100644 --- a/komorebi/Cargo.toml +++ b/komorebi/Cargo.toml @@ -9,21 +9,22 @@ edition = "2018" bindings = { package = "bindings", path = "../bindings" } komorebi-core = { path = "../komorebi-core" } -bitflags = "1.2.1" -color-eyre = "0.5.11" -crossbeam-channel = "0.5.1" -crossbeam-utils = "0.8.5" +bitflags = "1" +color-eyre = "0.5" +crossbeam-channel = "0.5" +crossbeam-utils = "0.8" ctrlc = "3" dirs = "3" -eyre = "0.6.5" -lazy_static = "1.4.0" -nanoid = "0.4.0" -paste = "1.0" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" +eyre = "0.6" +getset = "0.1" +lazy_static = "1" +nanoid = "0.4" +paste = "1" +serde = { version = "1", features = ["derive"] } +serde_json = "1" strum = { version = "0.21", features = ["derive"] } -sysinfo = "0.19" -tracing = "0.1.26" -tracing-appender = "0.1.2" -tracing-subscriber = "0.2.19" +sysinfo = "0.20" +tracing = "0.1" +tracing-appender = "0.1" +tracing-subscriber = "0.2" uds_windows = "1" \ No newline at end of file diff --git a/komorebi/src/container.rs b/komorebi/src/container.rs index 9300e5a5..1b3ca051 100644 --- a/komorebi/src/container.rs +++ b/komorebi/src/container.rs @@ -1,14 +1,16 @@ use std::collections::VecDeque; +use getset::Getters; use nanoid::nanoid; use serde::Serialize; use crate::ring::Ring; use crate::window::Window; -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Serialize, Getters)] pub struct Container { #[serde(skip_serializing)] + #[getset(get = "pub")] id: String, windows: Ring, } @@ -94,8 +96,4 @@ impl Container { tracing::info!("focusing window"); self.windows.focus(idx); } - - pub const fn id(&self) -> &String { - &self.id - } } diff --git a/komorebi/src/monitor.rs b/komorebi/src/monitor.rs index 7915db0e..3eb8a7e1 100644 --- a/komorebi/src/monitor.rs +++ b/komorebi/src/monitor.rs @@ -3,6 +3,9 @@ use std::collections::VecDeque; use color_eyre::eyre::ContextCompat; use color_eyre::Result; +use getset::CopyGetters; +use getset::Getters; +use getset::MutGetters; use serde::Serialize; use komorebi_core::Rect; @@ -11,13 +14,16 @@ use crate::container::Container; use crate::ring::Ring; use crate::workspace::Workspace; -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Serialize, Getters, CopyGetters, MutGetters)] pub struct Monitor { + #[getset(get_copy = "pub")] id: isize, monitor_size: Rect, + #[getset(get = "pub")] work_area_size: Rect, workspaces: Ring, #[serde(skip_serializing)] + #[getset(get_mut = "pub")] workspace_names: HashMap, } @@ -131,16 +137,4 @@ impl Monitor { Ok(()) } - - pub fn workspace_names_mut(&mut self) -> &mut HashMap { - &mut self.workspace_names - } - - pub const fn id(&self) -> isize { - self.id - } - - pub const fn work_area_size(&self) -> &Rect { - &self.work_area_size - } } diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index f7c8b607..2289b236 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -422,7 +422,9 @@ impl WindowManager { #[allow(clippy::match_same_arms)] match workspace.layout_flip() { - None => workspace.set_layout_flip(Option::from(layout_flip)), + None => { + workspace.set_layout_flip(Option::from(layout_flip)); + } Some(current_layout_flip) => { match current_layout_flip { LayoutFlip::Horizontal => match layout_flip { @@ -439,18 +441,16 @@ impl WindowManager { LayoutFlip::HorizontalAndVertical => workspace .set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)), }, - LayoutFlip::HorizontalAndVertical => { - match layout_flip { - LayoutFlip::Horizontal => { - workspace.set_layout_flip(Option::from(LayoutFlip::Vertical)); - } - LayoutFlip::Vertical => { - workspace.set_layout_flip(Option::from(LayoutFlip::Horizontal)); - } - LayoutFlip::HorizontalAndVertical => workspace.set_layout_flip(None), - }; - } - } + LayoutFlip::HorizontalAndVertical => match layout_flip { + LayoutFlip::Horizontal => { + workspace.set_layout_flip(Option::from(LayoutFlip::Vertical)) + } + LayoutFlip::Vertical => { + workspace.set_layout_flip(Option::from(LayoutFlip::Horizontal)) + } + LayoutFlip::HorizontalAndVertical => workspace.set_layout_flip(None), + }, + }; } } @@ -621,9 +621,7 @@ impl WindowManager { self.update_focused_workspace(false) } -} -impl WindowManager { pub fn focused_monitor_work_area(&self) -> Result { Ok(*self .focused_monitor() diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index 95f3742f..c1ea4f49 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -2,6 +2,10 @@ use std::collections::VecDeque; use color_eyre::eyre::ContextCompat; use color_eyre::Result; +use getset::CopyGetters; +use getset::Getters; +use getset::MutGetters; +use getset::Setters; use serde::Serialize; use komorebi_core::Layout; @@ -14,21 +18,31 @@ use crate::ring::Ring; use crate::window::Window; use crate::windows_api::WindowsApi; -#[derive(Debug, Clone, Serialize)] +#[derive(Debug, Clone, Serialize, Getters, CopyGetters, MutGetters, Setters)] pub struct Workspace { + #[getset(set = "pub")] name: Option, containers: Ring, + #[getset(get = "pub", get_mut = "pub", set = "pub")] monocle_container: Option, #[serde(skip_serializing)] + #[getset(get_copy = "pub", set = "pub")] monocle_restore_idx: Option, + #[getset(get = "pub", get_mut = "pub")] floating_windows: Vec, + #[getset(get_copy = "pub", set = "pub")] layout: Layout, + #[getset(get_copy = "pub", set = "pub")] layout_flip: Option, + #[getset(get_copy = "pub", set = "pub")] workspace_padding: Option, + #[getset(get_copy = "pub", set = "pub")] container_padding: Option, #[serde(skip_serializing)] + #[getset(get = "pub", set = "pub")] latest_layout: Vec, #[serde(skip_serializing)] + #[getset(get = "pub", get_mut = "pub")] resize_dimensions: Vec>, } @@ -456,14 +470,15 @@ impl Workspace { // inevitably reintegrated, it would be weird if it doesn't go back to the dimensions // it had before - self.monocle_container = Option::from(container); - self.monocle_restore_idx = Option::from(focused_idx); + self.set_monocle_container(Option::from(container)); + self.set_monocle_restore_idx(Option::from(focused_idx)); if focused_idx != 0 { self.focus_container(focused_idx - 1); } self.monocle_container_mut() + .as_mut() .context("there is no monocle container")? .load_focused_window(); @@ -477,6 +492,7 @@ impl Workspace { let container = self .monocle_container_mut() + .as_ref() .context("there is no monocle container")?; let container = container.clone(); @@ -491,23 +507,11 @@ impl Workspace { .context("there is no container")? .load_focused_window(); - self.monocle_container = None; + self.set_monocle_container(None); Ok(()) } - pub const fn monocle_container(&self) -> Option<&Container> { - self.monocle_container.as_ref() - } - - pub fn monocle_container_mut(&mut self) -> Option<&mut Container> { - self.monocle_container.as_mut() - } - - pub const fn monocle_restore_idx(&self) -> Option { - self.monocle_restore_idx - } - #[tracing::instrument(skip(self))] pub fn focus_container(&mut self, idx: usize) { tracing::info!("focusing container"); @@ -542,14 +546,6 @@ impl Workspace { } } - pub const fn floating_windows(&self) -> &Vec { - &self.floating_windows - } - - pub fn floating_windows_mut(&mut self) -> &mut Vec { - self.floating_windows.as_mut() - } - pub fn visible_windows_mut(&mut self) -> Vec> { let mut vec = vec![]; for container in self.containers_mut() { @@ -558,56 +554,4 @@ impl Workspace { vec } - - pub const fn layout(&self) -> Layout { - self.layout - } - - pub const fn layout_flip(&self) -> Option { - self.layout_flip - } - - pub const fn workspace_padding(&self) -> Option { - self.workspace_padding - } - - pub const fn container_padding(&self) -> Option { - self.container_padding - } - - pub const fn latest_layout(&self) -> &Vec { - &self.latest_layout - } - - pub fn set_name(&mut self, name: Option) { - self.name = name; - } - - pub fn set_layout(&mut self, layout: Layout) { - self.layout = layout; - } - - pub fn set_layout_flip(&mut self, layout_flip: Option) { - self.layout_flip = layout_flip; - } - - pub fn set_workspace_padding(&mut self, padding: Option) { - self.workspace_padding = padding; - } - - pub fn set_container_padding(&mut self, padding: Option) { - self.container_padding = padding; - } - - pub fn set_latest_layout(&mut self, layout: Vec) { - self.latest_layout = layout; - } - - pub const fn resize_dimensions(&self) -> &Vec> { - &self.resize_dimensions - } - - pub fn resize_dimensions_mut(&mut self) -> &mut Vec> { - &mut self.resize_dimensions - } } diff --git a/komorebic/Cargo.toml b/komorebic/Cargo.toml index 3ac31bb4..b460c1a4 100644 --- a/komorebic/Cargo.toml +++ b/komorebic/Cargo.toml @@ -10,9 +10,9 @@ bindings = { package = "bindings", path = "../bindings" } komorebi-core = { path = "../komorebi-core" } clap = "3.0.0-beta.2" -color-eyre = "0.5.11" +color-eyre = "0.5" dirs = "3" -powershell_script = "0.1.5" -serde = { version = "1.0", features = ["derive"] } -serde_json = "1.0" +powershell_script = "0.2" +serde = { version = "1", features = ["derive"] } +serde_json = "1" uds_windows = "1" \ No newline at end of file