1

Can I execute code when a window comes into focus (becomes the active window, e.g. when the window above it is closed)? I hoped windows would just have focus event or attribute, but that doesn't seem to be the case.

There are focus events in Qt. If Enaml doesn't offer this, what's the easiest way to access these underlying Qt events?

I'd like to be able to do something like:

enamldef MyWindow(Window):
    focus ::
        do_stuff()
jmilloy
  • 7,875
  • 11
  • 53
  • 86
  • You may want to just use Qt/PyQt. This is the first time I've heard of `enaml`, but after looking at it briefly, I'm not sure the limitations provided by the abstraction are really worth it. It also seems to be lacking in documentation. – Brendan Abel Jun 21 '16 at 16:55
  • 1
    I disagree that it lacks documentation. It may not have a flashy website, but the API docs are extremely complete and verbose. There is also a directory full of examples for *almost* every feature: http://nucleic.github.io/enaml/docs/api_ref/index.html https://github.com/nucleic/enaml/tree/0f63b494345f2e03ce521adc2c38c6a0ce920266/examples I think it's a bit unfair for you to criticize after taking only an admitted "brief" look at the project. – Chris Colbert Jun 22 '16 at 14:47
  • The code is easy to read and fairly well-documented. The examples are useful. What's lacking is tutorial. I do have to look at the code itself (not just the docs) more often than I'd like. – jmilloy Jun 27 '16 at 17:20

1 Answers1

1

If you just want to track which widget has focus, you can use a FocusTracker object. Just create an instance of this anywhere, and react to the focused_widget attribute: https://github.com/nucleic/enaml/blob/0f63b494345f2e03ce521adc2c38c6a0ce920266/enaml/widgets/focus_tracker.py

For handling focus on a specif widget, you need to enable the feature flag and reimplement the handler functions: https://github.com/nucleic/enaml/blob/0f63b494345f2e03ce521adc2c38c6a0ce920266/enaml/widgets/widget.py#L88 https://github.com/nucleic/enaml/blob/0f63b494345f2e03ce521adc2c38c6a0ce920266/enaml/widgets/widget.py#L133 https://github.com/nucleic/enaml/blob/0f63b494345f2e03ce521adc2c38c6a0ce920266/enaml/widgets/widget.py#L300-L318

enamldef MyWindow(Window):
    Field:
        features = Feature.FocusEvents
        focus_gained => ():
            print 'got focus'
        focus_lost => ():
            print 'lost focus'

The code is behind a feature flag since the work required by the backend is non-trivial, and we don't want to do that work when it's not necessary.

There aren't any examples of focus handling, but here are some examples working with declarative functions and other "hidden" features like drag-drop: https://github.com/nucleic/enaml/tree/0f63b494345f2e03ce521adc2c38c6a0ce920266/examples/functions https://github.com/nucleic/enaml/blob/0f63b494345f2e03ce521adc2c38c6a0ce920266/examples/widgets/drag_and_drop.enaml

Chris Colbert
  • 868
  • 7
  • 12