slint-ui / slint

Slint is a declarative GUI toolkit to build native user interfaces for Rust, C++, or JavaScript apps.
https://slint.dev
Other
16.93k stars 567 forks source link

Add support for querying the absolute x/y coordinates of elements #1691

Closed tronical closed 1 year ago

tronical commented 1 year ago

Sometimes it's useful to know the absolute x or y coordinate of an element within the entire window, not just the x/y coordinate relative to the parent.

This could be implemented in the compiler by providing synthetic absolute-x and absolute-y coordinates that, when used, are associated with a (generated) binding that walks up the chain of parent elements - like we do for popups.

threefoldo commented 1 year ago

Hi, how to get the relative x,y coordinate? I'm new to slint, don't find much information about it.

ogoffart commented 1 year ago

@threefoldo you can get the relative x, y coodinate using x and y property.

I am not quite sure what you are trying to do, but maybe this example helps? https://slint-ui.com/releases/0.3.1/editor/?snippet=import+%7B+Button%2C+VerticalBox+%7D+from+%22std-widgets.slint%22%3B%0Aexport+Demo+%3A%3D+Window+%7B%0A++++VerticalBox+%7B%0A++++++++alignment%3A+start%3B%0A++++++++Text+%7B%0A++++++++++++text%3A+%22Hello+World%21%22%3B%0A++++++++++++font-size%3A+24px%3B%0A++++++++++++horizontal-alignment%3A+center%3B%0A++++++++%7D%0A++++++++rect+%3A%3D+Rectangle+%7B%0A++++++++++++Text+%7B%0A++++++++++++++++text%3A+%22Rect+is+at+%28%5C%7Brect.x+%2F+1px%7D%2C%5C%7Brect.y+%2F+1px%7D%29%22%3B+%0A++++++++++++%7D%0A++++++++%7D%0A++++%7D%0A%7D%0A%0A

threefoldo commented 1 year ago

We want to generate UI automatically. In the example above, how to find the x,y,w,h of the previous Text component? And how to locate the rect object from the Window handle? I tried this, but didn't work:

slint::slint!(import {App} from "app.slint";);

pub fn main() {
    let app = App::new();
    app.show();
    let w = app.window();
    let pos = w.position();
    let size = w.size();

    println!("position: {},{}", pos.x, pos.y);
    println!("size: {},{}", size.width, size.height);
}

I can get the position and size of the window, but don't know how to enumerate all sub-components and get their properties.

flukejones commented 1 year ago

I'm finding I need this so that I can do bounds checking of PopupWindow and a few other items I'm setting with absolute coordinates (relative to parent).

flukejones commented 1 year ago

I should mention this is also highly desired for a root window popover that needs to display an overlay with components at the precise locations of the nested child components it is covering - for example a widget leftmost at 600px screen coords, I need this info to pass to the popover to show a ring/glow/dropdown/help-text etc. This info is also required to more precisely place a PopupWindow widget if called by deeply nested components.

flukejones commented 1 year ago

Example of why I need the absolute coords:

image