-
Notifications
You must be signed in to change notification settings - Fork 245
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
add new example: stopwatch #503
Conversation
One solution is not to use a thread at all; instead, a structure will hold:
Then:
This struct (probably in a You could also do it using a thread; you would pause the watch by stopping the thread (for example setting a |
Thank you for the help! I've now made the timer to start/pause/resume by responding to pressing "Space". It's the first time I implement a View so I ran into some problems. I also want to implement fn stop(&mut self) -> EventResult {
self.pause();
let cb = self.on_stop.clone().unwrap();
// We return a Callback Rc<|s| cb(s, &*v)>
EventResult::Consumed(Some(Callback::from_fn(move |s| {
cb(s, self.elapsed)
})))
} I get the error: |
I think when you write Also note: |
Exactly! Thank you! |
Adding the 'lap time' functionality might make this example too complicated. Instead, I am going to implement a fully-fledged TUI clock utility in a separate crate. |
examples/src/bin/stopwatch.rs
Outdated
} | ||
let result = if self.on_stop.is_some() { | ||
let cb = self.on_stop.clone().unwrap(); | ||
let stopwatch_data = self.stopwatch.clone(); // TODO: remove clone |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Almost done... Except in line 163 where I haven't figured out how not to clone
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeaah... the issue is that the callback will be called later, possibly after removing this view. So we can't depend on any data in the view itself; the callback must be self-supported.
The solution is to either clone the data as you are doing (making it standalone), or wrap it in a Rc
shared by both the view and the callback.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I just came up with another approach: use mem::replace
to replace self.stopwatch
with a new, zeroed StopWatch
, while getting the ownership of the 'freshly' produced data, which can then passed to the closure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You can use std::mem::take
which does this automatically for "default" types.
I realized that Now, this example pretty much works as I expected! Thank you so much for the guidance! |
Thanks! |
Thank you for all the help! I've simplified the example. Meanwhile, I've implemented the basic countdown timer in my crate. |
I think we can simplify a bit further the example - let's focus on one feature (a stopwatch) and look for the minimal example demonstrating this. For example: https://gist.github.com/gyscos/67910f22e304214eaef00157faecb2ce |
Cool, I've adopted your changes. Thank you! |
Great! I think you just need to rebase to resolve the readme conflict. |
Now the `OnEventView.on_pre_event_inner()` calls return `Some(EventResult::Consumed(Some(Callback)))` instead of `Some(EventResult::Consumed(None))`. This follows the guidelines from documentation of methods returning a `Callback`, which say that it should be ran on the `Cursive`. While in this example this doesn't make a difference, the previous version created confusion for new users who might not realize you can pass the `Callback`s to the `Cursive` this way.
…into stopwatch
Looks good now |
A stopwatch is implemented in this example.
This example is not complete. I managed to make the textbox update the elapsed time every second, but haven't figured out how I can pause or stop the stopwatch. @gyscos any suggestions?