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 '_build_layout_set_class' => as {
24 return $self->find_related_class('LayoutSet');
27 implements '_build_rendering_context_class' => as {
29 return $self->find_related_class('RenderingContext');
32 implements 'COMPONENT' => as {
33 my ($class, $app, $args) = @_;
34 return $class->new(%{$args||{}}, app => $app);
39 my $skin_name = $self->skin_name;
40 #XXX i guess we will add the path to installed reaction templates here
41 my $skin_path = $self->app->path_to('share','skin',$skin_name);
42 confess("'${skin_path}' is not a valid path for skin '${skin_name}'")
46 implements 'render_window' => as {
47 my ($self, $window) = @_;
48 my $root_vp = $window->focus_stack->vp_head;
49 $self->render_viewport(undef, $root_vp);
52 implements 'render_viewport' => as {
53 my ($self, $outer_rctx, $vp) = @_;
54 my $layout_set = $self->layout_set_for($vp);
55 my $rctx = $self->create_rendering_context(
56 layouts => $layout_set,
59 my $widget = $self->widget_for($vp, $layout_set);
60 $widget->render($rctx);
63 implements 'widget_for' => as {
64 my ($self, $vp, $layout_set) = @_;
65 return $self->widget_class_for($layout_set)
66 ->new(view => $self, viewport => $vp);
69 implements 'widget_class_for' => as {
70 my ($self, $layout_set) = @_;
71 my $base = $self->blessed;
72 my $tail = $layout_set->widget_type;
73 my $lset_name = $layout_set->name;
74 # eventually more stuff will go here i guess?
75 my $app_name = ref $self->app || $self->app;
76 my $cache = $self->_widget_class_cache;
77 return $cache->{ $lset_name } if exists $cache->{ $lset_name };
79 my @search_path = ($base, $app_name, 'Reaction::UI');
80 my @haystack = map { join '::', $_, 'Widget', $tail } @search_path;
81 for my $class (@haystack){
82 #here we should throw if exits and error instead of eating the error
83 #only next when !exists
84 eval { Class::MOP::load_class($class) };
85 #$@ ? next : return $class;
86 $@ ? next : return $cache->{ $lset_name } = $class;
88 confess "Couldn't load widget '$tail': tried: @haystack";
91 implements 'layout_set_for' => as {
93 #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
94 my $lset_name = eval { $vp->layout };
95 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
96 unless (length($lset_name)) {
97 my $vp_class = ref($vp) || $vp;
98 my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
99 my @fragments = split('::', $last);
100 $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
101 $lset_name = lc(join('/', @fragments));
102 #print STDERR "--- $vp_class is rendered as $lset_name\n";
104 my $cache = $self->_layout_set_cache;
105 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
108 implements 'create_layout_set' => as {
109 my ($self, $name) = @_;
110 return $self->layout_set_class->new(
111 $self->layout_set_args_for($name),
115 implements 'find_related_class' => as {
116 my ($self, $rel) = @_;
117 my $own_class = ref($self) || $self;
118 confess View." is abstract, you must subclass it" if $own_class eq View;
119 foreach my $super ($own_class->meta->class_precedence_list) {
120 next if $super eq View;
121 if ($super =~ /::View::/) {
122 (my $class = $super) =~ s/::View::/::${rel}::/;
123 if (eval { Class::MOP::load_class($class) }) {
128 confess "Unable to find related ${rel} class for ${own_class}";
131 implements 'layout_set_args_for' => as {
132 my ($self, $name) = @_;
133 return (name => $name, search_path => $self->layout_search_path);
136 implements 'layout_search_path' => as {
138 return $self->search_path_for_type('layout');
141 implements 'search_path_for_type' => as {
142 my ($self, $type) = @_;
143 return [ $self->app->path_to('share','skin',$self->skin_name,$type) ];
146 implements 'create_rendering_context' => as {
147 my ($self, @args) = @_;
148 return $self->rendering_context_class->new(
149 $self->rendering_context_args_for(@args),
154 implements 'rendering_context_args_for' => as {