-
Notifications
You must be signed in to change notification settings - Fork 569
Add new example for blocking functions #840
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
Merged
Merged
Changes from 1 commit
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
cc9cf4e
Merge pull request #1 from xi-editor/master
mastfissh 8a17b13
Add new example for blocking functions
mastfissh 439749a
Rename example
mastfissh d6fe279
Remove console suppression
mastfissh 9199f6b
Clean up code style
mastfissh 68c73a1
cargo fmt
mastfissh 2d13fa7
Merge branch 'master' into blocking_example
mastfissh f0788fd
Fix clippy warnings
mastfissh afaf7a5
cargo fmt
mastfissh 4a64b74
Merge pull request #2 from xi-editor/master
mastfissh 8b1083d
Merge branch 'master' into blocking_example
mastfissh a173ae7
Prevent wasm from building new example
mastfissh dd699a3
cargo fmt
mastfissh File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| #![windows_subsystem = "windows"] | ||
|
|
||
| use druid::ExtEventSink; | ||
| use druid::Selector; | ||
|
|
||
| use std::{thread, time}; | ||
|
|
||
| use druid::AppDelegate; | ||
| use druid::DelegateCtx; | ||
| use druid::Target; | ||
|
|
||
| use druid::{AppLauncher, Widget, WindowDesc}; | ||
| use druid::{Command, Data, Env, Lens, LocalizedString}; | ||
| pub struct Delegate { | ||
| eventsink: ExtEventSink, | ||
| } | ||
|
|
||
| #[derive(Clone, Default, Data, Lens)] | ||
| struct AppState { | ||
| processing: bool, | ||
| value: u32, | ||
| } | ||
|
|
||
| use druid::widget::{Button, Either, Flex, Label}; | ||
| use druid::WidgetExt; | ||
|
|
||
| pub const START_SLOW_FUNCTION: Selector = Selector::new("start_slow_function"); | ||
|
|
||
| pub const FINISH_SLOW_FUNCTION: Selector = Selector::new("finish_slow_function"); | ||
|
|
||
| // Pretend this is downloading a file, or doing heavy calculations... | ||
| fn slow_function(number: u32) -> u32 { | ||
| let a_while = time::Duration::from_millis(2000); | ||
| thread::sleep(a_while); | ||
| number + 1 | ||
| } | ||
|
|
||
| fn wrapped_slow_function(sink: ExtEventSink, number: u32) { | ||
| thread::spawn(move || { | ||
| let number = slow_function(number); | ||
| sink.submit_command(FINISH_SLOW_FUNCTION, number, None) | ||
| .expect("command failed to submit"); | ||
| }); | ||
| } | ||
|
|
||
| impl AppDelegate<AppState> for Delegate { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was wrapping my whole app in a Controller, this is a way better solution! |
||
| fn command( | ||
| &mut self, | ||
| _ctx: &mut DelegateCtx, | ||
| _target: &Target, | ||
| cmd: &Command, | ||
| data: &mut AppState, | ||
| _env: &Env, | ||
| ) -> bool { | ||
| match cmd.selector { | ||
| START_SLOW_FUNCTION => { | ||
| data.processing = true; | ||
| wrapped_slow_function(self.eventsink.clone(), data.value); | ||
| true | ||
| } | ||
| FINISH_SLOW_FUNCTION => { | ||
| data.processing = false; | ||
| let number = cmd.get_object::<u32>().expect("api violation"); | ||
| data.value = *number; | ||
| true | ||
| } | ||
| _ => true, | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn ui_builder() -> impl Widget<AppState> { | ||
| let button = Button::new("Start slow increment") | ||
| .on_click(|ctx, _data: &mut AppState, _env| { | ||
| let cmd = Command::new(START_SLOW_FUNCTION, 0); | ||
| ctx.submit_command(cmd, None); | ||
| }) | ||
| .padding(5.0); | ||
| let button_placeholder = Label::new(LocalizedString::new("Processing...")) | ||
| .padding(5.0) | ||
| .center(); | ||
|
|
||
| let text = LocalizedString::new("hello-counter") | ||
| .with_arg("count", |data: &AppState, _env| (data.value).into()); | ||
| let label = Label::new(text).padding(5.0).center(); | ||
|
|
||
| let either = Either::new(|data, _env| data.processing, button_placeholder, button); | ||
|
|
||
| Flex::column().with_child(label).with_child(either) | ||
| } | ||
| fn main() { | ||
| let main_window = | ||
| WindowDesc::new(|| ui_builder()).title(LocalizedString::new("Blocking functions")); | ||
| let app = AppLauncher::with_window(main_window); | ||
| let delegate = Delegate { | ||
| eventsink: app.get_external_handle(), | ||
| }; | ||
| app.delegate(delegate) | ||
| .use_simple_logger() | ||
| .launch(AppState::default()) | ||
| .expect("launch failed"); | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.