Repository: qutebrowser/qutebrowser

Issue:
Urgency hint is immediately unset
Running qutebrowser in i3-wm 4.13, Xorg-server 1.19.3-1, qt5 5.8.0; when opening a URL from e.g. console or another program using "qutebrowser -- https://test.whatever/", the urgency hint is set and then immediately unset, even if the qutebrowser instance isn't focused or even present on the same workspace.

Observed behaviour: the workspace number lights up for .2 seconds and then goes normal again. When switching to the workspace, there is no urgency highlighting on the window title.

Expected behaviour: the workspace number stays lighted indicating urgency on the workspace and window title.


Version info

qutebrowser v0.10.1
Git commit: 
Backend: QtWebEngine

CPython: 3.6.0
Qt: 5.8.0
PyQt: 5.8

sip: 4.19.1
colorama: no
pypeg2: 2.15
jinja2: 2.9.5
pygments: 2.2.0
yaml: 3.12
cssutils: no
typing: yes
PyQt5.QtWebEngineWidgets: yes
pdf.js: no
Webkit: 538.1
SSL: OpenSSL 1.0.2k  26 Jan 2017

Style: QFusionStyle
Platform: Linux-4.10.6-1-ARCH-x86_64-with-arch, 64bit
Frozen: False
Imported from /usr/lib/python3.6/site-packages/qutebrowser
Qt library executable path: /usr/lib/qt/libexec, data path: /usr/share/qt
OS Version: 

--- /etc/os-release ---
NAME="Arch Linux"
PRETTY_NAME="Arch Linux"
ID=arch
ID_LIKE=archlinux

Paths:
config: /run/user/1000/qutebrowser/default/config
data: /run/user/1000/qutebrowser/default/data
system_data: /usr/share/qutebrowser
cache: /run/user/1000/qutebrowser/default/cache
download: /run/user/1000/qutebrowser/default/download
runtime: /run/user/1000/qutebrowser/default/runtime



Discussion:
FWIW qutebrowser does:

```python
window.setWindowState(window.windowState() & ~Qt.WindowMinimized)
window.setWindowState(window.windowState() | Qt.WindowActive)
window.raise_()
window.activateWindow()
QApplication.instance().alert(window)
```

What might be happening here is that the raising/activating happens after the urgency hint is already set. Though I'd still expect i3 to not unset it, because it all happens on a different workspace. FWIW it works fine with herbstluftwm here.
Does [this](https://i3wm.org/docs/userguide.html#focus_on_window_activation) or [this](https://i3wm.org/docs/userguide.html#_delaying_urgency_hint_reset_on_workspace_change) make a difference?
Additional information: Opening 2 URLs in quick succession seems to result in the urgency hint remaining.

@lahwaacz's suggestions of using ```focus_on_window_activation``` or ```force_display_urgency_hint``` doesn't seem to make a difference; the hint disappears immediately regardless of these settings.
Seeing the same behavior on awesome-wm.
The workspace flashes the urgency color but returns to normal immediately after.
Should also add that so far I've only noticed this behavior with qutebrowser.

```
qutebrowser v0.10.1
Git commit: v0.10.0-666-g0f3a17ae8 (2017-05-23 10:09:06 +0200)
Backend: QtWebEngine (Chromium 49.0.2623.111)

CPython: 3.4.5
Qt: 5.7.1
PyQt: 5.8.2

sip: 4.19.1
colorama: 0.3.9
pypeg2: 2.15
jinja2: 2.8
pygments: 2.2.0
yaml: 3.12
cssutils: 1.0.1 $Id$
typing: no
PyQt5.QtWebEngineWidgets: yes
PyQt5.QtWebKitWidgets: yes
pdf.js: 1.7.225 (/usr/share/pdf.js/build/pdf.js)
SSL: OpenSSL 1.0.2k  26 Jan 2017

Style: QFusionStyle
Platform: Linux-4.11.0-sabayon-x86_64-Intel-R-_Core-TM-_i7-4710MQ_CPU_@_2.50GHz-with-gentoo-2.2, 64bit
Linux distribution: Sabayon/Linux (unknown)
Frozen: False
Imported from /usr/lib64/python3.4/site-packages/qutebrowser
Qt library executable path: /usr/lib64/qt5/libexec, data path: /usr/share/qt5
OS Version: 

--- /etc/system-release ---
Sabayon amd64 release 17.04

--- /etc/sabayon-release ---
Sabayon Linux amd64 17.04

--- /etc/os-release ---
NAME=Sabayon
ID=sabayon
PRETTY_NAME="Sabayon/Linux"

--- /etc/lsb-release ---
DISTRIB_ID="Sabayon"

--- /etc/gentoo-release ---
Gentoo Base System release 2.2

```
From IRC:

```
00:34 <MacGyver> The-Compiler: I do remember thinking quite recently that it actually is doing what you thought: raising/activating happens after the urgency hint is set and for some reason i3 unsets it then.
00:34 <Unode> MacGyver: ok. I'm actually looking for that 'raise' instruction on the issue. I wonder if this is fooling the WM to think the focus has been given even when on another desktop
00:34 <MacGyver> The-Compiler: I saw it happen on my dual-screen setup.
00:36 <MacGyver> The-Compiler: The urgency hint disappears *the moment* the new tab appears.
```
Did a bit more testing. I can also confirm what @MacGyverNL mentioned above.
Opening more than one url before focusing the browser keeps the urgency hint.

Steps to reproduce:
1) Start "qutebrowser --basedir /tmp/qute"
2) Change to another desktop
3) Run "qutebrowser --basedir /tmp/qute url"
4) Focus qutebrowser to make the urgency hint disappear.
5) Repeat 2) and 3) a few times before repeating 4)
6) Confirm behavior by closing browser and repeating 1-5 

I split the tests into a few different outcomes/behaviors.

a) works as expected:

```python
window.setWindowState(window.windowState() & ~Qt.WindowMinimized)
#window.setWindowState(window.windowState() | Qt.WindowActive)
window.raise_()
#window.activateWindow()
QApplication.instance().alert(window)
```

b) flickers on first url, **does not** work on second url.
After re-focusing qutebrowser and trying again it works as expected:

```python
#window.setWindowState(window.windowState() & ~Qt.WindowMinimized)
window.setWindowState(window.windowState() | Qt.WindowActive)
#window.raise_()
#window.activateWindow()
QApplication.instance().alert(window)
```

c) flickers on first url, **works** on second try

```python
#window.setWindowState(window.windowState() & ~Qt.WindowMinimized)
#window.setWindowState(window.windowState() | Qt.WindowActive)
#window.raise_()
window.activateWindow()
QApplication.instance().alert(window)
```

or

```python
window.setWindowState(window.windowState() & ~Qt.WindowMinimized)
window.setWindowState(window.windowState() | Qt.WindowActive)
window.raise_()
#window.activateWindow()
QApplication.instance().alert(window)
```
Any lead on how to fix this ?
I haven't looked into this yet, so I can't really tell you any more than what's in the comments above.
Wow thanks for the quick answer ! I understand it's low priority. Wish I had the skillsets to fix it and make a pull request

Resolving patch:
diff --git a/qutebrowser/mainwindow/mainwindow.py b/qutebrowser/mainwindow/mainwindow.py
--- a/qutebrowser/mainwindow/mainwindow.py
+++ b/qutebrowser/mainwindow/mainwindow.py
@@ -24,7 +24,8 @@
 import itertools
 import functools
 
-from PyQt5.QtCore import pyqtSlot, QRect, QPoint, QTimer, Qt
+from PyQt5.QtCore import (pyqtSlot, QRect, QPoint, QTimer, Qt,
+                          QCoreApplication, QEventLoop)
 from PyQt5.QtWidgets import QWidget, QVBoxLayout, QApplication, QSizePolicy
 
 from qutebrowser.commands import runners, cmdutils
@@ -98,6 +99,8 @@ def raise_window(window, alert=True):
     window.setWindowState(window.windowState() & ~Qt.WindowMinimized)
     window.setWindowState(window.windowState() | Qt.WindowActive)
     window.raise_()
+    QCoreApplication.processEvents(
+        QEventLoop.ExcludeUserInputEvents | QEventLoop.ExcludeSocketNotifiers)
     window.activateWindow()
 
     if alert:


Test patch: