Help language development. Donate to The Perl Foundation

Gnome::Gtk3 cpan:MARTIMM last updated on 2020-09-23

use v6;
use NativeCall;
use Test;

use Gnome::Glib::Main;
use Gnome::Gtk3::Main;

diag "\nGTK main loop test";

# initialize
my Gnome::Gtk3::Main $main .= new;

my Gnome::Glib::Main $gmain .= new;

diag "$*, Start thread";
my Promise $p = start {
  # wait for loop to start
  is $main.gtk-main-level, 1, "loop level now 1";

  diag "$*, Create context to invoke handler on thread";

  # This part is important that it happens in the thread where the
  # function is invoked in that context!
  my $main-context = $gmain.context-get-thread-default;

  diag "$*, Use g-main-context-invoke to invoke sub on thread";

    -> $d {

      diag "$*, In handler on same thread";
      diag "$*, Use gtk-main-quit() to stop loop";


  'test done'

is $main.gtk-main-level, 0, "loop level 0";
diag "$*, start loop with gtk-main()";
diag "$*, loop stopped";
is $main.gtk-main-level, 0, "loop level is 0 again";

await $p;
is $p.result, 'test done', 'result promise ok';