-
-
Notifications
You must be signed in to change notification settings - Fork 131
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
347 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
//! Helpers for keeping track of the event stream state. | ||
use std::collections::HashMap; | ||
|
||
use crate::{Event, KeyboardLayouts, Workspace}; | ||
|
||
/// Part of the state communicated via the event stream. | ||
pub trait EventStreamStatePart { | ||
/// Returns a sequence of events that reproduce this state from default initialization. | ||
fn reproduce(&self) -> Vec<Event>; | ||
|
||
/// Applies the event to this state. | ||
/// | ||
/// Returns `None` after applying the event, and `Some(event)` if the event is ignored by this | ||
/// part of the state. | ||
fn apply(&mut self, event: Event) -> Option<Event>; | ||
} | ||
|
||
/// The full state communicated over the event stream. | ||
#[derive(Debug, Default)] | ||
pub struct EventStreamState { | ||
/// State of workspaces. | ||
pub workspaces: WorkspacesState, | ||
|
||
/// State of the keyboard layouts. | ||
pub keyboard_layouts: KeyboardLayoutsState, | ||
} | ||
|
||
/// The workspaces state communicated over the event stream. | ||
#[derive(Debug, Default)] | ||
pub struct WorkspacesState { | ||
/// Map from a workspace id to the workspace. | ||
pub workspaces: HashMap<u64, Workspace>, | ||
} | ||
|
||
/// The keyboard layout state communicated over the event stream. | ||
#[derive(Debug, Default)] | ||
pub struct KeyboardLayoutsState { | ||
/// Configured keyboard layouts. | ||
pub keyboard_layouts: Option<KeyboardLayouts>, | ||
} | ||
|
||
impl EventStreamStatePart for EventStreamState { | ||
fn reproduce(&self) -> Vec<Event> { | ||
let mut events = Vec::new(); | ||
events.extend(self.workspaces.reproduce()); | ||
events.extend(self.keyboard_layouts.reproduce()); | ||
events | ||
} | ||
|
||
fn apply(&mut self, event: Event) -> Option<Event> { | ||
let event = self.workspaces.apply(event)?; | ||
let event = self.keyboard_layouts.apply(event)?; | ||
Some(event) | ||
} | ||
} | ||
|
||
impl EventStreamStatePart for WorkspacesState { | ||
fn reproduce(&self) -> Vec<Event> { | ||
let workspaces = self.workspaces.values().cloned().collect(); | ||
vec![Event::WorkspacesChanged { workspaces }] | ||
} | ||
|
||
fn apply(&mut self, event: Event) -> Option<Event> { | ||
match event { | ||
Event::WorkspacesChanged { workspaces } => { | ||
self.workspaces = workspaces.into_iter().map(|ws| (ws.id, ws)).collect(); | ||
} | ||
Event::WorkspaceSwitched { output, id } => { | ||
for ws in self.workspaces.values_mut() { | ||
if ws.output != output { | ||
continue; | ||
} | ||
|
||
ws.is_active = ws.id == id; | ||
} | ||
} | ||
event => return Some(event), | ||
} | ||
None | ||
} | ||
} | ||
|
||
impl EventStreamStatePart for KeyboardLayoutsState { | ||
fn reproduce(&self) -> Vec<Event> { | ||
if let Some(keyboard_layouts) = self.keyboard_layouts.clone() { | ||
vec![Event::KeyboardLayoutsChanged { keyboard_layouts }] | ||
} else { | ||
vec![] | ||
} | ||
} | ||
|
||
fn apply(&mut self, event: Event) -> Option<Event> { | ||
match event { | ||
Event::KeyboardLayoutsChanged { keyboard_layouts } => { | ||
self.keyboard_layouts = Some(keyboard_layouts); | ||
} | ||
Event::KeyboardLayoutSwitched { idx } => { | ||
let kb = self.keyboard_layouts.as_mut(); | ||
let kb = kb.expect("keyboard layouts must be set before a layout can be switched"); | ||
kb.current_idx = idx; | ||
} | ||
event => return Some(event), | ||
} | ||
None | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.