cursive_split_panel/
split_panel.rs1use super::{actions::*, pane::*, which::*};
2
3use cursive::{direction::*, event::*, view::*, views::*, *};
4
5pub struct SplitPanel {
23 pub(crate) orientation: Orientation,
24 pub(crate) border: bool,
25 pub(crate) visible_divider: bool,
26 pub(crate) movable_divider: bool,
27 pub(crate) front: Pane,
28 pub(crate) back: Pane,
29 pub(crate) divider: Option<usize>,
30 pub(crate) needs_relayout: bool,
31 pub(crate) size: Option<Vec2>,
32 pub(crate) focus: Option<WhichPane>,
33 pub(crate) moving_divider: bool,
34 pub(crate) actions: Actions,
35}
36
37impl SplitPanel {
38 pub fn new(orientation: Orientation) -> Self {
40 Self {
41 orientation,
42 border: true,
43 visible_divider: true,
44 movable_divider: true,
45 front: DummyView.into_boxed_view().into(),
46 back: DummyView.into_boxed_view().into(),
47 divider: None,
48 needs_relayout: true,
49 size: None,
50 focus: None,
51 moving_divider: false,
52 actions: Action::defaults(orientation),
53 }
54 }
55
56 pub fn horizontal() -> Self {
58 Self::new(Orientation::Horizontal)
59 }
60
61 pub fn vertical() -> Self {
63 Self::new(Orientation::Vertical)
64 }
65
66 pub fn orientation(&self) -> Orientation {
68 self.orientation
69 }
70
71 pub fn set_orientation(&mut self, orientation: Orientation) {
73 let old_orientation = self.orientation;
74 self.orientation = orientation;
75 if self.orientation != old_orientation {
76 self.needs_relayout = true;
77 }
78 }
79
80 pub fn with_orientation(self, orientation: Orientation) -> Self {
84 self.with(|self_| self_.set_orientation(orientation))
85 }
86
87 pub fn border(&self) -> bool {
89 self.border
90 }
91
92 pub fn set_border(&mut self, border: bool) {
94 let old_border = self.border;
95 self.border = border;
96 if self.border != old_border {
97 self.needs_relayout = true;
98 }
99 }
100
101 pub fn with_border(self, border: bool) -> Self {
105 self.with(|self_| self_.set_border(border))
106 }
107
108 pub fn visible_divider(&self) -> bool {
110 self.visible_divider
111 }
112
113 pub fn set_visible_divider(&mut self, visible_divider: bool) {
115 let old_visible_divider = self.border;
116 self.visible_divider = visible_divider;
117 if self.visible_divider != old_visible_divider {
118 self.needs_relayout = true;
119 }
120 }
121
122 pub fn with_visible_divider(self, visible_divider: bool) -> Self {
126 self.with(|self_| self_.set_visible_divider(visible_divider))
127 }
128
129 pub fn movable_divider(&self) -> bool {
131 self.movable_divider
132 }
133
134 pub fn set_movable_divider(&mut self, movable_divider: bool) {
136 self.movable_divider = movable_divider;
137 }
138
139 pub fn with_movable_divider(self, movable_divider: bool) -> Self {
143 self.with(|self_| self_.set_movable_divider(movable_divider))
144 }
145
146 pub fn front(&self) -> &dyn View {
148 self.front.view.as_ref()
149 }
150
151 pub fn front_mut(&mut self) -> &mut dyn View {
153 self.front.view.as_mut()
154 }
155
156 pub fn set_front<ViewT>(&mut self, view: ViewT)
158 where
159 ViewT: 'static + IntoBoxedView,
160 {
161 self.front = view.into();
162 self.needs_relayout = true;
163 }
164
165 pub fn with_front<ViewT>(self, view: ViewT) -> Self
169 where
170 ViewT: 'static + IntoBoxedView,
171 {
172 self.with(|self_| self_.set_front(view))
173 }
174
175 pub fn back(&self) -> &dyn View {
177 self.back.view.as_ref()
178 }
179
180 pub fn back_mut(&mut self) -> &mut dyn View {
182 self.back.view.as_mut()
183 }
184
185 pub fn set_back<ViewT>(&mut self, view: ViewT)
187 where
188 ViewT: 'static + IntoBoxedView,
189 {
190 self.back = view.into();
191 self.needs_relayout = true;
192 }
193
194 pub fn with_back<ViewT>(self, view: ViewT) -> Self
198 where
199 ViewT: 'static + IntoBoxedView,
200 {
201 self.with(|self_| self_.set_back(view))
202 }
203
204 pub fn divider(&self) -> Option<usize> {
208 self.divider
209 }
210
211 pub fn set_divider(&mut self, divider: usize) {
215 let old_divider = self.divider;
216 self.divider = Some(divider);
217 if self.divider != old_divider {
218 self.needs_relayout = true;
219 }
220 }
221
222 pub fn with_divider(self, divider: usize) -> Self {
228 self.with(|self_| self_.set_divider(divider))
229 }
230
231 pub fn actions(&self) -> &Actions {
233 &self.actions
234 }
235
236 pub fn actions_mut(&mut self) -> &mut Actions {
238 &mut self.actions
239 }
240
241 pub fn set_actions(&mut self, actions: Actions) {
243 self.actions = actions;
244 }
245
246 pub fn with_actions(self, actions: Actions) -> Self {
250 self.with(|self_| self_.set_actions(actions))
251 }
252
253 pub fn set_action<EventT>(&mut self, action: Action, event: EventT)
257 where
258 EventT: Into<Event>,
259 {
260 self.actions.insert(event.into(), action);
261 }
262
263 pub fn with_action<EventT>(self, action: Action, event: EventT) -> Self
269 where
270 EventT: Into<Event>,
271 {
272 self.with(|self_| self_.set_action(action, event))
273 }
274
275 pub fn remove_action(&mut self, action: Action) -> bool {
281 action.remove(&mut self.actions)
282 }
283
284 pub fn without_action(self, action: Action) -> Self {
290 self.with(|self_| _ = self_.remove_action(action))
291 }
292}
293
294impl Default for SplitPanel {
295 fn default() -> Self {
296 Self::new(Orientation::Horizontal)
297 }
298}