Skip to content

Commit

Permalink
Merge pull request #469 from kas-gui/work2
Browse files Browse the repository at this point in the history
Split trait Layout into Tile, Layout; impl Layout for Text
  • Loading branch information
dhardy authored Jan 21, 2025
2 parents d004933 + 238ecff commit 90a4347
Show file tree
Hide file tree
Showing 57 changed files with 932 additions and 936 deletions.
44 changes: 22 additions & 22 deletions crates/kas-core/src/core/collection.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
//! The [`Collection`] trait
use crate::layout::{GridCellInfo, GridDimensions};
use crate::{Layout, Node, Widget};
use crate::{Node, Tile, Widget};
use kas_macros::impl_scope;
use std::ops::RangeBounds;

Expand All @@ -31,11 +31,11 @@ pub trait Collection {
/// The number of widgets
fn len(&self) -> usize;

/// Get a widget as a [`Layout`]
fn get_layout(&self, index: usize) -> Option<&dyn Layout>;
/// Get a widget as a [`Tile`]
fn get_tile(&self, index: usize) -> Option<&dyn Tile>;

/// Get a widget as a mutable [`Layout`]
fn get_mut_layout(&mut self, index: usize) -> Option<&mut dyn Layout>;
/// Get a widget as a mutable [`Tile`]
fn get_mut_tile(&mut self, index: usize) -> Option<&mut dyn Tile>;

/// Operate on a widget as a [`Node`]
fn for_node(
Expand All @@ -45,11 +45,11 @@ pub trait Collection {
closure: Box<dyn FnOnce(Node<'_>) + '_>,
);

/// Iterate over elements as [`Layout`] items within `range`
/// Iterate over elements as [`Tile`] items within `range`
///
/// Note: there is currently no mutable equivalent due to the streaming
/// iterator problem.
fn iter_layout(&self, range: impl RangeBounds<usize>) -> CollectionIterLayout<'_, Self> {
fn iter_tile(&self, range: impl RangeBounds<usize>) -> CollectionIterTile<'_, Self> {
use std::ops::Bound::{Excluded, Included, Unbounded};
let start = match range.start_bound() {
Included(start) => *start,
Expand All @@ -61,7 +61,7 @@ pub trait Collection {
Excluded(end) => *end,
Unbounded => self.len(),
};
CollectionIterLayout {
CollectionIterTile {
start,
end,
collection: self,
Expand All @@ -80,12 +80,12 @@ pub trait Collection {
/// - `Some(Ok(index))` if an `Equal` element is found at `index`
/// - `Some(Err(index))` if no `Equal` element is found; in this case such
/// an element could be inserted at `index`
/// - `None` if [`Collection::get_layout`] returns `None` for some
/// - `None` if [`Collection::get_tile`] returns `None` for some
/// `index` less than [`Collection::len`]. This is an error case that
/// should not occur.
fn binary_search_by<'a, F>(&'a self, mut f: F) -> Option<Result<usize, usize>>
where
F: FnMut(&'a dyn Layout) -> std::cmp::Ordering,
F: FnMut(&'a dyn Tile) -> std::cmp::Ordering,
{
use std::cmp::Ordering::{Greater, Less};

Expand All @@ -99,7 +99,7 @@ pub trait Collection {
while left < right {
let mid = left + size / 2;

let cmp = f(self.get_layout(mid)?);
let cmp = f(self.get_tile(mid)?);

if cmp == Less {
left = mid + 1;
Expand Down Expand Up @@ -161,21 +161,21 @@ pub trait CellCollection: Collection {
}

impl_scope! {
/// An iterator over a [`Collection`] as [`Layout`] elements
pub struct CollectionIterLayout<'a, C: Collection + ?Sized> {
/// An iterator over a [`Collection`] as [`Tile`] elements
pub struct CollectionIterTile<'a, C: Collection + ?Sized> {
start: usize,
end: usize,
collection: &'a C,
}

impl Iterator for Self {
type Item = &'a dyn Layout;
type Item = &'a dyn Tile;

fn next(&mut self) -> Option<Self::Item> {
let index = self.start;
if index < self.end {
self.start += 1;
self.collection.get_layout(index)
self.collection.get_tile(index)
} else {
None
}
Expand All @@ -187,7 +187,7 @@ impl_scope! {
if self.start < self.end {
let index = self.end - 1;
self.end = index;
self.collection.get_layout(index)
self.collection.get_tile(index)
} else {
None
}
Expand Down Expand Up @@ -245,13 +245,13 @@ macro_rules! impl_slice {
}

#[inline]
fn get_layout(&self, index: usize) -> Option<&dyn Layout> {
self.get(index).map(|$pat| $w as &dyn Layout)
fn get_tile(&self, index: usize) -> Option<&dyn Tile> {
self.get(index).map(|$pat| $w as &dyn Tile)
}

#[inline]
fn get_mut_layout(&mut self, index: usize) -> Option<&mut dyn Layout> {
self.get_mut(index).map(|$pat| $w as &mut dyn Layout)
fn get_mut_tile(&mut self, index: usize) -> Option<&mut dyn Tile> {
self.get_mut(index).map(|$pat| $w as &mut dyn Tile)
}

#[inline]
Expand All @@ -269,9 +269,9 @@ macro_rules! impl_slice {
#[inline]
fn binary_search_by<'a, F>(&'a self, mut f: F) -> Option<Result<usize, usize>>
where
F: FnMut(&'a dyn Layout) -> std::cmp::Ordering,
F: FnMut(&'a dyn Tile) -> std::cmp::Ordering,
{
Some(<[_]>::binary_search_by(self, move |$pat| f($w.as_layout())))
Some(<[_]>::binary_search_by(self, move |$pat| f($w.as_tile())))
}
}
};
Expand Down
2 changes: 1 addition & 1 deletion crates/kas-core/src/core/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
use crate::event::{ConfigCx, Event, EventCx, FocusSource, IsUsed, Scroll, Unused, Used};
#[cfg(debug_assertions)] use crate::util::IdentifyWidget;
use crate::{Events, Id, Layout, NavAdvance, Node, Widget};
use crate::{Events, Id, NavAdvance, Node, Tile, Widget};

/// Generic implementation of [`Widget::_send`]
pub fn _send<W: Events>(
Expand Down
Loading

0 comments on commit 90a4347

Please sign in to comment.