mirror of
https://github.com/LGUG2Z/komorebi.git
synced 2026-11-16 22:01:22 +01:00
refactor(mac): take ring and lockable_sequence from the komorebi-collections crate
Deletes the macOS copies, adds the komorebi-collections path dependency via the workspace table, re-exports ring and lockable_sequence from lib.rs, #[macro_use] extern crate for impl_ring_elements!, and forwards the schemars feature. Cargo.lock updated. Verified: fast-check macOS fake-CC cargo check --workspace --all-targets: PASS. Not run: tests.
This commit is contained in:
1 parent
4a5733eb1d
commit
334e56b687
6 files changed
+17
-605
No files matched your search
Generated
+10
@@ -3165,6 +3165,7 @@ dependencies = [
|
||||
"dispatch2",
|
||||
"ed25519-dalek",
|
||||
"hotwatch",
|
||||
"komorebi-collections",
|
||||
"komorebi-layouts",
|
||||
"komorebi-themes",
|
||||
"lazy_static",
|
||||
@@ -3244,6 +3245,15 @@ dependencies = [
|
||||
"serde_json_lenient",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "komorebi-collections"
|
||||
version = "0.1.42"
|
||||
dependencies = [
|
||||
"pastey 0.2.3",
|
||||
"schemars 1.2.3",
|
||||
"serde",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "komorebi-layouts"
|
||||
version = "0.1.42"
|
||||
|
||||
@@ -14,6 +14,7 @@ version = "0.1.0"
|
||||
|
||||
[workspace.dependencies]
|
||||
komorebi-themes = { path = "../komorebi-themes" }
|
||||
komorebi-collections = { path = "../crates/komorebi-collections" }
|
||||
komorebi-layouts = { path = "../komorebi-layouts", features = [
|
||||
"darwin",
|
||||
"schemars",
|
||||
|
||||
@@ -6,6 +6,7 @@ edition = "2024"
|
||||
|
||||
[dependencies]
|
||||
komorebi-themes = { workspace = true }
|
||||
komorebi-collections = { workspace = true }
|
||||
komorebi-layouts = { workspace = true }
|
||||
quickxml_to_serde = { git = "https://github.com/LGUG2Z/quickxml_to_serde.git", version = "0.6.1" }
|
||||
version = { path = "../version" }
|
||||
@@ -127,4 +128,4 @@ uuid = { version = "1", features = ["v4"] }
|
||||
[features]
|
||||
default = ["schemars"]
|
||||
deadlock_detection = ["parking_lot/deadlock_detection"]
|
||||
schemars = ["dep:schemars"]
|
||||
schemars = ["dep:schemars", "komorebi-collections/schemars"]
|
||||
@@ -46,7 +46,9 @@ use std::sync::Arc;
|
||||
use std::sync::atomic::AtomicI32;
|
||||
|
||||
#[macro_use]
|
||||
pub mod ring;
|
||||
extern crate komorebi_collections;
|
||||
|
||||
pub use komorebi_collections::ring;
|
||||
|
||||
pub mod accessibility;
|
||||
pub mod animation;
|
||||
@@ -60,7 +62,7 @@ pub mod core_graphics;
|
||||
pub mod display_reconfiguration_listener;
|
||||
pub mod input_event_listener;
|
||||
pub mod ioreg;
|
||||
pub mod lockable_sequence;
|
||||
pub use komorebi_collections::lockable_sequence;
|
||||
pub mod macos_api;
|
||||
pub mod monitor;
|
||||
pub mod monitor_reconciliator;
|
||||
|
||||
@@ -1,368 +0,0 @@
|
||||
//! Lists in which some elements are locked in place.
|
||||
//!
|
||||
//! A locked container keeps its position in the layout when others are inserted, removed or
|
||||
//! swapped around it. [`LockableSequence`] does that bookkeeping for a [`VecDeque`].
|
||||
|
||||
use std::collections::VecDeque;
|
||||
|
||||
/// A trait for types that can be marked as locked or unlocked.
|
||||
pub trait Lockable {
|
||||
/// Returns `true` if the item is locked.
|
||||
fn locked(&self) -> bool;
|
||||
/// Sets the locked state of the item.
|
||||
fn set_locked(&mut self, locked: bool) -> &mut Self;
|
||||
}
|
||||
|
||||
/// A sequence supporting insertion, removal, and swapping of elements while preserving the absolute
|
||||
/// positions of locked items.
|
||||
pub trait LockableSequence<T: Lockable> {
|
||||
/// Inserts a value at `idx`, keeping locked elements at their absolute positions.
|
||||
fn insert_respecting_locks(&mut self, idx: usize, value: T) -> usize;
|
||||
/// Removes the element at `idx`, keeping locked elements at their absolute positions.
|
||||
fn remove_respecting_locks(&mut self, idx: usize) -> Option<T>;
|
||||
/// Swaps the elements at indices `i` and `j`, keeping locked elements at their absolute positions.
|
||||
fn swap_respecting_locks(&mut self, i: usize, j: usize);
|
||||
}
|
||||
|
||||
impl<T: Lockable> LockableSequence<T> for VecDeque<T> {
|
||||
/// Insert `value` at logical index `idx`, with trying to keep locked elements
|
||||
/// (`is_locked()`) anchored at their original positions.
|
||||
///
|
||||
/// Returns the final index of the inserted element.
|
||||
fn insert_respecting_locks(&mut self, mut idx: usize, value: T) -> usize {
|
||||
// 1. Bounds check: if index is out of range, simply append.
|
||||
if idx >= self.len() {
|
||||
self.push_back(value);
|
||||
return self.len() - 1; // last index
|
||||
}
|
||||
|
||||
// 2. Normal VecDeque insertion
|
||||
self.insert(idx, value);
|
||||
|
||||
// 3. Walk left-to-right once, swapping any misplaced locked element. After
|
||||
// the VecDeque::insert all items after `idx` have moved right by one. For every locked
|
||||
// element that is now to the right of an unlocked one, swap it back left exactly once.
|
||||
for index in (idx + 1)..self.len() {
|
||||
if self[index].locked() && !self[index - 1].locked() {
|
||||
self.swap(index - 1, index);
|
||||
|
||||
// If the element we just inserted participated in the swap,
|
||||
// update `idx` so we can return its final location.
|
||||
if idx == index - 1 {
|
||||
idx = index;
|
||||
}
|
||||
}
|
||||
}
|
||||
idx
|
||||
}
|
||||
|
||||
/// Remove element at `idx`, with trying to keep locked elements
|
||||
/// (`is_locked()`) anchored at their original positions.
|
||||
///
|
||||
/// Returns the removed element, or `None` if `idx` is out of bounds.
|
||||
fn remove_respecting_locks(&mut self, idx: usize) -> Option<T> {
|
||||
// 1. Bounds check: if index is out of range, do nothing.
|
||||
if idx >= self.len() {
|
||||
return None;
|
||||
}
|
||||
|
||||
// 2. Remove the element at the requested index.
|
||||
// All elements after idx are now shifted left by 1.
|
||||
let removed = self.remove(idx)?;
|
||||
|
||||
// 3. If less than 2 elements remain, nothing to shift.
|
||||
if self.len() < 2 {
|
||||
return Some(removed);
|
||||
}
|
||||
|
||||
// 4. Iterate from the element just after the removed spot up to the second-to-last
|
||||
// element, right-to-left. This loop "fixes" locked elements that were shifted left
|
||||
// off their anchored positions: If a locked element now has an unlocked element
|
||||
// to its right, swap them back to restore locked order.
|
||||
for index in (idx..self.len() - 1).rev() {
|
||||
// If current is locked and the next one is not locked, swap them.
|
||||
if self[index].locked() && !self[index + 1].locked() {
|
||||
self.swap(index, index + 1);
|
||||
}
|
||||
}
|
||||
|
||||
// 5. Return the removed value.
|
||||
Some(removed)
|
||||
}
|
||||
|
||||
/// Swaps the elements at indices `i` and `j`, along with their `locked` status, ensuring
|
||||
/// the lock state remains associated with the position rather than the element itself.
|
||||
fn swap_respecting_locks(&mut self, i: usize, j: usize) {
|
||||
self.swap(i, j);
|
||||
let locked_i = self[i].locked();
|
||||
let locked_j = self[j].locked();
|
||||
self[i].set_locked(locked_j);
|
||||
self[j].set_locked(locked_i);
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct TestItem {
|
||||
val: i32,
|
||||
locked: bool,
|
||||
}
|
||||
|
||||
impl Lockable for TestItem {
|
||||
fn locked(&self) -> bool {
|
||||
self.locked
|
||||
}
|
||||
|
||||
fn set_locked(&mut self, locked: bool) -> &mut Self {
|
||||
self.locked = locked;
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
fn vals(v: &VecDeque<TestItem>) -> Vec<i32> {
|
||||
v.iter().map(|x| x.val).collect()
|
||||
}
|
||||
|
||||
fn test_deque(items: &[(i32, bool)]) -> VecDeque<TestItem> {
|
||||
items
|
||||
.iter()
|
||||
.cloned()
|
||||
.map(|(val, locked)| TestItem { val, locked })
|
||||
.collect()
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert_respecting_locks() {
|
||||
// Test case 1: Basic insertion with locked index
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, false), (4, false)]);
|
||||
// Insert at index 0, should shift elements while keeping index 2 locked
|
||||
ring.insert_respecting_locks(
|
||||
0,
|
||||
TestItem {
|
||||
val: 99,
|
||||
locked: false,
|
||||
},
|
||||
);
|
||||
// Element '2' remains at index 2, element '1' that was at index 1 is now at index 3
|
||||
assert_eq!(vals(&ring), vec![99, 0, 2, 1, 3, 4]);
|
||||
}
|
||||
|
||||
// Test case 2: Insert at a locked index (should insert after locked)
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, false), (4, false)]);
|
||||
// Try to insert at locked index 2, should insert at index 3 instead
|
||||
let actual_index = ring.insert_respecting_locks(
|
||||
2,
|
||||
TestItem {
|
||||
val: 99,
|
||||
locked: false,
|
||||
},
|
||||
);
|
||||
assert_eq!(actual_index, 3);
|
||||
assert_eq!(vals(&ring), vec![0, 1, 2, 99, 3, 4]);
|
||||
}
|
||||
|
||||
// Test case 3: Multiple locked indices
|
||||
{
|
||||
// Lock index 1 and 3
|
||||
let mut ring = test_deque(&[(0, false), (1, true), (2, false), (3, true), (4, false)]);
|
||||
// Insert at index 0, should maintain locked indices
|
||||
ring.insert_respecting_locks(
|
||||
0,
|
||||
TestItem {
|
||||
val: 99,
|
||||
locked: false,
|
||||
},
|
||||
);
|
||||
// Elements '1' and '3' remain at indices 1 and 3
|
||||
assert_eq!(vals(&ring), vec![99, 1, 0, 3, 2, 4]);
|
||||
}
|
||||
|
||||
// Test case 4: Insert at end
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, false), (4, false)]);
|
||||
let actual_index = ring.insert_respecting_locks(
|
||||
5,
|
||||
TestItem {
|
||||
val: 99,
|
||||
locked: false,
|
||||
},
|
||||
);
|
||||
assert_eq!(actual_index, 5);
|
||||
assert_eq!(vals(&ring), vec![0, 1, 2, 3, 4, 99]);
|
||||
}
|
||||
|
||||
// Test case 5: Empty ring
|
||||
{
|
||||
let mut ring = test_deque(&[]);
|
||||
// Insert into empty deque
|
||||
let actual_index = ring.insert_respecting_locks(
|
||||
0,
|
||||
TestItem {
|
||||
val: 99,
|
||||
locked: false,
|
||||
},
|
||||
);
|
||||
assert_eq!(actual_index, 0);
|
||||
assert_eq!(vals(&ring), vec![99]);
|
||||
}
|
||||
|
||||
// Test case 6: All indices locked
|
||||
{
|
||||
// Lock all indices
|
||||
let mut ring = test_deque(&[(0, true), (1, true), (2, true), (3, true), (4, true)]);
|
||||
// Try to insert at index 2, should insert at the end
|
||||
let actual_index = ring.insert_respecting_locks(
|
||||
2,
|
||||
TestItem {
|
||||
val: 99,
|
||||
locked: false,
|
||||
},
|
||||
);
|
||||
assert_eq!(actual_index, 5);
|
||||
assert_eq!(vals(&ring), vec![0, 1, 2, 3, 4, 99]);
|
||||
}
|
||||
|
||||
// Test case 7: Consecutive locked indices
|
||||
{
|
||||
// Lock index 2 and 3
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, true), (4, false)]);
|
||||
// Insert at index 1, should maintain consecutive locked indices
|
||||
ring.insert_respecting_locks(
|
||||
1,
|
||||
TestItem {
|
||||
val: 99,
|
||||
locked: false,
|
||||
},
|
||||
);
|
||||
// Elements '2' and '3' remain at indices 2 and 3
|
||||
assert_eq!(vals(&ring), vec![0, 99, 2, 3, 1, 4]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_remove_respecting_locks() {
|
||||
// Test case 1: Remove a non-locked index before a locked index
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, false), (4, false)]);
|
||||
let removed = ring.remove_respecting_locks(0);
|
||||
assert_eq!(removed.map(|x| x.val), Some(0));
|
||||
// Elements '2' remain at index 2
|
||||
assert_eq!(vals(&ring), vec![1, 3, 2, 4]);
|
||||
}
|
||||
|
||||
// Test case 2: Remove a locked index
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, false), (4, false)]);
|
||||
let removed = ring.remove_respecting_locks(2);
|
||||
assert_eq!(removed.map(|x| x.val), Some(2));
|
||||
// Elements should stay at the same places
|
||||
assert_eq!(vals(&ring), vec![0, 1, 3, 4]);
|
||||
}
|
||||
|
||||
// Test case 3: Remove an index after a locked index
|
||||
{
|
||||
// Lock index 1
|
||||
let mut ring = test_deque(&[(0, false), (1, true), (2, false), (3, false), (4, false)]);
|
||||
let removed = ring.remove_respecting_locks(3);
|
||||
assert_eq!(removed.map(|x| x.val), Some(3));
|
||||
// Elements should stay at the same places
|
||||
assert_eq!(vals(&ring), vec![0, 1, 2, 4]);
|
||||
}
|
||||
|
||||
// Test case 4: Multiple locked indices
|
||||
{
|
||||
// Lock index 1 and 3
|
||||
let mut ring = test_deque(&[(0, false), (1, true), (2, false), (3, true), (4, false)]);
|
||||
let removed = ring.remove_respecting_locks(0);
|
||||
assert_eq!(removed.map(|x| x.val), Some(0));
|
||||
// Elements '1' and '3' remain at indices '1' and '3'
|
||||
assert_eq!(vals(&ring), vec![2, 1, 4, 3]);
|
||||
}
|
||||
|
||||
// Test case 5: Remove the last element
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, false), (4, false)]);
|
||||
let removed = ring.remove_respecting_locks(4);
|
||||
assert_eq!(removed.map(|x| x.val), Some(4));
|
||||
// Index 2 should still be at the same place
|
||||
assert_eq!(vals(&ring), vec![0, 1, 2, 3]);
|
||||
}
|
||||
|
||||
// Test case 6: Invalid index
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true), (3, false), (4, false)]);
|
||||
let removed = ring.remove_respecting_locks(10);
|
||||
assert_eq!(removed, None);
|
||||
// Deque unchanged
|
||||
assert_eq!(vals(&ring), vec![0, 1, 2, 3, 4]);
|
||||
}
|
||||
|
||||
// Test case 7: Remove enough elements to make a locked index invalid
|
||||
{
|
||||
// Lock index 2
|
||||
let mut ring = test_deque(&[(0, false), (1, false), (2, true)]);
|
||||
ring.remove_respecting_locks(0);
|
||||
// Index 2 should now be '1'
|
||||
assert_eq!(vals(&ring), vec![1, 2]);
|
||||
}
|
||||
|
||||
// Test case 8: Removing an element before multiple locked indices
|
||||
{
|
||||
// Lock index 2 and 4
|
||||
let mut ring = test_deque(&[
|
||||
(0, false),
|
||||
(1, false),
|
||||
(2, true),
|
||||
(3, false),
|
||||
(4, true),
|
||||
(5, false),
|
||||
]);
|
||||
let removed = ring.remove_respecting_locks(1);
|
||||
assert_eq!(removed.map(|x| x.val), Some(1));
|
||||
// Both indices should still be at the same place
|
||||
assert_eq!(vals(&ring), vec![0, 3, 2, 5, 4]);
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_swap_respecting_locks_various_cases() {
|
||||
// Swap unlocked and locked
|
||||
let mut ring = test_deque(&[(0, false), (1, true), (2, false), (3, false)]);
|
||||
ring.swap_respecting_locks(0, 1);
|
||||
assert_eq!(vals(&ring), vec![1, 0, 2, 3]);
|
||||
assert!(!ring[0].locked);
|
||||
assert!(ring[1].locked);
|
||||
ring.swap_respecting_locks(0, 1);
|
||||
assert_eq!(vals(&ring), vec![0, 1, 2, 3]);
|
||||
assert!(!ring[0].locked);
|
||||
assert!(ring[1].locked);
|
||||
|
||||
// Both locked
|
||||
let mut ring = test_deque(&[(0, true), (1, false), (2, true)]);
|
||||
ring.swap_respecting_locks(0, 2);
|
||||
assert_eq!(vals(&ring), vec![2, 1, 0]);
|
||||
assert!(ring[0].locked);
|
||||
assert!(!ring[1].locked);
|
||||
assert!(ring[2].locked);
|
||||
|
||||
// Both unlocked
|
||||
let mut ring = test_deque(&[(0, false), (1, true), (2, false)]);
|
||||
ring.swap_respecting_locks(0, 2);
|
||||
assert_eq!(vals(&ring), vec![2, 1, 0]);
|
||||
assert!(!ring[0].locked);
|
||||
assert!(ring[1].locked);
|
||||
assert!(!ring[2].locked);
|
||||
}
|
||||
}
|
||||
@@ -1,234 +0,0 @@
|
||||
//! [`Ring`], a list of elements with one of them focused.
|
||||
//!
|
||||
//! Monitors, workspaces and containers each keep their children in a ring, so that "the focused
|
||||
//! one" is the same thing everywhere, and `impl_ring_elements!` gives each of them the same
|
||||
//! accessors for it.
|
||||
|
||||
use std::collections::VecDeque;
|
||||
|
||||
use serde::Deserialize;
|
||||
use serde::Serialize;
|
||||
|
||||
/// A list of elements, one of which is focused.
|
||||
///
|
||||
/// The focused index is not guaranteed to be in range: [`Ring::focus`] stores what it is given, and
|
||||
/// [`Ring::focused`] and [`Ring::focused_mut`] return `None` when it is out of range, as it is for
|
||||
/// an empty ring.
|
||||
///
|
||||
/// # Example
|
||||
///
|
||||
/// ```rust
|
||||
/// use komorebi::ring::Ring;
|
||||
///
|
||||
/// let mut ring = Ring::default();
|
||||
/// ring.elements_mut().push_back("a");
|
||||
/// ring.elements_mut().push_back("b");
|
||||
/// ring.focus(1);
|
||||
///
|
||||
/// assert_eq!(ring.focused(), Some(&"b"));
|
||||
/// assert_eq!(ring.focused_idx(), 1);
|
||||
/// ```
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
|
||||
#[cfg_attr(feature = "schemars", derive(schemars::JsonSchema))]
|
||||
pub struct Ring<T> {
|
||||
elements: VecDeque<T>,
|
||||
focused: usize,
|
||||
}
|
||||
|
||||
impl<T> Default for Ring<T> {
|
||||
fn default() -> Self {
|
||||
Self {
|
||||
elements: VecDeque::default(),
|
||||
focused: 0,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Ring<T> {
|
||||
/// The elements, in order.
|
||||
pub const fn elements(&self) -> &VecDeque<T> {
|
||||
&self.elements
|
||||
}
|
||||
|
||||
/// The elements, in order, to add to or remove from.
|
||||
pub fn elements_mut(&mut self) -> &mut VecDeque<T> {
|
||||
&mut self.elements
|
||||
}
|
||||
|
||||
/// Focuses the element at `idx`.
|
||||
pub fn focus(&mut self, idx: usize) {
|
||||
self.focused = idx;
|
||||
}
|
||||
|
||||
/// The focused element, if the focused index is in range.
|
||||
pub fn focused(&self) -> Option<&T> {
|
||||
self.elements.get(self.focused)
|
||||
}
|
||||
|
||||
/// The index that is focused, in range or not.
|
||||
pub const fn focused_idx(&self) -> usize {
|
||||
self.focused
|
||||
}
|
||||
|
||||
/// The focused element, to change, if the focused index is in range.
|
||||
pub fn focused_mut(&mut self) -> Option<&mut T> {
|
||||
self.elements.get_mut(self.focused)
|
||||
}
|
||||
}
|
||||
|
||||
/// Gives a type that keeps its children in a [`Ring`] the accessors every such type has.
|
||||
///
|
||||
/// `impl_ring_elements!(Workspace, Container)` reads the field `containers: Ring<Container>` and
|
||||
/// gives `Workspace` the methods `containers()`, `containers_mut()`, `focused_container()`,
|
||||
/// `focused_container_idx()` and `focused_container_mut()`. The field is named after the element
|
||||
/// type in lower case with an `s` appended, and the methods after the field, so that "the focused
|
||||
/// container" is spelled the same way on every type that has one.
|
||||
macro_rules! impl_ring_elements {
|
||||
($name:ty, $element:ident) => {
|
||||
pastey::paste! {
|
||||
impl $name {
|
||||
pub const fn [<$element:lower s>](&self) -> &std::collections::VecDeque<$element> {
|
||||
self.[<$element:lower s>].elements()
|
||||
}
|
||||
|
||||
pub fn [<$element:lower s_mut>](&mut self) -> &mut std::collections::VecDeque<$element> {
|
||||
self.[<$element:lower s>].elements_mut()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn [<focused_ $element:lower>](&self) -> Option<&$element> {
|
||||
self.[<$element:lower s>].focused()
|
||||
}
|
||||
|
||||
pub const fn [<focused_ $element:lower _idx>](&self) -> usize {
|
||||
self.[<$element:lower s>].focused_idx()
|
||||
}
|
||||
|
||||
pub fn [<focused_ $element:lower _mut>](&mut self) -> Option<&mut $element> {
|
||||
self.[<$element:lower s>].focused_mut()
|
||||
}
|
||||
}
|
||||
}
|
||||
}; // This allows passing a different name to be used for the functions. For instance, the
|
||||
// `floating_windows` ring calls this as:
|
||||
// ```rust
|
||||
// impl_ring_elements!(Workspace, Window, "floating_window");
|
||||
// ```
|
||||
// Which allows using the `Window` element but name the functions as `floating_window`
|
||||
($name:ty, $element:ident, $el_name:literal) => {
|
||||
pastey::paste! {
|
||||
impl $name {
|
||||
pub const fn [<$el_name:lower s>](&self) -> &std::collections::VecDeque<$element> {
|
||||
self.[<$el_name:lower s>].elements()
|
||||
}
|
||||
|
||||
pub fn [<$el_name:lower s_mut>](&mut self) -> &mut std::collections::VecDeque<$element> {
|
||||
self.[<$el_name:lower s>].elements_mut()
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn [<focused_ $el_name:lower>](&self) -> Option<&$element> {
|
||||
self.[<$el_name:lower s>].focused()
|
||||
}
|
||||
|
||||
pub const fn [<focused_ $el_name:lower _idx>](&self) -> usize {
|
||||
self.[<$el_name:lower s>].focused_idx()
|
||||
}
|
||||
|
||||
pub fn [<focused_ $el_name:lower _mut>](&mut self) -> Option<&mut $element> {
|
||||
self.[<$el_name:lower s>].focused_mut()
|
||||
}
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[derive(Debug, PartialEq)]
|
||||
struct Book(&'static str);
|
||||
|
||||
struct Shelf {
|
||||
books: Ring<Book>,
|
||||
}
|
||||
|
||||
impl_ring_elements!(Shelf, Book);
|
||||
|
||||
#[test]
|
||||
fn impl_ring_elements_names_the_accessors_after_the_element() {
|
||||
let mut shelf = Shelf {
|
||||
books: Ring::default(),
|
||||
};
|
||||
shelf.books_mut().extend([Book("a"), Book("b")]);
|
||||
shelf.books.focus(1);
|
||||
|
||||
assert_eq!(shelf.books().len(), 2);
|
||||
assert_eq!(shelf.focused_book(), Some(&Book("b")));
|
||||
assert_eq!(shelf.focused_book_idx(), 1);
|
||||
|
||||
if let Some(book) = shelf.focused_book_mut() {
|
||||
*book = Book("c");
|
||||
}
|
||||
|
||||
assert_eq!(shelf.books()[1], Book("c"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn an_empty_ring_focuses_nothing() {
|
||||
let mut ring: Ring<u8> = Ring::default();
|
||||
|
||||
assert_eq!(ring.focused(), None);
|
||||
assert_eq!(ring.focused_mut(), None);
|
||||
assert_eq!(ring.focused_idx(), 0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn focus_selects_the_element_at_that_index() {
|
||||
let mut ring = Ring::default();
|
||||
ring.elements_mut().extend([10, 20, 30]);
|
||||
|
||||
ring.focus(2);
|
||||
|
||||
assert_eq!(ring.focused(), Some(&30));
|
||||
assert_eq!(ring.focused_idx(), 2);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn the_focused_element_can_be_changed_in_place() {
|
||||
let mut ring = Ring::default();
|
||||
ring.elements_mut().extend([1, 2]);
|
||||
ring.focus(1);
|
||||
|
||||
if let Some(element) = ring.focused_mut() {
|
||||
*element = 5;
|
||||
}
|
||||
|
||||
assert_eq!(ring.elements(), &[1, 5]);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_focused_index_past_the_end_focuses_nothing() {
|
||||
let mut ring = Ring::default();
|
||||
ring.elements_mut().push_back('a');
|
||||
|
||||
ring.focus(3);
|
||||
|
||||
assert_eq!(ring.focused(), None);
|
||||
assert_eq!(ring.focused_idx(), 3);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_ring_round_trips_through_json() {
|
||||
let mut ring = Ring::default();
|
||||
ring.elements_mut()
|
||||
.extend(["a".to_string(), "b".to_string()]);
|
||||
ring.focus(1);
|
||||
|
||||
let json = serde_json::to_string(&ring).expect("serializes");
|
||||
let back: Ring<String> = serde_json::from_str(&json).expect("deserializes");
|
||||
|
||||
assert_eq!(back, ring);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user