Skip to content

Commit

Permalink
Improving --use-clipboard to provide error message and exit
Browse files Browse the repository at this point in the history
  • Loading branch information
phase1geo committed Apr 19, 2024
1 parent 5bba1fb commit a9ec965
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 9 deletions.
5 changes: 4 additions & 1 deletion src/Application.vala
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,10 @@ public class Annotator : Gtk.Application {
Process.exit( 1 );
}
} else if( use_clipboard ) {
appwin.do_paste();
if( !appwin.do_paste_image() ) {
stderr.printf( _( "\nERROR: Image does not exist on the clipboard\n" ) );
Process.exit( 1 );
}
} else if( take_screenshot ) {
appwin.do_screenshot();
}
Expand Down
12 changes: 9 additions & 3 deletions src/Clipboard.vala
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,10 @@ public class AnnotatorClipboard {
}

/* Called to paste current item in clipboard to the given Canvas */
public static void paste( Editor editor ) {
public static bool paste( Editor editor, bool image_only = false ) {

var clipboard = Display.get_default().get_clipboard();
var retval = false;

try {
if( clipboard.get_formats().contain_mime_type( "image/png" ) ) {
Expand All @@ -85,21 +86,26 @@ public class AnnotatorClipboard {
editor.paste_image( pixbuf, true );
}
});
} else if( clipboard.get_formats().contain_mime_type( "application/xml" ) ) {
retval = true;
} else if( !image_only && clipboard.get_formats().contain_mime_type( "application/xml" ) ) {
clipboard.read_async.begin( {"application/xml"}, 0, null, (obj, res) => {
string str;
var stream = clipboard.read_async.end( res, out str );
var contents = Utils.read_stream( stream );
editor.paste_items( contents );
});
} else if( clipboard.get_formats().contain_gtype( Type.STRING ) ) {
retval = true;
} else if( !image_only && clipboard.get_formats().contain_gtype( Type.STRING ) ) {
clipboard.read_text_async.begin( null, (obj, res) => {
var text = clipboard.read_text_async.end( res );
editor.paste_text( text );
});
retval = true;
}
} catch( Error e ) {}

return( retval );

}

}
23 changes: 18 additions & 5 deletions src/MainWindow.vala
Original file line number Diff line number Diff line change
Expand Up @@ -505,12 +505,25 @@ public class MainWindow : Gtk.ApplicationWindow {
_editor.canvas.do_cut();
}

/* Pastes text or images to the editor */
/* Pastes clipboard contents to the editor. This may also paste only images, if specified. */
private bool do_paste_internal( bool image_only ) {
if( AnnotatorClipboard.paste( _editor, image_only ) ) {
_welcome.sensitive = false;
_zoom_btn.set_sensitive( true );
_export_btn.set_sensitive( true );
return( true );
}
return( false );
}

/* Pastes text, images or items to the editor */
public void do_paste() {
_welcome.sensitive = false;
AnnotatorClipboard.paste( _editor );
_zoom_btn.set_sensitive( true );
_export_btn.set_sensitive( true );
do_paste_internal( false );
}

/* Pasts only an image from the clipboard to the editor */
public bool do_paste_image() {
return( do_paste_internal( true ) );
}

public void do_screenshot() {
Expand Down

0 comments on commit a9ec965

Please sign in to comment.