runitx Claude Opus 5.5 commited on
Commit
6bc278f
·
unverified ·
1 Parent(s): 1ca425c

Extract view menu actions into Shutter::App::View

Browse files

Move fct_zoom_in/out/100/best, fct_fullscreen and fct_navigation_toolbar
into a functional module. bin/shutter keeps one-line wrappers with the
original names that pass \%session_screens + \&fct_get_current_file,
$window or $nav_toolbar explicitly. Adds t/Shutter/App/14_view.t.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>

bin/shutter CHANGED
@@ -11231,59 +11231,14 @@ modes are currently experimental, your desktop environment might fall back to an
11231
  }
11232
  }
11233
 
11234
- sub fct_zoom_in {
11235
- my $key = fct_get_current_file();
11236
- if ($key) {
11237
- $session_screens{$key}->{'image'}->zoom_in;
11238
- }
11239
- }
11240
-
11241
- sub fct_zoom_out {
11242
- my $key = fct_get_current_file();
11243
- if ($key) {
11244
- $session_screens{$key}->{'image'}->zoom_out;
11245
- }
11246
- }
11247
-
11248
- sub fct_zoom_100 {
11249
- my $key = fct_get_current_file();
11250
- if ($key) {
11251
- $session_screens{$key}->{'image'}->set_zoom(1);
11252
- }
11253
- }
11254
-
11255
- sub fct_zoom_best {
11256
- my $key = fct_get_current_file();
11257
- if ($key) {
11258
- $session_screens{$key}->{'image'}->set_fitting(TRUE);
11259
- }
11260
- }
11261
-
11262
- sub fct_fullscreen {
11263
- my ($widget) = @_;
11264
-
11265
- if ($widget->get_active) {
11266
- $window->fullscreen;
11267
- } else {
11268
- $window->unfullscreen;
11269
- }
11270
- }
11271
-
11272
- sub fct_navigation_toolbar {
11273
- my ($widget) = @_;
11274
-
11275
- if ($widget->get_active) {
11276
- $nav_toolbar->show;
11277
- foreach my $child ($nav_toolbar->get_children) {
11278
- $child->show_all;
11279
- }
11280
- } else {
11281
- $nav_toolbar->hide;
11282
- foreach my $child ($nav_toolbar->get_children) {
11283
- $child->hide_all;
11284
- }
11285
- }
11286
- }
11287
 
11288
  #--------------------------------------
11289
 
 
11231
  }
11232
  }
11233
 
11234
+ require Shutter::App::View;
11235
+
11236
+ sub fct_zoom_in { return Shutter::App::View::zoom_in(\%session_screens, \&fct_get_current_file) }
11237
+ sub fct_zoom_out { return Shutter::App::View::zoom_out(\%session_screens, \&fct_get_current_file) }
11238
+ sub fct_zoom_100 { return Shutter::App::View::zoom_100(\%session_screens, \&fct_get_current_file) }
11239
+ sub fct_zoom_best { return Shutter::App::View::zoom_best(\%session_screens, \&fct_get_current_file) }
11240
+ sub fct_fullscreen { return Shutter::App::View::fullscreen($window, @_) }
11241
+ sub fct_navigation_toolbar { return Shutter::App::View::navigation_toolbar($nav_toolbar, @_) }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
11242
 
11243
  #--------------------------------------
11244
 
share/shutter/resources/modules/Shutter/App/View.pm ADDED
@@ -0,0 +1,118 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ ###################################################
2
+ #
3
+ # Copyright (C) 2008-2013 Mario Kemper <mario.kemper@gmail.com>
4
+ #
5
+ # This file is part of Shutter.
6
+ #
7
+ # Shutter is free software; you can redistribute it and/or modify
8
+ # it under the terms of the GNU General Public License as published by
9
+ # the Free Software Foundation; either version 3 of the License, or
10
+ # (at your option) any later version.
11
+ #
12
+ # Shutter is distributed in the hope that it will be useful,
13
+ # but WITHOUT ANY WARRANTY; without even the implied warranty of
14
+ # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
+ # GNU General Public License for more details.
16
+ #
17
+ # You should have received a copy of the GNU General Public License
18
+ # along with Shutter; if not, write to the Free Software
19
+ # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
20
+ #
21
+ ###################################################
22
+
23
+ package Shutter::App::View;
24
+
25
+ # View menu actions: zooming the current image, fullscreen and the
26
+ # navigation toolbar.
27
+ #
28
+ # All state that used to come from file-scoped variables in bin/shutter is
29
+ # passed in explicitly:
30
+ # $session_screens - hashref, key => { image => Gtk3::ImageView, ... }
31
+ # $get_current_file - coderef returning the key of the current screenshot
32
+ # $window - the main Gtk3::Window
33
+ # $nav_toolbar - the navigation Gtk3::Toolbar
34
+
35
+ use utf8;
36
+ use strict;
37
+ use warnings;
38
+
39
+ use Exporter qw/ import /;
40
+ use Glib qw/ TRUE FALSE /;
41
+
42
+ our @EXPORT_OK = qw/ zoom_in zoom_out zoom_100 zoom_best fullscreen navigation_toolbar /;
43
+
44
+ sub zoom_in {
45
+ my ($session_screens, $get_current_file) = @_;
46
+
47
+ my $key = $get_current_file->();
48
+ if ($key) {
49
+ $session_screens->{$key}->{'image'}->zoom_in;
50
+ }
51
+
52
+ return;
53
+ }
54
+
55
+ sub zoom_out {
56
+ my ($session_screens, $get_current_file) = @_;
57
+
58
+ my $key = $get_current_file->();
59
+ if ($key) {
60
+ $session_screens->{$key}->{'image'}->zoom_out;
61
+ }
62
+
63
+ return;
64
+ }
65
+
66
+ sub zoom_100 {
67
+ my ($session_screens, $get_current_file) = @_;
68
+
69
+ my $key = $get_current_file->();
70
+ if ($key) {
71
+ $session_screens->{$key}->{'image'}->set_zoom(1);
72
+ }
73
+
74
+ return;
75
+ }
76
+
77
+ sub zoom_best {
78
+ my ($session_screens, $get_current_file) = @_;
79
+
80
+ my $key = $get_current_file->();
81
+ if ($key) {
82
+ $session_screens->{$key}->{'image'}->set_fitting(TRUE);
83
+ }
84
+
85
+ return;
86
+ }
87
+
88
+ sub fullscreen {
89
+ my ($window, $widget) = @_;
90
+
91
+ if ($widget->get_active) {
92
+ $window->fullscreen;
93
+ } else {
94
+ $window->unfullscreen;
95
+ }
96
+
97
+ return;
98
+ }
99
+
100
+ sub navigation_toolbar {
101
+ my ($nav_toolbar, $widget) = @_;
102
+
103
+ if ($widget->get_active) {
104
+ $nav_toolbar->show;
105
+ foreach my $child ($nav_toolbar->get_children) {
106
+ $child->show_all;
107
+ }
108
+ } else {
109
+ $nav_toolbar->hide;
110
+ foreach my $child ($nav_toolbar->get_children) {
111
+ $child->hide_all;
112
+ }
113
+ }
114
+
115
+ return;
116
+ }
117
+
118
+ 1;
t/Shutter/App/14_view.t ADDED
@@ -0,0 +1,111 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/usr/bin/perl
2
+ use 5.010;
3
+ use strict;
4
+ use warnings;
5
+
6
+ use Test::More;
7
+ use Test::MockModule;
8
+ use Glib qw/ TRUE FALSE /;
9
+ use Gtk3; # to escape warnings "Too late to run INIT block"
10
+
11
+ require_ok("Shutter::App::View");
12
+ can_ok("Shutter::App::View", qw/ zoom_in zoom_out zoom_100 zoom_best fullscreen navigation_toolbar /);
13
+
14
+ # Minimal recording stand-ins for the Gtk widgets the functions touch.
15
+ my $recorder_class = Test::MockModule->new("Local::Recorder", no_auto => 1);
16
+ $recorder_class->define(
17
+ new => sub {
18
+ my ($class, %args) = @_;
19
+ return bless {calls => [], %args}, $class;
20
+ },
21
+ calls => sub { return $_[0]->{calls} },
22
+ get_active => sub { return $_[0]->{active} },
23
+ get_children => sub { return @{$_[0]->{children} || []} },
24
+ );
25
+ for my $method (qw/ zoom_in zoom_out set_zoom set_fitting fullscreen unfullscreen show hide show_all hide_all /) {
26
+ $recorder_class->define(
27
+ $method => sub {
28
+ my ($self, @args) = @_;
29
+ push @{$self->{calls}}, [$method, @args];
30
+ return;
31
+ });
32
+ }
33
+
34
+ subtest "zoom functions act on the current image" => sub {
35
+ my %cases = (
36
+ zoom_in => [['zoom_in']],
37
+ zoom_out => [['zoom_out']],
38
+ zoom_100 => [['set_zoom', 1]],
39
+ zoom_best => [['set_fitting', TRUE]],
40
+ );
41
+
42
+ for my $fn (sort keys %cases) {
43
+ my $image = Local::Recorder->new;
44
+ my $other = Local::Recorder->new;
45
+ my %sessions = (a => {image => $image}, b => {image => $other});
46
+
47
+ my $code = Shutter::App::View->can($fn);
48
+ $code->(\%sessions, sub { return 'a' });
49
+
50
+ is_deeply($image->calls, $cases{$fn}, "$fn calls the right method on the current image");
51
+ is_deeply($other->calls, [], "$fn leaves other images alone");
52
+ }
53
+ };
54
+
55
+ subtest "zoom functions do nothing without a current file" => sub {
56
+ for my $fn (qw/ zoom_in zoom_out zoom_100 zoom_best /) {
57
+ my $image = Local::Recorder->new;
58
+ my %sessions = (a => {image => $image});
59
+ my $code = Shutter::App::View->can($fn);
60
+
61
+ $code->(\%sessions, sub { return FALSE });
62
+ $code->(\%sessions, sub { return undef });
63
+
64
+ is_deeply($image->calls, [], "$fn is a no-op when there is no current file");
65
+ is_deeply([sort keys %sessions], ['a'], "$fn does not autovivify session entries");
66
+ }
67
+ };
68
+
69
+ subtest "fullscreen" => sub {
70
+ my $window = Local::Recorder->new;
71
+ Shutter::App::View::fullscreen($window, Local::Recorder->new(active => TRUE));
72
+ Shutter::App::View::fullscreen($window, Local::Recorder->new(active => FALSE));
73
+ is_deeply($window->calls, [['fullscreen'], ['unfullscreen']], "toggles window fullscreen state");
74
+ };
75
+
76
+ subtest "navigation_toolbar" => sub {
77
+ my @children = map { Local::Recorder->new } 1 .. 2;
78
+ my $toolbar = Local::Recorder->new(children => \@children);
79
+
80
+ Shutter::App::View::navigation_toolbar($toolbar, Local::Recorder->new(active => TRUE));
81
+ is_deeply($toolbar->calls, [['show']], "shows toolbar when active");
82
+ is_deeply([map { $_->calls } @children], [[['show_all']], [['show_all']]], "shows all children");
83
+
84
+ $_->{calls} = [] for $toolbar, @children;
85
+
86
+ Shutter::App::View::navigation_toolbar($toolbar, Local::Recorder->new(active => FALSE));
87
+ is_deeply($toolbar->calls, [['hide']], "hides toolbar when inactive");
88
+ is_deeply([map { $_->calls } @children], [[['hide_all']], [['hide_all']]], "hides children");
89
+ };
90
+
91
+ SKIP: {
92
+ my $have_gtk = eval { Gtk3->init_check } || 0;
93
+ skip "no display for Gtk3", 1 unless $have_gtk;
94
+
95
+ subtest "real Gtk3 widgets" => sub {
96
+ my $toolbar = Gtk3::Toolbar->new;
97
+ my $toggle = Gtk3::ToggleButton->new;
98
+ $toolbar->hide;
99
+ $toggle->set_active(TRUE);
100
+ Shutter::App::View::navigation_toolbar($toolbar, $toggle);
101
+ ok($toolbar->get_visible, "toolbar visible after toggling on");
102
+
103
+ my $window = Gtk3::Window->new('toplevel');
104
+ $toggle->set_active(FALSE);
105
+ Shutter::App::View::fullscreen($window, $toggle);
106
+ pass("unfullscreen on a real window does not die");
107
+ $window->destroy;
108
+ };
109
+ }
110
+
111
+ done_testing();