Skip to content

Commit

Permalink
refactor: replace enum_dispatch with ambassador for better code c…
Browse files Browse the repository at this point in the history
…ompletion (#637)
  • Loading branch information
LucaCoduriV authored Aug 2, 2024
1 parent 8ed9649 commit 574d7e8
Show file tree
Hide file tree
Showing 8 changed files with 148 additions and 46 deletions.
42 changes: 29 additions & 13 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion packages/wm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ tauri-winres = { workspace = true }
[dependencies]
anyhow = { workspace = true }
clap = { version = "4", features = ["derive"] }
enum_dispatch = "0.3"
ambassador = "0.4"
enum-as-inner = "0.6"
futures-util = "0.3"
home = "0.5"
Expand Down
115 changes: 104 additions & 11 deletions packages/wm/src/containers/container.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,31 @@
use std::{
cell::{Ref, RefMut},
collections::VecDeque,
};

use ambassador::Delegate;
use enum_as_inner::EnumAsInner;
use enum_dispatch::enum_dispatch;
use uuid::Uuid;

use super::{traits::CommonGetters, RootContainer, SplitContainer};
use super::{RootContainer, SplitContainer};
use crate::{
common::{
platform::NativeWindow, Direction, DisplayState, LengthValue, Rect,
RectDelta, TilingDirection,
},
containers::{traits::*, ContainerDto},
monitors::Monitor,
windows::{NonTilingWindow, TilingWindow},
user_config::{UserConfig, WindowRuleConfig},
windows::{
traits::*, ActiveDrag, NonTilingWindow, TilingWindow, WindowState,
},
workspaces::Workspace,
};

/// A container of any type.
#[derive(Clone, Debug, EnumAsInner)]
#[enum_dispatch(CommonGetters, PositionGetters)]
#[derive(Clone, Debug, EnumAsInner, Delegate)]
#[delegate(CommonGetters)]
#[delegate(PositionGetters)]
pub enum Container {
Root(RootContainer),
Monitor(Monitor),
Expand All @@ -20,6 +35,42 @@ pub enum Container {
NonTilingWindow(NonTilingWindow),
}

impl From<RootContainer> for Container {
fn from(value: RootContainer) -> Self {
Container::Root(value)
}
}

impl From<Monitor> for Container {
fn from(value: Monitor) -> Self {
Container::Monitor(value)
}
}

impl From<Workspace> for Container {
fn from(value: Workspace) -> Self {
Container::Workspace(value)
}
}

impl From<SplitContainer> for Container {
fn from(value: SplitContainer) -> Self {
Container::Split(value)
}
}

impl From<NonTilingWindow> for Container {
fn from(value: NonTilingWindow) -> Self {
Container::NonTilingWindow(value)
}
}

impl From<TilingWindow> for Container {
fn from(value: TilingWindow) -> Self {
Container::TilingWindow(value)
}
}

impl From<TilingContainer> for Container {
fn from(tiling_container: TilingContainer) -> Self {
match tiling_container {
Expand Down Expand Up @@ -59,13 +110,27 @@ impl Eq for Container {}
/// * `CommonGetters`
/// * `PositionGetters`
/// * `TilingSizeGetters`
#[derive(Clone, Debug, EnumAsInner)]
#[enum_dispatch(CommonGetters, PositionGetters, TilingSizeGetters)]
#[derive(Clone, Debug, EnumAsInner, Delegate)]
#[delegate(CommonGetters)]
#[delegate(PositionGetters)]
#[delegate(TilingSizeGetters)]
pub enum TilingContainer {
Split(SplitContainer),
TilingWindow(TilingWindow),
}

impl From<SplitContainer> for TilingContainer {
fn from(value: SplitContainer) -> Self {
TilingContainer::Split(value)
}
}

impl From<TilingWindow> for TilingContainer {
fn from(value: TilingWindow) -> Self {
TilingContainer::TilingWindow(value)
}
}

impl TryFrom<Container> for TilingContainer {
type Error = &'static str;

Expand All @@ -90,13 +155,27 @@ impl Eq for TilingContainer {}
/// * `CommonGetters`
/// * `PositionGetters`
/// * `WindowGetters`
#[derive(Clone, Debug, EnumAsInner)]
#[enum_dispatch(CommonGetters, PositionGetters, WindowGetters)]
#[derive(Clone, Debug, EnumAsInner, Delegate)]
#[delegate(CommonGetters)]
#[delegate(PositionGetters)]
#[delegate(WindowGetters)]
pub enum WindowContainer {
TilingWindow(TilingWindow),
NonTilingWindow(NonTilingWindow),
}

impl From<TilingWindow> for WindowContainer {
fn from(value: TilingWindow) -> Self {
WindowContainer::TilingWindow(value)
}
}

impl From<NonTilingWindow> for WindowContainer {
fn from(value: NonTilingWindow) -> Self {
WindowContainer::NonTilingWindow(value)
}
}

impl TryFrom<Container> for WindowContainer {
type Error = &'static str;

Expand Down Expand Up @@ -136,13 +215,27 @@ impl Eq for WindowContainer {}
/// * `CommonGetters`
/// * `PositionGetters`
/// * `TilingDirectionGetters`
#[derive(Clone, Debug, EnumAsInner)]
#[enum_dispatch(CommonGetters, PositionGetters, TilingDirectionGetters)]
#[derive(Clone, Debug, EnumAsInner, Delegate)]
#[delegate(CommonGetters)]
#[delegate(PositionGetters)]
#[delegate(TilingDirectionGetters)]
pub enum DirectionContainer {
Workspace(Workspace),
Split(SplitContainer),
}

impl From<Workspace> for DirectionContainer {
fn from(value: Workspace) -> Self {
DirectionContainer::Workspace(value)
}
}

impl From<SplitContainer> for DirectionContainer {
fn from(value: SplitContainer) -> Self {
DirectionContainer::Split(value)
}
}

impl TryFrom<Container> for DirectionContainer {
type Error = &'static str;

Expand Down
9 changes: 4 additions & 5 deletions packages/wm/src/containers/traits/common_getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,19 @@ use std::{
collections::VecDeque,
};

use enum_dispatch::enum_dispatch;
use ambassador::delegatable_trait;
use uuid::Uuid;

use crate::{
containers::{
Container, ContainerDto, DirectionContainer, RootContainer,
SplitContainer, TilingContainer, WindowContainer,
Container, ContainerDto, DirectionContainer, TilingContainer,
WindowContainer,
},
monitors::Monitor,
windows::{NonTilingWindow, TilingWindow},
workspaces::Workspace,
};

#[enum_dispatch]
#[delegatable_trait]
pub trait CommonGetters {
/// A unique identifier for the container.
fn id(&self) -> Uuid;
Expand Down
11 changes: 3 additions & 8 deletions packages/wm/src/containers/traits/position_getters.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,8 @@
use enum_dispatch::enum_dispatch;
use ambassador::delegatable_trait;

use crate::{
common::Rect,
containers::{
Container, DirectionContainer, TilingContainer, WindowContainer,
},
};
use crate::common::Rect;

#[enum_dispatch]
#[delegatable_trait]
pub trait PositionGetters {
fn to_rect(&self) -> anyhow::Result<Rect>;
}
Expand Down
6 changes: 3 additions & 3 deletions packages/wm/src/containers/traits/tiling_direction_getters.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use enum_dispatch::enum_dispatch;
use ambassador::delegatable_trait;

use super::CommonGetters;
use crate::{
common::{Direction, TilingDirection},
containers::{DirectionContainer, TilingContainer},
containers::TilingContainer,
windows::TilingWindow,
};

#[enum_dispatch]
#[delegatable_trait]
pub trait TilingDirectionGetters: CommonGetters {
fn tiling_direction(&self) -> TilingDirection;

Expand Down
4 changes: 2 additions & 2 deletions packages/wm/src/containers/traits/tiling_size_getters.rs
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
use ambassador::delegatable_trait;
use anyhow::Context;
use enum_dispatch::enum_dispatch;

use super::{CommonGetters, TilingDirectionGetters};
use crate::{
common::{LengthValue, TilingDirection},
containers::{Container, DirectionContainer, TilingContainer},
};

#[enum_dispatch]
#[delegatable_trait]
pub trait TilingSizeGetters: CommonGetters {
fn tiling_size(&self) -> f32;

Expand Down
5 changes: 2 additions & 3 deletions packages/wm/src/windows/traits/window_getters.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use std::cell::Ref;

use enum_dispatch::enum_dispatch;
use ambassador::delegatable_trait;

use crate::{
common::{
platform::NativeWindow, DisplayState, LengthValue, Rect, RectDelta,
},
containers::WindowContainer,
user_config::{UserConfig, WindowRuleConfig},
windows::{active_drag::ActiveDrag, WindowState},
};

#[enum_dispatch]
#[delegatable_trait]
pub trait WindowGetters {
fn state(&self) -> WindowState;

Expand Down

0 comments on commit 574d7e8

Please sign in to comment.