slint icon indicating copy to clipboard operation
slint copied to clipboard

Bindings for default-font-size do not work when binding initially evaluates to 0

Open medhefgo opened this issue 2 years ago • 6 comments

The following snippet fails to apply the default font size (initially and on window resize). Note that the very same binding applied directly to text works as expected.

export component Test inherits Window {
    default-font-size: root.height * 0.3;
    Text {
        text: "Test";
        // font-size: root.height * 0.3;
    }
}

medhefgo avatar Feb 07 '23 15:02 medhefgo

I can reproduce the problem.

It seems to be specific to this one binding, because default-font-size: 1px + root.height * 0.3; actually works.
I think what happens is that the binding initially evaluates to 0 and then we initialize it to the default value from the backend. But we should make a difference between unset and actually 0.

ogoffart avatar Feb 07 '23 17:02 ogoffart

this testcase expose the problem. When the value at "start" is 0, the binding is broken. If we set a higher default, then it works.

ogoffart avatar Feb 07 '23 17:02 ogoffart

Indeed, this code (and a few other places) assume that 0 means unset:

          let default_font_size_prop =
                crate::items::WindowItem::FIELD_OFFSETS.default_font_size.apply_pin(window_item);
            if default_font_size_prop.get().get() <= 0 as Coord {
                default_font_size_prop.set(window_adapter.renderer().default_font_size());
            }

@medhefgo perhaps a workaround until we have the state of "unset" for properties would be for you to write

default-font-size: max(root.height * 0.3, 1px);

tronical avatar Feb 08 '23 14:02 tronical

@tronical I think it's probably fine to assume that 0 means unset. But the problem is that we do erase the value with default_font_size_prop.set

To solve the problem we could either:

  1. leave the default_font_size property to zero, and call renderer().default_font_size() each time we do query this property
  2. have a default binding on the Window default-font-size: __builtin-default-font-size() set by the compiler.

ogoffart avatar Jun 27 '23 16:06 ogoffart

Ah yes, you're right. I'm leaning towards (1). You?

tronical avatar Jun 27 '23 16:06 tronical

Maybe actually 2 is going to be needed for rem to work or if someone does debug(root.default-font-size)

ogoffart avatar Jun 27 '23 16:06 ogoffart