Reduce duplicate code calculating popup position

Authored by gladhorn on Aug 11 2019, 7:55 PM.



The popup/exec if was duplicated three times, only to calculate the y position.

Diff Detail

R108 KWin
Automatic diff as part of commit; lint not applicable.
Automatic diff as part of commit; unit tests not applicable.
gladhorn created this revision.Aug 11 2019, 7:55 PM
Restricted Application added a project: KWin. · View Herald TranscriptAug 11 2019, 7:55 PM
Restricted Application added a subscriber: kwin. · View Herald Transcript
gladhorn requested review of this revision.Aug 11 2019, 7:55 PM

Did you test the menu position in particular in edge cases?


Remove local var and just call

Workspace::self()->clientArea(ScreenArea, QPoint(x, y),

Thanks to your big duplicate reduction you can now remove the local var.

gladhorn updated this revision to Diff 63574.Aug 11 2019, 8:18 PM

Remove locals.

Hi @romangg, I did not test this. I added inline comments explaining why the code is completely unchanged. It does 100% the same as before, just without the duplication. All you need is to invert the two conditions inside the ifs and switch the else branch with the first branch of the if and you'll arrive at this.


If (y == we use the x, y position unchanged (I'm unsure why this special case is necessary, it skips the caclulations below, so it's a micro-optimization).


This bit was executed after the first if was !=. Unchanged.


>= is the inversion of <, so this corresponds to the second, inner else. It is the only case where x, y are modified - y is calculated.

romangg accepted this revision.Aug 12 2019, 9:54 AM

Your comments explain the new control flow just fine and why it is equivalent to the old one. You can add the const on push, no need to update the diff on Phab before.



This revision is now accepted and ready to land.Aug 12 2019, 9:54 AM

Maybe add a comment why the y value needs to be adjusted in this one special case.

zzag added a subscriber: zzag.Aug 12 2019, 9:38 PM
zzag added inline comments.

Remove this code as well.

zzag added inline comments.Aug 12 2019, 9:40 PM

meh, please do it in another revision

zzag accepted this revision.Aug 12 2019, 9:41 PM
This revision was automatically updated to reflect the committed changes.
gladhorn added inline comments.Aug 13 2019, 6:41 AM

I'm a bit unsure what you mean - the entire popup position trickery? I was wondering how much it's needed, but didn't want to break existing things. For now I assume the code is there for a reason - it seems to me that it want to move the popup high enough to not be positioned at the bottom in a way that it would be cut off. I'm not sure what Qt actually does... in theory I think it should move it up.

zzag added inline comments.Aug 13 2019, 8:16 AM

I don't see why we have to ensure that QMenu doesn't fall off screen. QMenu already does that for us. Besides that our code is incomplete(only one out of three cases is handled) and incorrect(y + popupHeight >= area.height() is wrong).

Remove if (y != and everything that goes inside.

zzag added inline comments.Aug 13 2019, 8:17 AM

Why do we have this check? Well, that's a good question! I'd like to know why ;-)

zzag added inline comments.Aug 13 2019, 8:18 AM

*out of four