| 1 | #!/usr/bin/perl | ||
| 2 | # made by: KorG | ||
| 3 | # vim: cc=119 et sw=4 ts=4 : | ||
| 4 | package X11::korgwm::Panel; | ||
| 5 | use strict; | ||
| 6 | use warnings; | ||
| 7 | use feature 'signatures'; | ||
| 8 | |||
| 9 | use Gtk3; | ||
| 10 | use Glib::Object::Introspection; | ||
| 11 | use AnyEvent; | ||
| 12 | use X11::korgwm::Common; | ||
| 13 | |||
| 14 | # Initialize GTK | ||
| 15 | gtk_init(); | ||
| 16 | |||
| 17 | # Prepare internal variables | ||
| 18 | my ($ready, @ws_names); | ||
| 19 | sub _init { | ||
| 20 | Glib::Object::Introspection->setup(basename => "GdkX11", version => "3.0", package => "Gtk3::Gdk"); | ||
| 21 | @ws_names = @{ $cfg->{ws_names} }; | ||
| 22 | $ready = 1; | ||
| 23 | } | ||
| 24 | |||
| 25 | # Set title (central label) text | ||
| 26 | sub title($self, $title = "") { | ||
| 27 | if (length($title) > $cfg->{title_max_len}) { | ||
| 28 | $title = substr $title, 0, $cfg->{title_max_len}; | ||
| 29 | $title .= "..."; | ||
| 30 | } | ||
| 31 | $self->{title}->set_text($title); | ||
| 32 | } | ||
| 33 | |||
| 34 | # Add/remove CSS class for a workspace | ||
| 35 | sub _ws_class_action($self, $action, $id, $class) { | ||
| 36 | my $ws = $self->{ws}->[$id - 1]; | ||
| 37 | $ws->{label}->get_style_context->$action($class); | ||
| 38 | } | ||
| 39 | sub ws_class_add($self, $id, $class) { $self->_ws_class_action('add_class', $id, $class) } | ||
| 40 | sub ws_class_remove($self, $id, $class) { $self->_ws_class_action('remove_class', $id, $class) } | ||
| 41 | |||
| 42 | # Set workspace visibility | ||
| 43 | sub ws_set_visible($self, $id, $new_visible = 1) { | ||
| 44 | my $meth = $new_visible ? "show" : "hide"; | ||
| 45 | my $ws = $self->{ws}->[$id - 1]; | ||
| 46 | |||
| 47 | # Prevent hiding of the last visible workspace | ||
| 48 | return if not $new_visible and $self->visbile_ws() <= 1; | ||
| 49 | |||
| 50 | $ws->{ebox}->$meth; | ||
| 51 | } | ||
| 52 | |||
| 53 | # Make certain workspace active | ||
| 54 | sub ws_set_active($self, $new_active) { | ||
| 55 | for my $ws (@{ $self->{ws} }) { | ||
| 56 | if ($ws->{active}) { | ||
| 57 | return if $ws->{id} == $new_active; | ||
| 58 | $ws->{active} = undef; | ||
| 59 | $self->ws_class_remove($ws->{id}, 'active'); | ||
| 60 | } | ||
| 61 | |||
| 62 | if ($ws->{id} == $new_active) { | ||
| 63 | $ws->{active} = 1; | ||
| 64 | $self->ws_class_add($ws->{id}, 'active'); | ||
| 65 | } | ||
| 66 | } | ||
| 67 | } | ||
| 68 | |||
| 69 | # Set workspace urgency | ||
| 70 | sub ws_set_urgent($self, $ws_id, $urgent = 1) { | ||
| 71 | my $ws = $self->{ws}->[$ws_id - 1]; | ||
| 72 | $self->_ws_class_action($urgent ? "add_class" : "remove_class", $ws->{id}, 'urgent'); | ||
| 73 | } | ||
| 74 | |||
| 75 | # Clean all appends | ||
| 76 | sub ws_drop_appends($self) { | ||
| 77 | $self->ws_class_remove($_->{id}, 'append') for @{ $self->{ws } }; | ||
| 78 | } | ||
| 79 | |||
| 80 | # Mark some ws as appended to current active. Ws should not be active or urgent | ||
| 81 | sub ws_add_append($self, $ws_id) { | ||
| 82 | $self->ws_class_add($ws_id + 1, 'append'); | ||
| 83 | } | ||
| 84 | |||
| 85 | # Create new workspace during initialization phase | ||
| 86 | sub ws_create($self, $title = "", $ws_cb = sub {1}) { | ||
| 87 | $self->{ws_num} = 0 unless defined $self->{ws_num}; | ||
| 88 | my $my_id = ++$self->{ws_num}; # closure | ||
| 89 | |||
| 90 | my $workspace = { id => $my_id }; | ||
| 91 | |||
| 92 | my $label = Gtk3::Label->new(); | ||
| 93 | $label->set_text($title); | ||
| 94 | $label->set_size_request($cfg->{panel_height}, $cfg->{panel_height}); | ||
| 95 | $label->set_yalign(0.9); | ||
| 96 | |||
| 97 | my $ebox = Gtk3::EventBox->new(); | ||
| 98 | $ebox->signal_connect('button-press-event', sub ($obj, $e) { | ||
| 99 | return unless $e->button == 1; | ||
| 100 | $ws_cb->($e->button, $my_id); | ||
| 101 | }); | ||
| 102 | $ebox->add($label); | ||
| 103 | |||
| 104 | $workspace->{label} = $label; | ||
| 105 | $workspace->{ebox} = $ebox; | ||
| 106 | $workspace; | ||
| 107 | } | ||
| 108 | |||
| 109 | # Hash for sanity of Panel:: modules (see Panel::Lang for example) | ||
| 110 | my %elements = (zhmylove =>\& Sergei::Zhmylev); | ||
| 111 | sub add_element($name, $watcher = undef) { | ||
| 112 | croak "Redefined Panel element $name" if defined $elements{$name}; | ||
| 113 | $elements{$name} = $watcher; | ||
| 114 | } | ||
| 115 | |||
| 116 | sub new($class, $panel_id, $panel_width, $panel_x, $panel_y, $ws_cb) { | ||
| 117 | my ($panel, $window, @workspaces, $label) = {}; | ||
| 118 | _init() unless $ready; | ||
| 119 | bless $panel, $class; | ||
| 120 | # Prepare main window | ||
| 121 | $window = Gtk3::Window->new('popup'); | ||
| 122 | $window->set_default_size($panel_width, $cfg->{panel_height}); | ||
| 123 | $window->move($panel_x, $panel_y); | ||
| 124 | $window->set_decorated(Gtk3::false); | ||
| 125 | $window->set_startup_id("korgwm-panel-$panel_id"); | ||
| 126 | |||
| 127 | # Create title label | ||
| 128 | $label = Gtk3::Label->new(); | ||
| 129 | $label->set_yalign(0.9); | ||
| 130 | $panel->{title} = $label; | ||
| 131 | |||
| 132 | # Save X coordinate for event handlers | ||
| 133 | $panel->{x} = $panel_x; | ||
| 134 | $panel->{y} = $panel_y; | ||
| 135 | $panel->{width} = $panel_width; | ||
| 136 | $panel->{height} = $cfg->{panel_height}; | ||
| 137 | |||
| 138 | # Create @workspaces | ||
| 139 | @workspaces = map { $panel->ws_create($_, $ws_cb) } @ws_names; | ||
| 140 | $panel->{ws} = \@workspaces; | ||
| 141 | $panel->ws_set_active(1); | ||
| 142 | |||
| 143 | # Render the panel | ||
| 144 | my $hdbar = Gtk3::Box->new(horizontal => 0); | ||
| 145 | $hdbar->pack_start($_->{ebox}, 0, 0, 0) for @workspaces; | ||
| 146 | $hdbar->set_center_widget($label); | ||
| 147 | |||
| 148 | # Add modules to the right-most side of the panel | ||
| 149 | for (reverse @{ $cfg->{panel_end} }) { | ||
| 150 | croak "Unknown element [$_] in cfg->{panel_end}" unless exists $elements{$_}; | ||
| 151 | my $el = Gtk3::Label->new(); | ||
| 152 | $el->set_yalign(0.9); | ||
| 153 | my $ebox = Gtk3::EventBox->new(); | ||
| 154 | $ebox->add($el); | ||
| 155 | $hdbar->pack_end($ebox, 0, 0, 0); | ||
| 156 | $panel->{$_} = $el; | ||
| 157 | $panel->{"_ebox:$_"} = $ebox; | ||
| 158 | $panel->{"_w:$_"} = $elements{$_}->($el, ebox => $ebox, panel => $panel) if defined $elements{$_}; | ||
| 159 | } | ||
| 160 | |||
| 161 | # Map window | ||
| 162 | $window->add($hdbar); | ||
| 163 | $window->show_all unless $cfg->{panel_hide}; | ||
| 164 | |||
| 165 | # Hide empty tags if needed | ||
| 166 | if ($cfg->{hide_empty_tags}) { | ||
| 167 | $panel->ws_set_visible($_, 0) for 2..@ws_names; | ||
| 168 | } | ||
| 169 | |||
| 170 | $panel->{window} = $window; | ||
| 171 | return $panel; | ||
| 172 | } | ||
| 173 | |||
| 174 | sub destroy($self) { | ||
| 175 | $self->{window}->destroy(); | ||
| 176 | $self->{calendar}->destroy() if $self->{calendar}; | ||
| 177 | %{ $self } = (); | ||
| 178 | } | ||
| 179 | |||
| 180 | sub iter { | ||
| 181 | Gtk3::main_iteration_do(0) while Gtk3::events_pending(); | ||
| 182 | } | ||
| 183 | |||
| 184 | sub visbile_ws($self) { | ||
| 185 | grep { $_->{ebox}->get_visible() } @{ $self->{ws} }; | ||
| 186 | } | ||
| 187 | |||
| 188 | # X11 id of the panel | ||
| 189 | sub xid($self) { | ||
| 190 | Gtk3::Gdk::X11Window::get_xid($self->{window}->get_window()); | ||
| 191 | } | ||
| 192 | |||
| 193 | 1; |