Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Slint callback not being (properly?) called #7508

Closed
JSMassmann opened this issue Jan 31, 2025 · 1 comment
Closed

Slint callback not being (properly?) called #7508

JSMassmann opened this issue Jan 31, 2025 · 1 comment
Labels
bug Something isn't working need triaging Issue that the owner of the area still need to triage

Comments

@JSMassmann
Copy link

Bug Description

I'm using Slint to make a relatively complex GUI, and currently I'm using Dialog to implement small forms. The current dialog has an ok StandardButton and cancel StandardButton. I've implemented it so that clicking the cancel button closes the Dialog (the doc page says that this should automatically happen, but that doesn't seem to be implemented?), and clicking the ok button does other stuff, then closes it too. However, when I actually click the ok button, it does close the dialog, but doesn't run the callback (which prints a test string), despite the minimal reproducible snippet below compiling without any warnings (other than val being unused). Sorry if this is simple, but it's weird that this callback isn't called, despite callbacks in other parts of the project working fine, and I don't see any other similar issues.

Reproducible Code (if applicable)

import { Button, LineEdit, StandardButton } from "std-widgets.slint";

export global MainLogic {
    callback dialog();
    callback do_stuff(string);
}

export component Popup inherits Dialog {
    entry := LineEdit {
        height: 30px;
        width: 250px;
        placeholder-text: "Input stuff here...";
    }

    StandardButton {
        kind: ok;
        height: 50px;
	clicked => {
            MainLogic.do_stuff(entry.text);
	    close.clicked();
        }
    }

    close := StandardButton {
        kind: cancel;
        height: 50px;
    }
}

export component MainScreen inherits Window {
    Button {
        text: "Menu!";
        x: 25px;
        y: 25px;
        height: 50px;
        width: 50px;
	clicked => {MainLogic.dialog();}
    }
}

// Rust code...

use slint::{SharedString, include_modules};

include_modules!();

fn do_stuff (val: SharedString) {
    println!("Test string...");
}

fn friend_dialog_cb () {
    let component = Popup::new().unwrap();
    let weak = component.as_weak();

    component.on_cancel_clicked(move || {println!("Closing."); weak.unwrap().window().hide().unwrap()});

    component.show().unwrap();
}

fn main () {
    let component = MainScreen::new().unwrap();

    component.global::<MainLogic>().on_dialog(friend_dialog_cb);
    component.global::<MainLogic>().on_do_stuff(do_stuff);

    component.run().unwrap();
}

Environment Details

  • Slint Version: 1.9.0
  • Platform/OS: Arch Linux, kernel version 6.12.10-arch1-1
  • Programming Language: Rust
  • Backend/Renderer: OpenGL + GTK

Product Impact

This is just a toy app I'm making so I can get better experience of Rust etc. It's not for an organization or anything.

@JSMassmann JSMassmann added bug Something isn't working need triaging Issue that the owner of the area still need to triage labels Jan 31, 2025
@ogoffart
Copy link
Member

ogoffart commented Feb 3, 2025

The issue here is that the global are actually not shared between components. (Issue #6877)

Try make sure that you setup callbacks on the global also for the dialog.
For example if you change your rust code to this:

use slint::{SharedString, include_modules};

include_modules!();

fn setup_global(global: &MainLogic) {
    global.on_dialog(friend_dialog_cb);
    global.on_do_stuff(do_stuff);
}

fn do_stuff (val: SharedString) {
    println!("Test string...");
}

fn friend_dialog_cb () {
    let component = Popup::new().unwrap();
    setup_global(&component.global::<MainLogic>());

    let weak = component.as_weak();
    component.on_cancel_clicked(move || {println!("Closing."); weak.unwrap().window().hide().unwrap()});

    component.show().unwrap();
}

fn main () {
    let component = MainScreen::new().unwrap();
    setup_global(&component.global::<MainLogic>());
    component.run().unwrap();
}

@ogoffart ogoffart closed this as completed Feb 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working need triaging Issue that the owner of the area still need to triage
Projects
None yet
Development

No branches or pull requests

2 participants