0

I'm trying to format the output of Gtk.SpinButton in GJS. The goal for now is to simply prepend a string "prefix" to SpinButton's text.

Here's a barebone implementation:

#!/usr/bin/gjs

imports.gi.versions.Gtk = "3.0";

const { Gtk } = imports.gi;

Gtk.init(null);

let window = new Gtk.Window();
let adjustment = Gtk.Adjustment.new(0, 0, 10, 1, 0, 0);
let spinButton = new Gtk.SpinButton();

spinButton.adjustment = adjustment;
spinButton.wrap = true;
spinButton.width_chars = 10;

window.add(spinButton);

spinButton.connect('output', formatOutput);
spinButton.connect('value-changed', reportChange);

function formatOutput(button) {
    let prefixed = `prefix${button.adjustment.value}`;
    button.set_text(prefixed);
    return true;
}

function reportChange(button) {
    log(`value changed ${button.value}`);
}

window.show_all();

window.connect('destroy', Gtk.main_quit);

Gtk.main();

As suggested by the documentation, I tap into Gtk.SpinButton's output signal.

The code above works fine when I:

  1. Scroll on the Gtk.SpinButton with mouse wheel
  2. Click and hold Gtk.SpinButton's -/+ buttons with mouse left button

However, single clicks on -/+ buttons do not change the value by more than two increments (e.g. if initial value is 0 and step-size is 1, incrementing with single clicks will only reach 1 (or -1 in other direction) and stop there).

Furthermore, it seems that whenever the output signal is connected (even if it merely returns true), a single click fires the value-changed signal twice: SpinButton's value is first set to zero and then incremented. The result is that, for single clicks, the value of the Gtk.SpinButton is stuck at the value 0+increment or 0-increment. I have no clue how to explain this behavior.. Any ideas? Thanks

1 Answers1

0

It seems that we also need to connect to Gtk.SpinButton's input signal (https://gjs-docs.gnome.org/gtk30~3.24.18/gtk.spinbutton#signal-input), which we cannot handle because it has a GPointer as one of it's parameters. Ultimately, I wanted to use Gtk.SpinButton for time of the day selection, but it doesn't seem to be possible with GJS. At least for now.