1 package Reaction::UI::View;
5 # declaring dependencies
6 use Reaction::UI::LayoutSet;
7 use Reaction::UI::RenderingContext;
8 use aliased 'Reaction::UI::Skin';
9 use aliased 'Path::Class::Dir';
13 has '_widget_class_cache' => (is => 'ro', default => sub { {} });
14 has '_widget_cache' => (is => 'ro', default => sub { {} });
16 has '_layout_set_cache' => (is => 'ro', default => sub { {} });
18 has 'app' => (is => 'ro', required => 1);
20 has 'skin_name' => (is => 'ro', required => 1);
23 is => 'ro', lazy_build => 1,
24 handles => [ qw(create_layout_set search_path_for_type) ]
27 has 'layout_set_class' => (is => 'ro', lazy_build => 1);
29 has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
31 implements '_build_layout_set_class' => as {
33 return $self->find_related_class('LayoutSet');
36 implements '_build_rendering_context_class' => as {
38 return $self->find_related_class('RenderingContext');
41 implements '_build_skin' => as {
44 name => $self->skin_name, view => $self,
45 skin_base_path => # returns a File, not a Dir. Thanks, Catalyst.
46 Dir->new($self->app->path_to('share', 'skin', $self->skin_name)),
50 implements 'COMPONENT' => as {
51 my ($class, $app, $args) = @_;
52 return $class->new(%{$args||{}}, app => $app);
55 implements 'render_window' => as {
56 my ($self, $window) = @_;
57 my $root_vp = $window->focus_stack->vp_head;
58 my $rctx = $self->create_rendering_context;
59 my ($widget, $args) = $self->render_viewport_args($root_vp);
60 $widget->render(widget => $rctx, $args);
63 implements 'render_viewport_args' => as {
65 my $layout_set = $self->layout_set_for($vp);
66 my $widget = $self->widget_for($vp, $layout_set);
67 return ($widget, { viewport => $vp });
70 implements 'widget_for' => as {
71 my ($self, $vp, $layout_set) = @_;
73 $self->_widget_cache->{$layout_set->name}
74 ||= $layout_set->widget_class
76 view => $self, layout_set => $layout_set
80 implements 'widget_class_for' => as {
81 my ($self, $layout_set) = @_;
82 my $base = $self->blessed;
83 my $widget_type = $layout_set->widget_type;
84 my $app_name = ref $self->app || $self->app;
85 return $self->_widget_class_cache->{$widget_type} ||= do {
87 my @search_path = ($base, $app_name, 'Reaction::UI');
88 my @haystack = map {join('::', $_, 'Widget', $widget_type)} @search_path;
90 foreach my $class (@haystack) {
91 #if the class is already loaded skip the call to Installed etc.
92 return $class if Class::MOP::is_class_loaded($class);
93 next unless Class::Inspector->installed($class);
95 my $ok = eval { Class::MOP::load_class($class) };
96 confess("Failed to load widget '${class}': $@") if $@;
99 confess "Couldn't locate widget '${widget_type}' for layout "
100 ."'${\$layout_set->name}': tried: ".join(", ", @haystack);
104 implements 'layout_set_for' => as {
105 my ($self, $vp) = @_;
106 #print STDERR "Getting layoutset for VP ".(ref($vp) || "SC:".$vp)."\n";
107 my $lset_name = eval { $vp->layout };
108 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
109 unless (length($lset_name)) {
110 my $vp_class = ref($vp) || $vp;
111 my ($last) = ($vp_class =~ /.*(?:::ViewPort::)(.+?)$/);
112 my @fragments = split('::', $last);
113 $_ = join("_", split(/(?=[A-Z])/, $_)) for @fragments;
114 $lset_name = lc(join('/', @fragments));
115 #print STDERR "--- $vp_class is rendered as $lset_name\n";
117 my $cache = $self->_layout_set_cache;
118 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
121 implements 'layout_set_file_extension' => as {
122 confess View." is abstract, you must subclass it";
125 implements 'find_related_class' => as {
126 my ($self, $rel) = @_;
127 my $own_class = ref($self) || $self;
128 confess View." is abstract, you must subclass it" if $own_class eq View;
129 foreach my $super ($own_class->meta->class_precedence_list) {
130 next if $super eq View;
131 if ($super =~ /::View::/) {
132 (my $class = $super) =~ s/::View::/::${rel}::/;
133 if (eval { Class::MOP::load_class($class) }) {
138 confess "Unable to find related ${rel} class for ${own_class}";
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 'rendering_context_args_for' => as {
153 implements 'layout_set_args_for' => as {