| 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.007'; | ||
| 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 | 0 | ▶ | 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__ | ||