| 1 | 3 | ▶ | use 5.008008; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 2 | 3 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 3 | 3 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 4 | |||
| 5 | package Ask::Tk; | ||
| 6 | |||
| 7 | our $AUTHORITY = 'cpan:TOBYINK'; | ||
| 8 | our $VERSION = '0.015'; | ||
| 9 | |||
| 10 | 3 | ▶ | use Moo; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 11 | 3 | ▶ | use Path::Tiny 'path'; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 12 | 0 | ▶ | use Tk; |
|
Subroutine
BEGIN: called CC=1 SCAR=2.6
| |||
| 13 | use namespace::autoclean; | ||
| 14 | |||
| 15 | with 'Ask::API'; | ||
| 16 | |||
| 17 | sub is_usable { | ||
| 18 | my ( $self ) = @_; | ||
| 19 | return !!$ENV{'DISPLAY'}; | ||
| 20 | } | ||
| 21 | |||
| 22 | sub quality { | ||
| 23 | return 30; | ||
| 24 | } | ||
| 25 | |||
| 26 | sub info { | ||
| 27 | my ( $self, %o ) = @_; | ||
| 28 | |||
| 29 | my $mw = "MainWindow"->new; | ||
| 30 | $mw->withdraw; | ||
| 31 | |||
| 32 | $o{messagebox_type} ||= 'ok'; | ||
| 33 | $o{messagebox_icon} ||= 'info'; | ||
| 34 | |||
| 35 | my $r = $mw->messageBox( | ||
| 36 | -title => $o{title} || '', | ||
| 37 | -message => $o{text} || '', | ||
| 38 | -type => $o{messagebox_type}, | ||
| 39 | -icon => $o{messagebox_icon}, | ||
| 40 | ); | ||
| 41 | $mw->destroy; | ||
| 42 | return $r; | ||
| 43 | } #/ sub info | ||
| 44 | |||
| 45 | sub warning { | ||
| 46 | my ( $self, %o ) = @_; | ||
| 47 | $self->info( messagebox_icon => 'warning', %o ); | ||
| 48 | } | ||
| 49 | |||
| 50 | sub error { | ||
| 51 | my ( $self, %o ) = @_; | ||
| 52 | $self->info( messagebox_icon => 'error', %o ); | ||
| 53 | } | ||
| 54 | |||
| 55 | sub question { | ||
| 56 | my ( $self, %o ) = @_; | ||
| 57 | 'Ok' eq $self->info( | ||
| 58 | messagebox_icon => 'question', | ||
| 59 | messagebox_type => 'OkCancel', | ||
| 60 | %o, | ||
| 61 | ); | ||
| 62 | } | ||
| 63 | |||
| 64 | sub entry { | ||
| 65 | my ( $self, %o ) = @_; | ||
| 66 | my $mw = "MainWindow"->new; | ||
| 67 | |||
| 68 | $mw->Label( -text => $o{text} )->pack | ||
| 69 | if exists $o{text}; | ||
| 70 | |||
| 71 | my $return = $o{entry_text}; | ||
| 72 | my $entry = $mw->Entry( | ||
| 73 | ( -show => '*' )x!!( $o{hide_text} ), | ||
| 74 | -textvariable => \$return, | ||
| 75 | )->pack; | ||
| 76 | |||
| 77 | $entry->bind( | ||
| 78 | '<Return>', | ||
| 79 | [ | ||
| 80 | sub { | ||
| 81 | $return = $entry->get; | ||
| 82 | $mw->destroy; | ||
| 83 | } | ||
| 84 | ] | ||
| 85 | ); | ||
| 86 | |||
| 87 | $entry->focus; | ||
| 88 | MainLoop; | ||
| 89 | return $return; | ||
| 90 | } #/ sub entry | ||
| 91 | |||
| 92 | sub file_selection { | ||
| 93 | my ( $self, %o ) = @_; | ||
| 94 | my @files; | ||
| 95 | my $mw = "MainWindow"->new; | ||
| 96 | $mw->withdraw; | ||
| 97 | |||
| 98 | my %TK = ( | ||
| 99 | -type => $o{directory} ? 'dir' : ( $o{save} ? 'save' : 'open' ), | ||
| 100 | ); | ||
| 101 | |||
| 102 | push @files, path $mw->FBox( %TK )->Show; | ||
| 103 | while ( $o{multiple} and $self->question( text => 'Select another?' ) ) { | ||
| 104 | push @files, path $mw->FBox( %TK )->Show; | ||
| 105 | } | ||
| 106 | |||
| 107 | $mw->destroy; | ||
| 108 | $o{multiple} ? @files : $files[0]; | ||
| 109 | } #/ sub file_selection | ||
| 110 | |||
| 111 | sub _choice { | ||
| 112 | my ( $self, %o ) = @_; | ||
| 113 | my $mw = "MainWindow"->new; | ||
| 114 | |||
| 115 | $mw->title( $o{title} ); | ||
| 116 | $mw->Label( -text => $o{text} )->pack if exists $o{text}; | ||
| 117 | |||
| 118 | my @return; | ||
| 119 | my $lbox = $mw->Listbox( -selectmode => ( $o{_mode} || 'single' ) )->pack; | ||
| 120 | $lbox->insert( end => map $_->[1], @{ $o{choices} } ); | ||
| 121 | |||
| 122 | $mw->Button( | ||
| 123 | -text => 'OK', | ||
| 124 | -command => sub { | ||
| 125 | @return = map $o{choices}[$_][0], $lbox->curselection; | ||
| 126 | $mw->destroy; | ||
| 127 | }, | ||
| 128 | )->pack; | ||
| 129 | |||
| 130 | MainLoop; | ||
| 131 | return @return; | ||
| 132 | } #/ sub _choice | ||
| 133 | |||
| 134 | sub multiple_choice { | ||
| 135 | my ( $self, %o ) = @_; | ||
| 136 | $o{title} = 'Choose' unless exists $o{title}; | ||
| 137 | $o{_mode} = 'multiple'; | ||
| 138 | my @r = $self->_choice( %o ); | ||
| 139 | return @r; | ||
| 140 | } | ||
| 141 | |||
| 142 | sub single_choice { | ||
| 143 | my ( $self, %o ) = @_; | ||
| 144 | $o{title} = 'Choose one' unless exists $o{title}; | ||
| 145 | $o{_mode} = 'single'; | ||
| 146 | my ( $r ) = $self->_choice( %o ); | ||
| 147 | return $r; | ||
| 148 | } | ||
| 149 | |||
| 150 | 1; | ||
| 151 | |||
| 152 | __END__ | ||