Skip to content

Commit

Permalink
feat(events): add client event implementations
Browse files Browse the repository at this point in the history
  • Loading branch information
vyfor committed Nov 22, 2024
1 parent 891ead5 commit cc85acf
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/messages/events/client/clear_activity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@ pub struct ClearActivityEvent;
use crate::messages::events::event::{EventContext, OnEvent};

impl OnEvent for ClearActivityEvent {
fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> {
fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> {
ctx.cord.rich_client.clear()?;

Ok(())
}
}
9 changes: 7 additions & 2 deletions src/messages/events/client/disconnect.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
#[derive(Debug, Default)]
pub struct DisconnectEvent;

use crate::messages::events::event::{EventContext, OnEvent};
use crate::{
ipc::pipe::PipeServerImpl,
messages::events::event::{EventContext, OnEvent},
};

impl OnEvent for DisconnectEvent {
fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> {
fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> {
ctx.cord.pipe.disconnect(ctx.client_id)?;

Ok(())
}
}
6 changes: 5 additions & 1 deletion src/messages/events/client/reset_timestamp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@ use crate::messages::events::event::{EventContext, OnEvent};
pub struct ResetTimestampEvent;

impl OnEvent for ResetTimestampEvent {
fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> {
fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> {
if let Some(config) = &mut ctx.cord.config {
config.timestamp = None;
}

Ok(())
}
}
15 changes: 14 additions & 1 deletion src/messages/events/client/update_activity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use std::sync::atomic::Ordering;

use crate::messages::events::event::{EventContext, OnEvent};
use crate::presence::activity::ActivityContext;
use crate::presence::types::Packet;

#[derive(Debug)]
pub struct UpdateActivityEvent {
Expand All @@ -13,7 +16,17 @@ impl UpdateActivityEvent {
}

impl OnEvent for UpdateActivityEvent {
fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> {
fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> {
if let Some(config) = &mut ctx.cord.config {
if !ctx.cord.rich_client.is_ready.load(Ordering::SeqCst) {
return Ok(());
}
ctx.cord.rich_client.update(&Packet::new(
ctx.cord.rich_client.pid,
Some(self.context.build(config)),
))?;
}

Ok(())
}
}
22 changes: 20 additions & 2 deletions src/messages/events/client/update_workspace.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
use crate::messages::events::event::{EventContext, OnEvent};
use std::borrow::Borrow;

use crate::{
messages::events::event::{EventContext, OnEvent},
types,
util::utils,
};

#[derive(Debug)]
pub struct UpdateWorkspaceEvent {
Expand All @@ -12,7 +18,19 @@ impl UpdateWorkspaceEvent {
}

impl OnEvent for UpdateWorkspaceEvent {
fn on_event(self, _ctx: &mut EventContext) -> crate::Result<()> {
fn on_event(self, ctx: &mut EventContext) -> crate::Result<()> {
if let Some(config) = &mut ctx.cord.config {
let workspace = utils::find_workspace(&self.workspace);
if let Some(filename) = workspace.file_name() {
let filename = filename.to_string_lossy();
config.workspace = filename.to_string();

types::validate_buttons(&mut config.buttons, filename.borrow());

// todo: check for workspace blacklist
}
}

Ok(())
}
}

0 comments on commit cc85acf

Please sign in to comment.