Bug 492229

Summary: Discover Does Not Remember It's Windowed Size If Closed When Maximised
Product: [Applications] Discover Reporter: Eamonn Rea <eamonnrea>
Component: discoverAssignee: Plasma Bugs List <plasma-bugs-null>
Status: RESOLVED FIXED    
Severity: normal CC: aleixpol, nate
Priority: NOR    
Version First Reported In: 6.1.4   
Target Milestone: ---   
Platform: Other   
OS: Linux   
Latest Commit: Version Fixed/Implemented In: 6.2.0 with Frameworks 6.6
Sentry Crash Report:

Description Eamonn Rea 2024-08-26 17:47:31 UTC
SUMMARY
When Discover is maximised and then closed, when it is re-opened it will remember that it was closed while maximised, but does not remember its windowed size if windowed again. It will instead become windowed but keep the size of its maximised state, e.g. it will become a near-1920x1080-sized window if maximised, closed, re-opened, and un-maximised again.

If Discover is windowed, maximised, and then un-maximised (so skipping the "close Discover" step) it will remember its windowed size. This problem only occurs if Discover is maximised, closed, re-opened, and then windowed again.

If Discover is closed while windowed, it will remember its windowed size when re-opened as well. This bug is only present when Discover is maximised, closed, re-opened, and un-maximised.

This happens irrespective of display configuration (single display/multi-display, no scaling, single scale factor, multi screen scale factors). No other KDE application tested seemed to exhibit this behaviour, only Discover. I am not sure when this started.

STEPS TO REPRODUCE
1. Open Discover when it is in a smaller, windowed state.
2. Maximise the window.
3. Un-maximise the window.
4. Discover will remember its windowed size.
5. Maximise Discover again.
6. Close Discover while it is maximised.
7. Re-open Discover.
8. Un-maximise Discover.
9. It will not remember its window size and will become windowed but take on the maximised window size, but in windowed mode.

OBSERVED RESULT
Discover does not remember its windowed size if closed when it is maximised.

EXPECTED RESULT
Discover should remember its windowed size, matching what I understand to be the behaviour of most other applications.

SOFTWARE/OS VERSIONS
Operating System: Arch Linux 
KDE Plasma Version: 6.1.4
KDE Frameworks Version: 6.5.0
Qt Version: 6.7.2
Kernel Version: 6.10.6-zen1-1-zen (64-bit)
Graphics Platform: Wayland
Processors: 16 × AMD Ryzen 7 3700X 8-Core Processor
Memory: 31.3 GiB of RAM
Graphics Processor: AMD Radeon RX 7900 XTX

ADDITIONAL INFORMATION
Tested on Wayland, did not try X11.
Comment 1 Nate Graham 2024-08-27 19:56:23 UTC
Can reproduce.
Comment 2 Eamonn Rea 2024-08-27 20:41:04 UTC
Could this be because Discover stores window geometry and not width, height, and maxmized flags separately as other KDE applications appear to do?

For example, `systemmonitorrc` stores window information like this (removed some irrelevant information):

```
[General]
height=984
maximized=true
width=1866
```

Note that the "maximized" flag is not present for System Monitor if the application is not windowed, at least in my observations. The "width" and "height" flags do not update.

Whereas Discover stores the window's geometry, and this lines up with observations in the code. For example it seems Discover loads the window geometry based on the information in this "geometry" key (https://invent.kde.org/plasma/discover/-/blob/e958c9c1afcf79f09409505ebbf786d5cfc2fe7a/discover/DiscoverObject.cpp#L521), which doesn't contain minimized/maximised information. My understanding in Qt is that you can set the maximised window flag independent of the window geometry, so in theory you could set window geometry while a window is maximised, and once it is not maximised, it will then assume the size and position assigned. 

```
[Window]
geometry=0,1080,2560,1377
visibility=4
```

I cannot actually see where in this file that Discover stores whether it was maximised or not, so perhaps this is managed separately.

Other applications store window information differently, for example Spectacle (which is generally just a dialog) stores its information differently to Discover and System Monitor. It's also possible I'm looking in the wrong place, as System Settings remembers its window geometry (although not whether it was maximised or not) but it does not seem to store a config rc file in `~/.config`. Similarly, Dolphin stores a dolphinrc file, but it does not seem to get updated and has some window positioning weirdness when unmaximised (which I am planning to file as a separate but.)

If this is indeed the cause of  the bug, then I guess Discover would need to change how it stores its geometry information. Maybe it shouldn't update the saved geometry if it is closed while maximised? It seems to already be able to know when it was closed while maximised, and I would guess the Discover `window` object should be able to tell when it is maximised. If that would be the fix, I guess it would go somewhere here, when the application checks for a close event: https://invent.kde.org/plasma/discover/-/blob/e958c9c1afcf79f09409505ebbf786d5cfc2fe7a/discover/DiscoverObject.cpp#L550-562
Comment 3 Eamonn Rea 2024-08-27 20:48:40 UTC
Actually, the solution of not saving window geometry if closing when maximised would not fix the problem. It would introduce a problem where if you resize the window, maximise it, and then close Discover, then you re-open and window it again, it'll remember the old size and not the resized-size from when it was resized before it was maximised.

System Monitor appears to save the window width and height on resize, I didn't check the codebase but from examining the `systemmonitorrc` it looks to write these values while the window is being resized. Using auto-reload in Kate I can see that even during resizing the values update, before I let go of the mouse.

Maybe if System Monitor is hooking into some event to know when to save width and height, Discover could do something similar.
Comment 4 Eamonn Rea 2024-08-27 20:57:29 UTC
There may have been a similar issue for System Settings in the past actually that was fixed by this PR, purely based on the description: https://invent.kde.org/plasma/plasma-systemmonitor/-/merge_requests/255
Comment 5 Nate Graham 2024-08-29 20:05:31 UTC
Yes, you've figured out what's going on. When the window is maximized, it should stop saving its geometry to the file — both immediately, and on close.

Ultimately Discover should be ported to use KConfig.WindowStateSaver which at least centralizes the logic in one place so that bugs like this can be fixed centrally.
Comment 6 Nate Graham 2024-08-31 23:37:58 UTC
Git commit 9731120606d549c9b2014a49588c9e29a1758a2c by Nate Graham, on behalf of Eamonn Rea.
Committed on 31/08/2024 at 23:37.
Pushed by ngraham into branch 'master'.

Use KConfig.WindowStateSaver to handle window sizing
FIXED-IN: 6.2.0 with Frameworks 6.6

M  +0    -17   discover/DiscoverObject.cpp
M  +0    -2    discover/DiscoverObject.h
M  +7    -2    discover/qml/DiscoverWindow.qml

https://invent.kde.org/plasma/discover/-/commit/9731120606d549c9b2014a49588c9e29a1758a2c