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 { {} });
13 has 'app' => (is => 'ro', required => 1);
15 has 'skin_name' => (is => 'ro', required => 1);
17 has 'layout_set_class' => (is => 'ro', lazy_build => 1);
19 has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
21 implements 'COMPONENT' => as {
22 my ($class, $app, $args) = @_;
23 return $class->new(%{$args||{}}, app => $app);
28 my $skin_name = $self->skin_name;
29 #XXX i guess we will add the path to installed reaction templates here
30 my $skin_path = $self->app->path_to('share','skin',$skin_name);
31 confess("'${skin_path}' is not a valid path for skin '${skin_name}'")
35 implements 'render_window' => as {
36 my ($self, $window) = @_;
37 my $root_vp = $window->focus_stack->vp_head;
38 $self->render_viewport(undef, $root_vp);
41 implements 'render_viewport' => as {
42 my ($self, $outer_rctx, $vp) = @_;
43 my $layout_set = $self->layout_set_for($vp);
44 my $rctx = $self->create_rendering_context(
45 layouts => $layout_set,
48 my $widget = $self->widget_for($vp, $layout_set);
49 $widget->render($rctx);
52 implements 'widget_for' => as {
53 my ($self, $vp, $layout_set) = @_;
54 return $self->widget_class_for($layout_set)
55 ->new(view => $self, viewport => $vp);
58 implements 'widget_class_for' => as {
59 my ($self, $layout_set) = @_;
60 my $base = $self->blessed;
61 my $tail = $layout_set->widget_type;
62 # eventually more stuff will go here i guess?
63 my $app_name = ref $self->app || $self->app;
65 my @search_path = ($base, $app_name, 'Reaction::UI');
66 my @haystack = map { join '::', $_, 'Widget', $tail } @search_path;
67 for my $class (@haystack){
68 eval { Class::MOP::load_class($class) };
69 $@ ? next : return $class;
71 confess "Couldn't load widget '$tail': tried: @haystack";
74 implements 'layout_set_for' => as {
76 #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
77 my $lset_name = eval { $vp->layout };
78 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
79 unless (length($lset_name)) {
80 my $vp_class = ref($vp) || $vp;
81 my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
82 my @fragments = split('::', $last);
83 $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
84 $lset_name = lc(join('/', @fragments));
85 #print STDERR "--- $vp_class is rendered as $lset_name\n";
87 my $cache = $self->_layout_set_cache;
88 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
91 implements 'create_layout_set' => as {
92 my ($self, $name) = @_;
93 return $self->layout_set_class->new(
94 $self->layout_set_args_for($name),
98 implements 'find_related_class' => as {
99 my ($self, $rel) = @_;
100 my $own_class = ref($self) || $self;
101 confess View." is abstract, you must subclass it" if $own_class eq View;
102 foreach my $super ($own_class->meta->class_precedence_list) {
103 next if $super eq View;
104 if ($super =~ /::View::/) {
105 (my $class = $super) =~ s/::View::/::${rel}::/;
106 if (eval { Class::MOP::load_class($class) }) {
111 confess "Unable to find related ${rel} class for ${own_class}";
114 implements 'build_layout_set_class' => as {
116 return $self->find_related_class('LayoutSet');
119 implements 'layout_set_args_for' => as {
120 my ($self, $name) = @_;
121 return (name => $name, search_path => $self->layout_search_path);
124 implements 'layout_search_path' => as {
126 return $self->search_path_for_type('layout');
129 implements 'search_path_for_type' => as {
130 my ($self, $type) = @_;
131 return [ $self->app->path_to('share','skin',$self->skin_name,$type) ];
134 implements 'create_rendering_context' => as {
135 my ($self, @args) = @_;
136 return $self->rendering_context_class->new(
137 $self->rendering_context_args_for(@args),
142 implements 'build_rendering_context_class' => as {
144 return $self->find_related_class('RenderingContext');
147 implements 'rendering_context_args_for' => as {