summaryrefslogtreecommitdiff
path: root/perl5
diff options
context:
space:
mode:
authorSean Whitton <spwhitton@spwhitton.name>2022-08-14 16:54:58 -0700
committerSean Whitton <spwhitton@spwhitton.name>2022-08-17 15:59:58 -0700
commit706bdd75455760e197cb29db27d39e8513548c67 (patch)
treef196c5b8a109fbe1cdbb27c88d9181585e3d521c /perl5
parentfaf9bffe02a1407990373453935705f5b8de378d (diff)
downloaddotfiles-706bdd75455760e197cb29db27d39e8513548c67.tar.gz
compact the workspace list, show & hide the workspace buttons
Diffstat (limited to 'perl5')
-rw-r--r--perl5/Local/Desktop.pm60
1 files changed, 53 insertions, 7 deletions
diff --git a/perl5/Local/Desktop.pm b/perl5/Local/Desktop.pm
index c5af6fc2..616ee89e 100644
--- a/perl5/Local/Desktop.pm
+++ b/perl5/Local/Desktop.pm
@@ -33,6 +33,7 @@ use List::Util "first";
our @EXPORT = qw(
$wmipc wmipc
fresh_workspace
+ compact_workspaces
select_wallpaper_files
ensure_resize_for_current_outputs
resize_for_current_outputs
@@ -44,6 +45,13 @@ sub wmipc { system "$wmipc -q " . join ", ", @_ }
my $output_re = qr/ ([0-9]+)x([0-9]+)\+([0-9]+)\+([0-9]+) /;
+my @all_workspaces = (
+ "1", "2", "3", "4", "5", "6",
+ "7", "8", "9", "10", "11:F1", "12:F2",
+ "13:F3", "14:F4", "15:F5", "16:F6", "17:F7", "18:F8",
+ "19:F9", "20:F10", "21:F11", "22:F12"
+);
+
=head fresh_workspace($send)
Switch to the next free workspace, if any. Return the name of that workspace,
@@ -53,12 +61,7 @@ the fresh workspace instead of switching focus there.
=cut
sub fresh_workspace {
- my %current_names = map +($_->{name}, undef),
- @{ decode_json `$wmipc -t get_workspaces` };
- my $next_free_workspace = first { not exists $current_names{$_} }
- "1", "2", "3", "4", "5", "6", "7", "8",
- "9", "10", "11:F1", "12:F2", "13:F3", "14:F4", "15:F5",
- "16:F6", "17:F7", "18:F8", "19:F9", "20:F10", "21:F11", "22:F12";
+ my $next_free_workspace = compact_workspaces(leave_gap => 1);
if ($next_free_workspace) {
my @cmds;
@@ -78,7 +81,48 @@ sub fresh_workspace {
wmipc @cmds;
}
$next_free_workspace
- }
+}
+
+=head compact_workspaces(%opts)
+
+Rename workspaces so as to remove gaps in the sequence of workspaces, and show
+or hide workspace buttons depending on whether there is >1 workspace.
+
+If C<$opts{leave_gap}>, ensure there is a gap of one workspace after the
+currently focused workspace and return the name of the gap workspace, or just
+return undef if there is no space for a gap.
+
+=cut
+
+sub compact_workspaces {
+ my %opts = @_;
+ my @workspaces = @{ decode_json `$wmipc -t get_workspaces` };
+ @workspaces < @all_workspaces or return;
+ my $buttons_cmd
+ = "bar bar-0 workspace_buttons " . (@workspaces > 1 ? "yes" : "no");
+ my ($current_workspace, $gap_workspace);
+ if ($opts{leave_gap}) {
+ $_->{focused} and $current_workspace = $_->{name}, last
+ for @workspaces
+ }
+
+ my ($i, @cmds);
+ while (my $next = shift @workspaces) {
+ my $workspace = $all_workspaces[$i++];
+ $opts{leave_gap}
+ and $next->{name} eq $current_workspace
+ and $gap_workspace = $all_workspaces[$i++];
+ next if $next->{name} eq $workspace;
+ my $pair = [$next->{name}, $workspace];
+ _wsnum($next->{name}) > _wsnum($workspace)
+ ? push @cmds, $pair
+ : unshift @cmds, $pair
+ }
+
+ wmipc $buttons_cmd, map "rename workspace $_->[0] to $_->[1]", @cmds;
+
+ $opts{leave_gap} and $gap_workspace
+}
=head select_wallpaper_files(@files)
@@ -188,4 +232,6 @@ sub _get_screen_size {
$canvas =~ /current ([0-9]+) x ([0-9]+)/;
}
+sub _wsnum { (split /:/, $_[0])[0] }
+
1;