scroll_layer.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126
  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_trait::async_trait;
  19. use miniquad::{KeyCode, KeyMods, MouseButton, TouchPhase};
  20. use std::sync::Arc;
  21. use crate::{
  22. gfx::{Point, Rectangle},
  23. prop::PropertyAtomicGuard,
  24. scene::{Pimpl, SceneNodeWeak},
  25. util::i18n::I18nBabelFish,
  26. ExecutorPtr,
  27. };
  28. use super::{DrawUpdate, Layer, LayerPtr, RedrawTrigger, UIObject};
  29. pub type ScrollLayerPtr = Arc<ScrollLayer>;
  30. pub struct ScrollLayer {
  31. inner: LayerPtr,
  32. }
  33. impl ScrollLayer {
  34. pub async fn new(
  35. node: SceneNodeWeak,
  36. renderer: crate::gfx::Renderer,
  37. redraw: RedrawTrigger,
  38. ) -> Pimpl {
  39. let layer = Layer::new(node.clone(), renderer, redraw).await;
  40. let inner = match layer {
  41. Pimpl::Layer(l) => l,
  42. _ => unreachable!(),
  43. };
  44. Pimpl::ScrollLayer(Arc::new(Self { inner }))
  45. }
  46. }
  47. #[async_trait]
  48. impl UIObject for ScrollLayer {
  49. fn priority(&self) -> u32 {
  50. self.inner.priority()
  51. }
  52. fn init(&self) {
  53. self.inner.init();
  54. }
  55. async fn start(self: Arc<Self>, ex: ExecutorPtr) {
  56. self.inner.clone().start(ex).await;
  57. }
  58. fn stop(&self) {
  59. self.inner.stop();
  60. }
  61. async fn draw(
  62. &self,
  63. parent_rect: Rectangle,
  64. atom: &mut PropertyAtomicGuard,
  65. ) -> Option<DrawUpdate> {
  66. self.inner.draw(parent_rect, atom).await
  67. }
  68. async fn handle_char(&self, key: char, mods: KeyMods, repeat: bool) -> bool {
  69. self.inner.handle_char(key, mods, repeat).await
  70. }
  71. async fn handle_key_down(&self, key: KeyCode, mods: KeyMods, repeat: bool) -> bool {
  72. self.inner.handle_key_down(key, mods, repeat).await
  73. }
  74. async fn handle_key_up(&self, key: KeyCode, mods: KeyMods) -> bool {
  75. self.inner.handle_key_up(key, mods).await
  76. }
  77. async fn handle_mouse_btn_down(&self, btn: MouseButton, mouse_pos: Point) -> bool {
  78. self.inner.handle_mouse_btn_down(btn, mouse_pos).await
  79. }
  80. async fn handle_mouse_btn_up(&self, btn: MouseButton, mouse_pos: Point) -> bool {
  81. self.inner.handle_mouse_btn_up(btn, mouse_pos).await
  82. }
  83. async fn handle_mouse_move(&self, mouse_pos: Point) -> bool {
  84. self.inner.handle_mouse_move(mouse_pos).await
  85. }
  86. async fn handle_mouse_wheel(&self, wheel_pos: Point) -> bool {
  87. self.inner.handle_mouse_wheel(wheel_pos).await
  88. }
  89. async fn handle_touch(&self, phase: TouchPhase, id: u64, touch_pos: Point) -> bool {
  90. self.inner.handle_touch(phase, id, touch_pos).await
  91. }
  92. fn handle_touch_sync(&self, phase: TouchPhase, id: u64, touch_pos: Point) -> bool {
  93. self.inner.handle_touch_sync(phase, id, touch_pos)
  94. }
  95. fn set_i18n(&self, i18n_fish: &I18nBabelFish) {
  96. self.inner.set_i18n(i18n_fish);
  97. }
  98. }
  99. impl std::fmt::Debug for ScrollLayer {
  100. fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
  101. self.inner.fmt(f)
  102. }
  103. }