5

I wrote this accessibility extension:

Which works as supposed in Gnome Shell v3.14 & v3.16 but not in v3.10. It shows the only the initial keyboard modifiers state after i restarted it and never update it after that.

Here the full code:

const St = imports.gi.St;
const Mainloop = imports.mainloop;
const Main = imports.ui.main;
const Gdk = imports.gi.Gdk

let button, label, keymap;

function _update() {
    let symbols = "⇧⇬⋀⌥①◆⌘⎇";
    let state = keymap.get_modifier_state();    
    label.text = " ";
    for (var i=0; i<=8; i++ ) { 
        if (state & 1<<i) {
            label.text += symbols[i];
        } else {
            //label.text += "";
        }
    }
    label.text += " ";
}

function init() {
    button = new St.Bin({ style_class: 'panel-button',
                          reactive: false,
                          can_focus: false,
                          x_fill: true,
                          y_fill: false,
                          track_hover: false });

    label = new St.Label({ style_class: "state-label", text: "" });
    button.set_child(label);

    keymap = Gdk.Keymap.get_default();
    keymap.connect('state_changed',  _update );
    Mainloop.timeout_add(1000, _update );
}

function enable() {
    Main.panel._rightBox.insert_child_at_index(button, 0);
}

function disable() {
    Main.panel._rightBox.remove_child(button);
}

Trying to debug, I modified the code to show (state label + a counter)

let c,button, label, keymap;
c=0;

function _update() {
    Gtk.main_iteration_do(false);
    c++;
    let symbols = "⇧⇬⋀⌥①◆⌘⎇";
    //let keymap = Gdk.Keymap.get_default()
    let state = keymap.get_modifier_state();
    label.text = " ";
    for (var i=0; i<=8; i++ ) {
        if (state & 1<<i) {
            label.text += symbols[i];
        } else {
            //label.text += "";
        }
    }
    label.text += " "+c+" ";
    return true;
}

I can confirm these:

  • keymap.connect('state_changed', _update ); this signal is never raised
  • timeout callback works well
  • label is updated and show the initial state & the incrementing counter

So I think there is something with event loop as it does not pull state update or does not process its events.

Could you please point me to way to fix this and what's the difference between v3.10 & v3.14?

user.dz
  • 962
  • 2
  • 19
  • 39

1 Answers1

1

Assuming that commenting out the definition of keymap was intentional, check that it is still assigned elsewhere in your code. Have you tried using a -(minus) rather than a _(underscore)? Most events use the former in JS space, rather than the latter and this has been the problem for me when in several cases where I was attaching events to changing the active workspace, where the back-end for Meta.Display fires workspace_switched, the GJS space connects through workspace-switched and there are a lot more examples there.

For official documentation, including the correct event, property and function names for within GJS space, refer to GNOME DevDocs I don't know when it became official, but they state that it is here

RivenSkaye
  • 440
  • 3
  • 13
  • Thank you, I will reply as soon as I could try it. – user.dz Jul 18 '18 at 06:54
  • 1
    @user.dz a simple check on devdocs.baznga.org allows you to check all of the availlable functions and events and GNOME adopted it as official documentation platform. It states that as of GDK 2.16 the event ```Gdk.state-changed()``` is fired, emitting the ```::state-changed``` signal – RivenSkaye Jul 18 '18 at 07:20