Skip to content

Commit

Permalink
Deal with not being able to require 'Cairo' better
Browse files Browse the repository at this point in the history
Previous the 'gtk_simple_use_cairo' would attempt to require Cairo
and would fail silently.  In turn the 'add-draw-handler' would then
attempt to to use values derived from the module by type lookup amd
pass them to a native routine, resulting in an exception in the
native code and subsequently a confusing MoarVM panic when they are
undefined.

This takes a belt and braces approach adding a Bool return to the
gtk_simple_use_cairo so user code can check and act appropriately
if Cairo can't be loaded, and the add-draw-handler will also
throw an exception if called without the module loaded.

Fixes #85
  • Loading branch information
jonathanstowe committed Oct 14, 2018
1 parent 4dadc90 commit 98dc17f
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 18 deletions.
36 changes: 21 additions & 15 deletions examples/18-cairo-draw-handler.pl6
Expand Up @@ -10,20 +10,26 @@ use GTK::Simple::DrawingArea;

my $app = GTK::Simple::App.new(:title<Test>);
my $da = GTK::Simple::DrawingArea.new;
gtk_simple_use_cairo;

$app.set-content( $da );
$app.border-width = 20;
$da.size-request(300,300);

sub rect-do( $d, $ctx ) {
given $ctx {
.rgb(0, 0.7, 0.9);
.rectangle(10, 10, 50, 50);
.fill :preserve; .rgb(1, 1, 1);
.stroke
};

if gtk_simple_use_cairo() {

$app.set-content( $da );
$app.border-width = 20;
$da.size-request(300,300);

sub rect-do( $d, $ctx ) {
given $ctx {
.rgb(0, 0.7, 0.9);
.rectangle(10, 10, 50, 50);
.fill :preserve; .rgb(1, 1, 1);
.stroke
};
}

my $ctx = $da.add-draw-handler( &rect-do );
$app.run;
}
else {
note "'Cairo' module is not installed - can't run example";

my $ctx = $da.add-draw-handler( &rect-do );
$app.run;
}
13 changes: 10 additions & 3 deletions lib/GTK/Simple/DrawingArea.pm6
Expand Up @@ -10,13 +10,19 @@ need GTK::Simple::ConnectionHandler;

my Mu $cairo_t;
my Mu $Cairo_Context;
my Bool $cairo-loaded = False;

sub gtk_simple_use_cairo() is export {
try {
require Cairo;
sub gtk_simple_use_cairo( --> Bool ) is export {
try require ::('Cairo');
if ::('Cairo') ~~ Failure {
$cairo-loaded = False;
}
else {
$cairo_t := ::('Cairo')::('cairo_t');
$Cairo_Context := ::('Cairo')::('Context');
$cairo-loaded = True;
}
$cairo-loaded;
}

unit class GTK::Simple::DrawingArea does GTK::Simple::Widget;
Expand All @@ -26,6 +32,7 @@ submethod BUILD() {
}

method add-draw-handler(&handler) {
die "'Cairo' module is not loaded, check it is installed and that you called 'gtk_simple_use_cairo'" unless $cairo-loaded;
my sub handler_wrapper($widget, $cairop) {
my $cairo = nqp::box_i($cairop.Int, $cairo_t);
my $ctx = $Cairo_Context.new($cairo);
Expand Down

0 comments on commit 98dc17f

Please sign in to comment.