use waft_embedder::TextDirection;
use crate::compose::ThreePaneScaffoldRole;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub struct ThreePaneScaffoldHorizontalOrder {
first: ThreePaneScaffoldRole,
second: ThreePaneScaffoldRole,
third: ThreePaneScaffoldRole,
}
impl ThreePaneScaffoldHorizontalOrder {
pub const LIST_DETAIL: ThreePaneScaffoldHorizontalOrder = ThreePaneScaffoldHorizontalOrder {
first: ThreePaneScaffoldRole::Secondary,
second: ThreePaneScaffoldRole::Primary,
third: ThreePaneScaffoldRole::Tertiary,
};
pub const SUPPORTING: ThreePaneScaffoldHorizontalOrder = ThreePaneScaffoldHorizontalOrder {
first: ThreePaneScaffoldRole::Primary,
second: ThreePaneScaffoldRole::Secondary,
third: ThreePaneScaffoldRole::Tertiary,
};
pub fn new(
first: ThreePaneScaffoldRole,
second: ThreePaneScaffoldRole,
third: ThreePaneScaffoldRole,
) -> ThreePaneScaffoldHorizontalOrder {
assert!(
first != second && second != third && first != third,
"invalid ThreePaneScaffoldHorizontalOrder({first:?}, {second:?}, {third:?}) - panes \
must be unique"
);
ThreePaneScaffoldHorizontalOrder {
first,
second,
third,
}
}
pub fn roles(self) -> [ThreePaneScaffoldRole; 3] {
[self.first, self.second, self.third]
}
pub fn get(self, index: usize) -> ThreePaneScaffoldRole {
self.roles()[index]
}
pub fn index_of(self, role: ThreePaneScaffoldRole) -> usize {
self.roles()
.iter()
.position(|candidate| *candidate == role)
.expect("an order holds all three roles")
}
pub fn to_ltr(self, text_direction: TextDirection) -> ThreePaneScaffoldHorizontalOrder {
if text_direction == TextDirection::Rtl {
ThreePaneScaffoldHorizontalOrder::new(self.third, self.second, self.first)
} else {
self
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::compose::ListDetailPaneScaffoldRole;
#[test]
fn the_list_detail_order_is_list_then_detail_and_mirrors_for_rtl() {
assert_eq!(
ThreePaneScaffoldHorizontalOrder::LIST_DETAIL.roles(),
[
ListDetailPaneScaffoldRole::LIST,
ListDetailPaneScaffoldRole::DETAIL,
ListDetailPaneScaffoldRole::EXTRA,
]
);
assert_eq!(
ThreePaneScaffoldHorizontalOrder::LIST_DETAIL
.index_of(ListDetailPaneScaffoldRole::DETAIL),
1
);
assert_eq!(
ThreePaneScaffoldHorizontalOrder::LIST_DETAIL.to_ltr(TextDirection::Rtl),
ThreePaneScaffoldHorizontalOrder::new(
ListDetailPaneScaffoldRole::EXTRA,
ListDetailPaneScaffoldRole::DETAIL,
ListDetailPaneScaffoldRole::LIST,
)
);
}
#[test]
#[should_panic(expected = "panes must be unique")]
fn an_order_with_a_repeated_role_panics() {
let _ = ThreePaneScaffoldHorizontalOrder::new(
ListDetailPaneScaffoldRole::LIST,
ListDetailPaneScaffoldRole::DETAIL,
ListDetailPaneScaffoldRole::DETAIL,
);
}
}