need your help for Gtk3 and Moose

[email protected] (tsuyoshi okita) Tue, 13 May 2014 15:02:18 +0900
Newsgroups perl.moose
Message-ID <[email protected]>
Dear,

I am a newbie to Moose.
I have been trying to use Moose for Gtk3, and I do not think Moose is 
happy with Builder.
After Moose load ui file, has 'root_window' tries to get main_window 
object, and it seems this is where the problem gets generated.

has 'root_window'    => (
     is    => 'rw',
     isa    => 'Gtk3::Window',
     #isa    => 'Gtk3::Object',
     lazy_build    => 1,
     trigger    => sub {
         my ( $self ) = @_;
         $self->root_window->signal_connect( destroy => \&quit_app );
     }
);
sub _build_root_window {
     my ( $self ) = @_;
     $self->builder->get_object('main_window');
}
when $builder->get_object('blah'); gets executed, it gives me a 
following error:

Following is the error msg I got:
#Attribute (root_window) does not pass the type constraint because: 
Validation failed for 'Gtk3::Object' with value undef at 
/usr/local/lib/perl/5.14.2/Moose/Exception.pm line 37
# 
Moose::Exception::_build_trace('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)') 
called at reader Moose::Exception::trace (defined at 
/usr/local/lib/perl/5.14.2/Moose/Exception.pm line 9) line 7
# 
Moose::Exception::trace('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)') 
called at /usr/local/lib/perl/5.14.2/Moose/Exception.pm line 49
# 
Moose::Exception::BUILD('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)', 
'HASH(0xa3fd840)') called at 
/usr/local/lib/perl/5.14.2/Class/MOP/Method.pm line 128
# Class::MOP::Method::execute('Moose::Meta::Method=HASH(0xa450e7c)', 
'Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)', 
'HASH(0xa3fd840)') called at /usr/local/lib/perl/5.14.2/Moose/Object.pm 
line 56
# 
Moose::Object::BUILDALL('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)', 
'HASH(0xa3fd840)') called at 
/usr/local/lib/perl/5.14.2/Moose/Meta/Class.pm line 282
# Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0xa40afac)', 
'HASH(0xa3fd840)') called at /usr/local/lib/perl/5.14.2/Moose/Object.pm 
line 27
# 
Moose::Object::new('Moose::Exception::ValidationFailedForInlineTypeConstraint', 
'type_constraint_message', 'Validation failed for \'Gtk3::Object\' with 
value undef', 'class_name', 'Tsuyoshi::Gtk3', 'attribute_name', 
'root_window', 'value', undef) called at 
/usr/local/lib/perl/5.14.2/Moose/Util.pm line 51
# 
Moose::Util::throw_exception('ValidationFailedForInlineTypeConstraint', 
'type_constraint_message', 'Validation failed for \'Gtk3::Object\' with 
value undef', 'class_name', 'Tsuyoshi::Gtk3', 'attribute_name', 
'root_window', 'value', undef) called at accessor 
Tsuyoshi::Gtk3::root_window (defined at ./gtk3_moose.pl line 44) line 22
#    Tsuyoshi::Gtk3::root_window('Tsuyoshi::Gtk3=HASH(0x9508500)') 
called at gtk3_moose.pl line 13
#    Tsuyoshi::Gtk3::BUILD('Tsuyoshi::Gtk3=HASH(0x9508500)', 
'HASH(0x9b0d168)') called at constructor Tsuyoshi::Gtk3::new (defined at 
./gtk3_moose.pl line 54) line 75
#    Tsuyoshi::Gtk3::new('Tsuyoshi::Gtk3') called at gtk3_moose.pl line 63

I tried withou Builder, and this worked fine:
has 'root_window'    => (
     is    => 'rw',
     isa    => 'Gtk3::Window',
     default    => sub { return Gtk3::Window->new; },
     trigger    => sub {
         my ( $self ) = @_;
         $self->root_window->signal_connect( destroy => \&quit_app );
     }
);

I attached two test cases, one with Builder, which does not work, and 
other without Builder, which works.

Do you think Glib::Object::Subclass module will do a job for this problem?

If you can give me any guidance, I would be grateful.
Thank you.

Tsuyoshi
new_test.ui (application/x-designer, 29.6 KB) - not displayed
gtk3_moose_with_builder.pl (application/x-perl, 3.5 KB)
#!/usr/bin/perl
#------------------
# PerlのクラスファイルにMAINパッケージも加えたければ、
# 1. 1行目は、#!/usr/bin/perl
# エラー1:
#Attribute (root_window) does not pass the type constraint because: Validation failed for 'Gtk3::Object' with value undef at /usr/local/lib/perl/5.14.2/Moose/Exception.pm line 37
#	Moose::Exception::_build_trace('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)') called at reader Moose::Exception::trace (defined at /usr/local/lib/perl/5.14.2/Moose/Exception.pm line 9) line 7
#	Moose::Exception::trace('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)') called at /usr/local/lib/perl/5.14.2/Moose/Exception.pm line 49
#	Moose::Exception::BUILD('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)', 'HASH(0xa3fd840)') called at /usr/local/lib/perl/5.14.2/Class/MOP/Method.pm line 128
#	Class::MOP::Method::execute('Moose::Meta::Method=HASH(0xa450e7c)', 'Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)', 'HASH(0xa3fd840)') called at /usr/local/lib/perl/5.14.2/Moose/Object.pm line 56
#	Moose::Object::BUILDALL('Moose::Exception::ValidationFailedForInlineTypeConstraint=HASH(0xa415b4c)', 'HASH(0xa3fd840)') called at /usr/local/lib/perl/5.14.2/Moose/Meta/Class.pm line 282
#	Moose::Meta::Class::new_object('Moose::Meta::Class=HASH(0xa40afac)', 'HASH(0xa3fd840)') called at /usr/local/lib/perl/5.14.2/Moose/Object.pm line 27
#	Moose::Object::new('Moose::Exception::ValidationFailedForInlineTypeConstraint', 'type_constraint_message', 'Validation failed for \'Gtk3::Object\' with value undef', 'class_name', 'Tsuyoshi::Gtk3', 'attribute_name', 'root_window', 'value', undef) called at /usr/local/lib/perl/5.14.2/Moose/Util.pm line 51
#	Moose::Util::throw_exception('ValidationFailedForInlineTypeConstraint', 'type_constraint_message', 'Validation failed for \'Gtk3::Object\' with value undef', 'class_name', 'Tsuyoshi::Gtk3', 'attribute_name', 'root_window', 'value', undef) called at accessor Tsuyoshi::Gtk3::root_window (defined at ./gtk3_moose.pl line 44) line 22
#	Tsuyoshi::Gtk3::root_window('Tsuyoshi::Gtk3=HASH(0x9508500)') called at gtk3_moose.pl line 13
#	Tsuyoshi::Gtk3::BUILD('Tsuyoshi::Gtk3=HASH(0x9508500)', 'HASH(0x9b0d168)') called at constructor Tsuyoshi::Gtk3::new (defined at ./gtk3_moose.pl line 54) line 75
#	Tsuyoshi::Gtk3::new('Tsuyoshi::Gtk3') called at gtk3_moose.pl line 63
#------------------

package Tsuyoshi::Gtk3;
use Moose;
use Gtk3 '-init';
use Glib;
use Glib::Object::Subclass;

sub BUILD {
	my ( $self ) = @_;

	$self->root_window->show_all;
	Gtk3::main;

	return $self;
}
has 'ui_file' => (
	is => 'ro',
	isa	=> 'Str',
	default	=> './new_test.ui'
);
has 'builder' => (
	is	=> 'rw',
	isa	=> 'Gtk3::Builder',
	default	=> sub {
		return Gtk3::Builder->new;
	},
	trigger	=> sub {
		my ( $self ) = @_;
		$self->builder->add_from_file($self->ui_file);
		$self->builder->connect_signals(undef);
	}
); # has
has 'root_window'	=> (
	is	=> 'rw',
#--------------------
# I believe this is causing the problem.
#--------------------
	isa	=> 'GObject',
	#isa	=> 'Gtk3::Object',
	lazy_build	=> 1,
	trigger	=> sub {
		my ( $self ) = @_;
		$self->root_window->signal_connect( destroy => \&quit_app );
	}
);
sub _build_root_window {
	my ( $self ) = @_;
#--------------------
# I believe this is causing the problem.
#--------------------
	return $self->builder->get_object('toplevel');
}
sub quit_app {
    Gtk3->main_quit;
    return;
}

__PACKAGE__->meta->make_immutable;
no Moose;
1;

package main;

my $gtk = Tsuyoshi::Gtk3->new;
gtk3_moose_without_builder.pl (application/x-perl, 792 B)
#!/usr/bin/perl

package Tsuyoshi::Gtk3;
use Moose;
use Gtk3 '-init';

sub BUILD {
	my ( $self ) = @_;

	$self->root_window->add($self->button);
	$self->root_window->show_all;
	Gtk3::main;

	return $self;
}
has 'button'	=> (
	is	=> 'rw',
	isa	=> 'Gtk3::Button',
	default	=> sub { return Gtk3::Button->new('quit'); },
	trigger	=> sub {
		my ( $self ) = @_;
		$self->button->signal_connect(clicked => sub { Gtk3::main_quit });
	}
);
has 'root_window'	=> (
	is	=> 'rw',
	isa	=> 'Gtk3::Window',
	default	=> sub { return Gtk3::Window->new; },
	trigger	=> sub {
		my ( $self ) = @_;
		$self->root_window->signal_connect( destroy => \&quit_app );
	}
);
sub quit_app {
    Gtk3->main_quit;
    return;
}

__PACKAGE__->meta->make_immutable;
no Moose;
1;

package main;

my $gtk = Tsuyoshi::Gtk3->new;