#!/usr/bin/perl
#------------------
# PerlのクラスファイルにMAINパッケージも加えたければ、
# 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';

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	=> '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 ) = @_;
#--------------------
# I believe this is causing the problem.
#--------------------
	$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;
