insets.rs 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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 miniquad::native::android::{self, ndk_sys, ndk_utils};
  19. use parking_lot::Mutex as SyncMutex;
  20. use std::sync::LazyLock;
  21. type Insets = [f32; 4];
  22. type InsetsSender = async_channel::Sender<Insets>;
  23. struct InsetsGlobals {
  24. sender: Option<InsetsSender>,
  25. insets: Insets,
  26. }
  27. static GLOBALS: LazyLock<SyncMutex<InsetsGlobals>> =
  28. LazyLock::new(|| SyncMutex::new(InsetsGlobals { sender: None, insets: [0.; 4] }));
  29. pub fn set_sender(sender: InsetsSender) {
  30. GLOBALS.lock().sender = Some(sender);
  31. }
  32. pub fn get_insets() -> Insets {
  33. GLOBALS.lock().insets.clone()
  34. }
  35. #[no_mangle]
  36. pub unsafe extern "C" fn Java_darkfi_darkfi_1app_ResizingLayout_onApplyInsets(
  37. _env: *mut ndk_sys::JNIEnv,
  38. _: ndk_sys::jobject,
  39. sys_left: ndk_sys::jint,
  40. sys_top: ndk_sys::jint,
  41. sys_right: ndk_sys::jint,
  42. sys_bottom: ndk_sys::jint,
  43. ime_left: ndk_sys::jint,
  44. ime_top: ndk_sys::jint,
  45. ime_right: ndk_sys::jint,
  46. ime_bottom: ndk_sys::jint,
  47. ) {
  48. debug!(
  49. target: "android::insets",
  50. "onApplyInsets() \
  51. sys=({sys_left}, {sys_top}, {sys_right}, {sys_bottom}) \
  52. ime=({ime_left}, {ime_top}, {ime_right}, {ime_bottom}) \
  53. )"
  54. );
  55. let mut globals = GLOBALS.lock();
  56. globals.insets = [sys_left as f32, sys_top as f32, sys_right as f32, sys_bottom as f32];
  57. if ime_bottom > 0 {
  58. globals.insets[3] = ime_bottom as f32;
  59. }
  60. if let Some(sender) = &globals.sender {
  61. let _ = sender.try_send(globals.insets.clone());
  62. } else {
  63. warn!(target: "android::insets", "Dropping insets notify since no sender is set");
  64. }
  65. }