repeat.rs 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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 key_down(&mut self, key: PressedKey, repeat: bool) -> u32 {
  43. //debug!(target: "PressedKeysSmoothRepeat", "key_down({:?}, {})", key, repeat);
  44. let is_initial_keypress = !repeat;
  45. if is_initial_keypress {
  46. //debug!(target: "PressedKeysSmoothRepeat", "remove key {:?}", key);
  47. self.pressed_keys.remove(&key);
  48. return 1;
  49. }
  50. // Insert key if not exists
  51. if !self.pressed_keys.contains_key(&key) {
  52. //debug!(target: "PressedKeysSmoothRepeat", "insert key {:?}", key);
  53. self.pressed_keys.insert(key.clone(), RepeatingKeyTimer::new());
  54. }
  55. let repeater = self.pressed_keys.get_mut(&key).expect("repeat map");
  56. repeater.update(self.start_delay, self.step_time)
  57. }
  58. /*
  59. fn key_up(&mut self, key: &PressedKey) {
  60. //debug!(target: "PressedKeysSmoothRepeat", "key_up({:?})", key);
  61. assert!(self.pressed_keys.contains_key(key));
  62. self.pressed_keys.remove(key).expect("key was pressed");
  63. }
  64. */
  65. }
  66. struct RepeatingKeyTimer {
  67. start: Instant,
  68. actions: u32,
  69. }
  70. impl RepeatingKeyTimer {
  71. fn new() -> Self {
  72. Self { start: Instant::now(), actions: 0 }
  73. }
  74. fn update(&mut self, start_delay: u32, step_time: u32) -> u32 {
  75. let elapsed = self.start.elapsed().as_millis();
  76. //debug!(target: "RepeatingKeyTimer", "update() elapsed={}, actions={}",
  77. // elapsed, self.actions);
  78. if elapsed < start_delay as u128 {
  79. return 0
  80. }
  81. let total_actions = ((elapsed - start_delay as u128) / step_time as u128) as u32;
  82. let remaining_actions = total_actions - self.actions;
  83. self.actions = total_actions;
  84. remaining_actions
  85. }
  86. }