@@ -42,7 +42,8 @@ public class MainWindow : Gtk.ApplicationWindow {
4242
4343 private const GLib . ActionEntry [] action_entries = {
4444 { " action_open" , do_open },
45- { " action_screenshot" , do_screenshot },
45+ { " action_screenshot" , action_screenshot },
46+ { " action_screenshot_nonportal" , action_screenshot_nonportal, " i" },
4647 { " action_save" , do_save },
4748 { " action_quit" , do_quit },
4849 { " action_undo" , do_undo },
@@ -196,7 +197,9 @@ public class MainWindow : Gtk.ApplicationWindow {
196197 _screenshot_btn = new Button .from_icon_name( get_icon_name( " insert-image" ) ) {
197198 tooltip_markup = Utils.tooltip_with_accel ( _ ( "Take Screenshot " ), "<Control>t" )
198199 };
199- _screenshot_btn.clicked.connect ( do_screenshot );
200+ _screenshot_btn.clicked.connect (() => {
201+ do_screenshot( _screenshot_btn );
202+ });
200203 header.pack_start ( _screenshot_btn );
201204
202205 _undo_btn = new Button .from_icon_name( get_icon_name( " edit-undo" ) ) {
@@ -355,7 +358,9 @@ public class MainWindow : Gtk.ApplicationWindow {
355358 paste.clicked.connect ( do_paste );
356359
357360 var screenshot = _welcome.append_button ( new ThemedIcon ( "insert -image " ), _ ( "Take A Screenshot " ), _ ( "Open an image from a screenshot " ) );
358- screenshot.clicked.connect ( do_screenshot );
361+ screenshot.clicked.connect (() => {
362+ do_screenshot( screenshot );
363+ });
359364
360365 /* Initialize the clipboard */
361366 AnnotatorClipboard.get_clipboard ().changed.connect (() => {
@@ -539,7 +544,148 @@ public class MainWindow : Gtk.ApplicationWindow {
539544 return ( do_paste_internal( true ) );
540545 }
541546
542- public void do_screenshot() {
547+ // -------------------------------------------------------------
548+ // Main procedure that initiates a screenshot. If we are using
549+ // elementary OS 7.1 or below, we will roll our own screenshot
550+ // UI and functionality; otherwise, we will use the screenshot
551+ // portal.
552+ public void do_screenshot( Widget ? parent ) {
553+ var os = Environment . get_os_info( " ID" );
554+ var version = Environment . get_os_info( " VERSION" );
555+ if ( (os == " elementary" ) && (double . parse(version) < 8.0 ) ) {
556+ if ( parent == null ) {
557+ do_screenshot_nonportal( CaptureType . SCREEN );
558+ } else {
559+ show_screenshot_popover( parent );
560+ }
561+ } else {
562+ do_screenshot_portal();
563+ }
564+ }
565+
566+ // -------------------------------------------------------------
567+ // Returns the capture mode as determined by the user
568+ private void show_screenshot_popover( Widget parent ) {
569+
570+ var box = new Box ( Orientation . VERTICAL , 10 ) {
571+ margin_start = 10 ,
572+ margin_end = 10 ,
573+ margin_top = 10 ,
574+ margin_bottom = 10
575+ };
576+
577+ var shot_menu = new GLib .Menu ();
578+
579+ for ( int i= 0 ; i< CaptureType . NUM ; i++ ) {
580+ var mode = (CaptureType )i;
581+ shot_menu. append( mode. label(), " win.action_screenshot_nonportal(%d )" . printf( i ) );
582+ }
583+
584+ var opt_menu = new GLib .Menu ();
585+
586+ var delay_item = new GLib .MenuItem ( null , null );
587+ delay_item. set_attribute( " custom" , " s" , " delay" );
588+ opt_menu. append_item( delay_item );
589+
590+ var delay = new Label ( _( " Delay (in seconds)" ) + " :" ) {
591+ halign = Align . START ,
592+ hexpand = true
593+ };
594+ var delay_sb = new SpinButton .with_range( 0 , 6 , 1 ) {
595+ halign = Align . END
596+ };
597+ delay_sb. value = Annotator . settings. get_int( " screenshot-delay" );
598+ delay_sb. value_changed. connect(() = > {
599+ Annotator . settings. set_int( " screenshot-delay" , (int )delay_sb. value );
600+ });
601+
602+ var dbox = new Box ( Orientation . HORIZONTAL , 10 );
603+ dbox. append( delay );
604+ dbox. append( delay_sb );
605+
606+ var include_item = new GLib .MenuItem ( null , null );
607+ include_item. set_attribute( " custom" , " s" , " include" );
608+ opt_menu. append_item( include_item );
609+
610+ var include = new Label ( _( " Include Annotator window" ) + " :" ) {
611+ halign = Align . START ,
612+ hexpand = true
613+ };
614+ var include_sw = new Switch () {
615+ halign = Align . END ,
616+ active = Annotator.settings.get_boolean ( "screenshot -include -win " )
617+ };
618+ include_sw.notify["active"].connect ((value ) => {
619+ Annotator . settings. set_boolean( " screenshot-include-win" , include_sw. active );
620+ });
621+
622+ var ibox = new Box ( Orientation . HORIZONTAL , 10 );
623+ ibox.append ( include );
624+ ibox.append ( include_sw );
625+
626+ var menu = new GLib .Menu ();
627+ menu.append_section ( null , shot_menu );
628+ menu.append_section ( null , opt_menu );
629+
630+ var popover = new PopoverMenu .from_model( menu ) {
631+ position = PositionType . BOTTOM
632+ };
633+ popover.set_parent ( parent );
634+ popover.add_child ( dbox , "delay " );
635+ popover.add_child ( ibox , "include " );
636+ popover.popup ();
637+
638+ }
639+
640+ private void action_screenshot() {
641+ do_screenshot( _screenshot_btn );
642+ }
643+
644+ private void action_screenshot_nonportal( SimpleAction action, Variant ? variant ) {
645+ if ( variant != null ) {
646+ var mode = (CaptureType )variant. get_int32();
647+ do_screenshot_nonportal( mode );
648+ }
649+ }
650+
651+ public void do_screenshot_nonportal( CaptureType capture_mode ) {
652+
653+ /* If we aren't capturing anything, end now */
654+ if ( capture_mode == CaptureType . NONE ) return ;
655+
656+ var backend = new ScreenshotBackend ();
657+ var delay = Annotator . settings. get_int( " screenshot-delay" );
658+ var include = Annotator . settings. get_boolean( " screenshot-include-win" );
659+
660+ /* Hide the application */
661+ if ( ! include ) {
662+ hide();
663+ }
664+
665+ backend. capture. begin (capture_mode, delay, false , false /* redact */ , (obj, res) = > {
666+ Gdk . Pixbuf ? pixbuf = null ;
667+ try {
668+ pixbuf = backend. capture. end (res);
669+ } catch (GLib . IOError . CANCELLED e) {
670+ // TBD
671+ } catch (Error e) {
672+ // TBD
673+ }
674+ if (pixbuf != null ) {
675+ _editor. paste_image( pixbuf, false );
676+ _zoom_btn. set_sensitive( true );
677+ _export_btn. set_sensitive( true );
678+ }
679+ if ( ! include ) {
680+ show();
681+ }
682+ });
683+
684+ }
685+
686+ // -------------------------------------------------------------
687+ // Generate a screenshot using the screenshot portal.
688+ private void do_screenshot_portal() {
543689
544690 _welcome. sensitive = false ;
545691
0 commit comments