Skip to content

Commit 4141ee2

Browse files
committed
chore: use the right way of timestamp
1 parent 97d0e3e commit 4141ee2

File tree

1 file changed

+18
-10
lines changed
  • iced_examples/iced_virtualkeyboard/src

1 file changed

+18
-10
lines changed

iced_examples/iced_virtualkeyboard/src/main.rs

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::ffi::CString;
1313
use std::fs::File;
1414
use std::io::Write;
1515
use std::path::PathBuf;
16+
use std::time::Instant;
1617
use xkbcommon::xkb;
1718

1819
use std::sync::LazyLock;
@@ -78,23 +79,26 @@ struct KeyCoords {
7879
size: Size,
7980
}
8081

81-
#[derive(Default)]
8282
struct KeyboardView {
8383
draw_cache: Cache,
84+
time: Instant,
8485
}
8586

8687
#[derive(Debug, Clone)]
8788
enum Message {
88-
InputKeyPressed(u32),
89+
InputKeyPressed { time_stamp: u32, key: u32 },
8990
}
9091

9192
impl TryInto<LayershellCustomActionWithId> for Message {
9293
type Error = Self;
9394
fn try_into(self) -> Result<LayershellCustomActionWithId, Self::Error> {
94-
let Message::InputKeyPressed(key) = self;
95+
let Message::InputKeyPressed { time_stamp, key } = self;
9596
Ok(LayershellCustomActionWithId(
9697
None,
97-
LayershellCustomAction::VirtualKeyboardPressed { time: 100, key },
98+
LayershellCustomAction::VirtualKeyboardPressed {
99+
time: time_stamp,
100+
key,
101+
},
98102
))
99103
}
100104
}
@@ -103,15 +107,14 @@ impl KeyboardView {
103107
fn new() -> (Self, Command<Message>) {
104108
(
105109
Self {
106-
..Default::default()
110+
draw_cache: Cache::default(),
111+
time: Instant::now(),
107112
},
108113
Command::none(),
109114
)
110115
}
111-
fn update(&mut self, message: Message) -> Command<Message> {
112-
match message {
113-
Message::InputKeyPressed(_) => Command::done(message),
114-
}
116+
fn update(&mut self, _message: Message) -> Command<Message> {
117+
Command::none()
115118
}
116119

117120
fn style(&self, theme: &iced::Theme) -> iced::theme::Style {
@@ -330,10 +333,15 @@ impl canvas::Program<Message> for KeyboardView {
330333
&& click_position.y >= key_position.y
331334
&& click_position.y <= key_position.y + key_size.height
332335
{
336+
let current_time = Instant::now();
337+
let time_stamp = (current_time - self.time).as_millis();
333338
// Clear the cache
334339
self.draw_cache.clear();
335340
if let Some(key_code) = get_key_code(label) {
336-
return Some(canvas::Action::publish(Message::InputKeyPressed(key_code)));
341+
return Some(canvas::Action::publish(Message::InputKeyPressed {
342+
key: key_code,
343+
time_stamp: time_stamp as u32,
344+
}));
337345
}
338346
}
339347
}

0 commit comments

Comments
 (0)