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 eval { Class::MOP::load_class($class) };
73 #$@ ? next : return $class;
74 $@ ? next : return $cache->{ $lset_name } = $class;
76 confess "Couldn't load widget '$tail': tried: @haystack";
79 implements 'layout_set_for' => as {
81 #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
82 my $lset_name = eval { $vp->layout };
83 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
84 unless (length($lset_name)) {
85 my $vp_class = ref($vp) || $vp;
86 my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
87 my @fragments = split('::', $last);
88 $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
89 $lset_name = lc(join('/', @fragments));
90 #print STDERR "--- $vp_class is rendered as $lset_name\n";
92 my $cache = $self->_layout_set_cache;
93 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
96 implements 'create_layout_set' => as {
97 my ($self, $name) = @_;
98 return $self->layout_set_class->new(
99 $self->layout_set_args_for($name),
103 implements 'find_related_class' => as {
104 my ($self, $rel) = @_;
105 my $own_class = ref($self) || $self;
106 confess View." is abstract, you must subclass it" if $own_class eq View;
107 foreach my $super ($own_class->meta->class_precedence_list) {
108 next if $super eq View;
109 if ($super =~ /::View::/) {
110 (my $class = $super) =~ s/::View::/::${rel}::/;
111 if (eval { Class::MOP::load_class($class) }) {
116 confess "Unable to find related ${rel} class for ${own_class}";
119 implements 'build_layout_set_class' => as {
121 return $self->find_related_class('LayoutSet');
124 implements 'layout_set_args_for' => as {
125 my ($self, $name) = @_;
126 return (name => $name, search_path => $self->layout_search_path);
129 implements 'layout_search_path' => as {
131 return $self->search_path_for_type('layout');
134 implements 'search_path_for_type' => as {
135 my ($self, $type) = @_;
136 return [ $self->app->path_to('share','skin',$self->skin_name,$type) ];
139 implements 'create_rendering_context' => as {
140 my ($self, @args) = @_;
141 return $self->rendering_context_class->new(
142 $self->rendering_context_args_for(@args),
147 implements 'build_rendering_context_class' => as {
149 return $self->find_related_class('RenderingContext');
152 implements 'rendering_context_args_for' => as {