Skip to content

Commit

Permalink
feat: removed enum_dispatch and using ambassador instead
Browse files Browse the repository at this point in the history
  • Loading branch information
LucaCoduriV committed Aug 1, 2024
1 parent 8ed9649 commit 50a6560
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 39 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.1"
enum-as-inner = "0.6"
futures-util = "0.3"
home = "0.5"
Expand Down
111 changes: 100 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,21 @@ pub enum Container {
NonTilingWindow(NonTilingWindow),
}

impl From<SplitContainer> for Container{
fn from(value: SplitContainer) -> Self {
Container::Split(value)
}
}
impl From<Workspace> for Container {
fn from(value: Workspace) -> Self {
Container::Workspace(value)
}
}
impl From<RootContainer> for Container{
fn from(value: RootContainer) -> Self {
Container::Root(value)
}
}
impl From<TilingContainer> for Container {
fn from(tiling_container: TilingContainer) -> Self {
match tiling_container {
Expand Down Expand Up @@ -59,13 +89,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 +134,44 @@ 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 From<NonTilingWindow> for Container {
fn from(value: NonTilingWindow) -> Self {
Container::NonTilingWindow(value)
}
}

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

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

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

Expand Down Expand Up @@ -136,13 +211,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<SplitContainer> for DirectionContainer{
fn from(value: SplitContainer) -> Self {
DirectionContainer::Split(value)
}
}

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

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

Expand Down
5 changes: 2 additions & 3 deletions packages/wm/src/containers/traits/common_getters.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,7 @@ use std::{
cell::{Ref, RefMut},
collections::VecDeque,
};

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

use crate::{
Expand All @@ -16,7 +15,7 @@ use crate::{
workspaces::Workspace,
};

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

use crate::{
common::Rect,
Expand All @@ -7,7 +7,7 @@ use crate::{
},
};

#[enum_dispatch]
#[delegatable_trait]
pub trait PositionGetters {
fn to_rect(&self) -> anyhow::Result<Rect>;
}
Expand Down
5 changes: 2 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,12 @@
use enum_dispatch::enum_dispatch;

use ambassador::delegatable_trait;
use super::CommonGetters;
use crate::{
common::{Direction, TilingDirection},
containers::{DirectionContainer, 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
6 changes: 2 additions & 4 deletions packages/wm/src/windows/traits/window_getters.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use std::cell::Ref;

use enum_dispatch::enum_dispatch;

use ambassador::delegatable_trait;
use crate::{
common::{
platform::NativeWindow, DisplayState, LengthValue, Rect, RectDelta,
Expand All @@ -11,7 +9,7 @@ use crate::{
windows::{active_drag::ActiveDrag, WindowState},
};

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

Expand Down

0 comments on commit 50a6560

Please sign in to comment.