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 { {} });
13 has '_widget_cache' => (is => 'ro', default => sub { {} });
15 has 'app' => (is => 'ro', required => 1);
17 has 'skin_name' => (is => 'ro', required => 1);
19 has 'layout_set_class' => (is => 'ro', lazy_build => 1);
21 has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
23 implements '_build_layout_set_class' => as {
25 return $self->find_related_class('LayoutSet');
28 implements '_build_rendering_context_class' => as {
30 return $self->find_related_class('RenderingContext');
33 implements 'COMPONENT' => as {
34 my ($class, $app, $args) = @_;
35 return $class->new(%{$args||{}}, app => $app);
40 my $skin_name = $self->skin_name;
41 #XXX i guess we will add the path to installed reaction templates here
42 my $skin_path = $self->app->path_to('share','skin',$skin_name);
43 confess("'${skin_path}' is not a valid path for skin '${skin_name}'")
47 implements 'render_window' => as {
48 my ($self, $window) = @_;
49 my $root_vp = $window->focus_stack->vp_head;
50 my $rctx = $self->create_rendering_context;
51 my ($widget, $args) = $self->render_viewport_args($root_vp);
52 $widget->render(widget => $rctx, $args);
55 implements 'render_viewport_args' => as {
57 my $layout_set = $self->layout_set_for($vp);
58 my $widget = $self->widget_for($vp, $layout_set);
59 return ($widget, { viewport => $vp });
62 implements 'widget_for' => as {
63 my ($self, $vp, $layout_set) = @_;
65 $self->_widget_cache->{$layout_set->name}
66 ||= $layout_set->widget_class
68 view => $self, layout_set => $layout_set
72 implements 'widget_class_for' => as {
73 my ($self, $layout_set) = @_;
74 my $base = $self->blessed;
75 my $tail = $layout_set->widget_type;
76 my $lset_name = $layout_set->name;
77 # eventually more stuff will go here i guess?
78 my $app_name = ref $self->app || $self->app;
79 my $cache = $self->_widget_class_cache;
80 return $cache->{ $lset_name } if exists $cache->{ $lset_name };
82 my @search_path = ($base, $app_name, 'Reaction::UI');
83 my @haystack = map { join '::', $_, 'Widget', $tail } @search_path;
84 for my $class (@haystack){
85 #here we should throw if exits and error instead of eating the error
86 #only next when !exists
87 eval { Class::MOP::load_class($class) };
88 #$@ ? next : return $class;
89 #warn "Loaded ${class}" unless $@;
90 $@ ? next : return $cache->{ $lset_name } = $class;
92 confess "Couldn't load widget '$tail' for layout '$lset_name': tried: " .
93 join(", ", @haystack);
96 implements 'layout_set_for' => as {
98 #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
99 my $lset_name = eval { $vp->layout };
100 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
101 unless (length($lset_name)) {
102 my $vp_class = ref($vp) || $vp;
103 my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
104 my @fragments = split('::', $last);
105 $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
106 $lset_name = lc(join('/', @fragments));
107 #print STDERR "--- $vp_class is rendered as $lset_name\n";
109 my $cache = $self->_layout_set_cache;
110 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
113 implements 'create_layout_set' => as {
114 my ($self, $name) = @_;
115 return $self->layout_set_class->new(
116 $self->layout_set_args_for($name),
120 implements 'find_related_class' => as {
121 my ($self, $rel) = @_;
122 my $own_class = ref($self) || $self;
123 confess View." is abstract, you must subclass it" if $own_class eq View;
124 foreach my $super ($own_class->meta->class_precedence_list) {
125 next if $super eq View;
126 if ($super =~ /::View::/) {
127 (my $class = $super) =~ s/::View::/::${rel}::/;
128 if (eval { Class::MOP::load_class($class) }) {
133 confess "Unable to find related ${rel} class for ${own_class}";
136 implements 'layout_set_args_for' => as {
137 my ($self, $name) = @_;
140 search_path => $self->layout_search_path,
145 implements 'layout_search_path' => as {
147 return $self->search_path_for_type('layout');
150 implements 'search_path_for_type' => as {
151 my ($self, $type) = @_;
152 return [ $self->app->path_to('share','skin',$self->skin_name,$type) ];
155 implements 'create_rendering_context' => as {
156 my ($self, @args) = @_;
157 return $self->rendering_context_class->new(
158 $self->rendering_context_args_for(@args),
163 implements 'rendering_context_args_for' => as {