Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Show previous message sent on up and down arrow keys press #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

chimosky
Copy link
Member

@chimosky chimosky commented Jan 4, 2023

Fixes #27

Pressing the up arrow key shows previously entered messages and the entry seems to lose focus but pressing the up arrow key still works even if there's no visible indication of the entry gaining focus, pressing the down arrow key at this point doesn't work as the entry doesn't have focus.

Pressing the down arrow key when the entry has focus works as expected and when they're no more messages to show, the entry is empty for any new messages.

I'm not sure if this is a Gtk or Sugar bug but same thing doesn't happen in a standalone program so it is most likely a Sugar problem.

I'm wondering if we should leave the behaviour as it is right now but I'm still taking a look to see if I"ll find a solution to the issue stated above.

@quozl kindly review.

Pressing the up arrow key shows previously entered messages and the entry seems to lose focus
but pressing the up arrow key still works even if there's no visible indication of the entry gaining focus,
pressing the down arrow key at this point doesn't work as the entry doesn't have focus.

Pressing the down arrow key when the entry has focus works as expected and when they're no more messages to
show, the entry is empty for any new messages.

I'm not sure if this is a Gtk or Sugar bug but same thing doesn't happen in a standalone program so it is
most likely a Sugar problem.

Signed-off-by: Chihurumnaya Ibiam <[email protected]>
@quozl
Copy link
Contributor

quozl commented Jan 4, 2023

Had a quick look, did not test. You mentioned another program that gets it right. What program was that? Sometimes we have had to fix problems in the Sugar Toolkit.

@chimosky
Copy link
Member Author

chimosky commented Jan 4, 2023

Had a quick look, did not test. You mentioned another program that gets it right. What program was that? Sometimes we have had to fix problems in the Sugar Toolkit.

A standalone I wrote to test the bug.

import gi
gi.require_version('Gtk', '3.0')

from gi.repository import Gtk, Gdk


class Entry(Gtk.Window):

    def __init__(self):
        Gtk.Window.__init__(self, type=Gtk.WindowType.TOPLEVEL)

        self.text = []
        self._index = -1

        fixed = Gtk.Fixed()
        self.add(fixed)
        fixed.show()
        

        self.entry = Gtk.Entry()
        fixed.put(self.entry, 0, 0)
        self.entry.connect('activate', self._entry_activated_cb)
        self.entry.connect('key-press-event', self._entry_keypress_cb)
        self.entry.show()

        self.show_all()

    def _entry_activated_cb(self, entry):
        text = entry.props.text

        if text:
            self.text.append(text)
            entry.props.text = ''

    def _entry_keypress_cb(self, widget, event):

        if event.keyval == Gdk.KEY_Up:
            self._last_key = 'Up'
            self._set_text_on_key_press(widget)
        elif event.keyval == Gdk.KEY_Down:
            self._last_key = 'Down'
            self._set_text_on_key_press(widget)

    def _set_text_on_key_press(self, entry):

        if self._index < -(len(self.text)):
            self._index = -(len(self.text))

        if self._last_key == 'Up' and len(self.text) > 0:
            entry.set_text(self.text[self._index])
            self._index -= 1

        if self._last_key == 'Down':
            if self._index < -1:
                self._index += 1
                entry.set_text(self.text[self._index])
            elif self._index == -1:
                entry.set_text("")

        self.entry.set_position(-1)
        self.entry.grab_focus_without_selecting()


if __name__ == "__main__":
    a = Entry()
    a.connect("destroy", Gtk.main_quit)
    Gtk.main()

@quozl
Copy link
Contributor

quozl commented Jan 5, 2023

Thanks. It shows the problem is likely to be the toolkit. I suggest adding the toolkit to the test program as a first step, to verify the theory.

@chimosky
Copy link
Member Author

chimosky commented Jan 10, 2023

Thanks. It shows the problem is likely to be the toolkit. I suggest adding the toolkit to the test program as a first step, to verify the theory.

I'd put the program in a test activity and the issue persisted, any ideas for what to look for?

set_canvas is one thing that seems common besides initializing sugar3.activity.activity.Activity and that's where the call to sugar3.graphics.window.Window happens which has a key-press-event cb but the handler doesn't seem to do anything unexpected.

@quozl
Copy link
Contributor

quozl commented Jan 11, 2023

Given that a contributing cause of the problem is the spontaneous change to focus, I suggest looking in particular at the widget.grab_focus and window.focus attempts in the toolkit. e.g. try commenting them out and testing again.

Testing with a modified toolkit can be as simple as copying src/sugar3 recursively into your activity source directory. Imports are then local, and can be proven with strace. It has been a while since I did this for testing.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Loss of focus on up arrow key
2 participants