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_cache' => (is => 'ro', default => sub { {} });
15 has '_layout_set_cache' => (is => 'ro', default => sub { {} });
17 has 'app' => (is => 'ro', required => 1);
19 has 'skin_name' => (is => 'ro', required => 1, default => 'default');
22 is => 'ro', lazy_build => 1,
23 handles => [ qw(create_layout_set search_path_for_type) ]
26 has 'layout_set_class' => (is => 'ro', lazy_build => 1);
28 has 'rendering_context_class' => (is => 'ro', lazy_build => 1);
30 implements '_build_layout_set_class' => as {
32 return $self->find_related_class('LayoutSet');
35 implements '_build_rendering_context_class' => as {
37 return $self->find_related_class('RenderingContext');
40 implements '_build_skin' => as {
43 name => $self->skin_name, view => $self,
44 # path_to returns a File, not a Dir. Thanks, Catalyst.
45 skin_base_dir => Dir->new($self->app->path_to('share', 'skin')),
49 implements 'COMPONENT' => as {
50 my ($class, $app, $args) = @_;
51 return $class->new(%{$args||{}}, app => $app);
54 implements 'render_window' => as {
55 my ($self, $window) = @_;
56 my $root_vp = $window->focus_stack->vp_head;
57 my $rctx = $self->create_rendering_context;
58 my ($widget, $args) = $self->render_viewport_args($root_vp);
59 $widget->render(widget => $rctx, $args);
62 implements 'render_viewport_args' => as {
64 my $layout_set = $self->layout_set_for($vp);
65 my $widget = $self->widget_for($vp, $layout_set);
66 return ($widget, { viewport => $vp });
69 implements 'widget_for' => as {
70 my ($self, $vp, $layout_set) = @_;
72 $self->_widget_cache->{$layout_set->name}
73 ||= $layout_set->widget_class
75 view => $self, layout_set => $layout_set
79 implements 'layout_set_for' => as {
81 my $lset_name = eval { $vp->layout };
82 confess "Couldn't call layout method on \$vp arg ${vp}: $@" if $@;
83 $lset_name = $self->layout_set_name_from_viewport( blessed($vp) )
84 unless (length($lset_name));
85 my $cache = $self->_layout_set_cache;
86 return $cache->{$lset_name} ||= $self->create_layout_set($lset_name);
89 #XXX if it ever comes to it: this could be memoized. not bothering yet.
90 implements 'layout_set_name_from_viewport' => as {
91 my ($self, $class) = @_;
92 my ($last) = ($class =~ /.*(?:::ViewPort::)(.+?)$/);
93 #split when a non-uppercase letter meets an uppercase or when an
94 #uppercase letter is followed by another uppercase and then a non-uppercase
95 #FooBar = foo_bar; Foo_Bar = foo_bar; FOOBar = foo_bar; FooBAR = foo_bar
97 join("_", split(/(?:(?<=[A-Z])(?=[A-Z][^_A-Z])|(?<=[^_A-Z])(?=[A-Z]))/, $_))
99 return lc(join('/', @fragments));
102 implements 'layout_set_file_extension' => as {
103 confess View." is abstract, you must subclass it";
106 implements 'find_related_class' => as {
107 my ($self, $rel) = @_;
108 my $own_class = ref($self) || $self;
109 confess View." is abstract, you must subclass it" if $own_class eq View;
110 foreach my $super ($own_class->meta->class_precedence_list) {
111 next if $super eq View;
112 if ($super =~ /::View::/) {
113 (my $class = $super) =~ s/::View::/::${rel}::/;
114 if (eval { Class::MOP::load_class($class) }) {
119 confess "Unable to find related ${rel} class for ${own_class}";
122 implements 'create_rendering_context' => as {
123 my ($self, @args) = @_;
124 return $self->rendering_context_class->new(
125 $self->rendering_context_args_for(@args),
130 implements 'rendering_context_args_for' => as {
134 implements 'layout_set_args_for' => as {