Cleaned up consts to enum
Browse files
+42 −13 src/layout/mod.rs
| @@ -12,24 +12,53 @@ pub struct GapConfig { | |||
| 12 | pub outer_vertical: u32, | 12 | pub outer_vertical: u32, |
| 13 | } | 13 | } |
| 14 | 14 | ||
| 15 | -pub const TILING: &str = "tiling"; | 15 | +pub enum LayoutType { |
| 16 | -pub const NORMIE: &str = "normie"; | 16 | + Tiling, |
| 17 | -pub const FLOATING: &str = "floating"; | 17 | + Normie, |
| 18 | +} | ||
| 18 | 19 | ||
| 19 | -pub fn layout_from_str(s: &str) -> Result<LayoutBox, String> { | 20 | +impl LayoutType { |
| 20 | - match s.to_lowercase().as_str() { | 21 | + pub fn new(&self) -> LayoutBox { |
| 21 | - TILING => Ok(Box::new(tiling::TilingLayout)), | 22 | + match self { |
| 22 | - NORMIE | FLOATING => Ok(Box::new(normie::NormieLayout)), | 23 | + Self::Tiling => Box::new(tiling::TilingLayout), |
| 23 | - _ => Err(format!("Unknown layout: {}", s)), | 24 | + Self::Normie => Box::new(normie::NormieLayout), |
| 25 | + } | ||
| 26 | + } | ||
| 27 | + | ||
| 28 | + pub fn next(&self) -> Self { | ||
| 29 | + match self { | ||
| 30 | + Self::Tiling => Self::Normie, | ||
| 31 | + Self::Normie => Self::Tiling, | ||
| 32 | + } | ||
| 33 | + } | ||
| 34 | + | ||
| 35 | + pub fn as_str(&self) -> &'static str { | ||
| 36 | + match self { | ||
| 37 | + Self::Tiling => "tiling", | ||
| 38 | + Self::Normie => "normie", | ||
| 39 | + } | ||
| 24 | } | 40 | } |
| 41 | + | ||
| 42 | + pub fn from_str(s: &str) -> Result<Self, String> { | ||
| 43 | + match s.to_lowercase().as_str() { | ||
| 44 | + "tiling" => Ok(Self::Tiling), | ||
| 45 | + "normie" | "floating" => Ok(Self::Normie), | ||
| 46 | + _ => Err(format!("Invalid Layout Type: {}", s)), | ||
| 47 | + } | ||
| 48 | + } | ||
| 49 | +} | ||
| 50 | + | ||
| 51 | +pub fn layout_from_str(s: &str) -> Result<LayoutBox, String> { | ||
| 52 | + let layout_type = LayoutType::from_str(s)?; | ||
| 53 | + Ok(layout_type.new()) | ||
| 25 | } | 54 | } |
| 26 | 55 | ||
| 27 | pub fn next_layout(current_name: &str) -> &'static str { | 56 | pub fn next_layout(current_name: &str) -> &'static str { |
| 28 | - match current_name { | 57 | + LayoutType::from_str(current_name) |
| 29 | - TILING => NORMIE, | 58 | + .ok() |
| 30 | - NORMIE => TILING, | 59 | + .map(|layout_type| layout_type.next()) |
| 31 | - _ => TILING, | 60 | + .unwrap_or(LayoutType::Tiling) |
| 32 | - } | 61 | + .as_str() |
| 33 | } | 62 | } |
| 34 | 63 | ||
| 35 | pub trait Layout { | 64 | pub trait Layout { |
+1 −1 src/layout/normie.rs
| @@ -6,7 +6,7 @@ pub struct NormieLayout; | |||
| 6 | // This layout should return a no-op similar to DWM.C's "null" mode. | 6 | // This layout should return a no-op similar to DWM.C's "null" mode. |
| 7 | impl Layout for NormieLayout { | 7 | impl Layout for NormieLayout { |
| 8 | fn name(&self) -> &'static str { | 8 | fn name(&self) -> &'static str { |
| 9 | - super::NORMIE | 9 | + super::LayoutType::Normie.as_str() |
| 10 | } | 10 | } |
| 11 | 11 | ||
| 12 | fn symbol(&self) -> &'static str { | 12 | fn symbol(&self) -> &'static str { |
+1 −1 src/layout/tiling.rs
| @@ -5,7 +5,7 @@ pub struct TilingLayout; | |||
| 5 | 5 | ||
| 6 | impl Layout for TilingLayout { | 6 | impl Layout for TilingLayout { |
| 7 | fn name(&self) -> &'static str { | 7 | fn name(&self) -> &'static str { |
| 8 | - super::TILING | 8 | + super::LayoutType::Tiling.as_str() |
| 9 | } | 9 | } |
| 10 | 10 | ||
| 11 | fn symbol(&self) -> &'static str { | 11 | fn symbol(&self) -> &'static str { |
+2 −2 src/window_manager.rs
| @@ -4,7 +4,7 @@ use crate::errors::WmError; | |||
| 4 | use crate::keyboard::{self, Arg, KeyAction, handlers}; | 4 | use crate::keyboard::{self, Arg, KeyAction, handlers}; |
| 5 | use crate::layout::GapConfig; | 5 | use crate::layout::GapConfig; |
| 6 | use crate::layout::tiling::TilingLayout; | 6 | use crate::layout::tiling::TilingLayout; |
| 7 | -use crate::layout::{Layout, LayoutBox, layout_from_str, next_layout}; | 7 | +use crate::layout::{Layout, LayoutBox, LayoutType, layout_from_str, next_layout}; |
| 8 | use crate::monitor::{Monitor, detect_monitors}; | 8 | use crate::monitor::{Monitor, detect_monitors}; |
| 9 | use std::collections::HashSet; | 9 | use std::collections::HashSet; |
| 10 | use x11rb::cursor::Handle as CursorHandle; | 10 | use x11rb::cursor::Handle as CursorHandle; |
| @@ -1422,7 +1422,7 @@ impl WindowManager { | |||
| 1422 | return Ok(()); | 1422 | return Ok(()); |
| 1423 | } | 1423 | } |
| 1424 | 1424 | ||
| 1425 | - if self.layout.name() == crate::layout::NORMIE { | 1425 | + if self.layout.name() == LayoutType::Normie.as_str() { |
| 1426 | return Ok(()); | 1426 | return Ok(()); |
| 1427 | } | 1427 | } |
| 1428 | 1428 | ||