use super::{actions::*, pane::*, which::*};
use cursive::{direction::*, event::*, view::*, views::*, *};
pub struct SplitPanel {
pub(crate) orientation: Orientation,
pub(crate) border: bool,
pub(crate) visible_divider: bool,
pub(crate) movable_divider: bool,
pub(crate) front: Pane,
pub(crate) back: Pane,
pub(crate) divider: Option<usize>,
pub(crate) needs_relayout: bool,
pub(crate) size: Option<Vec2>,
pub(crate) focus: Option<WhichPane>,
pub(crate) moving_divider: bool,
pub(crate) actions: Actions,
}
impl SplitPanel {
pub fn new(orientation: Orientation) -> Self {
Self {
orientation,
border: true,
visible_divider: true,
movable_divider: true,
front: DummyView.into_boxed_view().into(),
back: DummyView.into_boxed_view().into(),
divider: None,
needs_relayout: true,
size: None,
focus: None,
moving_divider: false,
actions: Action::defaults(orientation),
}
}
pub fn horizontal() -> Self {
Self::new(Orientation::Horizontal)
}
pub fn vertical() -> Self {
Self::new(Orientation::Vertical)
}
pub fn orientation(&self) -> Orientation {
self.orientation
}
pub fn set_orientation(&mut self, orientation: Orientation) {
let old_orientation = self.orientation;
self.orientation = orientation;
if self.orientation != old_orientation {
self.needs_relayout = true;
}
}
pub fn with_orientation(self, orientation: Orientation) -> Self {
self.with(|self_| self_.set_orientation(orientation))
}
pub fn border(&self) -> bool {
self.border
}
pub fn set_border(&mut self, border: bool) {
let old_border = self.border;
self.border = border;
if self.border != old_border {
self.needs_relayout = true;
}
}
pub fn with_border(self, border: bool) -> Self {
self.with(|self_| self_.set_border(border))
}
pub fn visible_divider(&self) -> bool {
self.visible_divider
}
pub fn set_visible_divider(&mut self, visible_divider: bool) {
let old_visible_divider = self.border;
self.visible_divider = visible_divider;
if self.visible_divider != old_visible_divider {
self.needs_relayout = true;
}
}
pub fn with_visible_divider(self, visible_divider: bool) -> Self {
self.with(|self_| self_.set_visible_divider(visible_divider))
}
pub fn movable_divider(&self) -> bool {
self.movable_divider
}
pub fn set_movable_divider(&mut self, movable_divider: bool) {
self.movable_divider = movable_divider;
}
pub fn with_movable_divider(self, movable_divider: bool) -> Self {
self.with(|self_| self_.set_movable_divider(movable_divider))
}
pub fn front(&self) -> &dyn View {
self.front.view.as_ref()
}
pub fn front_mut(&mut self) -> &mut dyn View {
self.front.view.as_mut()
}
pub fn set_front<ViewT>(&mut self, view: ViewT)
where
ViewT: 'static + IntoBoxedView,
{
self.front = view.into();
self.needs_relayout = true;
}
pub fn with_front<ViewT>(self, view: ViewT) -> Self
where
ViewT: 'static + IntoBoxedView,
{
self.with(|self_| self_.set_front(view))
}
pub fn back(&self) -> &dyn View {
self.back.view.as_ref()
}
pub fn back_mut(&mut self) -> &mut dyn View {
self.back.view.as_mut()
}
pub fn set_back<ViewT>(&mut self, view: ViewT)
where
ViewT: 'static + IntoBoxedView,
{
self.back = view.into();
self.needs_relayout = true;
}
pub fn with_back<ViewT>(self, view: ViewT) -> Self
where
ViewT: 'static + IntoBoxedView,
{
self.with(|self_| self_.set_back(view))
}
pub fn divider(&self) -> Option<usize> {
self.divider
}
pub fn set_divider(&mut self, divider: usize) {
let old_divider = self.divider;
self.divider = Some(divider);
if self.divider != old_divider {
self.needs_relayout = true;
}
}
pub fn with_divider(self, divider: usize) -> Self {
self.with(|self_| self_.set_divider(divider))
}
pub fn actions(&self) -> &Actions {
&self.actions
}
pub fn actions_mut(&mut self) -> &mut Actions {
&mut self.actions
}
pub fn set_actions(&mut self, actions: Actions) {
self.actions = actions;
}
pub fn with_actions(self, actions: Actions) -> Self {
self.with(|self_| self_.set_actions(actions))
}
pub fn set_action<EventT>(&mut self, action: Action, event: EventT)
where
EventT: Into<Event>,
{
self.actions.insert(event.into(), action);
}
pub fn with_action<EventT>(self, action: Action, event: EventT) -> Self
where
EventT: Into<Event>,
{
self.with(|self_| self_.set_action(action, event))
}
pub fn remove_action(&mut self, action: Action) -> bool {
action.remove(&mut self.actions)
}
pub fn without_action(self, action: Action) -> Self {
self.with(|self_| _ = self_.remove_action(action))
}
}
impl Default for SplitPanel {
fn default() -> Self {
Self::new(Orientation::Horizontal)
}
}