mod.rs 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2026 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 async_channel::Sender as AsyncSender;
  19. use parking_lot::Mutex as SyncMutex;
  20. use std::sync::Arc;
  21. mod gametextinput;
  22. mod jni;
  23. use gametextinput::{GameTextInput, GAME_TEXT_INPUT};
  24. macro_rules! t { ($($arg:tt)*) => { trace!(target: "android::textinput", $($arg)*); } }
  25. // Text input state exposed to the rest of the app
  26. #[derive(Debug, Clone, Default)]
  27. pub struct AndroidTextInputState {
  28. pub text: String,
  29. pub select: (usize, usize),
  30. pub compose: Option<(usize, usize)>,
  31. }
  32. struct SharedState {
  33. state: AndroidTextInputState,
  34. /// Used so we know whether to also update the GameTextInput in Android.
  35. /// We should only do so when active.
  36. is_active: bool,
  37. sender: AsyncSender<AndroidTextInputState>,
  38. }
  39. impl SharedState {
  40. fn new(sender: AsyncSender<AndroidTextInputState>) -> Self {
  41. Self { state: Default::default(), is_active: false, sender }
  42. }
  43. }
  44. pub(self) type SharedStatePtr = Arc<SyncMutex<SharedState>>;
  45. pub struct AndroidTextInput {
  46. state: SharedStatePtr,
  47. }
  48. impl AndroidTextInput {
  49. pub fn new(sender: AsyncSender<AndroidTextInputState>) -> Self {
  50. Self { state: Arc::new(SyncMutex::new(SharedState::new(sender))) }
  51. }
  52. pub fn show(&self) {
  53. t!("show IME");
  54. let gti = GAME_TEXT_INPUT.get().unwrap();
  55. gti.focus(self.state.clone());
  56. gti.show_ime(0);
  57. }
  58. pub fn hide(&self) {
  59. t!("hide IME");
  60. let gti = GAME_TEXT_INPUT.get().unwrap();
  61. gti.hide_ime(0);
  62. }
  63. pub fn set_state(&self, state: AndroidTextInputState) {
  64. t!("set_state({state:?})");
  65. // Always update our own state.
  66. let mut ours = self.state.lock();
  67. ours.state = state.clone();
  68. let is_active = ours.is_active;
  69. drop(ours);
  70. // Only update java state when this input is active
  71. if is_active {
  72. let gti = GAME_TEXT_INPUT.get().unwrap();
  73. gti.push_update(&state);
  74. }
  75. }
  76. pub fn set_select(&self, select_start: usize, select_end: usize) {
  77. //t!("set_select({select_start}, {select_end})");
  78. // Always update our own state.
  79. let mut ours = self.state.lock();
  80. let state = &mut ours.state;
  81. assert!(select_start <= state.text.len());
  82. assert!(select_end <= state.text.len());
  83. state.select = (select_start, select_end);
  84. let is_active = ours.is_active;
  85. drop(ours);
  86. // Only update java state when this input is active
  87. if is_active {
  88. let gti = GAME_TEXT_INPUT.get().unwrap();
  89. gti.set_select(select_start as i32, select_end as i32).unwrap();
  90. }
  91. }
  92. }