scorninpc / php-gtk3

GTK3 extension for PHP
https://andor.com.br/
GNU Lesser General Public License v3.0
118 stars 13 forks source link

Gtk::source_remove #125

Closed d47081 closed 4 months ago

d47081 commented 4 months ago

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()
scorninpc commented 4 months ago

To remove timeout you can just return false

btw i'll look deep it

scorninpc commented 4 months ago

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();