NotepadNext icon indicating copy to clipboard operation
NotepadNext copied to clipboard

Bring existing window to front primary instance is detected (ubuntu/gnome)

Open zorba128 opened this issue 2 years ago • 4 comments

Running v0.5.1 on Ubuntu 18.04; when executing command to open file while existing instance is already running - it gets nicely added to tab list, but window is not brought to front (and one needs to manually dig it from below other applications).

[     0.216] I: =============================
[     0.216] I: NotepadNext v0.5.1
[     0.216] I: Build Date/Time: Apr 30 2022 19:14:55
[     0.216] I: Qt: 5.15.2
[     0.216] I: OS: Ubuntu 18.04.6 LTS
[     0.216] I: Locale: en_US
[     0.216] I: CPU: x86_64
[     0.216] I: File Path: /tmp/.mount_notepapY14eR/usr/bin/NotepadNext
[     0.216] I: Arguments: /tmp/.mount_notepapY14eR/AppRun.wrapped /tmp/a.txt
[     0.217] I: =============================
[     0.217] I: Primary instance already running. PID: 28998
[     0.220] I: Secondary instance closing...

zorba128 avatar May 31 '22 21:05 zorba128

Forceably bringing a window to the foreground is not as easy as it seems. For Windows there is some very OS specific code to get it to behave as desired:

https://github.com/dail8859/NotepadNext/blob/6888d4b9fb7544a5ba9ce06228ba603f37e06e5d/src/NotepadNext/dialogs/MainWindow.cpp#L1292-L1303

I have not found a good plain Qt way to do this (Maybe I just haven't found the right combination of Qt calls). Maximizing the main window works easily with plain Qt, but if the main window is up but just behind other windows...that's the difficult part. If anyone has suggestions, I'm open to trying them out.

dail8859 avatar Jun 01 '22 02:06 dail8859

For reference QOwnNotes uses this (similar to what I tried before but might work better on Ubuntu):

https://github.com/pbek/QOwnNotes/blob/9b2df420d4c7060651f730172b2a0efca8eeb833/src/main.cpp#L630-L645

            w.show();
            w.raise();
            w.activateWindow();

            // in case the window was minimized show it normal again
            // (it didn't came up when it was minimized on KDE)
            if (w.isMinimized()) {
                w.showNormal();
            }

dail8859 avatar Jun 27 '22 13:06 dail8859

If you are interested in testing this out you can download one of the AppImages from here

I tried it out myself on Ubuntu and it partially worked but wasn't perfect. I haven't been able to find any solid information how to make this happen. Other Qt projects due what I've shown above...which makes me think if that's all they are using that might be the best thing for Qt apps.

dail8859 avatar Jul 27 '22 00:07 dail8859

There's a few other suggestions here that might be worth one last attempt:

https://stackoverflow.com/questions/6087887/bring-window-to-front-raise-show-activatewindow-don-t-work

dail8859 avatar Jul 28 '22 13:07 dail8859

I can't find any guaranteed way to force it to the foreground on Linux. I took suggestions from https://stackoverflow.com/questions/6087887/bring-window-to-front-raise-show-activatewindow-don-t-work but even that doesn't seem to always work. Even with the changes there doesn't seem to be consistent behavior.

I'll call this 'good enough' for now as I'm not sure a valid, clean, Linux-generic solution even exists. Someone else will have to contribute any improvements in this area.

dail8859 avatar Nov 19 '22 23:11 dail8859

I don't know low level languages and OSes APIs. A dumb way to achieve this on linux is to call another app that can bring other windows to front. From what I see in Linux world it's pretty normal to call some other app to achieve the result and you can do a lot of stuff with your desktop by calling various apps (for instance: volume change).

It can be a temporary soltion, IMO it's a bit ugly, but it works and it even switches to virtual desktop where the app exists too (at least wmctrl).

Of course you should test for availablity of these apps, probably just before calling them is ok, if it adds 15ms it's not a problem.

So again - ugly, but still better than keeping the app in background :)

dzek69 avatar Jan 04 '23 17:01 dzek69

A dumb way to achieve this on linux is...

Interesting. I guess that is one possibility for now as long as it fails gracefully. I don't use Linux much any more so someone else might be able to getting around to trying something like this before I do.

So again - ugly, but still better than keeping the app in background :)

Agreed

dail8859 avatar Jan 04 '23 17:01 dail8859

as long as it fails gracefully

if you check if you can call for example wmctrl then it's a graceful solution, or you can take shortcuts and call wmctrl -a "Notepad Next" || true - it won't crash, it will either focus the window or do nothing

In the example Notepad Next should be a window title I feel. Not sure if that's a regex or just a *windowTitle* pattern, it works with just "Notepad Next" for sure.

And that's all I know. I couldn't get xdotool to work, but it's not a common package to have installed from what I know, there is much higher chance for wmctrl

Do it or leave it, I'm just sharing what I know, all I can do :)

dzek69 avatar Jan 04 '23 18:01 dzek69

@dzek69 Appreciate the info! Thanks!

dail8859 avatar Jan 04 '23 20:01 dail8859