App/Chart/Gtk2/Ex/ToplevelSingleton.pm

filter: Statementstmt 52.2% 23 / 44 Branchbran 16.6% 1 / 6 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 57.1% 8 / 14 Podpod 0.0% 0 / 5 totaltotal 43.2% 32 / 74 CC 5 SCAR 10.0
CC
5
Cov
45%
__ANON__
6.0
instance_for_screen
6.0
new_instance
3.8
CRAP 2.7
« App/Chart/Suffix/KEX.pm ↑ Summary App/Chart/DownloadHandler/DividendsPage.pm »
1 # instance per-screen ?
2 # instance per-display and move to screen ?
3 #
4 # searching through list_toplevels picks up non-instance created windows
5 # too, maybe flag those created this way ...
6 #
7 # isa() in search might rightly or wrongly pick up subclasses
8  
9  
10 # Copyright 2009, 2010 Kevin Ryde
11  
12 # This file is part of Chart.
13 #
14 # Chart is free software; you can redistribute it and/or modify
15 # it under the terms of the GNU General Public License as published by the
16 # Free Software Foundation; either version 3, or (at your option) any later
17 # version.
18 #
19 # Chart is distributed in the hope that it will be useful, but
20 # WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
21 # or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
22 # for more details.
23 #
24 # You should have received a copy of the GNU General Public License along
25 # with Chart. If not, see <http://www.gnu.org/licenses/>.
26  
27 package App::Chart::Gtk2::Ex::ToplevelSingleton;
28 1 ▶use 5.008;
Subroutine BEGIN: called CC=1 SCAR=0.0
29 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
30 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
31 1 ▶use List::Util;
Subroutine BEGIN: called CC=1 SCAR=0.0
32 1 ▶use App::Chart::Gtk2::Ex::ToplevelBits;
Subroutine BEGIN: called CC=1 SCAR=0.0
33  
34 # uncomment this to run the ### lines
35 #use Smart::Comments;
36  
37 sub import {
38 1 ▶ my ($class, %options) = @_;
Subroutine import: called CC=2 SCAR=7.6
39 1 ▶ if (%options) {
Branch: 1 branch
BranchTrueFalseTotal
if (%options) 0 1 1
40 1 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
41 0 ${"${class}::_instance_options"} = \%options;
42 }
43 }
44  
45 # ClassName->has_instance ()
46 sub has_instance {
47 0 ▶ my $class = shift;
Subroutine has_instance: not called CC=1 SCAR=6.9
Pod undocumented
48 0 return $class->has_instance_for_screen (undef, @_);
49 }
50 # ClassName->has_instance_for_screen ($screen)
51 sub has_instance_for_screen {
52 0 ▶ my ($class, $screen) = @_;
Subroutine has_instance_for_screen: not called CC=1 SCAR=6.9
Pod undocumented
53 0 $screen = App::Chart::Gtk2::Ex::ToplevelBits::_screen($screen);
54  
55 # searching like this isn't fast, but it allows a toplevel to be moved to
56 # a different screen
57 0 ▶ return List::Util::first { ($_->isa($class) && $_->get_screen == $screen) }
Subroutine __ANON__: not called CC=2 SCAR=17.9
Branch: 1 branch
BranchTrueFalseTotal
$_->isa($class) and $_->get_screen == $screen 0 0 0
58 0 Gtk2::Window->list_toplevels;
59 }
60  
61 # ClassName->instance (args...)
62 sub instance {
63 0 ▶ my $class = shift;
Subroutine instance: not called CC=1 SCAR=6.9
Pod undocumented
64 0 return $class->instance_for_screen (undef, @_);
65 }
66  
67 # ClassName->instance_for_screen ($screen, args...)
68 sub instance_for_screen {
69 0 ▶ my ($class, $screen) = (shift, shift);
Subroutine instance_for_screen: not called CC=2 SCAR=17.9
Pod undocumented
70  
71 return $class->has_instance_for_screen($screen)
72 0 ▶ || do {
Condition0%0/3
$class->has_instance_for_screen($screen) || do {
l!l&&r!l&&!r
000
MC/DC0%0/2
$class->has_instance_for_screen($screen) || do { $class->new_instance('screen', App::Chart::Gtk2::Ex::ToplevelBits::_screen($screen), @_) }
$class->has_instance_for_screen($screen) do { $class->new_instance('screen', App::Chart::Gtk2::Ex::ToplevelBits::_screen($screen), @_) }
Truth table0%0/3
A || B
A= $class->has_instance_for_screen($screen)B= do { $class->new_instance('screen', App::Chart::Gtk2::Ex::ToplevelBits::_screen($screen), @_) }
ABresult
1X1
011
000
73 ### ToplevelSingleton instance create
74 0 $class->new_instance (screen => App::Chart::Gtk2::Ex::ToplevelBits::_screen($screen),
75 @_);
76 };
77 }
78  
79 # default instance constructor
80 sub new_instance {
81 0 ▶ my ($class) = @_;
Subroutine new_instance: not called CC=2 SCAR=13.4
Pod undocumented
82 0 my $self = Glib::Object::new (@_);
83 1 ▶ my $options = do { no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.2
84 0 ${"${class}::_instance_options"} };
85 0 ▶ if ($options->{'hide_on_delete'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"hide_on_delete"}) 0 0 0
86 0 $self->signal_connect (delete_event => \&Gtk2::Widget::hide_on_delete);
87 }
88 0 return $self;
89 }
90  
91 1 1;
92 __END__
93  
94  
95 # # ClassName->has_instance_for_display ($display)
96 # sub has_instance_for_display {
97 # my ($class, $display) = @_;
98 # $display = _display($display);
99 #
100 # # searching like this isn't fast, but it allows a toplevel to be moved to
101 # # a different display
102 # return List::Util::first { ($_->isa($class) && $_->get_display == $display) }
103 # Gtk2::Window->list_toplevels;
104 # }
105 # # ClassName->instance_for_display ($display, args...)
106 # sub instance_for_display {
107 # my ($class, $display) = (shift, shift);
108 #
109 # return ($class->has_instance_for_display($display)
110 # || $class->instance_for_screen($display, @_));
111 # }
112 # sub _display {
113 # my ($obj) = @_;
114 # if (! defined $obj) {
115 # return Gtk2::Gdk::Screen->get_default->get_display;
116 # }
117 # if ($obj->can('get_display')) {
118 # $obj = $obj->get_display
119 # || croak "No display for target $obj";
120 # }
121 # return $obj;
122 # }
123  
124 # return ($display->{(__PACKAGE__)}->{$class} ||= do {
125  
126 # $instance->signal_connect (destroy => \&_do_destroy, $class);
127 # sub _do_destroy {
128 # my ($instance, $class) = @_;
129 # my $display = $instance->get_display;
130 # if (($display->{(__PACKAGE__)}->{$class}||0) == $instance) {
131 # delete $display->{(__PACKAGE__)}->{$class};
132 # }
133 # }
134  
135 # use Class::Singleton 1.04; # 1.04 for has_instance()
136 # use base 'Class::Singleton';
137 #
138 # sub instance {
139 # my $class = shift;
140 # return $class->has_instance
141 # || do {
142 # my $instance = $class->SUPER::instance;
143 # $instance->signal_connect (destroy => \&_do_destroy, $ivar);
144 # $instance
145 # };
146 # }
147 #
148 # sub _do_destroy {
149 # my ($instance, $ivar) = @_;
150 # if (($instance->has_instance || 0) == $instance) {
151 # undef $ivar;
152 # }
153 # }
154  
155 =for stopwords toplevel multi
156  
157 =head1 NAME
158  
159 App::Chart::Gtk2::Ex::ToplevelSingleton -- single instance of toplevel window
160  
161 =for test_synopsis my ($toplevel)
162  
163 =head1 SYNOPSIS
164  
165 package MyToplevel;
166 use Gtk2;
167 use Glib::Object::Subclass 'Gtk2::Window';
168 use base 'App::Chart::Gtk2::Ex::ToplevelSingleton';
169  
170 use App::Chart::Gtk2::Ex::ToplevelSingleton hide_on_delete => 1;
171  
172 $toplevel = MyToplevel->instance;
173
174 if (MyToplevel->has_instance) { do_something() }
175  
176 =head1 DESCRIPTION
177  
178 This package is designed as a multi-inheritance mix-in for subclasses of
179 C<Gtk2::Window> which normally want only a single window instance, to be
180 used throughout a program.
181  
182 After adding C<ToplevelSingleton> to your C<@ISA>,
183 C<< MyToplevel->instance >> returns a single shared instance of a
184 C<MyToplevel> window.
185  
186 =head1 SEE ALSO
187  
188 L<Class::Singleton>
189  
190 =cut
« App/Chart/Suffix/KEX.pm ↑ Summary App/Chart/DownloadHandler/DividendsPage.pm »