Skip to main content

Global

Trait Global 

pub trait Global<'a, Component> {
    type StaticSelf: 'static + StrongHandle;

    // Required methods
    fn get(component: &'a Component) -> Self;
    fn as_weak(&self) -> Weak<Self::StaticSelf>;
}
Expand description

This trait is used to obtain references to global singletons exported in .slint markup. Alternatively, you can use ComponentHandle::global to obtain access.

This trait is implemented by the compiler for each global singleton that’s exported.

§Example

The following example of .slint markup defines a global singleton called Palette, exports it and modifies it from Rust code:

slint::slint!{
export global Palette {
    in property<color> foreground-color;
    in property<color> background-color;
}

export component App inherits Window {
   background: Palette.background-color;
   Text {
      text: "Hello";
      color: Palette.foreground-color;
   }
   // ...
}
}
let app = App::new().unwrap();
app.global::<Palette>().set_background_color(slint::Color::from_rgb_u8(0, 0, 0));

// alternate way to access the global singleton:
Palette::get(&app).set_foreground_color(slint::Color::from_rgb_u8(255, 255, 255));

See also the language documentation for global singletons for more information.

Note: Only globals that are exported or re-exported from the main .slint file will be exposed in the API

§Storing References to Globals

Globals are strong references to the window they are attached to, unless stored in a Weak reference (see the StrongHandle trait). This means that if you store a reference to a global, it will keep the entire window alive and prevent it from being dropped.

To make this less error-prone, when accessing a global from a window, it is initially bound to the lifetime of the Window it belongs to. This prevents you from accidentally capturing the global in a callback closure, which would result in the window never being dropped.

To store references to a global in a callback or Rust struct, you can convert it into a weak reference using the Global::as_weak function. This will also extend the lifetime of the global to 'static.

Once the window is dropped, upgrading the weak reference will return None.

§Example

slint::slint!{
export global Palette {
    in property<color> foreground-color;
    in property<color> background-color;
}

export component App inherits Window {
   background: Palette.background-color;
   // ...
}
}

struct PaletteBackend {
    global: slint::Weak<Palette<'static>>,
}

impl PaletteBackend {
    fn global(&self) -> Palette<'static> {
        self.global.upgrade().expect("The window was dropped, the global is no longer available")
    }
}

let app = App::new().unwrap();

let palette_backend = PaletteBackend { global: app.global::<Palette>().as_weak() };

Required Associated Types§

type StaticSelf: 'static + StrongHandle

The Self type, with a 'static lifetime.

Required Methods§

fn get(component: &'a Component) -> Self

Returns a reference to the global.

fn as_weak(&self) -> Weak<Self::StaticSelf>

Convert this Global reference into a weak reference.

This will also extend the lifetime of this global to 'static, to allow storing the Weak reference in a struct that does not have a lifetime itself.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§