Bug 468184

Summary: Kwin moving windows and resizing them very poorly to unusable states on multi-monitor setups during unplug events
Product: [Plasma] kwin Reporter: Michael Butash <michael>
Component: multi-screenAssignee: KWin default assignee <kwin-bugs-null>
Status: RESOLVED DUPLICATE    
Severity: normal CC: g.guerin, nate
Priority: NOR    
Version: 5.27.2   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed In:
Sentry Crash Report:

Description Michael Butash 2023-04-05 21:52:10 UTC
SUMMARY
KDE in general has been bad at dealing with windows placements for some time for me in using 4x displays is my normal, but every time geometry changes with undocking (or a power hit/glitch simulating one), removing 3x displays then moves windows and resizes them to unusable and unfindable widths, and never restores them to proper placement/geometry upon restoration of desired kscreen arrangement.

STEPS TO REPRODUCE
1. Configure 4x displays in extended mode right to left in a 15360x2160 display set
2. Arrange various applications such as firefox, konsole, okular, steam, keepassxc, libreoffice, and other applications across all 4 displays.
3. Disconnect the Thunderbolt 4 dock adding the 3x external ports/displays to the unit
4. Watch KDE move windows the laptop display normally the far left-most display, adjusting placement to the right left of the screen, and resizing them horizontally to a much smaller with (5-100px wide), but not vertically.
5. Reconnect thunderbolt dock, observe KDE restore 3x additional displays and arrangement.
6. Observe some of the moved windows are replaced back to their original placement, but typically only a small subset
7. Observe others are left on the far left-most laptop display with poorly resized horizontal pixel width

OBSERVED RESULT
Using 4x displays, 1x built-in on a laptop, and 3x external connected to a Thunderbolt 4 dock, when dock is disconnected, kwin shuffles applications to the remaining laptop display, but in doing so commonly will mangle horizontal window geometry of most, but not all open applications, and often overlap them very non-optimal ways.

Once the dock is reconnected/restored, kscreen restores the display arrangement, but kwin is not restoring all the windows to their proper placement or geometry.  Even stranger is it's almost random where it moves things each time, when it happens moving things small and small widths when resizing, and moving some displays half off different parts of the screens.

Seeing as this can happen at times several times a day, shuffling my applications about each time, resizing them so small horizontally even finding them on a tiny 15" laptop displays is extremely difficult where the 3x other displays are 50" TV's.  Even stranger is it gets incrementally worse each time it happens.

EXPECTED RESULT
At bare minimum, seeing these are all the same 3840x2160 displays, it should have no reason to resize the horizontal widths between across different resolutions, and once kscreen re-applies its given screen profile for the given displays to restore geometry, those displays should be replaced across the original set of displays.

SOFTWARE/OS VERSIONS
Linux/KDE Plasma: Arch Linux, kernel 6.2.5-arch1-1
KDE Plasma Version: 5.27.2
KDE Frameworks Version: 5.103.0
Qt Version: 5.15.8

ADDITIONAL INFORMATION
This has been happening for some time across prior and new laptops for years now commonly, where recently clearing kscreen, plasmascreenrc, desktop-appletsrc files from home .config/.local files helped this this initially, but each time is incrementally regressing worse in worse in moving/resizing different apps in different places/sizes.

It seems if it needs to resize a window, it should never consider resizing a something from 2000px wide to only 5px wide.  As well, it would be nice if when it does move all my windows to a single display to at least tile them in some rough fashion vs. a dogpile into the left and bottom-most corner on the display.

I have attempted to add using the kwin script "restoreToScreen" to help this, but with or without doesn't seem to matter.  Feedback on this script's github is that kwin has taken this feature on, but seems to do so poorly with or without the script.  https://github.com/Hegemonia123/restoreToScreen/issues

I am unsure where to look for troubleshooting anything related windows placement state tracking, but would happy to provide requested information.
Comment 1 Michael Butash 2023-04-05 22:01:40 UTC
Some additional feedback, not all windows will get resized, or not all the time.  Mostly commonly affected resizing tends affect firefox, libreoffice, konsole, ksettings, pluma, others, but NOT keepassxc, okular, dolphin, telegram/signal electron apps, again others.  It's enough varieties it's not prone to any one UI like QT vs. GTK or like.

As well the resizing after clearing settings only started after 2 weeks, where originally it was perfect restoring my windows to the right places, but slowly gets worse in moving/resizing over time.  Each time now seems to bring stranger and stranger placements where windows are moved or resized to.
Comment 2 Gauthier 2023-04-23 21:16:35 UTC
I also have an issue along those lines. 

When unpluging my external screen from my laptop, many windows (e.g. firefox and gnucash) are not (or not appropriately) resized when moved to the laptop screen. Specifically they are too heigh with the title bar being out of the screen, meaning I cannot move/minmize/maximize/close them, I have to use the shortcut Ctrl + F5 to move them and then resize manually.

External screen is a 34" 3440x1440 whereas laptop screen is 14" 1920x1080 (no scaling applied to either).

Operating System: KDE neon 5.27
KDE Plasma Version: 5.27.4
KDE Frameworks Version: 5.105.0
Qt Version: 5.15.9
Kernel Version: 6.1.22-060122-generic (64-bit)
Graphics Platform: Wayland
Processors: 8 × Intel® Core™ i5-8350U CPU @ 1.70GHz
Memory: 15.3 GiB of RAM
Graphics Processor: Mesa Intel® UHD Graphics 620

I have no idea how Kwin handles this currently (and so I don't want to tell dev how to do things) but just in case it's useful, it sounds like a systematic approach when it comes to window size and placement could be:

To define window size on any given screen:
====================================
width ratio = (window width in px / screen width in px)
height ratio = (window height in px/ screen height in px)
>> those width/height ratios can then be applied to different size screens' height / width in plug / unplug events which would conserve the same relative window size across any screen (independently of absolute screen size or aspect ration).

To define windows placement:
=========================
horizontal ratio: distance between screen left edge and window horizontal centre / distance between screen left edge and screen horizontal centre (i.e. half screen width)
vertical ratio: distance between screen top edge and window vertical centre / distance between screen top edge and screen vertical centre (i.e. half screen height)
>> those ratio can then be applied to the window (horizontal / vertical) centre in plug / unplug events so the relative placement is conserved across any screens (independently of absolute screen size or aspect ration).
Comment 3 Gauthier 2023-04-23 21:25:43 UTC
Also wonder if this is related to the bug 452118, at least they both relate to window resizing on plug / unplug.
Comment 4 Michael Butash 2023-04-25 18:23:17 UTC
Agree with Gauthier, there are some deeply rooted problems here common to various scenarios folks seem to see in more exotic display setups (sadly anything +1 or more).

Checking back in on this, I'd been working with the dev of restoreToScreen that did make some changes, and seems to be much better behaved now, but per Zamundaaa's posting to restoreToScreen's github issues, supposedly kwin does this now too, so which should we really be relying on and/or fixing?  If kde should be managing screens without the restoreToScreen kwin script, it certainly doesn't do so properly, so should that be fixed, or simply not used or at least trusted to work?

Since restoreToScreen made changes for current kde, my windows mostly track the screens properly again except a few windows, notably virtualbox and gkrellm still that refuse to move, or if so badly.  Occasionally however the problems seem to come when my edid adapters or even the display sorta go braindead during a display geometry change event, and kde/kscreen *learns* a new display array profile until I physically reset power on them (reliable hardware is hard to find).  When having issues prior, I had some 40-ish display profiles saved by kscreen after 6 months of a new laptop that upon clearing out, life got much better, but again that grows now.  

When I get severely bad window resizing, it's usually because my displays landed on something new like missing a display suddenly, or displays otherwise change.  All displays are 4k however, and in theory nothing should have to change such as a display suddenly going to 640x480 and back, so not sure why it so grossly resizes my windows to like 5px wide, but still normally tall.  On a 4k display, when they all move to my 15" display, I hate my life to find and resize back.  It should simply never, like ever, do this in its worst days, or have some sympathy to undo it's screw-up to put things back as they were.

It would be nice to have a way to manage these auto-learned display array setups to either accept or summarily reject, but either way not retain indefinitely to confuse kde over time, particularly across major versions such as 5.27 with multi-monitor changes.  Otherwise, it would be nice to properly scope how kde/kscreen *should* handle window placements with or without external script help.
Comment 5 Nate Graham 2023-04-26 19:49:44 UTC

*** This bug has been marked as a duplicate of bug 452118 ***