How to detect the end of a text selection?

First, I tried using Gtk.GestureClick, but the Gtk.GestureClick “release” doesn’t fire when the mouse button is released at the end of the selection; it only fires after normal mouse clicks.

Then I tried using Gtk.GestureDrag, but the Gtk.GestureDrag “end” fires immediately after the first letter is selected and doesn’t fire again regardless of whether the selection continues. It also doesn’t fire when the mouse button is released at the end of the selection.

I also tried a “notify” connected to Gtk.TextView's “has-selection,” but it always fires as soon as the first letter is selected, just like Gtk.GestureDrag.

I’m really confused here, because Gtk.GestureDrag seems like the correct solution. Its purpose is to display a formatting bar that only appears when text is selected.

Hi,

What should happen when the user uses Shift+Arrows to select the text?

Try to use the “capture” propagation phase, instead of the default “bubble” one.

1 Like

Did some quick testing, did you try “unpaired-release” signal?

import gi
gi.require_version('Gtk', '4.0')
from gi.repository import Gtk

class Main:
  def __init__(self, app):
    self.mainwin = Gtk.ApplicationWindow.new(app)
    self.textview = Gtk.TextView.new()
    controller = Gtk.GestureClick.new()
    controller.set_button(0)
    controller.connect("unpaired-release", self.button_cb)
    self.textview.add_controller(controller)
    
    self.mainwin.set_child(self.textview)
    self.mainwin.set_default_size(400, 300)
    self.mainwin.present()

  def button_cb(self, _1, _2, _3, _4, _5=None):
    print(_1)

app = Gtk.Application()
app.connect('activate', Main)
app.run(None)
2 Likes

Thanks, I hadn’t tried the unpaired-release signal, it worked.