scorninpc/php-gtk3

Gtk::source_remove

d47081 opened this issue · 2 comments

I want to reset GTK timeout added using native API, seems that Gtk::source_remove not implemented yet?

here is an example in Python

import gi
gi.require_version('Gtk', '3.0')
from gi.repository import Gtk, GLib

class EntryWithDelay(Gtk.Entry):
    def __init__(self):
        Gtk.Entry.__init__(self)
        self.timeout_id = None
        self.connect("key-press-event", self.on_key_press)

    def on_key_press(self, widget, event):
        if self.timeout_id:
            GLib.source_remove(self.timeout_id)  # Remove previous timeout if exists

        # Set a new timeout for 500 milliseconds
        self.timeout_id = GLib.timeout_add(500, self.on_timeout, widget)
    
    def on_timeout(self, widget):
        text = widget.get_text()
        print(f"Entry text after delay: {text}")
        self.timeout_id = None
        return False

win = Gtk.Window()
entry = EntryWithDelay()
win.add(entry)
win.connect("destroy", Gtk.main_quit)
win.show_all()
Gtk.main()

To remove timeout you can just return false

btw i'll look deep it

done as https://docs.gtk.org/glib/type_func.Source.remove.html

<?php

Gtk::init();

$count = 0;
$timer = Gtk::timeout_add(1000, function() use (&$count) {
	
	global $timer;

	$count++;

	echo "OK " . $count .  "\n";

	
	if($count >= 3) {
		echo "Finish\n";
		\Gtk::source_remove($timer);
	}

	// tell to continue calling
	return TRUE;
});

Gtk::main();