| 1 | 1 | ▶ | use Renard::Incunabula::Common::Setup; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | package Intertangle::API::Gtk3::Helper; | ||
| 3 | $Intertangle::API::Gtk3::Helper::VERSION = '0.006'; | ||
| 4 | # ABSTRACT: Collection of helper utilities for Gtk3 and Glib | ||
| 5 | |||
| 6 | |||
| 7 | 1 | ▶ | use Renard::Incunabula::Common::Types qw(Str); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 8 | 1 | ▶ | use Env qw($DISPLAY $INTERTANGLE_API_GTK3_USE_XINPUT2); |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use Class::Method::Modifiers; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | 1 | ▶ | use Gtk3; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 11 | use Intertangle::API::Gtk3::Helper::Gtk3::Adjustment; | ||
| 12 | use Function::Parameters; | ||
| 13 | |||
| 14 | our $LOADED = 0; | ||
| 15 | |||
| 16 | fun _scrolled_window_viewport_shim() { | ||
| 17 | # Note: The code below is marked as uncoverable because it only applies | ||
| 18 | # to a single version of GTK+ and thus is not part of the general | ||
| 19 | # coverage. The functionality that it adds is tested in other ways. | ||
| 20 | # uncoverable branch true | ||
| 21 | if( not Gtk3::CHECK_VERSION('3', '8', '0') ) { | ||
| 22 | # For versions of Gtk+ less than v3.8.0, we need to call | ||
| 23 | # `Gtk3::ScrolledWindow->add_with_viewport( ... )` so that the | ||
| 24 | # child widget gets placed in a viewport. | ||
| 25 | # | ||
| 26 | # Newer versions of Gtk+ automatically create the viewport when | ||
| 27 | # `Gtk3::ScrolledWindow->add( ... )` is called. | ||
| 28 | # | ||
| 29 | # See: | ||
| 30 | # - <https://developer.gnome.org/gtk3/3.6/GtkScrolledWindow.html> | ||
| 31 | # - <https://developer.gnome.org/gtk3/3.8/GtkScrolledWindow.html> | ||
| 32 | Class::Method::Modifiers::install_modifier | ||
| 33 | "Gtk3::ScrolledWindow", | ||
| 34 | around => add => fun(@) { | ||
| 35 | # uncoverable subroutine | ||
| 36 | my $orig = shift; # uncoverable statement | ||
| 37 | my $self = shift; # uncoverable statement | ||
| 38 | $self->add_with_viewport(@_); # uncoverable statement | ||
| 39 | }; # uncoverable statement | ||
| 40 | } | ||
| 41 | } | ||
| 42 | |||
| 43 | fun _can_set_theme() { | ||
| 44 | # uncoverable branch true | ||
| 45 | return $^O ne 'MSWin32' && Gtk3::CHECK_VERSION('3', '20', '1'); | ||
| 46 | } | ||
| 47 | |||
| 48 | fun _set_theme( (Str) $theme_name ) { | ||
| 49 | # uncoverable subroutine | ||
| 50 | # uncoverable branch true | ||
| 51 | return unless _can_set_theme; | ||
| 52 | |||
| 53 | my $adwaita = Gtk3::CssProvider::get_named('Adawaita')->to_string; # uncoverable statement | ||
| 54 | my $custom = Gtk3::CssProvider::get_named($theme_name)->to_string; # uncoverable statement | ||
| 55 | if( $adwaita ne $custom ) { # uncoverable statement | ||
| 56 | my $settings = Gtk3::Settings::get_default; # uncoverable statement | ||
| 57 | $settings->set_property('gtk-theme-name', $theme_name); # uncoverable statement | ||
| 58 | } else { # uncoverable statement | ||
| 59 | warn "Not able to find theme ${theme_name}.\n"; # uncoverable statement | ||
| 60 | } # uncoverable statement | ||
| 61 | } | ||
| 62 | |||
| 63 | |||
| 64 | fun _set_icon_theme( (Str) $icon_theme_name ) { | ||
| 65 | # uncoverable subroutine | ||
| 66 | # uncoverable branch true | ||
| 67 | return unless _can_set_theme; | ||
| 68 | |||
| 69 | my $i = Gtk3::IconTheme->new; # uncoverable statement | ||
| 70 | $i->set_custom_theme($icon_theme_name); # uncoverable statement | ||
| 71 | my $n = $i->choose_icon_for_scale(['gtk-open'], 16, 1, 'no-svg'); # uncoverable statement | ||
| 72 | my $expected_path = qr,/$icon_theme_name/.*\Qgtk-open.png\E$,; # uncoverable statement | ||
| 73 | if ( defined $n && $n->get_filename =~ $expected_path ) { # uncoverable statement | ||
| 74 | my $settings = Gtk3::Settings::get_default; # uncoverable statement | ||
| 75 | $settings->set_property('gtk-icon-theme-name', # uncoverable statement | ||
| 76 | $icon_theme_name); # uncoverable statement | ||
| 77 | } else { # uncoverable statement | ||
| 78 | warn "Not able to find icon-theme ${icon_theme_name}.\n"; # uncoverable statement | ||
| 79 | } # uncoverable statement | ||
| 80 | } | ||
| 81 | |||
| 82 | fun _setup_disable_xinput2() { | ||
| 83 | if( $DISPLAY && ! $INTERTANGLE_API_GTK3_USE_XINPUT2 ) { | ||
| 84 | Gtk3::Gdk::disable_multidevice(); | ||
| 85 | } | ||
| 86 | } | ||
| 87 | |||
| 88 | fun _setup_gtk() { | ||
| 89 | _setup_disable_xinput2; | ||
| 90 | Gtk3::init; | ||
| 91 | } | ||
| 92 | |||
| 93 | fun _gtk_box_shim() { | ||
| 94 | my $shim = fun(@) { | ||
| 95 | my $orig = shift; | ||
| 96 | my $self = shift; | ||
| 97 | my $widget = shift; | ||
| 98 | if( $widget->can('_gtk_widget') ) { | ||
| 99 | $orig->($self, $widget->_gtk_widget, @_); | ||
| 100 | } else { | ||
| 101 | $orig->($self, $widget, @_); | ||
| 102 | } | ||
| 103 | }; | ||
| 104 | for my $method (qw(pack_start add)) { | ||
| 105 | Class::Method::Modifiers::install_modifier | ||
| 106 | "Gtk3::Box", | ||
| 107 | around => $method => $shim; | ||
| 108 | } | ||
| 109 | } | ||
| 110 | |||
| 111 | sub import { | ||
| 112 | unless( $LOADED ) { | ||
| 113 | _setup_gtk(); | ||
| 114 | _scrolled_window_viewport_shim; | ||
| 115 | _gtk_box_shim; | ||
| 116 | #_set_theme('Flat-Plat'); | ||
| 117 | #_set_icon_theme('Arc'); | ||
| 118 | $LOADED = 1; | ||
| 119 | } | ||
| 120 | return; | ||
| 121 | } | ||
| 122 | |||
| 123 | # Note: :prototype($$) would help here, but is only possible on Perl v5.20+ | ||
| 124 | |||
| 125 | classmethod gval( $glib_typename, $value ) { ## no critic | ||
| 126 | # GValue wrapper shortcut | ||
| 127 | Glib::Object::Introspection::GValueWrapper->new('Glib::'.ucfirst($glib_typename) => $value); | ||
| 128 | } | ||
| 129 | |||
| 130 | classmethod genum( $package, $sv ) { | ||
| 131 | Glib::Object::Introspection->convert_sv_to_enum($package, $sv); | ||
| 132 | } | ||
| 133 | |||
| 134 | classmethod callback( $invocant, $callback_name, @args ) { | ||
| 135 | my $fun = $invocant->can( $callback_name ); | ||
| 136 | $fun->( @args, $invocant ); | ||
| 137 | } | ||
| 138 | |||
| 139 | 1; | ||
| 140 | |||
| 141 | __END__ | ||
| 142 | |||
| 143 | =pod | ||
| 144 | |||
| 145 | =encoding UTF-8 | ||
| 146 | |||
| 147 | =for stopwords gval genum | ||
| 148 | |||
| 149 | =head1 NAME | ||
| 150 | |||
| 151 | Intertangle::API::Gtk3::Helper - Collection of helper utilities for Gtk3 and Glib | ||
| 152 | |||
| 153 | =head1 VERSION | ||
| 154 | |||
| 155 | version 0.006 | ||
| 156 | |||
| 157 | =head1 FUNCTIONS | ||
| 158 | |||
| 159 | =head2 _setup_disable_xinput2 | ||
| 160 | |||
| 161 | fun _setup_disable_xinput2() { | ||
| 162 | |||
| 163 | Checks to see if running under X11 and that the environment variable | ||
| 164 | C<< $ENV{INTERTANGLE_API_GTK3_USE_XINPUT2} >> is not set to a true value. | ||
| 165 | If so, disables the L<XInput 2 extension|https://www.x.org/wiki/guide/extensions/#index2h2>. | ||
| 166 | |||
| 167 | This is necessary because GDK has an issue with some forms of mouse scrolling | ||
| 168 | under XInput 2. XInput 2 provides support for smooth scrolling, but sometimes | ||
| 169 | it sends emulated smooth scroll events that have zero for their deltas when | ||
| 170 | dealing with mice that do not support smooth scrolling natively (i.e., they use | ||
| 171 | buttons 4 and 5). This prevents widgets from responding. | ||
| 172 | |||
| 173 | This is similar to C<< $ENV{GDK_CORE_DEVICE_EVENTS} >> as documented here | ||
| 174 | L<https://developer.gnome.org/gtk3/stable/gtk-x11.html>. | ||
| 175 | |||
| 176 | =head2 _setup_gtk | ||
| 177 | |||
| 178 | fun _setup_gtk() | ||
| 179 | |||
| 180 | Setup Gtk3. | ||
| 181 | |||
| 182 | First, this calls C<< _setup_disable_xinput2() >>. | ||
| 183 | |||
| 184 | Then this initialises Gtk3. | ||
| 185 | |||
| 186 | =head1 CLASS METHODS | ||
| 187 | |||
| 188 | =head2 C<gval> | ||
| 189 | |||
| 190 | classmethod gval( $glib_typename, $value ) | ||
| 191 | |||
| 192 | Given a L<Glib type name|https://developer.gnome.org/glib/stable/glib-Basic-Types.html>, wraps a | ||
| 193 | Perl value in an object that can be passed to other L<Glib>-based functions. | ||
| 194 | |||
| 195 | =over 4 | ||
| 196 | |||
| 197 | =item * | ||
| 198 | |||
| 199 | C<$glib_typename> | ||
| 200 | |||
| 201 | The name of a type under the C<Glib::> namespace. For | ||
| 202 | example, passing in C<"int"> gives a wrapper to the C<gint> | ||
| 203 | C type which is known as C<Glib::Int> in Perl. | ||
| 204 | |||
| 205 | =item * | ||
| 206 | |||
| 207 | C<$value> | ||
| 208 | |||
| 209 | The value to put inside the wrapper. | ||
| 210 | |||
| 211 | =back | ||
| 212 | |||
| 213 | See L<Glib::Object::Introspection::GValueWrapper> in | ||
| 214 | L<Glib::Object::Introspection> for more information. | ||
| 215 | |||
| 216 | =head2 genum | ||
| 217 | |||
| 218 | classmethod genum( $package, $sv ) | ||
| 219 | |||
| 220 | Returns an enumeration value of type C<$package> which contains the matching | ||
| 221 | enum value given in C<$sv> as a string. | ||
| 222 | |||
| 223 | =over 4 | ||
| 224 | |||
| 225 | =item * | ||
| 226 | |||
| 227 | C<$package> | ||
| 228 | |||
| 229 | The package name of a L<Glib> enumeration. | ||
| 230 | |||
| 231 | =item * | ||
| 232 | |||
| 233 | C<$sv> | ||
| 234 | |||
| 235 | A string representation of the enumeration value. | ||
| 236 | |||
| 237 | =back | ||
| 238 | |||
| 239 | For example, for | ||
| 240 | L<GtkPackType|https://developer.gnome.org/gtk3/stable/gtk3-Standard-Enumerations.html#GtkPackType> | ||
| 241 | enum, we set C<$package> to C<'Gtk3::PackType'> and C<$sv> to C<'GTK_PACK_START'>. | ||
| 242 | This can be passed on to other L<Glib::Object::Introspection> methods. | ||
| 243 | |||
| 244 | =head2 callback | ||
| 245 | |||
| 246 | classmethod callback( $invocant, $callback_name, @args ) | ||
| 247 | |||
| 248 | A helper function to redirect to other callback functions. Given an | ||
| 249 | C<$invocant> and the name of the callback function, C<$callback_name>, which is | ||
| 250 | defined in the package of C<$invocant>, calls that function with arguments | ||
| 251 | C<( @args, $invocant )>. | ||
| 252 | |||
| 253 | For example, if we are trying to call the callback function | ||
| 254 | C<Target::Package::on_event_cb> and C<$target> is a blessed reference of type | ||
| 255 | C<Target::Package>, then by using | ||
| 256 | |||
| 257 | Intertangle::API::Gtk3::Helper->callback( $target, on_event_cb => @rest_of_args ); | ||
| 258 | |||
| 259 | effectively calls | ||
| 260 | |||
| 261 | Target::Package::on_event_cb( @rest_of_args, $target ); | ||
| 262 | |||
| 263 | =head1 AUTHOR | ||
| 264 | |||
| 265 | Zakariyya Mughal <zmughal@cpan.org> | ||
| 266 | |||
| 267 | =head1 COPYRIGHT AND LICENSE | ||
| 268 | |||
| 269 | This software is copyright (c) 2017 by Zakariyya Mughal. | ||
| 270 | |||
| 271 | This is free software; you can redistribute it and/or modify it under | ||
| 272 | the same terms as the Perl 5 programming language system itself. | ||
| 273 | |||
| 274 | =cut | ||