slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.

Home Page:https://slint.dev

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ComponentContainer: `absolute-position` has wrong value when in live preview with toolbar

cEvolve05 opened this issue · comments

C++/Slint 1.7.1

When using preview with toolbar enabled, absolute-position always gives wrong value, bigger than what it should be. This seems to be caused by taking toolbar size into consideration. (Tested in SlintPad, local preview and editor-provided preview)

With the following code, the green rectangle should exactly cover the red rectangle in the preview, regardless of whether the toolbar is enabled or disabled.

component Example {
    Rectangle {
        x: 50px;
        y: 50px;
        height: 100px;
        width: 100px;
        border-width: 2px;
        border-color: yellow;
        target := Rectangle {
            x: 50px;
            y: 50px;
            height: 50px;
            width: 50px;
            background: red;
        }
    }

    Rectangle {
        x: target.absolute-position.x;
        y: target.absolute-position.y;
        height: target.height;
        width: target.width;
        background: green;
    }
}

If I change

x: target.absolute-position.x;
y: target.absolute-position.y;

to

x: layout-guest-button.absolute-position.x - root.absolute-position.x;
y: layout-guest-button.absolute-position.y - root.absolute-position.y;

Then the green exactly covers the red.

This solution puts the element in the correct position, but the problem still exists that absolute-position is different when toolbar enabled and disabled.

Thanks for filling an issue.
Indeed, the absolute-position is relative to the actual Slint Window, which is the preview itself and not the application, as the application is embedded in a ComponentContainer.

We have to change the computation to make it relative to the previewed thing and not the window.