forked from sminez/penrose
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstack_set.rs
1495 lines (1258 loc) · 49 KB
/
stack_set.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
use crate::{
core::layout::LayoutStack,
pop_where,
pure::{
diff::{ScreenState, Snapshot},
geometry::{Rect, RelativeRect, RelativeTo},
workspace::check_workspace_invariants,
Position, Screen, Stack, Workspace,
},
stack, Error, Result, Xid,
};
use std::{
cmp::Ordering,
collections::{HashMap, VecDeque},
hash::Hash,
mem::{swap, take},
};
/// The side-effect free internal state representation of the window manager.
#[derive(Default, Debug, Clone)]
pub struct StackSet<C>
where
C: Clone + PartialEq + Eq + Hash,
{
pub(crate) screens: Stack<Screen<C>>, // Workspaces visible on screens
pub(crate) hidden: VecDeque<Workspace<C>>, // Workspaces not currently on any screen
pub(crate) floating: HashMap<C, RelativeRect>, // Floating windows
pub(crate) previous_tag: String, // The last tag to be focused before the current one
pub(crate) invisible_tags: Vec<String>, // Tags that should never be focused
pub(crate) killed_clients: Vec<C>, // clients that have been removed and need processing on the X side
}
impl<C> StackSet<C>
where
C: Clone + PartialEq + Eq + Hash,
{
/// Create a new [StackSet] of empty stacks with the given workspace names.
///
/// # Errors
/// This method will error if there are not enough workspaces to cover the
/// attached screens or if no screens are attached.
pub fn try_new<I, J, T>(layouts: LayoutStack, ws_tags: I, screen_details: J) -> Result<Self>
where
T: Into<String>,
I: IntoIterator<Item = T>,
J: IntoIterator<Item = Rect>,
{
let workspaces: Vec<Workspace<C>> = ws_tags
.into_iter()
.enumerate()
.map(|(i, tag)| Workspace::new(i, tag, layouts.clone(), None))
.collect();
let screen_details: Vec<Rect> = screen_details.into_iter().collect();
Self::try_new_concrete(workspaces, screen_details, HashMap::new())
}
pub(crate) fn try_new_concrete(
mut workspaces: Vec<Workspace<C>>,
screen_details: Vec<Rect>,
floating: HashMap<C, RelativeRect>,
) -> Result<Self> {
check_workspace_invariants(&workspaces)?;
match (workspaces.len(), screen_details.len()) {
(_, 0) => return Err(Error::NoScreens),
(n_ws, n_screens) if n_ws < n_screens => {
return Err(Error::InsufficientWorkspaces { n_ws, n_screens })
}
_ => (),
}
let hidden: VecDeque<Workspace<C>> = workspaces
.split_off(screen_details.len())
.into_iter()
.collect();
let screens =
Stack::from_iter_unchecked(workspaces.into_iter().zip(screen_details).enumerate().map(
|(index, (workspace, r))| Screen {
workspace,
index,
r,
},
));
let previous_tag = screens.focus.workspace.tag.clone();
Ok(Self {
screens,
hidden,
floating,
previous_tag,
invisible_tags: vec![],
killed_clients: vec![],
})
}
/// Set focus to the [Screen] with the specified index.
///
/// If there is no matching screen then the [StackSet] is unmodified.
pub fn focus_screen(&mut self, screen_index: usize) {
let current = self.screens.focus.index;
if current == screen_index {
return;
}
loop {
self.screens.focus_down();
if [current, screen_index].contains(&self.screens.focus.index) {
break;
}
}
}
fn update_previous_tag(&mut self, new: String) {
if self.invisible_tags.contains(&new) {
return;
}
self.previous_tag = new;
}
/// Set focus to the [Workspace] with the specified tag.
///
/// If there is no matching workspace then the [StackSet] is unmodified.
/// If the [Workspace] is currently visible then focus moves to the screen
/// containing that workspace, otherwise the workspace replaces whatever
/// was on the active screen.
///
/// If you always want to focus the given tag on the active screen, see
/// [StackSet::pull_tag_to_screen] instead.
pub fn focus_tag(&mut self, tag: impl AsRef<str>) {
let tag = tag.as_ref();
if self.screens.focus.workspace.tag == tag {
return; // already focused
}
// If the tag is visible on another screen, focus moves to that screen
if !self.try_cycle_screen_to_tag(tag) {
// If the tag is hidden then it gets moved to the current screen
self.try_swap_on_screen_workspace_with_hidden(tag);
}
// If nothing matched by this point then the requested tag is unknown
// so there is nothing for us to do
}
fn try_cycle_screen_to_tag(&mut self, tag: &str) -> bool {
let current_tag = self.screens.focus.workspace.tag.clone();
loop {
self.screens.focus_down();
match &self.screens.focus.workspace.tag {
// we've found and focused the tag
t if t == tag => {
self.update_previous_tag(current_tag);
return true;
}
// we've looped so this tag isn't visible
t if t == ¤t_tag => return false,
// try the next tag
_ => (),
}
}
}
fn try_swap_on_screen_workspace_with_hidden(&mut self, tag: &str) {
if let Some(mut w) = pop_where!(self, hidden, |w: &Workspace<C>| w.tag == tag) {
self.update_previous_tag(self.screens.focus.workspace.tag.clone());
swap(&mut w, &mut self.screens.focus.workspace);
self.hidden.push_back(w);
}
}
// true if we swapped otherwise false
fn try_swap_focused_workspace_with_tag(&mut self, tag: &str) -> bool {
if self.screens.focus.workspace.tag == tag {
return false;
}
let p = |s: &&mut Screen<C>| s.workspace.tag == tag;
let in_up = self.screens.up.iter_mut().find(p);
let in_down = self.screens.down.iter_mut().find(p);
if let Some(s) = in_up.or(in_down) {
swap(&mut self.screens.focus.workspace, &mut s.workspace);
return true;
}
false
}
/// Focus the requested tag on the current screen, swapping the current
/// tag with it.
pub fn pull_tag_to_screen(&mut self, tag: impl AsRef<str>) {
let tag = tag.as_ref();
if self.screens.focus.workspace.tag == tag {
return;
}
if !self.try_swap_focused_workspace_with_tag(tag) {
self.try_swap_on_screen_workspace_with_hidden(tag);
}
}
/// Toggle focus back to the previously focused [Workspace] based on its tag
pub fn toggle_tag(&mut self) {
self.focus_tag(self.previous_tag.clone());
}
/// Focus the given client and set its [Workspace] as current (see
/// focus_tag).
///
/// If the client is unknown then this is a no-op.
pub fn focus_client(&mut self, client: &C) {
if self.current_client() == Some(client) {
return; // already focused
}
let tag = match self.tag_for_client(client) {
Some(tag) => tag.to_string(),
None => return, // unknown client
};
self.focus_tag(&tag);
while self.current_client() != Some(client) {
self.focus_up()
}
}
/// Insert the given client to the current [Stack] in a default [Position].
pub fn insert(&mut self, client: C) {
self.insert_at(Position::default(), client)
}
/// Insert the given client to the current [Stack] at the requested [Position].
/// If the client is already present somewhere in the [StackSet] the stack_set is unmodified.
pub fn insert_at(&mut self, pos: Position, client: C) {
if self.contains(&client) {
return;
}
self.modify(|current_stack| match current_stack {
Some(mut s) => {
s.insert_at(pos, client);
Some(s)
}
None => Some(stack!(client)),
})
}
pub(crate) fn float_unchecked<R: RelativeTo>(&mut self, client: C, r: R) {
let screen = self.screen_for_client(&client).expect("client to be known");
let r = r.relative_to(&screen.r);
self.floating.insert(client, r);
}
/// Clear the floating status of a client, returning its previous preferred
/// screen position if the client was known, otherwise `None`.
pub fn sink(&mut self, client: &C) -> Option<Rect> {
self.floating
.remove(client)
.map(|rr| rr.applied_to(&self.screens.focus.r))
}
/// Check whether a given tag currently has any floating windows present.
///
/// Returns false if the tag given is unknown to this StackSet.
pub fn has_floating_windows(&self, tag: impl AsRef<str>) -> bool {
self.workspace(tag.as_ref())
.map(|w| w.clients().any(|id| self.floating.contains_key(id)))
.unwrap_or(false)
}
/// Delete a client from this [StackSet].
pub fn remove_client(&mut self, client: &C) -> Option<C> {
self.sink(client); // Clear any floating information we might have
self.workspaces_mut()
.map(|w| w.remove(client))
.find(|opt| opt.is_some())
.flatten()
}
/// Remove the currently focused client from this stack if there is one.
///
/// The client is returned to the caller as `Some(C)` if there was one.
pub fn remove_focused(&mut self) -> Option<C> {
let client = self.current_client()?.clone();
self.remove_client(&client)
}
/// Delete the currently focused client from this stack if there is one.
///
/// The following diff will send a kill client message to this client on
/// refresh.
pub fn kill_focused(&mut self) {
if let Some(client) = self.remove_focused() {
self.killed_clients.push(client);
}
}
/// Move the focused client of the current [Workspace] to the focused position
/// of the workspace matching the provided `tag`.
pub fn move_focused_to_tag(&mut self, tag: impl AsRef<str>) {
let tag = tag.as_ref();
if self.current_tag() == tag || !self.contains_tag(tag) {
return;
}
let c = match self.screens.focus.workspace.remove_focused() {
None => return,
Some(c) => c,
};
self.insert_as_focus_for(tag, c)
}
/// Move the given client to the focused position of the [Workspace] matching
/// the provided `tag`. If the client is already on the target workspace it is
/// moved to the focused position.
pub fn move_client_to_tag(&mut self, client: &C, tag: impl AsRef<str>) {
let tag = tag.as_ref();
if !self.contains_tag(tag) {
return;
}
// Not calling self.remove_client as that will also sink the client if it
// was floating
let maybe_removed = self
.workspaces_mut()
.map(|w| w.remove(client))
.find(|opt| opt.is_some())
.flatten();
let c = match maybe_removed {
None => return,
Some(c) => c,
};
self.insert_as_focus_for(tag, c)
}
/// Move the given client to the focused position of the current [Workspace].
/// If the client is already on the target workspace it is moved to the focused position.
pub fn move_client_to_current_tag(&mut self, client: &C) {
self.move_client_to_tag(client, self.screens.focus.workspace.tag.clone());
}
/// Insert a client as the current focus for the given tag.
///
/// NOTE: This will silently fail if the tag is not in the StackSet which
/// is why the method is not in the public API
pub(crate) fn insert_as_focus_for(&mut self, tag: &str, c: C) {
self.modify_workspace(tag, |w| {
w.stack = Some(match take(&mut w.stack) {
None => stack!(c),
Some(mut s) => {
s.insert_at(Position::Focus, c);
s
}
});
});
}
/// Is the given tag present in the [StackSet]?
pub fn contains_tag(&self, tag: &str) -> bool {
self.workspaces().any(|w| w.tag == tag)
}
/// All [Workspace] tags in this [StackSet] order by their id that have not been
/// marked as being invisible.
pub fn ordered_tags(&self) -> Vec<String> {
let mut indexed: Vec<_> = self
.workspaces()
.map(|w| (w.id, w.tag.clone()))
.filter(|(_, t)| !self.invisible_tags.contains(t))
.collect();
indexed.sort_by_key(|(id, _)| *id);
indexed.into_iter().map(|(_, tag)| tag).collect()
}
/// All Workspaces in this [StackSet] order by their id that have not been
/// marked as being invisible.
pub fn ordered_workspaces(&self) -> impl Iterator<Item = &Workspace<C>> {
let mut wss: Vec<_> = self
.workspaces()
.filter(|w| !self.invisible_tags.contains(&w.tag))
.collect();
wss.sort_by_key(|w| w.id());
wss.into_iter()
}
/// Find the tag of the [Workspace] currently displayed on [Screen] `index`.
///
/// Returns [None] if the index is out of bounds
pub fn tag_for_screen(&self, index: usize) -> Option<&str> {
self.screens()
.find(|s| s.index == index)
.map(|s| s.workspace.tag.as_str())
}
/// Find the tag of the [Workspace] containing a given client.
/// Returns Some(tag) if the client is known otherwise None.
pub fn tag_for_client(&self, client: &C) -> Option<&str> {
self.workspaces()
.find(|w| {
w.stack
.as_ref()
.map(|s| s.iter().any(|elem| elem == client))
.unwrap_or(false)
})
.map(|w| w.tag.as_str())
}
/// If the given client is currently visible on a screen return a
/// reference to that screen, otherwise None.
pub fn screen_for_client(&self, client: &C) -> Option<&Screen<C>> {
self.screens.iter().find(|s| s.workspace.contains(client))
}
/// Find the tag of the [Workspace] with the given NetWmDesktop ID.
pub fn tag_for_workspace_id(&self, id: usize) -> Option<String> {
self.workspaces()
.find(|w| w.id == id)
.map(|w| w.tag.clone())
}
/// Returns `true` if the [StackSet] contains an element equal to the given value.
pub fn contains(&self, client: &C) -> bool {
self.clients().any(|c| c == client)
}
/// Extract a reference to the focused element of the current [Stack]
pub fn current_client(&self) -> Option<&C> {
self.screens
.focus
.workspace
.stack
.as_ref()
.map(|s| &s.focus)
}
/// An immutable reference to the currently focused [Screen]
pub fn current_screen(&self) -> &Screen<C> {
&self.screens.focus
}
/// An immutable reference to the current [Workspace]
pub fn current_workspace(&self) -> &Workspace<C> {
&self.screens.focus.workspace
}
/// A mutable reference to the current [Workspace]
pub fn current_workspace_mut(&mut self) -> &mut Workspace<C> {
&mut self.screens.focus.workspace
}
/// An immutable reference to the current [Stack] if there is one
pub fn current_stack(&self) -> Option<&Stack<C>> {
self.screens.focus.workspace.stack.as_ref()
}
/// The `tag` of the current [Workspace]
pub fn current_tag(&self) -> &str {
&self.screens.focus.workspace.tag
}
/// Add a new [Workspace] to this [StackSet].
///
/// The id assigned to this workspace will be max(workspace ids) + 1.
///
/// # Errors
/// This function will error with `NonUniqueTags` if the given tag is already present.
pub fn add_workspace<T>(&mut self, tag: T, layouts: LayoutStack) -> Result<()>
where
T: Into<String>,
{
let tag = tag.into();
if self.contains_tag(&tag) {
return Err(Error::NonUniqueTags { tags: vec![tag] });
}
let id = self
.workspaces()
.map(|w| w.id)
.max()
.expect("at least one workspace")
+ 1;
let ws = Workspace::new(id, tag, layouts, None);
self.hidden.push_front(ws);
Ok(())
}
/// Add a new invisible [Workspace] to this [StackSet].
///
/// It will not be possible to focus this workspace on a screen but its
/// state will be tracked and clients can be placed on it.
/// The id assigned to this workspace will be max(workspace ids) + 1.
///
/// # Errors
/// This function will error with `NonUniqueTags` if the given tag is already present.
pub fn add_invisible_workspace<T>(&mut self, tag: T) -> Result<()>
where
T: Into<String>,
{
let tag = tag.into();
self.add_workspace(tag.clone(), LayoutStack::default())?;
self.invisible_tags.push(tag);
Ok(())
}
/// A reference to the [Workspace] with a tag of `tag` if there is one
pub fn workspace(&self, tag: &str) -> Option<&Workspace<C>> {
self.workspaces().find(|w| w.tag == tag)
}
/// A mutable reference to the [Workspace] with a tag of `tag` if there is one
pub fn workspace_mut(&mut self, tag: &str) -> Option<&mut Workspace<C>> {
self.workspaces_mut().find(|w| w.tag == tag)
}
/// Switch to the next available [Layout][crate::core::layout::Layout] on the focused [Workspace]
pub fn next_layout(&mut self) {
self.screens.focus.workspace.next_layout()
}
/// Switch to the previous available [Layout][crate::core::layout::Layout] on the focused [Workspace]
pub fn previous_layout(&mut self) {
self.screens.focus.workspace.previous_layout()
}
/// Move focus to the next [Screen]
pub fn next_screen(&mut self) {
if self.screens.len() == 1 {
return;
}
self.update_previous_tag(self.screens.focus.workspace.tag.clone());
self.screens.focus_down();
}
/// Move focus to the previous [Screen]
pub fn previous_screen(&mut self) {
if self.screens.len() == 1 {
return;
}
self.update_previous_tag(self.screens.focus.workspace.tag.clone());
self.screens.focus_up();
}
/// Move focus to the next [Workspace] tag ordered by their id (creation order)
pub fn next_tag(&mut self) {
let tags = self.ordered_tags();
if tags.is_empty() {
return;
}
let i = tags
.iter()
.position(|a| a == self.current_tag())
.expect("current tag is a known tag");
let new_i = (i + 1) % tags.len();
let new_tag = &tags[new_i];
self.focus_tag(new_tag);
}
/// Move focus to previous [Workspace] tag ordered by their id (creation order)
pub fn previous_tag(&mut self) {
let tags = self.ordered_tags();
if tags.is_empty() {
return;
}
let i = tags
.iter()
.position(|a| a == self.current_tag())
.expect("current tag is a known tag");
let new_i = if i == 0 { tags.len() - 1 } else { i - 1 };
let new_tag = &tags[new_i];
self.focus_tag(new_tag);
}
/// Drag the focused workspace onto the next [Screen], holding focus
pub fn drag_workspace_forward(&mut self) {
if self.screens.len() == 1 {
return;
}
// We stash the previous tag so that we can restore it after we've
// cycled the screens and pulled over tag we were on before.
let true_previous_tag = self.previous_tag.clone();
self.next_screen();
self.try_swap_focused_workspace_with_tag(&self.previous_tag.clone());
self.previous_tag = true_previous_tag;
}
/// Drag the focused workspace onto the previous [Screen], holding focus
pub fn drag_workspace_backward(&mut self) {
if self.screens.len() == 1 {
return;
}
// We stash the previous tag so that we can restore it after we've
// cycled the screens and pulled over tag we were on before.
let true_previous_tag = self.previous_tag.clone();
self.previous_screen();
self.try_swap_focused_workspace_with_tag(&self.previous_tag.clone());
self.previous_tag = true_previous_tag;
}
/// If the current [Stack] is [None], return `default` otherwise
/// apply the function to it to generate a value
pub fn with<T, F>(&self, default: T, f: F) -> T
where
F: Fn(&Stack<C>) -> T,
{
self.current_stack().map(f).unwrap_or_else(|| default)
}
/// Apply a function to modify the current [Stack] if there is one
/// or compute and inject a default value if it is currently [None]
pub fn modify<F>(&mut self, f: F)
where
F: FnOnce(Option<Stack<C>>) -> Option<Stack<C>>,
{
self.screens.focus.workspace.stack = f(take(&mut self.screens.focus.workspace.stack));
}
/// Apply a function to modify the current [Stack] if it is non-empty
/// without allowing for emptying it entirely.
pub fn modify_occupied<F>(&mut self, f: F)
where
F: FnOnce(Stack<C>) -> Stack<C>,
{
self.modify(|s| s.map(f))
}
fn modify_workspace<F>(&mut self, tag: &str, f: F)
where
F: FnOnce(&mut Workspace<C>),
{
self.workspaces_mut().find(|w| w.tag == tag).map(f);
}
/// Iterate over each [Screen] in this [StackSet] in an arbitrary order.
pub fn screens(&self) -> impl Iterator<Item = &Screen<C>> {
self.screens.iter()
}
/// Mutably iterate over each [Screen] in this [StackSet] in an arbitrary order.
pub fn screens_mut(&mut self) -> impl Iterator<Item = &mut Screen<C>> {
self.screens.iter_mut()
}
/// Iterate over each [Workspace] in this [StackSet] in an arbitrary order.
pub fn workspaces(&self) -> impl Iterator<Item = &Workspace<C>> {
self.screens
.iter()
.map(|s| &s.workspace)
.chain(self.hidden.iter())
}
/// Iterate over each non-hidden [Workspace] in this [StackSet] in an arbitrary order.
pub fn non_hidden_workspaces(&self) -> impl Iterator<Item = &Workspace<C>> {
self.workspaces()
.filter(|w| !self.invisible_tags.contains(&w.tag))
}
/// Mutably iterate over each [Workspace] in this [StackSet] in an arbitrary order.
pub fn workspaces_mut(&mut self) -> impl Iterator<Item = &mut Workspace<C>> {
self.screens
.iter_mut()
.map(|s| &mut s.workspace)
.chain(self.hidden.iter_mut())
}
/// Iterate over the [Workspace] currently displayed on a screen in an arbitrary order.
pub fn on_screen_workspaces(&self) -> impl Iterator<Item = &Workspace<C>> {
self.screens.iter().map(|s| &s.workspace)
}
/// Iterate over the currently hidden [Workspace] in this [StackSet] in an arbitrary order.
pub fn hidden_workspaces(&self) -> impl Iterator<Item = &Workspace<C>> {
self.hidden.iter()
}
/// Iterate over the currently hidden [Workspace] in this [StackSet] in an arbitrary order.
pub fn hidden_workspaces_mut(&mut self) -> impl Iterator<Item = &mut Workspace<C>> {
self.hidden.iter_mut()
}
/// Iterate over each client in this [StackSet] in an arbitrary order.
pub fn clients(&self) -> impl Iterator<Item = &C> {
self.workspaces().flat_map(|w| w.clients())
}
/// Iterate over clients present in on-screen Workspaces.
///
/// *NOTE*: this does _not_ mean that every client returned by this iterator
/// is visible on the screen: only that it is currently assigned to a workspace
/// that is displayed on a screen.
pub fn on_screen_workspace_clients(&self) -> impl Iterator<Item = &C> {
self.on_screen_workspaces().flat_map(|w| w.clients())
}
/// Iterate over clients from workspaces not currently mapped to a screen.
pub fn hidden_workspace_clients(&self) -> impl Iterator<Item = &C> {
self.hidden_workspaces().flat_map(|w| w.clients())
}
}
#[cfg(test)]
impl StackSet<Xid> {
/// This is a test implementation that runs the `State::visible_client_positions`
/// logic using a stub XConn and no layout hook.
pub(crate) fn visible_client_positions(&self) -> Vec<(Xid, Rect)> {
let mut s = crate::core::State {
client_set: self.clone(),
config: Default::default(),
extensions: anymap::AnyMap::new(),
root: Xid(0),
mapped: Default::default(),
pending_unmap: Default::default(),
current_event: None,
diff: Default::default(),
};
s.visible_client_positions(&crate::x::StubXConn)
}
/// This is a test implementation that runs the `State::position_and_snapshot`
/// logic using a stub XConn and no layout hook.
pub(crate) fn position_and_snapshot(&mut self) -> Snapshot<Xid> {
let positions = self.visible_client_positions();
self.snapshot(positions)
}
}
impl StackSet<Xid> {
/// Record a known client as floating, giving its preferred screen position.
///
/// # Errors
/// This method with return [Error::UnknownClient] if the given client is
/// not already managed in this stack_set.
///
/// This method with return [Error::ClientIsNotVisible] if the given client is
/// not currently mapped to a screen. This is required to determine the correct
/// relative positioning for the floating client as is it is moved between
/// screens.
pub fn float(&mut self, client: Xid, r: Rect) -> Result<()> {
if !self.contains(&client) {
return Err(Error::UnknownClient(client));
}
if self.screen_for_client(&client).is_none() {
return Err(Error::ClientIsNotVisible(client));
}
self.float_unchecked(client, r);
Ok(())
}
pub(crate) fn update_screens(&mut self, rects: Vec<Rect>) -> Result<()> {
let n_old = self.screens.len();
let n_new = rects.len();
if n_new == 0 {
return Err(Error::NoScreens);
}
match n_new.cmp(&n_old) {
// Just a change in dimensions
Ordering::Equal => (),
// We have more screens now: pull in hidden workspaces to fill them
// If we run out of workspaces we backfill using generated defaults
Ordering::Greater => {
let padding = self.take_from_hidden(n_new - n_old);
for (n, w) in padding.into_iter().enumerate() {
self.screens.insert_at(
Position::Tail,
Screen {
workspace: w,
index: n_old + n,
r: Rect::default(),
},
);
}
}
// We have fewer screens now: focus moves to the first screen and
// we drop from the back of the stack
Ordering::Less => {
let mut raw = take(&mut self.screens).flatten();
let removed = raw.split_off(n_new);
self.hidden.extend(removed.into_iter().map(|s| s.workspace));
self.screens = Stack::from_iter_unchecked(raw);
}
}
// self.screens.len() is now correct so update the screen dimensions
for (s, r) in self.screens.iter_mut().zip(rects) {
s.r = r;
}
Ok(())
}
// This is a little fiddly...
// Rather than hard erroring if we end up with new screens being detected that
// push us over the number of available workspaces, we pad the workspace set
// with ones we generate with default values. In doing this we need to make sure
// that any _invisible_ workspaces are kept to one side so that they do not end
// up focused on a screen by mistake.
fn take_from_hidden(&mut self, n: usize) -> Vec<Workspace<Xid>> {
let next_id = self.workspaces().map(|w| w.id).max().unwrap_or(0) + 1;
let mut tmp = Vec::with_capacity(self.hidden.len());
let mut hidden = VecDeque::new();
// Filter out any hidden tags first
for w in take(&mut self.hidden) {
if self.invisible_tags.contains(&w.tag) {
hidden.push_front(w);
} else {
tmp.push(w);
}
}
// Sort so that we populate the new screens with workspaces in order of ID.
// Without this we are basing things off of the order we ended up with after
// whatever workspace focus changes the user has made while we are running.
tmp.sort_by_key(|w| w.id);
// Pad the remaining workspace count with empty default workspaces if we are
// below the number we need.
if tmp.len() < n {
for m in 0..(n - tmp.len()) {
tmp.push(Workspace::new_default(next_id + m));
}
} else {
let extra = tmp.split_off(n);
hidden.extend(extra);
}
self.hidden = hidden;
tmp
}
}
impl<C> StackSet<C>
where
C: Copy + Clone + PartialEq + Eq + Hash,
{
pub(crate) fn snapshot(&mut self, positions: Vec<(C, Rect)>) -> Snapshot<C> {
let visible = self
.screens
.unravel()
.skip(1) // skip the focused element
.map(ScreenState::from)
.collect();
Snapshot {
focused_client: self.current_client().copied(),
focused: ScreenState::from(&self.screens.focus),
visible,
positions,
hidden_clients: self.hidden_workspace_clients().copied().collect(),
killed_clients: take(&mut self.killed_clients),
}
}
}
macro_rules! defer_to_current_stack {
($(
$(#[$doc_str:meta])*
$method:ident
),+) => {
impl<C> StackSet<C>
where
C: Clone + PartialEq + Eq + Hash
{
$(
$(#[$doc_str])*
pub fn $method(&mut self) {
if let Some(ref mut stack) = self.screens.focus.workspace.stack {
stack.$method();
}
}
)+
}
}
}
defer_to_current_stack!(
/// Move focus from the current element up the [Stack], wrapping to
/// the bottom if focus is already at the top.
/// This is a no-op if the current stack is empty.
focus_up,
/// Move focus from the current element down the [Stack], wrapping to
/// the top if focus is already at the bottom.
/// This is a no-op if the current stack is empty.
focus_down,
/// Swap the position of the focused element with one above it.
/// The currently focused element is maintained by this operation.
/// This is a no-op if the current stack is empty.
swap_up,
/// Swap the position of the focused element with one below it.
/// The currently focused element is maintained by this operation.
/// This is a no-op if the current stack is empty.
swap_down,
/// Rotate all elements of the stack forward, wrapping from top to bottom.
/// The currently focused position in the stack is maintained by this operation.
/// This is a no-op if the current stack is empty.
rotate_up,
/// Rotate all elements of the stack back, wrapping from bottom to top.
/// The currently focused position in the stack is maintained by this operation.
/// This is a no-op if the current stack is empty.
rotate_down,
/// Rotate the Stack until the current focused element is in the head position.
/// This is a no-op if the current stack is empty.
rotate_focus_to_head,
/// Move focus to the element in the head position.
/// This is a no-op if the current stack is empty.
focus_head,
/// Swap the current head element with the focused element in the
/// stack order. Focus stays with the original focused element.
/// This is a no-op if the current stack is empty.
swap_focus_and_head
);
#[cfg(test)]
pub mod tests {
use super::*;
use simple_test_case::test_case;
fn _test_stack_set<C>(n_tags: usize, n_screens: usize) -> StackSet<C>
where
C: Copy + Clone + PartialEq + Eq + Hash,
{
let tags = (1..=n_tags).map(|n| n.to_string());
let screens: Vec<Rect> = (0..(n_screens as u32))
.map(|k| Rect::new(k * 1000, k * 2000, 1000, 2000))
.collect();
StackSet::try_new(LayoutStack::default(), tags, screens).unwrap()
}
pub fn test_stack_set(n_tags: usize, n_screens: usize) -> StackSet<u8> {
_test_stack_set(n_tags, n_screens)
}
pub fn test_xid_stack_set(n_tags: usize, n_screens: usize) -> StackSet<Xid> {
_test_stack_set(n_tags, n_screens)
}
pub fn test_stack_set_with_stacks<C>(stacks: Vec<Option<Stack<C>>>, n: usize) -> StackSet<C>
where
C: Copy + Clone + PartialEq + Eq + Hash,
{
let workspaces: Vec<Workspace<C>> = stacks
.into_iter()
.enumerate()
.map(|(i, s)| Workspace::new(i, (i + 1).to_string(), LayoutStack::default(), s))
.collect();
match StackSet::try_new_concrete(
workspaces,
(0..(n as u32))
.map(|k| Rect::new(k * 1000, k * 2000, 1000, 2000))
.collect(),
HashMap::new(),
) {
Ok(s) => s,
Err(e) => panic!("{e}"),
}
}
#[test_case("1", &["1", "2"]; "current focused workspace")]
#[test_case("2", &["1", "2"]; "visible on other screen")]
#[test_case("3", &["3", "2"]; "currently hidden")]
#[test]
fn focus_tag_sets_correct_visible_workspaces(target: &str, vis: &[&str]) {
let mut s = test_stack_set(5, 2);