Skip to content

Commit 14ae563

Browse files
authored
Add new example for blocking functions (#840)
* Add new example for blocking functions * Rename example * Remove console suppression * Clean up code style * cargo fmt * Fix clippy warnings * cargo fmt * Prevent wasm from building new example * cargo fmt
1 parent c5e87a2 commit 14ae563

File tree

2 files changed

+113
-2
lines changed

2 files changed

+113
-2
lines changed

druid/examples/blocking_function.rs

+110
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,110 @@
1+
// Copyright 2019 The xi-editor Authors.
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
//! An example of a blocking function running in another thread.
16+
17+
use std::{thread, time};
18+
19+
use druid::{
20+
AppDelegate, AppLauncher, Command, Data, DelegateCtx, Env, ExtEventSink, Lens, LocalizedString,
21+
Selector, Target, Widget, WidgetExt, WindowDesc,
22+
};
23+
24+
use druid::widget::{Button, Either, Flex, Label};
25+
26+
const START_SLOW_FUNCTION: Selector = Selector::new("start_slow_function");
27+
28+
const FINISH_SLOW_FUNCTION: Selector = Selector::new("finish_slow_function");
29+
30+
struct Delegate {
31+
eventsink: ExtEventSink,
32+
}
33+
34+
#[derive(Clone, Default, Data, Lens)]
35+
struct AppState {
36+
processing: bool,
37+
value: u32,
38+
}
39+
40+
// Pretend this is downloading a file, or doing heavy calculations...
41+
fn slow_function(number: u32) -> u32 {
42+
let a_while = time::Duration::from_millis(2000);
43+
thread::sleep(a_while);
44+
number + 1
45+
}
46+
47+
fn wrapped_slow_function(sink: ExtEventSink, number: u32) {
48+
thread::spawn(move || {
49+
let number = slow_function(number);
50+
sink.submit_command(FINISH_SLOW_FUNCTION, number, None)
51+
.expect("command failed to submit");
52+
});
53+
}
54+
55+
impl AppDelegate<AppState> for Delegate {
56+
fn command(
57+
&mut self,
58+
_ctx: &mut DelegateCtx,
59+
_target: &Target,
60+
cmd: &Command,
61+
data: &mut AppState,
62+
_env: &Env,
63+
) -> bool {
64+
match cmd.selector {
65+
START_SLOW_FUNCTION => {
66+
data.processing = true;
67+
wrapped_slow_function(self.eventsink.clone(), data.value);
68+
true
69+
}
70+
FINISH_SLOW_FUNCTION => {
71+
data.processing = false;
72+
let number = cmd.get_object::<u32>().expect("api violation");
73+
data.value = *number;
74+
true
75+
}
76+
_ => true,
77+
}
78+
}
79+
}
80+
81+
fn ui_builder() -> impl Widget<AppState> {
82+
let button = Button::new("Start slow increment")
83+
.on_click(|ctx, _data: &mut AppState, _env| {
84+
let cmd = Command::new(START_SLOW_FUNCTION, 0);
85+
ctx.submit_command(cmd, None);
86+
})
87+
.padding(5.0);
88+
let button_placeholder = Label::new(LocalizedString::new("Processing..."))
89+
.padding(5.0)
90+
.center();
91+
92+
let text = LocalizedString::new("hello-counter")
93+
.with_arg("count", |data: &AppState, _env| (data.value).into());
94+
let label = Label::new(text).padding(5.0).center();
95+
96+
let either = Either::new(|data, _env| data.processing, button_placeholder, button);
97+
98+
Flex::column().with_child(label).with_child(either)
99+
}
100+
fn main() {
101+
let main_window = WindowDesc::new(ui_builder).title(LocalizedString::new("Blocking functions"));
102+
let app = AppLauncher::with_window(main_window);
103+
let delegate = Delegate {
104+
eventsink: app.get_external_handle(),
105+
};
106+
app.delegate(delegate)
107+
.use_simple_logger()
108+
.launch(AppState::default())
109+
.expect("launch failed");
110+
}

druid/examples/wasm/build.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,9 @@ use std::{env, fs};
1818

1919
/// Examples known to not work with WASM are skipped. Ideally this list will eventually be empty.
2020
const EXCEPTIONS: &[&str] = &[
21-
"svg", // usvg doesn't currently build with WASM.
22-
"ext_event", // WASM doesn't currently support spawning threads.
21+
"svg", // usvg doesn't currently build with WASM.
22+
"ext_event", // WASM doesn't currently support spawning threads.
23+
"blocking_function", // WASM doesn't currently support spawning threads.
2324
];
2425

2526
/// Create a platform specific link from `src` to the `dst` directory.

0 commit comments

Comments
 (0)