slint icon indicating copy to clipboard operation
slint copied to clipboard

WIP: Add a `Window::close(int)` builtin function

Open ogoffart opened this issue 2 years ago • 5 comments

This will close the window (and currently the application because there is only one window)

Also add an exit_code int to the eventloop quit function and make thi run_in_event_loop return a i32

Compatibility note:

  • In C++, i think it is OK to do the change because of the default argument and the return value should also be ok-ish breaking change as i don't think anyone rely on the exact type

  • In rust, this is more complicated, adding a -> i32 might be a problem as it changes the type of the function. Or if the function were called at the end of a block without a ; Maybe we shouldn't do the change and add new function with a different name?

  • Also in rust, the quit_event_loop function can't be re-used so just add an internal function with a different name. That function is probably not going to be called from rust with an exit code.

Todo:

  • Fix or discuss compatibility issue
  • More tests

ogoffart avatar Sep 09 '21 18:09 ogoffart

Maybe we actually don't want this API like that and want to have it only for Dialog or something perhaps this could be a callback instead.

ogoffart avatar Sep 09 '21 18:09 ogoffart

Hi, I hacked an update for this commit that compiles and runs (on my windows setup, and I will test it later on my linux setup as well).

Is this PR something you're still interested in? I wouldn't mind finishing the work on it if it has a chance of being merged, although I might need some guidance since I'm not very familiar with slint.

For the record, last time I wanted to use slint-viewer for a personal project, I gave up on it because there was no close function, and I wanted the window to close on a key press, so I definitely think this is valuable.

edit: forgot to add the link to my fork

nivpgir avatar Jun 27 '22 13:06 nivpgir

Hi @nivpgir ! Since this PR we've added a Dialog element and extended the viewer with support for it, so that when pressing one of the standard buttons it exits. We used that in this blog post.

We also added the ability to react to the user closing the window in https://github.com/slint-ui/slint/pull/1011 , but that has only has API for setting the callback, not for initiating the close.

I wonder if we still need a close() function then.

It's possible to react to user / system initiated window closing and it's possible to hide() a window programmatically (developer initiated).

tronical avatar Jun 29 '22 08:06 tronical

it's possible to hide() a window programmatically (developer initiated).

Hmm, this might be good enough for me, since what I really wanted was to initiate the close from the code (developer initiated).

For reference, this is the core of what I'm trying to do:

Example := Window {
    forward-focus: my-key-handler;
    my-key-handler := FocusScope {
        key-pressed(event) => {
            debug(event);
            if (event.text == Keys.Escape) {
                debug("Esc key was pressed");
 		root.close(0);
            }
            if (event.text == "q") {
                debug("q key was pressed");
                root.close(0);
            }
            accept
        }
    }
}

Essentially a keyboard centered interface, and the ability to close the window without a confirm dialog. although I don't care too much about the exit code, so many of the changes here aren't necessary for me, just the close builtin function.

nivpgir avatar Jun 29 '22 08:06 nivpgir

Excellent. Then perhaps you can have a callback close(); in your Example, set the handler from Rust/C++ and call hide() there. If that is the last window open, then run() or slint::run_event_loop(); should also return. Let us know how it goes :)

tronical avatar Jun 29 '22 08:06 tronical