1 package Reaction::UI::View;
5 # declaring dependencies
6 use Reaction::UI::LayoutSet;
7 use Reaction::UI::RenderingContext;
8 use aliased 'Reaction::UI::Skin';
9 use aliased 'Path::Class::Dir';
13 has '_widget_class_cache' => (is => 'ro', default => sub { {} });
14 has '_widget_cache' => (is => 'ro', default => sub { {} });
16 has '_layout_set_cache' => (is => 'ro', default => sub { {} });
18 has 'app' => (is => 'ro', required => 1);
20 has 'skin_name' => (is => 'ro', required => 1);
23 is => 'ro', lazy_build => 1,
24 handles => [ qw(create_layout_set search_path_for_type) ]
27 has 'layout_set_class' => (is => 'ro', lazy_build => 1);
29 has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
31 implements '_build_layout_set_class' => as {
33 return $self->find_related_class('LayoutSet');
36 implements '_build_rendering_context_class' => as {
38 return $self->find_related_class('RenderingContext');
41 implements '_build_skin' => as {
44 name => $self->skin_name, view => $self,
45 skin_base_path => # returns a File, not a Dir. Thanks, Catalyst.
46 Dir->new($self->app->path_to('share', 'skin', $self->skin_name)),
50 implements 'COMPONENT' => as {
51 my ($class, $app, $args) = @_;
52 return $class->new(%{$args||{}}, app => $app);
55 implements 'render_window' => as {
56 my ($self, $window) = @_;
57 my $root_vp = $window->focus_stack->vp_head;
58 my $rctx = $self->create_rendering_context;
59 my ($widget, $args) = $self->render_viewport_args($root_vp);
60 $widget->render(widget => $rctx, $args);
63 implements 'render_viewport_args' => as {
65 my $layout_set = $self->layout_set_for($vp);
66 my $widget = $self->widget_for($vp, $layout_set);
67 return ($widget, { viewport => $vp });
70 implements 'widget_for' => as {
71 my ($self, $vp, $layout_set) = @_;
73 $self->_widget_cache->{$layout_set->name}
74 ||= $layout_set->widget_class
76 view => $self, layout_set => $layout_set
80 implements 'widget_class_for' => as {
81 my ($self, $layout_set) = @_;
82 my $base = $self->blessed;
83 my $widget_type = $layout_set->widget_type;
84 my $app_name = ref $self->app || $self->app;
85 return $self->_widget_class_cache->{$widget_type} ||= do {
87 my @search_path = ($base, $app_name, 'Reaction::UI');
88 my @haystack = map { join('::', $_, 'Widget', $widget_type) }
91 foreach my $class (@haystack) {
92 #here we should throw if exits and error instead of eating the error
93 #only next when !exists
94 eval { Class::MOP::load_class($class) };
95 #$@ ? next : return $class;
96 #warn "Loaded ${class}" unless $@;
97 #warn "Boom loading ${class}: $@" if $@;
104 confess "Couldn't load widget '${widget_type}'"
105 ." for layout '${\$layout_set->name}':"
106 ." tried: ".join(", ", @haystack);
112 implements 'layout_set_for' => as {
113 my ($self, $vp) = @_;
114 #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
115 my $lset_name = eval { $vp->layout };
116 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
117 unless (length($lset_name)) {
118 my $vp_class = ref($vp) || $vp;
119 my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
120 my @fragments = split('::', $last);
121 $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
122 $lset_name = lc(join('/', @fragments));
123 #print STDERR "--- $vp_class is rendered as $lset_name\n";
125 my $cache = $self->_layout_set_cache;
126 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
129 implements 'find_related_class' => as {
130 my ($self, $rel) = @_;
131 my $own_class = ref($self) || $self;
132 confess View." is abstract, you must subclass it" if $own_class eq View;
133 foreach my $super ($own_class->meta->class_precedence_list) {
134 next if $super eq View;
135 if ($super =~ /::View::/) {
136 (my $class = $super) =~ s/::View::/::${rel}::/;
137 if (eval { Class::MOP::load_class($class) }) {
142 confess "Unable to find related ${rel} class for ${own_class}";
145 implements 'create_rendering_context' => as {
146 my ($self, @args) = @_;
147 return $self->rendering_context_class->new(
148 $self->rendering_context_args_for(@args),
153 implements 'rendering_context_args_for' => as {
157 implements 'layout_set_args_for' => as {