diff --git a/komorebi-core/src/cycle_direction.rs b/komorebi-core/src/cycle_direction.rs index 521c804d..fafa76b2 100644 --- a/komorebi-core/src/cycle_direction.rs +++ b/komorebi-core/src/cycle_direction.rs @@ -12,7 +12,8 @@ pub enum CycleDirection { } impl CycleDirection { - pub fn next_idx(&self, idx: usize, len: usize) -> usize { + #[must_use] + pub const fn next_idx(&self, idx: usize, len: usize) -> usize { match self { CycleDirection::Previous => { if idx == 0 { diff --git a/komorebi-core/src/layout.rs b/komorebi-core/src/layout.rs index a5960fca..b975033d 100644 --- a/komorebi-core/src/layout.rs +++ b/komorebi-core/src/layout.rs @@ -20,13 +20,15 @@ pub enum Layout { #[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)] #[strum(serialize_all = "snake_case")] -pub enum LayoutFlip { +pub enum Flip { Horizontal, Vertical, HorizontalAndVertical, } impl Layout { + #[must_use] + #[allow(clippy::cast_precision_loss)] pub fn resize( &self, unaltered: &Rect, @@ -35,10 +37,14 @@ impl Layout { sizing: Sizing, step: Option, ) -> Option { + if !matches!(self, Self::BSP) { + return None; + }; + let max_divisor = 1.005; let mut r = resize.unwrap_or_default(); - let resize_step = if let Some(step) = step { step } else { 50 }; + let resize_step = step.unwrap_or(50); match edge { OperationDirection::Left => match sizing { @@ -117,19 +123,21 @@ impl Layout { }, }; - if !r.eq(&Rect::default()) { - Option::from(r) - } else { + if r.eq(&Rect::default()) { None + } else { + Option::from(r) } } + #[must_use] + #[allow(clippy::cast_possible_truncation, clippy::cast_possible_wrap)] pub fn calculate( &self, area: &Rect, len: NonZeroUsize, container_padding: Option, - layout_flip: Option, + layout_flip: Option, resize_dimensions: &[Option], ) -> Vec { let len = usize::from(len); @@ -187,24 +195,6 @@ impl Layout { } } -impl Layout { - pub fn next(&mut self) { - match self { - Layout::BSP => *self = Layout::Columns, - Layout::Columns => *self = Layout::Rows, - Layout::Rows => *self = Layout::BSP, - } - } - - pub fn previous(&mut self) { - match self { - Layout::BSP => *self = Layout::Rows, - Layout::Columns => *self = Layout::BSP, - Layout::Rows => *self = Layout::Columns, - } - } -} - fn calculate_resize_adjustments(resize_dimensions: &[Option]) -> Vec> { let mut resize_adjustments = resize_dimensions.to_vec(); @@ -213,6 +203,7 @@ fn calculate_resize_adjustments(resize_dimensions: &[Option]) -> Vec 0 { if resize_ref.left != 0 { + #[allow(clippy::if_not_else)] let range = if i == 1 { 0..1 } else if i & 1 != 0 { @@ -291,7 +282,7 @@ fn recursive_fibonacci( idx: usize, count: usize, area: &Rect, - layout_flip: Option, + layout_flip: Option, resize_adjustments: Vec>, ) -> Vec { let mut a = *area; @@ -313,37 +304,37 @@ fn recursive_fibonacci( let (main_x, alt_x, alt_y, main_y); - match layout_flip { - Some(flip) => match flip { - LayoutFlip::Horizontal => { + if let Some(flip) = layout_flip { + match flip { + Flip::Horizontal => { main_x = resized.left + half_width + (half_width - half_resized_width); alt_x = resized.left; alt_y = resized.top + half_resized_height; main_y = resized.top; } - LayoutFlip::Vertical => { + Flip::Vertical => { main_y = resized.top + half_height + (half_height - half_resized_height); alt_y = resized.top; main_x = resized.left; alt_x = resized.left + half_resized_width; } - LayoutFlip::HorizontalAndVertical => { + Flip::HorizontalAndVertical => { main_x = resized.left + half_width + (half_width - half_resized_width); alt_x = resized.left; main_y = resized.top + half_height + (half_height - half_resized_height); alt_y = resized.top; } - }, - None => { - main_x = resized.left; - alt_x = resized.left + half_resized_width; - main_y = resized.top; - alt_y = resized.top + half_resized_height; } + } else { + main_x = resized.left; + alt_x = resized.left + half_resized_width; + main_y = resized.top; + alt_y = resized.top + half_resized_height; } + #[allow(clippy::if_not_else)] if count == 0 { vec![] } else if count == 1 { diff --git a/komorebi-core/src/lib.rs b/komorebi-core/src/lib.rs index 5a01e402..d40091b5 100644 --- a/komorebi-core/src/lib.rs +++ b/komorebi-core/src/lib.rs @@ -1,3 +1,6 @@ +#![warn(clippy::all, clippy::nursery, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] + use std::str::FromStr; use clap::ArgEnum; @@ -8,8 +11,8 @@ use strum::Display; use strum::EnumString; pub use cycle_direction::CycleDirection; +pub use layout::Flip; pub use layout::Layout; -pub use layout::LayoutFlip; pub use operation_direction::OperationDirection; pub use rect::Rect; @@ -39,7 +42,7 @@ pub enum SocketMessage { AdjustContainerPadding(Sizing, i32), AdjustWorkspacePadding(Sizing, i32), ChangeLayout(Layout), - FlipLayout(LayoutFlip), + FlipLayout(Flip), // Monitor and Workspace Commands EnsureWorkspaces(usize, usize), NewWorkspace, @@ -99,7 +102,8 @@ pub enum Sizing { } impl Sizing { - pub fn adjust_by(&self, value: i32, adjustment: i32) -> i32 { + #[must_use] + pub const fn adjust_by(&self, value: i32, adjustment: i32) -> i32 { match self { Sizing::Increase => value + adjustment, Sizing::Decrease => { diff --git a/komorebi-core/src/operation_direction.rs b/komorebi-core/src/operation_direction.rs index 4e19281f..61bec052 100644 --- a/komorebi-core/src/operation_direction.rs +++ b/komorebi-core/src/operation_direction.rs @@ -4,8 +4,8 @@ use serde::Serialize; use strum::Display; use strum::EnumString; +use crate::Flip; use crate::Layout; -use crate::LayoutFlip; #[derive(Clone, Copy, Debug, Serialize, Deserialize, Display, EnumString, ArgEnum)] #[strum(serialize_all = "snake_case")] @@ -17,59 +17,46 @@ pub enum OperationDirection { } impl OperationDirection { - pub fn opposite(self) -> Self { + #[must_use] + pub const fn opposite(self) -> Self { match self { - OperationDirection::Left => OperationDirection::Right, - OperationDirection::Right => OperationDirection::Left, - OperationDirection::Up => OperationDirection::Down, - OperationDirection::Down => OperationDirection::Up, + Self::Left => Self::Right, + Self::Right => Self::Left, + Self::Up => Self::Down, + Self::Down => Self::Up, } } - fn flip_direction( - direction: &OperationDirection, - layout_flip: Option, - ) -> OperationDirection { - if let Some(flip) = layout_flip { - match direction { - OperationDirection::Left => match flip { - LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => { - OperationDirection::Right - } - _ => *direction, - }, - OperationDirection::Right => match flip { - LayoutFlip::Horizontal | LayoutFlip::HorizontalAndVertical => { - OperationDirection::Left - } - _ => *direction, - }, - OperationDirection::Up => match flip { - LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => { - OperationDirection::Down - } - _ => *direction, - }, - OperationDirection::Down => match flip { - LayoutFlip::Vertical | LayoutFlip::HorizontalAndVertical => { - OperationDirection::Up - } - _ => *direction, - }, - } - } else { - *direction - } + fn flip_direction(direction: Self, layout_flip: Option) -> Self { + layout_flip.map_or(direction, |flip| match direction { + Self::Left => match flip { + Flip::Horizontal | Flip::HorizontalAndVertical => Self::Right, + Flip::Vertical => direction, + }, + Self::Right => match flip { + Flip::Horizontal | Flip::HorizontalAndVertical => Self::Left, + Flip::Vertical => direction, + }, + Self::Up => match flip { + Flip::Vertical | Flip::HorizontalAndVertical => Self::Down, + Flip::Horizontal => direction, + }, + Self::Down => match flip { + Flip::Vertical | Flip::HorizontalAndVertical => Self::Up, + Flip::Horizontal => direction, + }, + }) } + #[must_use] pub fn is_valid( - &self, + self, layout: Layout, - layout_flip: Option, + layout_flip: Option, idx: usize, len: usize, ) -> bool { - match OperationDirection::flip_direction(self, layout_flip) { + match Self::flip_direction(self, layout_flip) { OperationDirection::Up => match layout { Layout::BSP => len > 2 && idx != 0 && idx != 1, Layout::Columns => false, @@ -93,9 +80,10 @@ impl OperationDirection { } } - pub fn new_idx(&self, layout: Layout, layout_flip: Option, idx: usize) -> usize { - match OperationDirection::flip_direction(self, layout_flip) { - OperationDirection::Up => match layout { + #[must_use] + pub fn new_idx(self, layout: Layout, layout_flip: Option, idx: usize) -> usize { + match Self::flip_direction(self, layout_flip) { + Self::Up => match layout { Layout::BSP => { if idx % 2 == 0 { idx - 1 @@ -106,11 +94,11 @@ impl OperationDirection { Layout::Columns => unreachable!(), Layout::Rows => idx - 1, }, - OperationDirection::Down => match layout { + Self::Down => match layout { Layout::BSP | Layout::Rows => idx + 1, Layout::Columns => unreachable!(), }, - OperationDirection::Left => match layout { + Self::Left => match layout { Layout::BSP => { if idx % 2 == 0 { idx - 2 @@ -121,7 +109,7 @@ impl OperationDirection { Layout::Columns => idx - 1, Layout::Rows => unreachable!(), }, - OperationDirection::Right => match layout { + Self::Right => match layout { Layout::BSP | Layout::Columns => idx + 1, Layout::Rows => unreachable!(), }, diff --git a/komorebi-core/src/rect.rs b/komorebi-core/src/rect.rs index fb8e15e7..85f21b89 100644 --- a/komorebi-core/src/rect.rs +++ b/komorebi-core/src/rect.rs @@ -12,7 +12,7 @@ pub struct Rect { impl Default for Rect { fn default() -> Self { - Rect { + Self { left: 0, top: 0, right: 0, @@ -23,7 +23,7 @@ impl Default for Rect { impl From for Rect { fn from(rect: RECT) -> Self { - Rect { + Self { left: rect.left, top: rect.top, right: rect.right - rect.left, @@ -42,7 +42,8 @@ impl Rect { } } - pub fn contains_point(&self, point: (i32, i32)) -> bool { + #[must_use] + pub const fn contains_point(&self, point: (i32, i32)) -> bool { point.0 >= self.left && point.0 <= self.left + self.right && point.1 >= self.top diff --git a/komorebi/src/window.rs b/komorebi/src/window.rs index dd32e7de..19e0757c 100644 --- a/komorebi/src/window.rs +++ b/komorebi/src/window.rs @@ -267,7 +267,7 @@ impl Window { && (allow_layered || !ex_style.contains(GwlExStyle::LAYERED)) || managed_override { - return Ok(true) + return Ok(true); } else if event.is_some() { tracing::debug!("ignoring (exe: {}, title: {})", exe_name, title); } diff --git a/komorebi/src/window_manager.rs b/komorebi/src/window_manager.rs index 8b694794..607315dd 100644 --- a/komorebi/src/window_manager.rs +++ b/komorebi/src/window_manager.rs @@ -15,8 +15,8 @@ use serde::Serialize; use uds_windows::UnixListener; use komorebi_core::CycleDirection; +use komorebi_core::Flip; use komorebi_core::Layout; -use komorebi_core::LayoutFlip; use komorebi_core::OperationDirection; use komorebi_core::Rect; use komorebi_core::Sizing; @@ -432,21 +432,21 @@ impl WindowManager { // can flip them however they need to be flipped once the resizing has been done if let Some(flip) = workspace.layout_flip() { match flip { - LayoutFlip::Horizontal => { + Flip::Horizontal => { if matches!(direction, OperationDirection::Left) || matches!(direction, OperationDirection::Right) { direction = direction.opposite(); } } - LayoutFlip::Vertical => { + Flip::Vertical => { if matches!(direction, OperationDirection::Up) || matches!(direction, OperationDirection::Down) { direction = direction.opposite(); } } - LayoutFlip::HorizontalAndVertical => direction = direction.opposite(), + Flip::HorizontalAndVertical => direction = direction.opposite(), } } @@ -746,7 +746,7 @@ impl WindowManager { } #[tracing::instrument(skip(self))] - pub fn flip_layout(&mut self, layout_flip: LayoutFlip) -> Result<()> { + pub fn flip_layout(&mut self, layout_flip: Flip) -> Result<()> { tracing::info!("flipping layout"); let workspace = self.focused_workspace_mut()?; @@ -758,28 +758,28 @@ impl WindowManager { } Some(current_layout_flip) => { match current_layout_flip { - LayoutFlip::Horizontal => match layout_flip { - LayoutFlip::Horizontal => workspace.set_layout_flip(None), - LayoutFlip::Vertical => workspace - .set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)), - LayoutFlip::HorizontalAndVertical => workspace - .set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)), - }, - LayoutFlip::Vertical => match layout_flip { - LayoutFlip::Horizontal => workspace - .set_layout_flip(Option::from(LayoutFlip::HorizontalAndVertical)), - LayoutFlip::Vertical => workspace.set_layout_flip(None), - 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)) + Flip::Horizontal => match layout_flip { + Flip::Horizontal => workspace.set_layout_flip(None), + Flip::Vertical => { + workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical)) } - LayoutFlip::Vertical => { - workspace.set_layout_flip(Option::from(LayoutFlip::Horizontal)) + Flip::HorizontalAndVertical => { + workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical)) } - LayoutFlip::HorizontalAndVertical => workspace.set_layout_flip(None), + }, + Flip::Vertical => match layout_flip { + Flip::Horizontal => { + workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical)) + } + Flip::Vertical => workspace.set_layout_flip(None), + Flip::HorizontalAndVertical => { + workspace.set_layout_flip(Option::from(Flip::HorizontalAndVertical)) + } + }, + Flip::HorizontalAndVertical => match layout_flip { + Flip::Horizontal => workspace.set_layout_flip(Option::from(Flip::Vertical)), + Flip::Vertical => workspace.set_layout_flip(Option::from(Flip::Horizontal)), + Flip::HorizontalAndVertical => workspace.set_layout_flip(None), }, }; } diff --git a/komorebi/src/workspace.rs b/komorebi/src/workspace.rs index d2e16c1c..a6355581 100644 --- a/komorebi/src/workspace.rs +++ b/komorebi/src/workspace.rs @@ -9,8 +9,8 @@ use getset::MutGetters; use getset::Setters; use serde::Serialize; +use komorebi_core::Flip; use komorebi_core::Layout; -use komorebi_core::LayoutFlip; use komorebi_core::OperationDirection; use komorebi_core::Rect; @@ -39,7 +39,7 @@ pub struct Workspace { #[getset(get_copy = "pub", set = "pub")] layout: Layout, #[getset(get_copy = "pub", set = "pub")] - layout_flip: Option, + layout_flip: Option, #[getset(get_copy = "pub", set = "pub")] workspace_padding: Option, #[getset(get_copy = "pub", set = "pub")] diff --git a/komorebic/src/main.rs b/komorebic/src/main.rs index 3fd84f41..ca658e7e 100644 --- a/komorebic/src/main.rs +++ b/komorebic/src/main.rs @@ -1,3 +1,6 @@ +#![warn(clippy::all, clippy::nursery, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] + use std::fs::File; use std::io::BufRead; use std::io::BufReader; @@ -22,8 +25,8 @@ use bindings::Windows::Win32::UI::WindowsAndMessaging::SHOW_WINDOW_CMD; use bindings::Windows::Win32::UI::WindowsAndMessaging::SW_RESTORE; use komorebi_core::ApplicationIdentifier; use komorebi_core::CycleDirection; +use komorebi_core::Flip; use komorebi_core::Layout; -use komorebi_core::LayoutFlip; use komorebi_core::OperationDirection; use komorebi_core::Sizing; use komorebi_core::SocketMessage; @@ -63,7 +66,7 @@ gen_enum_subcommand_args! { Move: OperationDirection, Stack: OperationDirection, CycleStack: CycleDirection, - FlipLayout: LayoutFlip, + FlipLayout: Flip, SetLayout: Layout, WatchConfiguration: BooleanState, FocusFollowsMouse: BooleanState @@ -303,6 +306,7 @@ pub fn send_message(bytes: &[u8]) -> Result<()> { Ok(stream.write_all(&*bytes)?) } +#[allow(clippy::too_many_lines)] fn main() -> Result<()> { let opts: Opts = Opts::parse(); @@ -313,7 +317,7 @@ fn main() -> Result<()> { let file = TailedFile::new(File::open(color_log)?); let locked = file.lock(); for line in locked.lines() { - println!("{}", line?) + println!("{}", line?); } } SubCommand::Focus(arg) => { @@ -449,7 +453,7 @@ fn main() -> Result<()> { send_message(&*SocketMessage::ChangeLayout(arg.layout).as_bytes()?)?; } SubCommand::FlipLayout(arg) => { - send_message(&*SocketMessage::FlipLayout(arg.layout_flip).as_bytes()?)?; + send_message(&*SocketMessage::FlipLayout(arg.flip).as_bytes()?)?; } SubCommand::FocusMonitor(arg) => { send_message(&*SocketMessage::FocusMonitorNumber(arg.target).as_bytes()?)?; @@ -496,13 +500,13 @@ fn main() -> Result<()> { Ok(incoming) => { let stream = BufReader::new(incoming.0); for line in stream.lines() { - println!("{}", line?) + println!("{}", line?); } return Ok(()); } Err(error) => { - panic!("{}", error) + panic!("{}", error); } } }