move viewport to being %_ arg only, not widget attribute, cache widget construction
[catagits/Reaction.git] / lib / Reaction / UI / View.pm
1 package Reaction::UI::View;
2
3 use Reaction::Class;
4
5 # declaring dependencies
6 use Reaction::UI::LayoutSet;
7 use Reaction::UI::RenderingContext;
8
9 class View which {
10
11   has '_layout_set_cache'   => (is => 'ro', default => sub { {} });
12   has '_widget_class_cache' => (is => 'ro', default => sub { {} });
13   has '_widget_cache' => (is => 'ro', default => sub { {} });
14
15   has 'app' => (is => 'ro', required => 1);
16
17   has 'skin_name' => (is => 'ro', required => 1);
18
19   has 'layout_set_class' => (is => 'ro', lazy_build => 1);
20
21   has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
22
23   implements '_build_layout_set_class' => as {
24     my ($self) = @_;
25     return $self->find_related_class('LayoutSet');
26   };
27
28   implements '_build_rendering_context_class' => as {
29     my ($self) = @_;
30     return $self->find_related_class('RenderingContext');
31   };
32
33   implements 'COMPONENT' => as {
34     my ($class, $app, $args) = @_;
35     return $class->new(%{$args||{}}, app => $app);
36   };
37
38   sub BUILD{
39     my $self = shift;
40     my $skin_name = $self->skin_name;
41     #XXX i guess we will add the path to installed reaction templates here
42     my $skin_path = $self->app->path_to('share','skin',$skin_name);
43     confess("'${skin_path}' is not a valid path for skin '${skin_name}'")
44       unless -d $skin_path;
45   }
46
47   implements 'render_window' => as {
48     my ($self, $window) = @_;
49     my $root_vp = $window->focus_stack->vp_head;
50     my $rctx = $self->create_rendering_context;
51     $self->render_viewport($rctx, $root_vp);
52   };
53
54   implements 'render_viewport' => as {
55     my ($self, $rctx, $vp) = @_;
56     my $layout_set = $self->layout_set_for($vp);
57     my $widget = $self->widget_for($vp, $layout_set);
58     $widget->render($rctx, { viewport => $vp });
59   };
60
61   implements 'widget_for' => as {
62     my ($self, $vp, $layout_set) = @_;
63     return
64       $self->_widget_cache->{$layout_set->name}
65         ||= $self->widget_class_for($layout_set)
66                  ->new(
67                      view => $self, layout_set => $layout_set
68                    );
69   };
70
71   implements 'widget_class_for' => as {
72     my ($self, $layout_set) = @_;
73     my $base = $self->blessed;
74     my $tail = $layout_set->widget_type;
75     my $lset_name = $layout_set->name;
76     # eventually more stuff will go here i guess?
77     my $app_name = ref $self->app || $self->app;
78     my $cache = $self->_widget_class_cache;
79     return $cache->{ $lset_name } if exists $cache->{ $lset_name };
80
81     my @search_path = ($base, $app_name, 'Reaction::UI');
82     my @haystack    = map { join '::', $_, 'Widget', $tail } @search_path;
83     for my $class (@haystack){
84       #here we should throw if exits and error instead of eating the error
85       #only next when !exists
86       eval { Class::MOP::load_class($class) };
87       #$@ ? next : return  $class;
88       $@ ? next : return $cache->{ $lset_name } = $class;
89     }
90     confess "Couldn't load widget '$tail': tried: @haystack";
91   };
92
93   implements 'layout_set_for' => as {
94     my ($self, $vp) = @_;
95     #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
96     my $lset_name = eval { $vp->layout };
97     confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
98     unless (length($lset_name)) {
99       my $vp_class = ref($vp) || $vp;
100       my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
101       my @fragments = split('::', $last);
102       $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
103       $lset_name = lc(join('/', @fragments));
104       #print STDERR "--- $vp_class is rendered as $lset_name\n";
105     }
106     my $cache = $self->_layout_set_cache;
107     return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
108   };
109
110   implements 'create_layout_set' => as {
111     my ($self, $name) = @_;
112     return $self->layout_set_class->new(
113              $self->layout_set_args_for($name),
114            );
115   };
116
117   implements 'find_related_class' => as {
118     my ($self, $rel) = @_;
119     my $own_class = ref($self) || $self;
120     confess View." is abstract, you must subclass it" if $own_class eq View;
121     foreach my $super ($own_class->meta->class_precedence_list) {
122       next if $super eq View;
123       if ($super =~ /::View::/) {
124         (my $class = $super) =~ s/::View::/::${rel}::/;
125         if (eval { Class::MOP::load_class($class) }) {
126           return $class;
127         }
128       }
129     }
130     confess "Unable to find related ${rel} class for ${own_class}";
131   };
132
133   implements 'layout_set_args_for' => as {
134     my ($self, $name) = @_;
135     return (name => $name, search_path => $self->layout_search_path);
136   };
137
138   implements 'layout_search_path' => as {
139     my ($self) = @_;
140     return $self->search_path_for_type('layout');
141   };
142
143   implements 'search_path_for_type' => as {
144     my ($self, $type) = @_;
145     return [ $self->app->path_to('share','skin',$self->skin_name,$type) ];
146   };
147
148   implements 'create_rendering_context' => as {
149     my ($self, @args) = @_;
150     return $self->rendering_context_class->new(
151              $self->rendering_context_args_for(@args),
152              @args,
153            );
154   };
155
156   implements 'rendering_context_args_for' => as {
157     return ();
158   };
159
160 };
161
162 1;