File Coverage

blib/lib/Renard/Curie/Helper.pm
Criterion Covered Total %
statement 10 12 83.3
branch n/a
condition n/a
subroutine 4 4 100.0
pod n/a
total 14 16 87.5


line stmt bran cond sub pod time code
1 3     3   1173 use Renard::Curie::Setup;
  3         6  
  3         19  
2             package Renard::Curie::Helper;
3             $Renard::Curie::Helper::VERSION = '0.002';
4             # ABSTRACT: Collection of helper utilities for Gtk3 and Glib
5              
6              
7 3     3   20 use Renard::Curie::Types qw(Str);
  3         6  
  3         33  
8 3     3   3434 use Class::Method::Modifiers;
  3         5374  
  3         158  
9 3     3   2828 use Gtk3;
  0            
  0            
10             use Function::Parameters;
11              
12             fun _scrolled_window_viewport_shim() {
13             # Note: The code below is marked as uncoverable because it only applies
14             # to a single version of GTK+ and thus is not part of the general
15             # coverage. The functionality that it adds is tested in other ways.
16             # uncoverable branch true
17             if( not Gtk3::CHECK_VERSION('3', '8', '0') ) {
18             # For versions of Gtk+ less than v3.8.0, we need to call
19             # `Gtk3::ScrolledWindow->add_with_viewport( ... )` so that the
20             # child widget gets placed in a viewport.
21             #
22             # Newer versions of Gtk+ automatically create the viewport when
23             # `Gtk3::ScrolledWindow->add( ... )` is called.
24             #
25             # See:
26             # - <https://developer.gnome.org/gtk3/3.6/GtkScrolledWindow.html>
27             # - <https://developer.gnome.org/gtk3/3.8/GtkScrolledWindow.html>
28             Class::Method::Modifiers::install_modifier
29             "Gtk3::ScrolledWindow",
30             around => add => fun(@) {
31             # uncoverable subroutine
32             my $orig = shift; # uncoverable statement
33             my $self = shift; # uncoverable statement
34             $self->add_with_viewport(@_); # uncoverable statement
35             }; # uncoverable statement
36             }
37             }
38              
39             fun _can_set_theme() {
40             # uncoverable branch true
41             return $^O ne 'MSWin32' && Gtk3::CHECK_VERSION('3', '20', '1');
42             }
43              
44             fun _set_theme( (Str) $theme_name ) {
45             # uncoverable subroutine
46             # uncoverable branch true
47             return unless _can_set_theme;
48              
49             my $adwaita = Gtk3::CssProvider::get_named('Adawaita')->to_string; # uncoverable statement
50             my $custom = Gtk3::CssProvider::get_named($theme_name)->to_string; # uncoverable statement
51             if( $adwaita ne $custom ) { # uncoverable statement
52             my $settings = Gtk3::Settings::get_default; # uncoverable statement
53             $settings->set_property('gtk-theme-name', $theme_name); # uncoverable statement
54             } else { # uncoverable statement
55             warn "Not able to find theme ${theme_name}.\n"; # uncoverable statement
56             } # uncoverable statement
57             }
58              
59              
60             fun _set_icon_theme( (Str) $icon_theme_name ) {
61             # uncoverable subroutine
62             # uncoverable branch true
63             return unless _can_set_theme;
64              
65             my $i = Gtk3::IconTheme->new; # uncoverable statement
66             $i->set_custom_theme($icon_theme_name); # uncoverable statement
67             my $n = $i->choose_icon_for_scale(['gtk-open'], 16, 1, 'no-svg'); # uncoverable statement
68             my $expected_path = qr,/$icon_theme_name/.*\Qgtk-open.png\E$,; # uncoverable statement
69             if ( defined $n && $n->get_filename =~ $expected_path ) { # uncoverable statement
70             my $settings = Gtk3::Settings::get_default; # uncoverable statement
71             $settings->set_property('gtk-icon-theme-name', # uncoverable statement
72             $icon_theme_name); # uncoverable statement
73             } else { # uncoverable statement
74             warn "Not able to find icon-theme ${icon_theme_name}.\n"; # uncoverable statement
75             } # uncoverable statement
76             }
77              
78             sub import {
79             _scrolled_window_viewport_shim;
80             _set_theme('Flat-Plat');
81             _set_icon_theme('Arc');
82             return;
83             }
84              
85             # Note: :prototype($$) would help here, but is only possible on Perl v5.20+
86              
87             classmethod gval( $glib_typename, $value ) { ## no critic
88             # GValue wrapper shortcut
89             Glib::Object::Introspection::GValueWrapper->new('Glib::'.ucfirst($glib_typename) => $value);
90             }
91              
92             classmethod genum( $package, $sv ) {
93             Glib::Object::Introspection->convert_sv_to_enum($package, $sv);
94             }
95              
96             classmethod callback( $invocant, $callback_name, @args ) {
97             my $fun = $invocant->can( $callback_name );
98             $fun->( @args, $invocant );
99             }
100              
101             1;
102              
103             __END__
104              
105             =pod
106              
107             =encoding UTF-8
108              
109             =for stopwords gval genum
110              
111             =head1 NAME
112              
113             Renard::Curie::Helper - Collection of helper utilities for Gtk3 and Glib
114              
115             =head1 VERSION
116              
117             version 0.002
118              
119             =head1 CLASS METHODS
120              
121             =head2 C<gval>
122              
123             classmethod gval( $glib_typename, $value )
124              
125             Given a L<Glib type name|https://developer.gnome.org/glib/stable/glib-Basic-Types.html>, wraps a
126             Perl value in an object that can be passed to other L<Glib>-based functions.
127              
128             =over 4
129              
130             =item *
131              
132             C<$glib_typename>
133              
134             The name of a type under the C<Glib::> namespace. For
135             example, passing in C<"int"> gives a wrapper to the C<gint>
136             C type which is known as C<Glib::Int> in Perl.
137              
138             =item *
139              
140             C<$value>
141              
142             The value to put inside the wrapper.
143              
144             =back
145              
146             See L<Glib::Object::Introspection::GValueWrapper> in
147             L<Glib::Object::Introspection> for more information.
148              
149             =head2 genum
150              
151             classmethod genum( $package, $sv )
152              
153             Returns an enumeration value of type C<$package> which contains the matching
154             enum value given in C<$sv> as a string.
155              
156             =over 4
157              
158             =item *
159              
160             C<$package>
161              
162             The package name of a L<Glib> enumeration.
163              
164             =item *
165              
166             C<$sv>
167              
168             A string representation of the enumeration value.
169              
170             =back
171              
172             For example, for
173             L<GtkPackType|https://developer.gnome.org/gtk3/stable/gtk3-Standard-Enumerations.html#GtkPackType>
174             enum, we set C<$package> to C<'Gtk3::PackType'> and C<$sv> to C<'GTK_PACK_START'>.
175             This can be passed on to other L<Glib::Object::Introspection> methods.
176              
177             =head2 callback
178              
179             classmethod callback( $invocant, $callback_name, @args )
180              
181             A helper function to redirect to other callback functions. Given an
182             C<$invocant> and the name of the callback function, C<$callback_name>, which is
183             defined in the package of C<$invocant>, calls that function with arguments
184             C<( @args, $invocant )>.
185              
186             For example, if we are trying to call the callback function
187             C<Target::Package::on_event_cb> and C<$target> is a blessed reference of type
188             C<Target::Package>, then by using
189              
190             Renard::Curie::Helper->callback( $target, on_event_cb => @rest_of_args );
191              
192             effectively calls
193              
194             Target::Package::on_event_cb( @rest_of_args, $target );
195              
196             =head1 AUTHOR
197              
198             Project Renard
199              
200             =head1 COPYRIGHT AND LICENSE
201              
202             This software is copyright (c) 2016 by Project Renard.
203              
204             This is free software; you can redistribute it and/or modify it under
205             the same terms as the Perl 5 programming language system itself.
206              
207             =cut