slint icon indicating copy to clipboard operation
slint copied to clipboard

Replace global state in Slint with user-facing context API

Open tronical opened this issue 1 year ago • 0 comments
trafficstars

At the moment we have global state in Slint in various places, which we store typically in thread local storage. This includes for example the backend singleton or the currently evaluating property. This makes it hard to run two event loops in separate threads, for example.

We've had this idea for a while to replace that with an explicit context API, so let's track that in this issue:

//let slint = SlintContext::new();
let slint = SlintContext::with_backend(...); // clone, explicitly shared


// let ui = MainWindow::new();

let ui = MainWindow::new(&slint); // clones SlintContext

ui.on_text_edited(|slint: &SlintContext, ui: &MainWindow, text: SharedString| {
    let slint = ui.ctx();
});

let proxy = slint.proxy();
std::thread::spawn(move || {
    proxy.invoke_from_event_loop(|slint : &SlintContext| {
        let ui = ui_weak.upgrade().unwrap();
        ui.window().dispach_event(WindowEvent::KeyEnter); //triger text-edited
    })
});

slint.run_event_loop();


// within slint:
thread_local!(GLOBAL_CTX: SlintContext)

We can keep iterating on this, and the acceptance criteria for this issue would be merging a first version or deciding not to do this altogether.

tronical avatar Jan 08 '24 14:01 tronical