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

Provide restart() function and more on Timer pseudo-element #6369

Open
Enyium opened this issue Sep 29, 2024 · 3 comments
Open

Provide restart() function and more on Timer pseudo-element #6369

Enyium opened this issue Sep 29, 2024 · 3 comments
Labels
a:language-slint Compiler for the .slint language (mO,bF) enhancement New feature or request

Comments

@Enyium
Copy link
Contributor

Enyium commented Sep 29, 2024

I have a few widget pages that control different parts of my app. I want to call through to Rust code that accepts the widgets' content batch-wise (always a whole "page"), after user actions subsided for a short duration in that context. This means I must constantly restart a timer on user action, and only call through to Rust in the Timer's triggered callback.

With the Timer pseudo-element's current state, I have to use this workaround:

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

export component Demo {
    timer := Timer {
        interval: 1s;

        triggered => {
            t.text = t.text.to-float() + 1;
        }

        // HERE:
        property <bool> plus: true;
        function restart() {
            self.interval = self.interval + (self.plus ? 1ms : -1ms);
            self.plus = !self.plus;

            self.running = true;
        }
    }

    VerticalBox {
        alignment: start;

        t := Text {
            text: "0";
        }

        Button {
            text: "Restart Timer (Click Often)";

            clicked => {
                timer.restart();
            }
        }
    }
}

Timer should provide a restart() function that starts or restarts the timer for you, without the need to tamper with the interval.

Precedential cases:

  • Java Swing's Timer has a restart() method.
  • Qt's QTimer's start() restarts the timer, if it was already started.
  • It's for measuring time, but .NET's Stopwatch class has a Restart() method.

I think restart() would be more unambiguous than start() for a function that restarts, if already started.


Further considerations:

  • The convenience function stop() would also be worth considering to make your code more expressive than with timer.running = false;.
  • Also, a oneshot convenience boolean property on Timer could be seen as fitting for this use case, making Slint run timer.running = false; after it called the Timer's triggered callback.
  • A prepone() function would also be very useful and code-simplifying for this use case, because in my GUI, the widget page identity changing, because another list entry was chosen, requires immediate acceptance of the page's current state, before it'll be filled with other data. Whether the page needs to be accepted at all depends on whether Timer's new restart() function was called before. If it wasn't, meaning the Timer isn't running, prepone() would just need to be a no-op.
@Enyium
Copy link
Contributor Author

Enyium commented Sep 30, 2024

BTW, if setting interval to a value that only differs by a millisecond restarts the timer, it should also be restarted, if it's set to the same value. If that's technically not possible, the behavior in this regard should be clearly documented.

If this isn't technically possible, there should probably also be a general-purpose method for setting the interval that always restarts. Qt's QTimer has two overloads of its start() method, one parameterless, one with the new interval to be used. So, the new restart() method this issue proposes should also exist with an additional overload (if technically possible) or with a name like restart-with-interval().

@ogoffart ogoffart added the need triaging Issue that the owner of the area still need to triage label Sep 30, 2024
@Enyium
Copy link
Contributor Author

Enyium commented Sep 30, 2024

This SlintPad demo shows details of what I mean using an implementation of an ExtendedTimer.

@Enyium Enyium changed the title Provide restart() function on Timer pseudo-element Provide restart() function and more on Timer pseudo-element Sep 30, 2024
@tronical
Copy link
Member

tronical commented Oct 1, 2024

I vaguely remember discussing restart() with @ogoffart but I think we said we'd leave it out for now. The Rust API has it, so ... it's probably more of a question of "do we want this?".

I for one am not opposed to it.

@ogoffart ogoffart added enhancement New feature or request a:language-slint Compiler for the .slint language (mO,bF) and removed need triaging Issue that the owner of the area still need to triage labels Oct 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
a:language-slint Compiler for the .slint language (mO,bF) enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants