1 package Reaction::UI::View;
5 # declaring dependencies
6 use Reaction::UI::LayoutSet;
7 use Reaction::UI::RenderingContext;
11 has '_layout_set_cache' => (is => 'ro', default => sub { {} });
12 has '_widget_class_cache' => (is => 'ro', default => sub { {} });
14 has 'app' => (is => 'ro', required => 1);
16 has 'skin_name' => (is => 'ro', required => 1);
18 has 'layout_set_class' => (is => 'ro', lazy_build => 1);
20 has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
22 implements 'COMPONENT' => as {
23 my ($class, $app, $args) = @_;
24 return $class->new(%{$args||{}}, app => $app);
29 my $skin_name = $self->skin_name;
30 #XXX i guess we will add the path to installed reaction templates here
31 my $skin_path = $self->app->path_to('share','skin',$skin_name);
32 confess("'${skin_path}' is not a valid path for skin '${skin_name}'")
36 implements 'render_window' => as {
37 my ($self, $window) = @_;
38 my $root_vp = $window->focus_stack->vp_head;
39 $self->render_viewport(undef, $root_vp);
42 implements 'render_viewport' => as {
43 my ($self, $outer_rctx, $vp) = @_;
44 my $layout_set = $self->layout_set_for($vp);
45 my $rctx = $self->create_rendering_context(
46 layouts => $layout_set,
49 my $widget = $self->widget_for($vp, $layout_set);
50 $widget->render($rctx);
53 implements 'widget_for' => as {
54 my ($self, $vp, $layout_set) = @_;
55 return $self->widget_class_for($layout_set)
56 ->new(view => $self, viewport => $vp);
59 implements 'widget_class_for' => as {
60 my ($self, $layout_set) = @_;
61 my $base = $self->blessed;
62 my $tail = $layout_set->widget_type;
63 my $lset_name = $layout_set->name;
64 # eventually more stuff will go here i guess?
65 my $app_name = ref $self->app || $self->app;
66 my $cache = $self->_widget_class_cache;
67 return $cache->{ $lset_name } if exists $cache->{ $lset_name };
69 my @search_path = ($base, $app_name, 'Reaction::UI');
70 my @haystack = map { join '::', $_, 'Widget', $tail } @search_path;
71 for my $class (@haystack){
72 #here we should throw if exits and error instead of eating the error
73 #only next when !exists
74 eval { Class::MOP::load_class($class) };
75 #$@ ? next : return $class;
76 $@ ? next : return $cache->{ $lset_name } = $class;
78 confess "Couldn't load widget '$tail': tried: @haystack";
81 implements 'layout_set_for' => as {
83 #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
84 my $lset_name = eval { $vp->layout };
85 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
86 unless (length($lset_name)) {
87 my $vp_class = ref($vp) || $vp;
88 my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
89 my @fragments = split('::', $last);
90 $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
91 $lset_name = lc(join('/', @fragments));
92 #print STDERR "--- $vp_class is rendered as $lset_name\n";
94 my $cache = $self->_layout_set_cache;
95 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
98 implements 'create_layout_set' => as {
99 my ($self, $name) = @_;
100 return $self->layout_set_class->new(
101 $self->layout_set_args_for($name),
105 implements 'find_related_class' => as {
106 my ($self, $rel) = @_;
107 my $own_class = ref($self) || $self;
108 confess View." is abstract, you must subclass it" if $own_class eq View;
109 foreach my $super ($own_class->meta->class_precedence_list) {
110 next if $super eq View;
111 if ($super =~ /::View::/) {
112 (my $class = $super) =~ s/::View::/::${rel}::/;
113 if (eval { Class::MOP::load_class($class) }) {
118 confess "Unable to find related ${rel} class for ${own_class}";
121 implements 'build_layout_set_class' => as {
123 return $self->find_related_class('LayoutSet');
126 implements 'layout_set_args_for' => as {
127 my ($self, $name) = @_;
128 return (name => $name, search_path => $self->layout_search_path);
131 implements 'layout_search_path' => as {
133 return $self->search_path_for_type('layout');
136 implements 'search_path_for_type' => as {
137 my ($self, $type) = @_;
138 return [ $self->app->path_to('share','skin',$self->skin_name,$type) ];
141 implements 'create_rendering_context' => as {
142 my ($self, @args) = @_;
143 return $self->rendering_context_class->new(
144 $self->rendering_context_args_for(@args),
149 implements 'build_rendering_context_class' => as {
151 return $self->find_related_class('RenderingContext');
154 implements 'rendering_context_args_for' => as {