-
-
Notifications
You must be signed in to change notification settings - Fork 702
/
Copy pathmod.rs
2444 lines (2367 loc) · 98.9 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
mod pane_resizer;
mod stacked_panes;
mod tiled_pane_grid;
use crate::resize_pty;
use tiled_pane_grid::{split, TiledPaneGrid, RESIZE_PERCENT};
use crate::{
os_input_output::ServerOsApi,
output::Output,
panes::{ActivePanes, PaneId},
plugins::PluginInstruction,
tab::{pane_info_for_pane, Pane, MIN_TERMINAL_HEIGHT, MIN_TERMINAL_WIDTH},
thread_bus::ThreadSenders,
ui::boundaries::Boundaries,
ui::pane_contents_and_ui::PaneContentsAndUi,
ClientId,
};
use stacked_panes::StackedPanes;
use zellij_utils::{
data::{Direction, ModeInfo, PaneInfo, Resize, ResizeStrategy, Style, Styling},
errors::prelude::*,
input::{
command::RunCommand,
layout::{Run, RunPluginOrAlias, SplitDirection},
},
pane_size::{Offset, PaneGeom, Size, SizeInPixels, Viewport},
};
use std::{
cell::RefCell,
collections::{BTreeMap, HashMap, HashSet},
rc::Rc,
time::Instant,
};
fn pane_content_offset(position_and_size: &PaneGeom, viewport: &Viewport) -> (usize, usize) {
// (columns_offset, rows_offset)
// if the pane is not on the bottom or right edge on the screen, we need to reserve one space
// from its content to leave room for the boundary between it and the next pane (if it doesn't
// draw its own frame)
let columns_offset = if position_and_size.x + position_and_size.cols.as_usize() < viewport.cols
{
1
} else {
0
};
let rows_offset = if position_and_size.y + position_and_size.rows.as_usize() < viewport.rows {
1
} else {
0
};
(columns_offset, rows_offset)
}
pub struct TiledPanes {
pub panes: BTreeMap<PaneId, Box<dyn Pane>>,
display_area: Rc<RefCell<Size>>,
viewport: Rc<RefCell<Viewport>>,
connected_clients: Rc<RefCell<HashSet<ClientId>>>,
connected_clients_in_app: Rc<RefCell<HashSet<ClientId>>>,
mode_info: Rc<RefCell<HashMap<ClientId, ModeInfo>>>,
character_cell_size: Rc<RefCell<Option<SizeInPixels>>>,
stacked_resize: Rc<RefCell<bool>>,
default_mode_info: ModeInfo,
style: Style,
session_is_mirrored: bool,
active_panes: ActivePanes,
draw_pane_frames: bool,
panes_to_hide: HashSet<PaneId>,
fullscreen_is_active: Option<PaneId>,
senders: ThreadSenders,
window_title: Option<String>,
client_id_to_boundaries: HashMap<ClientId, Boundaries>,
tombstones_before_increase: Option<(PaneId, Vec<HashMap<PaneId, PaneGeom>>)>,
tombstones_before_decrease: Option<(PaneId, Vec<HashMap<PaneId, PaneGeom>>)>,
}
impl TiledPanes {
#[allow(clippy::too_many_arguments)]
pub fn new(
display_area: Rc<RefCell<Size>>,
viewport: Rc<RefCell<Viewport>>,
connected_clients: Rc<RefCell<HashSet<ClientId>>>,
connected_clients_in_app: Rc<RefCell<HashSet<ClientId>>>,
mode_info: Rc<RefCell<HashMap<ClientId, ModeInfo>>>,
character_cell_size: Rc<RefCell<Option<SizeInPixels>>>,
stacked_resize: Rc<RefCell<bool>>,
session_is_mirrored: bool,
draw_pane_frames: bool,
default_mode_info: ModeInfo,
style: Style,
os_api: Box<dyn ServerOsApi>,
senders: ThreadSenders,
) -> Self {
TiledPanes {
panes: BTreeMap::new(),
display_area,
viewport,
connected_clients,
connected_clients_in_app,
mode_info,
character_cell_size,
stacked_resize,
default_mode_info,
style,
session_is_mirrored,
active_panes: ActivePanes::new(&os_api),
draw_pane_frames,
panes_to_hide: HashSet::new(),
fullscreen_is_active: None,
senders,
window_title: None,
client_id_to_boundaries: HashMap::new(),
tombstones_before_increase: None,
tombstones_before_decrease: None,
}
}
pub fn add_pane_with_existing_geom(&mut self, pane_id: PaneId, mut pane: Box<dyn Pane>) {
if self.draw_pane_frames {
pane.set_content_offset(Offset::frame(1));
}
self.panes.insert(pane_id, pane);
}
pub fn replace_active_pane(
&mut self,
pane: Box<dyn Pane>,
client_id: ClientId,
) -> Option<Box<dyn Pane>> {
let pane_id = pane.pid();
// remove the currently active pane
let previously_active_pane = self
.active_panes
.get(&client_id)
.copied()
.and_then(|active_pane_id| self.replace_pane(active_pane_id, pane));
// move clients from the previously active pane to the new pane we just inserted
if let Some(previously_active_pane) = previously_active_pane.as_ref() {
let previously_active_pane_id = previously_active_pane.pid();
self.move_clients_between_panes(previously_active_pane_id, pane_id);
}
previously_active_pane
}
pub fn replace_pane(
&mut self,
pane_id: PaneId,
mut with_pane: Box<dyn Pane>,
) -> Option<Box<dyn Pane>> {
let with_pane_id = with_pane.pid();
if self.draw_pane_frames && !with_pane.borderless() {
with_pane.set_content_offset(Offset::frame(1));
}
let removed_pane = self.panes.remove(&pane_id).map(|removed_pane| {
let with_pane_id = with_pane.pid();
let removed_pane_geom = removed_pane.position_and_size();
let removed_pane_geom_override = removed_pane.geom_override();
with_pane.set_geom(removed_pane_geom);
match removed_pane_geom_override {
Some(geom_override) => with_pane.set_geom_override(geom_override),
None => with_pane.reset_size_and_position_override(),
};
self.panes.insert(with_pane_id, with_pane);
removed_pane
});
// move clients from the previously active pane to the new pane we just inserted
self.move_clients_between_panes(pane_id, with_pane_id);
self.reapply_pane_frames();
removed_pane
}
pub fn insert_pane(
&mut self,
pane_id: PaneId,
pane: Box<dyn Pane>,
client_id: Option<ClientId>,
) {
let should_relayout = true;
self.add_pane(pane_id, pane, should_relayout, client_id);
}
pub fn insert_pane_without_relayout(
&mut self,
pane_id: PaneId,
pane: Box<dyn Pane>,
client_id: Option<ClientId>,
) {
let should_relayout = false;
self.add_pane(pane_id, pane, should_relayout, client_id);
}
pub fn has_room_for_new_pane(&mut self) -> bool {
let cursor_height_width_ratio = self.cursor_height_width_ratio();
let pane_grid = TiledPaneGrid::new(
&mut self.panes,
&self.panes_to_hide,
*self.display_area.borrow(),
*self.viewport.borrow(),
);
let has_room_for_new_pane = pane_grid
.find_room_for_new_pane(cursor_height_width_ratio)
.is_some();
has_room_for_new_pane || pane_grid.has_room_for_new_stacked_pane() || self.panes.is_empty()
}
pub fn assign_geom_for_pane_with_run(&mut self, run: Option<Run>) {
// here we're removing the first pane we find with this run instruction and re-adding it so
// that it gets a new geom similar to how it would when being added to the tab originally
if let Some(pane_id) = self
.panes
.iter()
.find_map(|(pid, p)| {
if p.invoked_with() == &run {
Some(pid)
} else {
None
}
})
.copied()
{
if let Some(mut pane) = self.panes.remove(&pane_id) {
// we must strip the logical position here because it's likely a straggler from
// this pane's previous tab and would cause chaos if considered in the new one
let mut pane_geom = pane.position_and_size();
pane_geom.logical_position = None;
pane.set_geom(pane_geom);
self.add_pane_with_existing_geom(pane.pid(), pane);
}
}
}
fn add_pane(
&mut self,
pane_id: PaneId,
pane: Box<dyn Pane>,
should_relayout: bool,
client_id: Option<ClientId>,
) {
if self.panes.is_empty() {
self.panes.insert(pane_id, pane);
return;
}
let stacked_resize = { *self.stacked_resize.borrow() };
if let Some(client_id) = client_id {
if stacked_resize && self.is_connected(&client_id) {
self.add_pane_with_stacked_resize(pane_id, pane, should_relayout, client_id);
return;
}
}
self.add_pane_without_stacked_resize(pane_id, pane, should_relayout)
}
fn add_pane_without_stacked_resize(
&mut self,
pane_id: PaneId,
mut pane: Box<dyn Pane>,
should_relayout: bool,
) {
let cursor_height_width_ratio = self.cursor_height_width_ratio();
let mut pane_grid = TiledPaneGrid::new(
&mut self.panes,
&self.panes_to_hide,
*self.display_area.borrow(),
*self.viewport.borrow(),
);
let pane_id_and_split_direction =
pane_grid.find_room_for_new_pane(cursor_height_width_ratio);
match pane_id_and_split_direction {
Some((pane_id_to_split, split_direction)) => {
// this unwrap is safe because floating panes should not be visible if there are no floating panes
let pane_to_split = self.panes.get_mut(&pane_id_to_split).unwrap();
let size_of_both_panes = pane_to_split.position_and_size();
if let Some((first_geom, second_geom)) = split(split_direction, &size_of_both_panes)
{
pane_to_split.set_geom(first_geom);
pane.set_geom(second_geom);
self.panes.insert(pane_id, pane);
if should_relayout {
self.relayout(!split_direction);
}
}
},
None => {
// we couldn't add the pane normally, let's see if there's room in one of the
// stacks...
match pane_grid.make_room_in_stack_for_pane() {
Ok(new_pane_geom) => {
pane.set_geom(new_pane_geom);
self.panes.insert(pane_id, pane); // TODO: is set_geom the right one?
},
Err(e) => {
log::error!("Failed to add pane to stack: {:?}", e);
},
}
},
}
}
fn add_pane_with_stacked_resize(
&mut self,
pane_id: PaneId,
mut pane: Box<dyn Pane>,
should_relayout: bool,
client_id: ClientId,
) {
let cursor_height_width_ratio = self.cursor_height_width_ratio();
let mut pane_grid = TiledPaneGrid::new(
&mut self.panes,
&self.panes_to_hide,
*self.display_area.borrow(),
*self.viewport.borrow(),
);
let Some(active_pane_id) = self.active_panes.get(&client_id) else {
log::error!("Could not find active pane id for client_id");
return;
};
if pane_grid
.get_pane_geom(active_pane_id)
.map(|p| p.is_stacked())
.unwrap_or(false)
{
// try to add the pane to the stack of the active pane
match pane_grid.make_room_in_stack_of_pane_id_for_pane(active_pane_id) {
Ok(new_pane_geom) => {
pane.set_geom(new_pane_geom);
self.panes.insert(pane_id, pane);
self.set_force_render(); // TODO: why do we need this?
return;
},
Err(_e) => {
return self.add_pane_without_stacked_resize(pane_id, pane, should_relayout);
},
}
}
let pane_id_and_split_direction =
pane_grid.split_pane(active_pane_id, cursor_height_width_ratio);
match pane_id_and_split_direction {
Some((pane_id_to_split, split_direction)) => {
// this unwrap is safe because floating panes should not be visible if there are no floating panes
let pane_to_split = self.panes.get_mut(&pane_id_to_split).unwrap();
let size_of_both_panes = pane_to_split.position_and_size();
if let Some((first_geom, second_geom)) = split(split_direction, &size_of_both_panes)
{
pane_to_split.set_geom(first_geom);
pane.set_geom(second_geom);
self.panes.insert(pane_id, pane);
if should_relayout {
self.relayout(!split_direction);
}
}
},
None => {
// we couldn't add the pane normally, let's see if there's room in one of the
// stacks...
let _ = pane_grid.make_pane_stacked(active_pane_id);
match pane_grid.make_room_in_stack_of_pane_id_for_pane(active_pane_id) {
Ok(new_pane_geom) => {
pane.set_geom(new_pane_geom);
self.panes.insert(pane_id, pane); // TODO: is set_geom the right one?
return;
},
Err(_e) => {
return self.add_pane_without_stacked_resize(
pane_id,
pane,
should_relayout,
);
},
}
},
}
}
pub fn fixed_pane_geoms(&self) -> Vec<Viewport> {
self.panes
.values()
.filter_map(|p| {
let geom = p.position_and_size();
if geom.cols.is_fixed() || geom.rows.is_fixed() {
Some(geom.into())
} else {
None
}
})
.collect()
}
pub fn borderless_pane_geoms(&self) -> Vec<Viewport> {
self.panes
.values()
.filter_map(|p| {
let geom = p.position_and_size();
if p.borderless() {
Some(geom.into())
} else {
None
}
})
.collect()
}
pub fn non_selectable_pane_geoms_inside_viewport(&self) -> Vec<Viewport> {
self.panes
.values()
.filter_map(|p| {
let geom = p.position_and_size();
if !p.selectable() && is_inside_viewport(&self.viewport.borrow(), p) {
Some(geom.into())
} else {
None
}
})
.collect()
}
pub fn first_selectable_pane_id(&self) -> Option<PaneId> {
self.panes
.iter()
.filter(|(_id, pane)| pane.selectable())
.map(|(id, _)| id.to_owned())
.next()
}
pub fn pane_ids(&self) -> impl Iterator<Item = &PaneId> {
self.panes.keys()
}
pub fn relayout(&mut self, direction: SplitDirection) {
let mut pane_grid = TiledPaneGrid::new(
&mut self.panes,
&self.panes_to_hide,
*self.display_area.borrow(),
*self.viewport.borrow(),
);
match direction {
SplitDirection::Horizontal => {
pane_grid.layout(direction, (*self.display_area.borrow()).cols)
},
SplitDirection::Vertical => {
pane_grid.layout(direction, (*self.display_area.borrow()).rows)
},
}
.or_else(|e| Err(anyError::msg(e)))
.with_context(|| format!("{:?} relayout of tab failed", direction))
.non_fatal();
self.set_pane_frames(self.draw_pane_frames);
}
pub fn reapply_pane_frames(&mut self) {
// same as set_pane_frames except it reapplies the current situation
self.set_pane_frames(self.draw_pane_frames);
}
pub fn set_pane_frames(&mut self, draw_pane_frames: bool) {
self.draw_pane_frames = draw_pane_frames;
let viewport = *self.viewport.borrow();
for pane in self.panes.values_mut() {
if !pane.borderless() {
pane.set_frame(draw_pane_frames);
}
#[allow(clippy::if_same_then_else)]
if draw_pane_frames && !pane.borderless() {
// there's definitely a frame around this pane, offset its contents
pane.set_content_offset(Offset::frame(1));
} else if draw_pane_frames && pane.borderless() {
// there's no frame around this pane, and the tab isn't handling the boundaries
// between panes (they each draw their own frames as they please)
// this one doesn't - do not offset its content
pane.set_content_offset(Offset::default());
} else if !is_inside_viewport(&viewport, pane) {
// this pane is outside the viewport and has no border - it should not have an offset
pane.set_content_offset(Offset::default());
} else {
// no draw_pane_frames and this pane should have a separation to other panes
// according to its position in the viewport (eg. no separation if its at the
// viewport bottom) - offset its content accordingly
let position_and_size = pane.current_geom();
let (pane_columns_offset, pane_rows_offset) =
pane_content_offset(&position_and_size, &viewport);
if !draw_pane_frames && pane.current_geom().is_stacked() {
// stacked panes should always leave 1 top row for a title
pane.set_content_offset(Offset::shift_right_and_top(pane_columns_offset, 1));
} else {
pane.set_content_offset(Offset::shift(pane_rows_offset, pane_columns_offset));
}
}
resize_pty!(pane, self.os_api, self.senders, self.character_cell_size).unwrap();
}
self.reset_boundaries();
}
pub fn can_split_pane_horizontally(&mut self, client_id: ClientId) -> bool {
if let Some(active_pane_id) = &self.active_panes.get(&client_id) {
if let Some(active_pane) = self.panes.get_mut(active_pane_id) {
let mut full_pane_size = active_pane.position_and_size();
if full_pane_size.is_stacked() {
let Some(position_and_size_of_stack) =
StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.position_and_size_of_stack(&active_pane_id)
else {
log::error!("Failed to find position and size of stack");
return false;
};
full_pane_size = position_and_size_of_stack;
}
if full_pane_size.rows.as_usize() < MIN_TERMINAL_HEIGHT * 2 {
return false;
} else {
return split(SplitDirection::Horizontal, &full_pane_size).is_some();
}
}
}
false
}
pub fn can_split_pane_vertically(&mut self, client_id: ClientId) -> bool {
if let Some(active_pane_id) = &self.active_panes.get(&client_id) {
if let Some(active_pane) = self.panes.get_mut(active_pane_id) {
let mut full_pane_size = active_pane.position_and_size();
if full_pane_size.is_stacked() {
let Some(position_and_size_of_stack) =
StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.position_and_size_of_stack(&active_pane_id)
else {
log::error!("Failed to find position and size of stack");
return false;
};
full_pane_size = position_and_size_of_stack;
}
if full_pane_size.cols.as_usize() < MIN_TERMINAL_WIDTH * 2 {
return false;
}
return split(SplitDirection::Vertical, &full_pane_size).is_some();
}
}
false
}
pub fn split_pane_horizontally(
&mut self,
pid: PaneId,
mut new_pane: Box<dyn Pane>,
client_id: ClientId,
) {
let active_pane_id = &self.active_panes.get(&client_id).unwrap();
let mut full_pane_size = self
.panes
.get(active_pane_id)
.map(|p| p.position_and_size())
.unwrap();
if full_pane_size.is_stacked() {
match StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.position_and_size_of_stack(&active_pane_id)
{
Some(position_and_size_of_stack) => {
full_pane_size = position_and_size_of_stack;
},
None => {
log::error!("Failed to find position and size of stack");
},
}
}
let active_pane = self.panes.get_mut(active_pane_id).unwrap();
if let Some((top_winsize, bottom_winsize)) =
split(SplitDirection::Horizontal, &full_pane_size)
{
if active_pane.position_and_size().is_stacked() {
match StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.resize_panes_in_stack(&active_pane_id, top_winsize)
{
Ok(_) => {},
Err(e) => {
log::error!("Failed to resize stack: {}", e);
},
}
} else {
active_pane.set_geom(top_winsize);
}
new_pane.set_geom(bottom_winsize);
self.panes.insert(pid, new_pane);
self.relayout(SplitDirection::Vertical);
}
}
pub fn split_pane_vertically(
&mut self,
pid: PaneId,
mut new_pane: Box<dyn Pane>,
client_id: ClientId,
) {
let active_pane_id = &self.active_panes.get(&client_id).unwrap();
let mut full_pane_size = self
.panes
.get(active_pane_id)
.map(|p| p.position_and_size())
.unwrap();
if full_pane_size.is_stacked() {
match StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.position_and_size_of_stack(&active_pane_id)
{
Some(position_and_size_of_stack) => {
full_pane_size = position_and_size_of_stack;
},
None => {
log::error!("Failed to find position and size of stack");
},
}
}
let active_pane = self.panes.get_mut(active_pane_id).unwrap();
if let Some((left_winsize, right_winsize)) =
split(SplitDirection::Vertical, &full_pane_size)
{
if active_pane.position_and_size().is_stacked() {
match StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.resize_panes_in_stack(&active_pane_id, left_winsize)
{
Ok(_) => {},
Err(e) => {
log::error!("Failed to resize stack: {}", e);
},
}
} else {
active_pane.set_geom(left_winsize);
}
new_pane.set_geom(right_winsize);
self.panes.insert(pid, new_pane);
self.relayout(SplitDirection::Horizontal);
}
}
pub fn focus_pane_for_all_clients(&mut self, pane_id: PaneId) {
let connected_clients: Vec<ClientId> =
self.connected_clients.borrow().iter().copied().collect();
for client_id in connected_clients {
if self
.panes
.get(&pane_id)
.map(|p| p.current_geom().is_stacked())
.unwrap_or(false)
{
let _ = StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.expand_pane(&pane_id);
}
self.active_panes
.insert(client_id, pane_id, &mut self.panes);
self.set_pane_active_at(pane_id);
}
self.set_force_render();
self.reapply_pane_frames();
}
pub fn reapply_pane_focus(&mut self) {
let connected_clients: Vec<ClientId> =
self.connected_clients.borrow().iter().copied().collect();
for client_id in connected_clients {
match &self.active_panes.get(&client_id).copied() {
Some(pane_id) => {
if self
.panes
.get(&pane_id)
.map(|p| p.current_geom().is_stacked())
.unwrap_or(false)
{
let _ =
StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.expand_pane(&pane_id);
}
self.active_panes
.insert(client_id, *pane_id, &mut self.panes);
self.set_pane_active_at(*pane_id);
},
None => {
if let Some(first_pane_id) = self.first_selectable_pane_id() {
let pane_id = first_pane_id; // TODO: combine with above
if self
.panes
.get(&pane_id)
.map(|p| p.current_geom().is_stacked())
.unwrap_or(false)
{
let _ = StackedPanes::new_from_btreemap(
&mut self.panes,
&self.panes_to_hide,
)
.expand_pane(&pane_id);
}
self.active_panes
.insert(client_id, pane_id, &mut self.panes);
self.set_pane_active_at(pane_id);
}
},
}
}
self.set_force_render();
self.reapply_pane_frames();
}
pub fn expand_pane_in_stack(&mut self, pane_id: PaneId) -> Vec<PaneId> {
// returns all pane ids in stack
match StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.expand_pane(&pane_id)
{
Ok(all_panes_in_stack) => {
let connected_clients: Vec<ClientId> =
self.connected_clients.borrow().iter().copied().collect();
for client_id in connected_clients {
if let Some(focused_pane_id_for_client) = self.active_panes.get(&client_id) {
if all_panes_in_stack.contains(focused_pane_id_for_client) {
self.active_panes
.insert(client_id, pane_id, &mut self.panes);
self.set_pane_active_at(pane_id);
}
}
}
self.set_force_render();
self.reapply_pane_frames();
all_panes_in_stack
},
Err(e) => {
log::error!("Failed to expand pane in stack: {:?}", e);
vec![]
},
}
}
pub fn focus_pane(&mut self, pane_id: PaneId, client_id: ClientId) {
let pane_is_selectable = self
.panes
.get(&pane_id)
.map(|p| p.selectable())
.unwrap_or(false);
if !pane_is_selectable {
log::error!("Cannot focus pane {:?} as it is not selectable!", pane_id);
return;
}
if self.panes_to_hide.contains(&pane_id) {
// this means there is a fullscreen pane that is not the current pane, let's unset it
// before changing focus
self.unset_fullscreen();
}
if self
.panes
.get(&pane_id)
.map(|p| p.current_geom().is_stacked())
.unwrap_or(false)
{
let _ = StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.expand_pane(&pane_id);
self.reapply_pane_frames();
}
self.active_panes
.insert(client_id, pane_id, &mut self.panes);
if self.session_is_mirrored {
// move all clients
let connected_clients: Vec<ClientId> =
self.connected_clients.borrow().iter().copied().collect();
for client_id in connected_clients {
self.active_panes
.insert(client_id, pane_id, &mut self.panes);
}
}
self.reset_boundaries();
}
pub fn focus_pane_if_exists(&mut self, pane_id: PaneId, client_id: ClientId) -> Result<()> {
if self.panes.get(&pane_id).is_some() {
self.focus_pane(pane_id, client_id);
Ok(())
} else {
Err(anyhow!("Pane not found"))
}
}
pub fn focus_pane_at_position(&mut self, position_and_size: PaneGeom, client_id: ClientId) {
if let Some(pane_id) = self
.panes
.iter()
.find(|(_pid, pane)| pane.position_and_size() == position_and_size)
.map(|(pid, _p)| *pid)
{
if let Some(currently_active_pane_id) = self.active_panes.get(&client_id) {
let prev_geom = {
if let Some(currently_focused_pane) =
self.panes.get_mut(currently_active_pane_id)
{
let prev_geom = currently_focused_pane.position_and_size();
currently_focused_pane.set_geom(position_and_size);
Some(prev_geom)
} else {
None
}
};
if let Some(prev_geom) = prev_geom {
if let Some(previous_pane) = self.panes.get_mut(&pane_id) {
previous_pane.set_geom(prev_geom);
self.reset_boundaries();
}
}
}
}
}
pub fn focus_pane_if_client_not_focused(&mut self, pane_id: PaneId, client_id: ClientId) {
match self.active_panes.get(&client_id) {
Some(already_focused_pane_id) => self.focus_pane(*already_focused_pane_id, client_id),
None => self.focus_pane(pane_id, client_id),
}
}
pub fn clear_active_panes(&mut self) {
self.active_panes.clear(&mut self.panes);
self.reset_boundaries();
}
pub fn first_active_pane_id(&self) -> Option<PaneId> {
self.connected_clients
.borrow()
.iter()
.next()
.and_then(|first_client_id| self.active_panes.get(first_client_id).copied())
}
pub fn focused_pane_id(&self, client_id: ClientId) -> Option<PaneId> {
self.active_panes.get(&client_id).copied()
}
#[allow(clippy::borrowed_box)]
pub fn get_pane(&self, pane_id: PaneId) -> Option<&Box<dyn Pane>> {
self.panes.get(&pane_id)
}
pub fn get_pane_mut(&mut self, pane_id: PaneId) -> Option<&mut Box<dyn Pane>> {
self.panes.get_mut(&pane_id)
}
pub fn get_active_pane_id(&self, client_id: ClientId) -> Option<PaneId> {
self.active_panes.get(&client_id).copied()
}
pub fn panes_contain(&self, pane_id: &PaneId) -> bool {
self.panes.contains_key(pane_id)
}
pub fn set_force_render(&mut self) {
for pane in self.panes.values_mut() {
pane.set_should_render(true);
pane.set_should_render_boundaries(true);
pane.render_full_viewport();
}
self.reset_boundaries();
}
pub fn has_active_panes(&self) -> bool {
!self.active_panes.is_empty()
}
pub fn has_panes(&self) -> bool {
!self.panes.is_empty()
}
pub fn render(&mut self, output: &mut Output, floating_panes_are_visible: bool) -> Result<()> {
let err_context = || "failed to render tiled panes";
let connected_clients: Vec<ClientId> =
{ self.connected_clients.borrow().iter().copied().collect() };
let multiple_users_exist_in_session = { self.connected_clients_in_app.borrow().len() > 1 };
let mut client_id_to_boundaries: HashMap<ClientId, Boundaries> = HashMap::new();
let active_panes = if floating_panes_are_visible {
HashMap::new()
} else {
self.active_panes
.iter()
.filter(|(client_id, _pane_id)| connected_clients.contains(client_id))
.map(|(client_id, pane_id)| (*client_id, *pane_id))
.collect()
};
let (stacked_pane_ids_under_flexible_pane, stacked_pane_ids_over_flexible_pane) = {
// TODO: do not recalculate this every time on render
StackedPanes::new_from_btreemap(&mut self.panes, &self.panes_to_hide)
.stacked_pane_ids_under_and_over_flexible_panes()
.unwrap() // TODO: no unwrap
};
for (kind, pane) in self.panes.iter_mut() {
if !self.panes_to_hide.contains(&pane.pid()) {
let pane_is_stacked_under =
stacked_pane_ids_under_flexible_pane.contains(&pane.pid());
let pane_is_stacked_over =
stacked_pane_ids_over_flexible_pane.contains(&pane.pid());
let should_draw_pane_frames = self.draw_pane_frames;
let pane_is_stacked = pane.current_geom().is_stacked();
let mut pane_contents_and_ui = PaneContentsAndUi::new(
pane,
output,
self.style,
&active_panes,
multiple_users_exist_in_session,
None,
pane_is_stacked_under,
pane_is_stacked_over,
should_draw_pane_frames,
);
for client_id in &connected_clients {
let client_mode = self
.mode_info
.borrow()
.get(client_id)
.unwrap_or(&self.default_mode_info)
.mode;
let err_context =
|| format!("failed to render tiled panes for client {client_id}");
if let PaneId::Plugin(..) = kind {
pane_contents_and_ui
.render_pane_contents_for_client(*client_id)
.with_context(err_context)?;
}
let is_floating = false;
if self.draw_pane_frames {
pane_contents_and_ui
.render_pane_frame(
*client_id,
client_mode,
self.session_is_mirrored,
is_floating,
)
.with_context(err_context)?;
} else if pane_is_stacked {
// if we have no pane frames but the pane is stacked, we need to render its
// frame which will amount to only rendering the title line
pane_contents_and_ui
.render_pane_frame(
*client_id,
client_mode,
self.session_is_mirrored,
is_floating,
)
.with_context(err_context)?;
// we also need to render its boundaries as normal
let boundaries = client_id_to_boundaries
.entry(*client_id)
.or_insert_with(|| Boundaries::new(*self.viewport.borrow()));
pane_contents_and_ui.render_pane_boundaries(
*client_id,
client_mode,
boundaries,
self.session_is_mirrored,
);
} else {
let boundaries = client_id_to_boundaries
.entry(*client_id)
.or_insert_with(|| Boundaries::new(*self.viewport.borrow()));
pane_contents_and_ui.render_pane_boundaries(
*client_id,
client_mode,
boundaries,
self.session_is_mirrored,
);
}
pane_contents_and_ui.render_terminal_title_if_needed(
*client_id,
client_mode,
&mut self.window_title,
);
// this is done for panes that don't have their own cursor (eg. panes of
// another user)
pane_contents_and_ui
.render_fake_cursor_if_needed(*client_id)
.with_context(err_context)?;
}
if let PaneId::Terminal(..) = kind {
pane_contents_and_ui
.render_pane_contents_to_multiple_clients(connected_clients.iter().copied())
.with_context(err_context)?;
}
}
}
// render boundaries if needed
for (client_id, boundaries) in client_id_to_boundaries {
let boundaries_to_render = boundaries
.render(self.client_id_to_boundaries.get(&client_id))
.with_context(err_context)?;
self.client_id_to_boundaries.insert(client_id, boundaries);
output
.add_character_chunks_to_client(client_id, boundaries_to_render, None)
.with_context(err_context)?;
}
Ok(())
}
pub fn get_panes(&self) -> impl Iterator<Item = (&PaneId, &Box<dyn Pane>)> {
self.panes.iter()
}
pub fn set_geom_for_pane_with_run(
&mut self,
run: Option<Run>,
geom: PaneGeom,
borderless: bool,
) {
match self
.panes
.iter_mut()
.find(|(_, p)| p.invoked_with() == &run)
{
Some((_, pane)) => {
pane.set_geom(geom);
pane.set_borderless(borderless);
if self.draw_pane_frames {
pane.set_content_offset(Offset::frame(1));
}
},
None => {
log::error!("Failed to find pane with run: {:?}", run);
},
}
}
pub fn set_geom_for_pane_with_id(&mut self, pane_id: &PaneId, geom: PaneGeom) {
match self.panes.get_mut(pane_id) {
Some(pane) => {
pane.set_geom(geom);
},
None => {
log::error!("Failed to find pane with id: {:?}", pane_id);
},
}
}
fn display_area_changed(&self, new_screen_size: Size) -> bool {
let display_area = self.display_area.borrow();
new_screen_size.rows != display_area.rows || new_screen_size.cols != display_area.cols
}