Skip to content

Commit 1259ddd

Browse files
authored
Update iced example to 0.13 (#145)
1 parent b0e542d commit 1259ddd

File tree

2 files changed

+52
-57
lines changed

2 files changed

+52
-57
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,5 +46,5 @@ x11-dl = "2.21"
4646
winit = "0.30"
4747
tao = "0.30"
4848
eframe = "0.27"
49-
iced = "0.12.1"
49+
iced = "0.13.1"
5050
async-std = "1.12.0"

examples/iced.rs

Lines changed: 51 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,16 @@
11
use global_hotkey::hotkey::{Code, HotKey, Modifiers};
22
use global_hotkey::{GlobalHotKeyEvent, GlobalHotKeyManager};
33

4-
use iced::futures::SinkExt;
4+
use iced::futures::{SinkExt, Stream};
5+
use iced::stream::channel;
56
use iced::widget::{container, row, text};
6-
use iced::{executor, Application, Command, Element, Subscription, Theme};
7+
use iced::{application, Element, Subscription, Task, Theme};
78

89
fn main() -> iced::Result {
9-
Example::run(iced::Settings::default())
10+
application("Iced Example!", update, view)
11+
.subscription(subscription)
12+
.theme(|_| Theme::Dark)
13+
.run_with(new)
1014
}
1115

1216
struct Example {
@@ -21,67 +25,58 @@ enum ProgramCommands {
2125
Received(String),
2226
}
2327

24-
impl Application for Example {
25-
type Executor = executor::Default;
26-
type Message = ProgramCommands;
27-
type Theme = Theme;
28-
type Flags = ();
28+
fn new() -> (Example, Task<ProgramCommands>) {
29+
let manager = GlobalHotKeyManager::new().unwrap();
30+
let hotkey_1 = HotKey::new(Some(Modifiers::CONTROL), Code::ArrowRight);
31+
let hotkey_2 = HotKey::new(None, Code::ArrowUp);
2932

30-
fn new(_flags: Self::Flags) -> (Example, iced::Command<Self::Message>) {
31-
let manager = GlobalHotKeyManager::new().unwrap();
32-
let hotkey_1 = HotKey::new(Some(Modifiers::CONTROL), Code::ArrowRight);
33-
let hotkey_2 = HotKey::new(None, Code::ArrowUp);
33+
manager.register(hotkey_1).unwrap();
34+
manager.register(hotkey_2).unwrap();
3435

35-
manager.register(hotkey_1).unwrap();
36-
manager.register(hotkey_2).unwrap();
37-
(
38-
Example {
39-
last_pressed: "".to_string(),
40-
_manager: manager,
41-
},
42-
Command::none(),
43-
)
44-
}
45-
fn title(&self) -> String {
46-
String::from("Iced example!")
47-
}
36+
(
37+
Example {
38+
last_pressed: "".to_string(),
39+
_manager: manager,
40+
},
41+
Task::none(),
42+
)
43+
}
4844

49-
fn theme(&self) -> Self::Theme {
50-
Theme::Dark // dark theme :D
51-
}
52-
fn update(&mut self, msg: Self::Message) -> iced::Command<ProgramCommands> {
53-
match msg {
54-
Self::Message::Received(code) => {
55-
// update the text widget
56-
self.last_pressed = code.to_string();
45+
fn update(state: &mut Example, msg: ProgramCommands) -> Task<ProgramCommands> {
46+
match msg {
47+
ProgramCommands::Received(code) => {
48+
// update the text widget
49+
state.last_pressed = code.to_string();
5750

58-
Command::none()
59-
}
51+
Task::none()
6052
}
6153
}
62-
fn view(&self) -> Element<'_, Self::Message> {
63-
container(row![text("You pressed: "), text(self.last_pressed.clone())]).into()
64-
}
54+
}
6555

66-
fn subscription(&self) -> Subscription<Self::Message> {
67-
self.hotkey_sub()
68-
}
56+
fn view(state: &Example) -> Element<'_, ProgramCommands> {
57+
container(row![
58+
text("You pressed: "),
59+
text(state.last_pressed.clone())
60+
])
61+
.into()
62+
}
63+
64+
fn subscription(_state: &Example) -> Subscription<ProgramCommands> {
65+
Subscription::run(hotkey_sub)
6966
}
7067

71-
impl Example {
72-
pub fn hotkey_sub(&self) -> Subscription<ProgramCommands> {
73-
iced::subscription::channel(0, 32, |mut sender| async move {
74-
let receiver = GlobalHotKeyEvent::receiver();
75-
// poll for global hotkey events every 50ms
76-
loop {
77-
if let Ok(event) = receiver.try_recv() {
78-
sender
79-
.send(ProgramCommands::Received(format!("{:?}", event)))
80-
.await
81-
.unwrap();
82-
}
83-
async_std::task::sleep(std::time::Duration::from_millis(50)).await;
68+
fn hotkey_sub() -> impl Stream<Item = ProgramCommands> {
69+
channel(32, |mut sender| async move {
70+
let receiver = GlobalHotKeyEvent::receiver();
71+
// poll for global hotkey events every 50ms
72+
loop {
73+
if let Ok(event) = receiver.try_recv() {
74+
sender
75+
.send(ProgramCommands::Received(format!("{:?}", event)))
76+
.await
77+
.unwrap();
8478
}
85-
})
86-
}
79+
async_std::task::sleep(std::time::Duration::from_millis(50)).await;
80+
}
81+
})
8782
}

0 commit comments

Comments
 (0)