It sounds like in both cases you actually want to use the viewport, and not the window or screen size.
The viewport is the visible area of the layout, so if you want to place things to fill the view or relative to the view, you need to position them relative to the viewport.
The viewport sometimes happens to match up with the window or screen size, so sometimes using the wrong values will work by accident. However as soon as you scroll, scale or rotate the view, or use certain fullscreen modes, it won't line up any more and will appear broken.
The viewport system expressions can return the viewport size. I just realised the scripting APIs don't cover the viewport yet, so I'll add something for that.