Strange behaviour from GestureClick in vala class (left/middle/right click)

I have a class that extends Gtk.Button

I have added a controller to the class that handle the button_pressed and button_released events

the buttons are in a Gtk.Grid object

when i middle click or right click the button label changes accordingly (and immediately)

but if i left click - the label changes and then does not respond to further middle or right click events

this is the bare bones of the class

using Gtk;

public class CalcButton : Gtk.Button {

//	controller

	private GestureClick button_pressed = new GestureClick ();

//	properties

	public int row { get; set; }
	public int column { get; set; }
	public int width { get; set;}
	public int height { get; set; }

//	constructor

	public CalcButton () {

		button_pressed.set_button (0);
		button_pressed.pressed.connect (calc_button_pressed);
		button_pressed.released.connect (calc_button_pressed);
		this.add_controller (button_pressed);

	}

//	signal handler

	private void calc_button_pressed (GestureClick gesture,
									  int n_press,
									  double x,
									  double y) {

		switch (gesture.get_current_button ()) {

		case 1:
			this.label = "mouse left";
			break;

		case 2:
			this.label = "middle";
			break;


		case 3:
			this.label = "mouse right";
			break;

		default:
			this.label = "something else";
			break;
		}
	}
}

given that with Gtk 4 the move is away from add_events to controllers this seems odd behaviour

i’ve seen some mention that Gtk.Button has its own handlers for pressed and release - but these events are not exposed in Gtk.Button - only the clicked event is exposed

cheers,
G.

further update to this

added this to the controller

		button_pressed.set_propagation_phase (1);

so that my controller gets the event during the capture phase
now behaving as it should - my bad for not reading further
cheers,
G.

to update this - i cleaned up the code and used the appropriate enums from Gdk and Gtk

using Gtk;
using Gdk;

public class CalcButton : Gtk.Button {

//	controller

	private GestureClick button_pressed = new GestureClick () {
		button = 0,
		propagation_phase = PropagationPhase.CAPTURE
	};

//	methods
//	get and set

	public int row { get; set; }
	public int column { get; set; }
	public int width { get; set;}
	public int height { get; set; }

//	constructor

	public CalcButton () {

		this.button_pressed.pressed.connect (calc_button_pressed);
		this.add_controller (button_pressed);

	}

//	signal handler

	private void calc_button_pressed (GestureClick gesture,
									  int n_press,
									  double x,
									  double y) {

		switch (gesture.get_current_button ()) {

		case BUTTON_PRIMARY:
			this.label = "mouse left";
			break;

		case BUTTON_MIDDLE:
			this.label = "middle";
			break;

		case BUTTON_SECONDARY:
			this.label = "mouse right";
			break;

		default:
			break;
		}
	}
}

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.