slint-ui / slint

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

Allow accessing `parent` in exported component #6840

Open bryce-happel-walton opened 2 days ago

bryce-happel-walton commented 2 days ago

Feature Description

Allow to access the parent attribute in components. Alternatively allow properties to have a component type to bind to another component.

component GaugeAttachedSymbol inherits SymbolLight {
    in property <angle> angle: 0deg;
    in property <length> radius-offset: 25px;
    in property <length> radius: max(self.width, self.height) / 2;

    in property <length> center-x: 0px;
    in property <length> center-y: 0px;
    in property <length> center-radius: 1px;

    x: center-x - self.width / 2 + ((center-radius + radius + radius-offset) * cos(angle));
    y: center-y - self.height / 2 - ((center-radius + radius + radius-offset) * sin(angle));
}

I would like to do one of two things with the above code:

component GaugeAttachedSymbol inherits SymbolLight {
    in property <angle> angle: 0deg;
    in property <length> radius-offset: 25px;
    in property <length> radius: max(self.width, self.height) / 2;

    x: parent.x - self.width / 2 + ((parent.width / 2 + radius + radius-offset) * cos(angle));
    y: parent.y - self.height / 2 - ((parent.height / 2 + radius + radius-offset) * sin(angle));
}

OR

component GaugeAttachedSymbol inherits SymbolLight {
    in property <angle> angle: 0deg;
    in property <length> radius-offset: 25px;
    in property <length> radius: max(self.width, self.height) / 2;

    in property <T where T: Position + Size>  center-around;

    x: center-around.x - self.width / 2 + ((center-around.width / 2 + radius + radius-offset) * cos(angle));
    y: center-around.y - self.height / 2 - ((center-around.height / 2 + radius + radius-offset) * sin(angle));
}

I used Rust syntax where there is no Slint equivalent for coherency, actual trait based typing may be asking a bit much.

In theory, the only component without parent should be Window and those that inherit Window. Every other component should default the parent to the main window until they have been built. I understand that inferring the properties of the parent without knowing its type is not possible, but everything has position and size, so those should be accessible at least and everything else can error.

Product Impact

Parent accessibility is fundamental. Object binding is a must-have.