repeat.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use miniquad::KeyCode;
  19. use std::{collections::HashMap, time::Instant};
  20. #[derive(Debug, Clone, Eq, Hash, PartialEq)]
  21. pub enum PressedKey {
  22. Char(char),
  23. Key(KeyCode),
  24. }
  25. /// On key press (repeat=false), we immediately process the event.
  26. /// Then there's a delay (repeat=true) and then for every step time
  27. /// while key press events are being sent, we allow an event.
  28. /// This ensures smooth typing in the editbox.
  29. pub struct PressedKeysSmoothRepeat {
  30. /// When holding keys, we track from start and last sent time.
  31. /// This is useful for initial delay and smooth scrolling.
  32. pressed_keys: HashMap<PressedKey, RepeatingKeyTimer>,
  33. /// Initial delay before allowing keys
  34. start_delay: u32,
  35. /// Minimum time between repeated keys
  36. step_time: u32,
  37. }
  38. impl PressedKeysSmoothRepeat {
  39. pub fn new(start_delay: u32, step_time: u32) -> Self {
  40. Self { pressed_keys: HashMap::new(), start_delay, step_time }
  41. }
  42. pub fn clear(&mut self) {
  43. self.pressed_keys.clear()
  44. }
  45. pub fn key_down(&mut self, key: PressedKey, repeat: bool) -> u32 {
  46. trace!(target: "PressedKeysSmoothRepeat", "key_down({:?}, {})", key, repeat);
  47. let is_initial_keypress = !repeat;
  48. if is_initial_keypress {
  49. trace!(target: "PressedKeysSmoothRepeat", "remove key {:?}", key);
  50. self.pressed_keys.remove(&key);
  51. return 1
  52. }
  53. // Insert key if not exists
  54. if !self.pressed_keys.contains_key(&key) {
  55. trace!(target: "PressedKeysSmoothRepeat", "insert key {:?}", key);
  56. self.pressed_keys.insert(key.clone(), RepeatingKeyTimer::new());
  57. }
  58. let repeater = self.pressed_keys.get_mut(&key).expect("repeat map");
  59. let actions = repeater.update(self.start_delay, self.step_time);
  60. // This is a temporary workaround due to a miniquad issue.
  61. // See https://github.com/not-fl3/miniquad/issues/517
  62. std::cmp::min(1, actions)
  63. }
  64. /*
  65. fn key_up(&mut self, key: &PressedKey) {
  66. //trace!(target: "PressedKeysSmoothRepeat", "key_up({:?})", key);
  67. assert!(self.pressed_keys.contains_key(key));
  68. self.pressed_keys.remove(key).expect("key was pressed");
  69. }
  70. */
  71. }
  72. struct RepeatingKeyTimer {
  73. start: Instant,
  74. actions: u32,
  75. }
  76. impl RepeatingKeyTimer {
  77. fn new() -> Self {
  78. Self { start: Instant::now(), actions: 0 }
  79. }
  80. fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
  81. let elapsed = self.start.elapsed().as_millis();
  82. trace!(target: "RepeatingKeyTimer", "update() elapsed={}, actions={}",
  83. elapsed, self.actions);
  84. if elapsed < start_delay as u128 {
  85. return 0
  86. }
  87. let total_actions = ((elapsed - start_delay as u128) / step_time as u128) as u32;
  88. let remaining_actions = total_actions - self.actions;
  89. self.actions = total_actions;
  90. remaining_actions
  91. }
  92. }