|
| 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 | +} |
0 commit comments