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