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

How to write methods in config.py #1748

Closed
zordsdavini opened this issue May 25, 2020 · 14 comments
Closed

How to write methods in config.py #1748

zordsdavini opened this issue May 25, 2020 · 14 comments

Comments

@zordsdavini
Copy link
Contributor

Issue description

Wanted to write method that should be run on hook and at start up: change top bar background color (indicate active/inactive screen).

Later solved issue could be transformed into documentation :)

Qtile version

0.15.1.dev122+gaaebfb55

Stack traces

2020-05-25 09:35:07,720 ERROR libqtile hook.py:fire():L358 Error in hook client_mouse_enter
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/libqtile/hook.py", line 356, in fire
    i(*args, **kwargs)
  File "/home/arnas/.config/qtile/config.py", line 584, in activate_screen_on_mouse_enter
    z_update_bar_bg(qtile)
  File "/home/arnas/.config/qtile/config.py", line 107, in z_update_bar_bg
    bar.draw()
  File "/usr/lib/python3.8/site-packages/libqtile/bar.py", line 310, in draw
    self.qtile.call_soon(self._actual_draw)
AttributeError: 'NoneType' object has no attribute 'call_soon'

Configuration

<...>
def z_update_bar_bg(qtile):
    current_screen = qtile.current_screen
    for screen in screens:
        bg_color = GREEN if current_screen == screen else RED
        bar = screen.top
        bar.background = bg_color
        bar.draw()
<...>
@hook.subscribe.screen_change
def restart_on_randr(qtile, ev):
    commands.reload_screen()
    qtile.cmd_restart()
    z_update_bar_bg(qtile)

@hook.subscribe.client_mouse_enter
def activate_screen_on_mouse_enter(window):
    qtile = window.group.screen.qtile
    window_screen = window.group.screen
    current_screen = qtile.current_screen

    z_update_bar_bg(qtile)
    if current_screen != window_screen:
        window.group.focus(window)

# @hook.subscribe.current_screen_change
# def update_active_top_bar_bg():
#     z_update_bar_bg()
@zordsdavini
Copy link
Contributor Author

tried with CommandClient but it looks like Qtile is still not initialized

@ramnes
Copy link
Member

ramnes commented May 25, 2020

I think you just have to iterate on qtile.screens in your function, not screens.

@zordsdavini
Copy link
Contributor Author

ok, error is gone but bar backgrouns is not change. I even tried to set background in config, but it looks it doesn't react on that.

Other thing is @hook.subscribe.current_screen_change. It doesn't accept any argument, so how can I get Qtile instance in code?

And group.focus(window) (in another screen than current) doesn't activate that window

@m-col
Copy link
Member

m-col commented May 25, 2020

That hook can get access to the Qtile instance if you define it in your main function:

def main(qtile):
    ....
    @hook...
    def func():
        qtile.do_something

As for the background not changing - widgets take their background from the bar's background only if they don't have their own set so I'd check that. Otherwise, maybe confirm that the widgets' draw methods are being called?

@ramnes
Copy link
Member

ramnes commented May 25, 2020

Other thing is @hook.subscribe.current_screen_change. It doesn't accept any argument, so how can I get Qtile instance in code?

Related to #1719

@ramnes
Copy link
Member

ramnes commented May 25, 2020

That hook can get access to the Qtile instance if you define it in your main function

Yes, but what a nasty hack!

@zordsdavini
Copy link
Contributor Author

Thanks @m-col @ramnes it is more clear now. Background is changing now, but not as I expected. Will try with main later.
But after all tests it looks like activating window doesn't activate screen :-/ I need to go through couple windows to get screen activated

@ramnes
Copy link
Member

ramnes commented May 25, 2020

I think it would be much simpler for you to add a current_screen_background_color argument or such to Bar, either by subclassing Bar in your configuration, or by adding it directly into Qtile's codebase.

That being said, I do find this interesting because it's yet another example to me that it's overly complicated to interact with the different Qtile objects from the configuration and that we have something wrong. I'm opening another issue one this subject.

@zordsdavini
Copy link
Contributor Author

the lasts code:

def z_update_bar_bg(qtile):
    current_screen = qtile.current_screen
    for screen in qtile.screens:
        bg_color = GREEN if current_screen == screen else RED
        top = screen.top
        for w in top.widgets:
            if 'windowname' == w.name:
                w.background = bg_color
        top.draw()

makes it quite simple, just needs to take right hook

@tych0
Copy link
Member

tych0 commented May 25, 2020

To me this just seems like we really do need to fix #1719.

@zordsdavini
Copy link
Contributor Author

@tych0 you mean to add qtile instance in all hooks?

@tych0
Copy link
Member

tych0 commented May 25, 2020

The title of the bug is "more sensible". I think adding the qtile object to all hooks is probably a good idea, but also for example current_screen_change should also receive the screen that is now focused. setgroup should get the newly focused group, etc.

@github-actions
Copy link

This issue is stale because it has been open 90 days with no activity. Remove stale label or comment or this will be closed in 30 days.

@ramnes
Copy link
Member

ramnes commented Aug 26, 2020

The qtile object can now be imported from libqtile directly. See #1791

@ramnes ramnes closed this as completed Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants