korgwm/Tag.pm

filter: Statementstmt 88.8% 16 / 18 Branchbran n/a Conditioncond n/a MC/DCmcdc n/a Subroutinesub 100% 6 / 6 Podpod n/a totaltotal 91.6% 22 / 24 CC 1 SCAR 0.5
CC
1
Cov
89%
BEGIN
1.3
BEGIN
1.0
BEGIN
1.0
CRAP 1.0
« korgwm/Config.pm ↑ Summary korgwm/Window.pm »
1 #!/usr/bin/perl
2 # made by: KorG
3 # vim: cc=119 et sw=4 ts=4 :
4 package X11::korgwm::Tag;
5 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use feature 'signatures';
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 1 ▶use List::Util qw( first );
Subroutine BEGIN: called CC=1 SCAR=0.0
10 1 ▶use Scalar::Util qw( weaken );
Subroutine BEGIN: called CC=1 SCAR=0.0
11 0 ▶use X11::XCB ':all';
Subroutine BEGIN: called CC=1 SCAR=2.6
12 use X11::korgwm::Common;
13 use X11::korgwm::Layout;
14  
15 sub new($class, $screen) {
16 weaken($screen);
17 bless {
18 idx => undef,
19 screen => $screen,
20 layout => undef,
21 max_window => undef,
22 focus_prev => [],
23 windows_float => [],
24 windows_tiled => [],
25 windows_appended => [],
26 }, $class;
27 }
28  
29 sub windows($self) {
30 grep defined, $self->{max_window}, @{ $self->{windows_float} }, @{ $self->{windows_tiled} };
31 }
32  
33 sub destroy($self, $new_screen) {
34 # Move windows to a $new_screen
35 my $old_screen = $self->{screen};
36 my $old_screen_idx = $old_screen->{idx};
37 my $old_tag_idx = $self->{idx};
38 my $new_tag = $new_screen->{tags}->[$old_tag_idx] || $new_screen->{tags}->[0];
39  
40 for my $win ($self->windows()) {
41 $new_tag->win_add($win);
42 $self->win_remove($win);
43  
44 $win->floating_move_screen($old_screen, $new_screen);
45 $win->hide() unless $new_tag == $new_screen->current_tag();
46  
47 # Save preferred screen to be able to restore window position
48 $win->{pref_position}->[@screens] = [$old_screen_idx, $old_tag_idx];
49 }
50  
51 %{ $self } = ();
52 }
53  
54 sub hide($self) {
55 # Remove layout and hide on panel if we're hiding an empty tag
56 unless ($self->first_window(1)) {
57 $self->{layout} = undef;
58 $self->{screen}->{panel}->ws_set_visible($self->{idx} + 1, 0) if $cfg->{hide_empty_tags};
59 }
60  
61 # Hide all windows and drop focus
62 $_->hide() for $self->windows();
63 $X->flush();
64 }
65  
66 # Makes the $self tag visible
67 # Options:
68 # - noselect => do not call select() to avoid warp_pointer()
69 sub show($self, %opts) {
70 # Redefine layout if needed
71 $self->{layout} //= X11::korgwm::Layout->new(
72 func => $self->{layout_func},
73 reverse_windows => $self->{reverse_windows},
74 );
75 DEBUG8 and carp "tag->show($self, @{[%opts]})";
76  
77 # Map all windows from the tag
78 my ($w, $h, $x, $y) = @{ $self->{screen} }{qw( w h x y )};
79 if (defined $self->{max_window}) {
80 # If we have maximized window, just place it over the screen
81 # I believe there is no need to process focus here, right?
82 $self->{max_window}->resize_and_move(@{ $self->{screen} }{qw( x y w h )}, 0);
83 $self->{max_window}->show();
84 $_->show() for $self->{max_window}->transients();
85 } else {
86 $self->{screen}->{panel}->ws_set_visible($self->{idx} + 1, 1) if $cfg->{hide_empty_tags};
87  
88 # Firstly move
89 $h -= $cfg->{panel_height};
90 $y += $cfg->{panel_height};
91 $self->{layout}->arrange_windows($self->{windows_tiled}, $w, $h, $x, $y);
92  
93 # Only then -- show
94 for my $win (reverse grep defined,
95 @{ $self->{screen}->{always_on} },
96 @{ $self->{windows_float} },
97 @{ $self->{windows_tiled} }) {
98 $win->show();
99 $win->reset_border() if $win != ($self->{screen}->{focus} // 0);
100 }
101  
102 # XXX There should be no need to restack windows here.
103 # The only case: if the window stack order was changed on another tag. Fix if any bugs found
104  
105 $X->flush();
106 }
107  
108 # Handle focus change
109 $focus->{screen} = $self->{screen};
110 my $focus_win = $self->{screen}->{focus};
111 if (defined $focus_win and exists $focus_win->{on_tags}->{$self}) {
112 # If this window is focused on this tag, just give it a focus
113 $focus_win->focus();
114 } elsif (my $win = $self->{focus} || focus_prev_get($self->{focus_prev}) || $self->first_window()) {
115 # Try to focus previously focused window (or any window on the tag)
116 if ($opts{noselect}) {
117 # Looks like for the situations, when we do not want to warp any case (see Screen.pm /panel)
118 $win->focus();
119 } else {
120 $win->select(bypass_single_window_warp => 1);
121 }
122 } else {
123 # If focused window is hidden (it's on the same screen and another tag) we want to drop focus
124 $focus->{screen}->focus();
125 }
126  
127 $X->flush();
128 }
129  
130 sub win_add($self, $win) {
131 $win->{on_tags}->{$self} = $self;
132  
133 if ($win->urgency_get()) {
134 $self->{urgent_windows}->{$win} = undef;
135 $self->{screen}->{panel}->ws_set_urgent($self->{idx} + 1, 1);
136 }
137 $self->{screen}->{panel}->ws_set_visible($self->{idx} + 1);
138  
139 $self->{max_window} = $win if $win->{maximized};
140  
141 my $arr = $win->{floating} ? $self->{windows_float} : $self->{windows_tiled};
142 # For windows calling map multiple times
143 unshift @{ $arr }, $win unless first { $_ == $win } @{ $arr };
144 }
145  
146 # XXX this function is also used for appended windows
147 sub win_remove($self, $win, $norefresh = undef) {
148 delete $win->{on_tags}->{$self};
149  
150 # Process urgency
151 delete $self->{urgent_windows}->{$win};
152 $self->{screen}->{panel}->ws_set_urgent($self->{idx} + 1, 0) unless keys %{ $self->{urgent_windows} };
153  
154 $self->{max_window} = undef if $win == ($self->{max_window} // 0);
155  
156 for my $arr (map { $self->{$_} } qw( windows_float windows_tiled )) {
157 @{ $arr } = grep { $win != $_ } @{ $arr };
158 }
159  
160 # Remove title when removing focused window
161 $self->{screen}->{panel}->title() if $win == $focus->{window};
162  
163 # Remove layout when removing the last window
164 $self->{layout} = undef unless $self->first_window(1);
165  
166 # Update panel if tag becomes empty
167 $self->{screen}->{panel}->ws_set_visible($self->{idx} + 1, 0)
168 if $cfg->{hide_empty_tags} and not $self->first_window();
169  
170 # Remove the window from tag->focus_prev
171 focus_prev_remove($win, $self->{focus_prev});
172  
173 # Clean preferred focus for this tag if needed
174 $self->{focus} = focus_prev_get($self->{focus_prev}) if $win == $self->{focus};
175  
176 # If this tag is visible, call screen refresh
177 $self->{screen}->refresh() if not $norefresh and $self == $self->{screen}->current_tag();
178 }
179  
180 sub win_float($self, $win, $floating=undef) {
181 # Move $win to appropriate array
182 my ($arr_from, $arr_to) = map { $self->{$_} } "windows_float", "windows_tiled";
183 ($arr_from, $arr_to) = ($arr_to, $arr_from) if $floating;
184  
185 @{ $arr_from } = grep { $win != $_ } @{ $arr_from };
186 unshift @{ $arr_to }, $win;
187 }
188  
189 # Select some window, if any
190 sub first_window($self, $only_tag = undef) {
191 return $self->{max_window} || $self->{windows_float}->[0] || $self->{windows_tiled}->[0] ||
192 ($only_tag ? () : $self->{screen}->{always_on}->[0]);
193 }
194  
195 # Select next window
196 sub next_window($self, $backward = undef) {
197 # There is no point in next window when user sees only the maximized one
198 return $self->{max_window} if defined $self->{max_window};
199  
200 # Look up the focused one
201 my $win_curr = $self->{screen}->{focus} or return;
202  
203 # Prepare list for search
204 my ($idx, $found) = $backward ? -1 : 0;
205 my @win_float = (@{ $self->{windows_float} }, @{ $self->{screen}->{always_on} });
206 my @win_tiled = @{ $self->{windows_tiled} };
207  
208 # If win_curr belongs to other tag, we need any window from the current one
209 $found = 1 unless $win_curr->{on_tags}->{$self} or $win_curr->{always_on};
210  
211 # Reverse them if searching backward
212 if ($backward) {
213 @win_float = reverse @win_float;
214 @win_tiled = reverse @win_tiled;
215 }
216  
217 # Do actual search
218 for my $win (grep defined, @win_float, @win_tiled, $win_float[$idx], $win_tiled[$idx]) {
219 return $win if $found;
220 $found = 1 if $win == $win_curr;
221 }
222  
223 # To avoid ''
224 undef;
225 }
226  
227 # Append windows from $other tag to $self
228 sub append($self, $other) {
229 # Obvious check
230 return if $self == $other;
231  
232 # Prevent adding tags with maximized windows
233 return if $other->{max_window};
234  
235 # Skip empty
236 return unless $other->first_window(1);
237  
238 for my $win (map { @{ $other->{$_} } } qw( windows_float windows_tiled )) {
239 # Add $win to $self manually, bypassing on_tags modification
240 my $arr = $win->{floating} ? $self->{windows_float} : $self->{windows_tiled};
241 unless (first { $_ == $win } @{ $arr }) {
242 unshift @{ $arr }, $win;
243  
244 # The window was really added, save it for later use
245 unshift @{ $self->{windows_appended} }, $win;
246  
247 # Save $self to window
248 $win->{also_tags}->{$self} = $self;
249 }
250 }
251  
252 $self->{screen}->{panel}->ws_add_append($other->{idx});
253 }
254  
255 # Remove all the windows from appends
256 sub drop_appends($self) {
257 return unless @{ $self->{windows_appended} };
258  
259 for my $win (@{ $self->{windows_appended} }) {
260 $self->win_remove($win, 1);
261 delete $win->{also_tags}->{$self};
262 $win->hide();
263 }
264  
265 $self->{screen}->{panel}->ws_drop_appends();
266  
267 $self->{windows_appended} = [];
268 }
269  
270 # Set layout function and windows order for tag; layout should be re-created using show()
271 sub layout_set($self, $func_name, $reverse_windows=1) {
272 return unless exists $X11::korgwm::Layout::layouts{ $func_name };
273  
274 $self->{layout_func} = $func_name;
275 $self->{reverse_windows} = $reverse_windows;
276 $self->{layout} = undef;
277  
278 return 1; # Executor relies on the rc to call show()
279 }
280  
281 1;
« korgwm/Config.pm ↑ Summary korgwm/Window.pm »