The London Perl and Raku Workshop takes place on 26th Oct 2024. If your company depends on Perl, please consider sponsoring and/or attending.

NAME

AnyEvent::I3X::Workspace::OnDemand - An I3 workspace loader

VERSION

version 0.003

SYNOPSIS

    use AnyEvent::I3X::Workspace::OnDemand;

    my $i3 = AnyEvent::I3X::Workspace::OnDemand->new(
        debug => 0,
        layout_path => "$ENV{HOME}/.config/i3",
        workspaces => {
            foo => {
                layout => 'foo.json',
            },
            bar => {
                layout => 'bar.json',
                groups => {
                    foo => undef,
                    # Override the layout for group bar
                    bar => { layout => 'foo.json' },
                }
            },
            baz => {
                layout => 'baz.json',
                groups => {
                    all => undef,
                }
            }
        },
        groups => [
            qw(foo bar baz)
        ],
        swallows => [
            {
                cmd => 'kitty',
                match => {
                    class => '^kitty$',
                }
            },
            {
                # Start firefox on group bar
                cmd => 'firefox',
                on => {
                    group => 'bar',
                }
                match => {
                    window_role => '^browser$',
                }
            },
            {
                cmd => 'google-chrome',
                on => {
                    group => 'foo',
                }
                match => {
                    window_role => '^browser$',
                }
            }
        ],
    );

DESCRIPTION

Workspace switcher for i3.

This module listens to tick events which are named group:$name where the name corresponds to the workspace groups you have defined. When you send a tick event the current workspaces get renamed to $former_group:$workspace_name and leaves new workspaces for the ones you have defined.

In your .config/i3/config you can set something like this to switch groups:

  bindsym $mod+w mode "Activities"
  mode "Activities" {
    bindsym 0 exec i3-msg -t send_tick group:foo; mode default
    bindsym 9 exec i3-msg -t send_tick group:bar; mode default
    bindsym 8 exec i3-msg -t send_tick group:baz; mode default
    bindsym Return mode "default"
    bindsym Escape mode "default"
  }

METHODS

$self->subscribe

See "subscribe" in AnyEvent::I3

$self->get_i3

Get the AnyEvent::I3 instance

$self->command(@args)

Execute a command, the command can be in scalar or list context.

See also "command" in AnyEvent::I3.

$self->debug(1)

Enable or disable debug

$self->log($msg)

Print warns when debug is enabled

$self->on_tick($payload, $sub)

Subscribe to a tick event with $payload and perform the action. Your sub needs to support the following prototype:

    sub foo($self, $i3, $event) {
        print "Yay processed foo tick";
    }

    $self->on_tick('foo', \&foo);

$self->on_workspace($name, $type, $sub)

Subscribe to a workspace event for workspace $name of $type with $sub.

$type can be any of the following events from i3 plus any or *

    $i3->on_workspace(
      'www', 'init',
      sub {
        my $self  = shift;
        my $i3    = shift;
        my $event = shift;
        $self->append_layout($event->{current}{name}, '/path/to/layout.json');
      }
    );

$self->add_swallow($match, $cmd, $on)

Add a command that can be used to start after a layout has been appended

    $self->add_swallow({ class => '^kitty$' }, 'exec --no-startup-id kitty');

    # or only on this group
    $self->add_swallow(
      { class => '^kitty$' },
      'exec --no-startup-id kitty',
      { group => 'foo' }
    );

    # or workspace
    $self->add_swallow(
      { class => '^kitty$' },
      'exec --no-startup-id kitty',
      { workspace => 'foo' }
    );

$self->workspace($name, @cmds)

Runs commands on workspace by name. Without a command you only switch workspaces

AUTHOR

Wesley Schwengle <waterkip@cpan.org>

COPYRIGHT AND LICENSE

This software is Copyright (c) 2024 by Wesley Schwengle.

This is free software, licensed under:

  The (three-clause) BSD License