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.55k stars 601 forks source link

internal globals (such as NativeStyleMetrics) accessible in user code #3793

Closed qarmin closed 1 year ago

qarmin commented 1 year ago

Edit: NativeStyleMetrics is marked as is_internal, and yet it can be looked up in user code. There is a check that we don't lookup internal elements, but that check is not active for globals.

Original description

Slint 1.3.0 - 3deb620953f1307de1b9c0b5b757b5d8ed31a3a1

When trying to preview left_side_panel.slint project file in VScode I got this crash

thread 'main' panicked at 'Cannot find native global NativeStyleMetrics', internal/interpreter/global_component.rs:107:21
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace
[Info  - 9:38:33 PM] Connection to server got closed. Server will restart.
The Slint Language Server crashed. This is a bug.
Please open an issue on https://github.com/slint-ui/slint/issues

Compilation also fails

error[E0412]: cannot find type `NativeStyleMetrics` in this scope
     --> /home/rafal/Projekty/Rust/czkawka/target/debug/build/czkawka_slint-e3a803d91d1c663c/out/main_window.rs:27072:341
      |
27072 | ...Metrics : :: core :: pin :: Pin < sp :: Rc < r#NativeStyleMetrics >> , global_ColorPalette_257 : :: core :: pin :: Pin < sp :: Rc < In...
      |                                                 ^^^^^^^^^^^^^^^^^^^^ not found in this scope
      |
help: you might be missing a type parameter
      |
27071 |      # [allow (dead_code)] struct Globals_MainWindow<NativeStyleMetrics> {
      |                                                     ++++++++++++++++++++

Changes that caused problem - https://github.com/qarmin/czkawka/commit/f128dd6a881fa58fd3000762d8f8844222cf3100

I started to use global NativeStyleMetrics to get info if it is dark mode or not

export global ColorPalette {
    // Tabs at left side
    in-out property<color> tab_selected_color: NativeStyleMetrics.dark-color-scheme ? red : blue;
    in-out property<color> tab_hovered_color: NativeStyleMetrics.dark-color-scheme ? green : yellow;
}
FloVanGH commented 1 year ago

Hi,

it should be StyleMetrics, because NativeStyleMetrics is only used internal. Maybe that causes the trouble.

import { StyleMetrics } from "std-widgets.slint";

export component MyComponent }
    in property <bool> is-dark: StyleMetrics.dark-color-scheme;
}

Two hints to StyleMetrics. It's not official now and api changes are possible. The dark-color-scheme property cannot be change from outside.

ogoffart commented 1 year ago

Thanks for the bug report. As FloVanGH already mentioned, NativeStyleMetrics is completely internal and shouldn't be used. The bug here is that it is not an error in the Slint compiler. NativeStyleMEtrics shouldn't be exposed. (And you get undefined error later because it is only enabled when the qt backend is available)