I cannot make TextView
inside a ListView
to capture mouse click properly. For some reason, I need to click the TextView
exactly five time (or three times if I set single click focus) to focus it. It happens only for TextView
, and other widgets like Button
and Entry
can be focused on a single click.
This happens first in a Rust gtk-rs project, then I replicated in a minimal Pygobject example. I don’t know if it’s a bug or intended, so I will post the script here:
import sys
import gi
gi.require_version("Gtk", "4.0")
from gi.repository import GObject, GLib, Gio, Gtk
class IntegerObject(GObject.Object):
value = GObject.Property(type=int, default=0)
def __init__(self, value=0):
super().__init__()
self.value = value
class MyApplication(Gtk.ApplicationWindow):
def __init__(self, **kwargs):
super().__init__(**kwargs, title="TextView in ListView")
GLib.set_application_name("Test TextView in ListView")
# The store
store = Gio.ListStore()
store.append(IntegerObject(0))
store.append(IntegerObject(42))
# Factory
factory = Gtk.SignalListItemFactory()
factory.connect("setup", self.on_setup)
factory.connect("bind", self.on_bind)
# The view
list_view = Gtk.ListView.new(Gtk.NoSelection.new(store), factory)
sw = Gtk.ScrolledWindow(hexpand=True, vexpand=True)
sw.set_child(list_view)
self.set_child(sw)
def on_setup(self, w, list_item):
tv = Gtk.TextView(hexpand=True, vexpand=True, height_request=64)
frame = Gtk.Frame(hexpand=True, vexpand=True)
frame.set_child(tv)
list_item.set_child(frame)
def on_bind(self, w, list_item):
int_obj = list_item.props.item
tv = list_item.props.child.props.child
int_obj.bind_property(
"value", tv.props.buffer, "text", GObject.BindingFlags.BIDIRECTIONAL
)
def on_activate(self):
window = MyApplication(application=self, default_width=640, default_height=480)
window.present()
app = Gtk.Application(application_id="com.example.TextViewListView")
app.connect("activate", on_activate)
status = app.run(sys.argv)
sys.exit(status)
Did I miss something? Is there some properties I have to set?
(BTW, the Pygobject documentation is bad. If I don’t already have a little experience in gtk-rs, I would never figure out how to do anything substantial in it.)