Sfoglia il codice sorgente

app: remove content_height and scroll property from edit widget, make them internal values. make min/max_height only for the multiline edit.

darkfi 10 mesi fa
parent
commit
a25698c2e6
3 ha cambiato i file con 56 aggiunte e 86 eliminazioni
  1. 2 26
      bin/app/src/app/node.rs
  2. 19 18
      bin/app/src/ui/edit/behave.rs
  3. 35 42
      bin/app/src/ui/edit/mod.rs

+ 2 - 26
bin/app/src/app/node.rs

@@ -256,12 +256,6 @@ pub fn create_baseedit(name: &str) -> SceneNode {
     prop.set_array_len(4);
     node.add_property(prop).unwrap();
 
-    // Deprecate this
-    // No need to be a property
-    let mut prop = Property::new("scroll", PropertyType::Float32, PropertySubType::Pixel);
-    prop.set_range_f32(0., f32::MAX);
-    node.add_property(prop).unwrap();
-
     let mut prop = Property::new("scroll_speed", PropertyType::Float32, PropertySubType::Pixel);
     prop.set_ui_text("Scroll Speed", "Scrolling speed");
     prop.set_defaults_f32(vec![4.]).unwrap();
@@ -367,22 +361,8 @@ pub fn create_baseedit(name: &str) -> SceneNode {
 }
 
 pub fn create_singleline_edit(name: &str) -> SceneNode {
-    let mut node = create_baseedit(name);
-
-    // TEMPORARY
-
-    let mut prop = Property::new("height_range", PropertyType::Float32, PropertySubType::Pixel);
-    prop.set_ui_text("Min/Max Height", "Minimum and Maximum height");
-    prop.set_range_f32(0., f32::MAX);
-    prop.set_array_len(2);
-    node.add_property(prop).unwrap();
-
-    // Maybe this shouldnt even be a property
-    let mut prop = Property::new("content_height", PropertyType::Float32, PropertySubType::Pixel);
-    prop.set_ui_text("Content Height", "The actual text's inner height");
-    node.add_property(prop).unwrap();
-
-    node
+    // No additional properties to add
+    create_baseedit(name)
 }
 
 pub fn create_multiline_edit(name: &str) -> SceneNode {
@@ -394,10 +374,6 @@ pub fn create_multiline_edit(name: &str) -> SceneNode {
     prop.set_array_len(2);
     node.add_property(prop).unwrap();
 
-    let mut prop = Property::new("content_height", PropertyType::Float32, PropertySubType::Pixel);
-    prop.set_ui_text("Content Height", "The actual text's inner height");
-    node.add_property(prop).unwrap();
-
     node
 }
 

+ 19 - 18
bin/app/src/ui/edit/behave.rs

@@ -17,8 +17,9 @@
 
 use async_lock::Mutex as AsyncMutex;
 use async_trait::async_trait;
+use atomic_float::AtomicF32;
 use parking_lot::Mutex as SyncMutex;
-use std::sync::Arc;
+use std::sync::{atomic::Ordering, Arc};
 
 use crate::{
     gfx::{Point, Rectangle},
@@ -81,14 +82,14 @@ impl ScrollDir {
 pub(super) struct MultiLine {
     pub min_height: PropertyFloat32,
     pub max_height: PropertyFloat32,
-    pub content_height: PropertyFloat32,
-    pub scroll: PropertyFloat32,
     pub rect: PropertyRect,
     pub baseline: PropertyFloat32,
     pub padding: PropertyPtr,
     pub cursor_descent: PropertyFloat32,
     pub parent_rect: Arc<SyncMutex<Option<Rectangle>>>,
     pub editor: Arc<AsyncMutex<Option<Editor>>>,
+    pub content_height: AtomicF32,
+    pub scroll: Arc<AtomicF32>,
 }
 
 impl MultiLine {
@@ -144,7 +145,7 @@ impl EditorBehavior for MultiLine {
             editor.refresh().await;
             editor.height()
         };
-        self.content_height.set(atom, content_height);
+        self.content_height.store(content_height, Ordering::Relaxed);
         let outer_height = content_height + self.padding_top() + self.padding_bottom();
         let rect_h = self.bounded_height(outer_height);
         self.rect.prop().set_f32(atom, Role::Internal, 3, rect_h).unwrap();
@@ -168,7 +169,7 @@ impl EditorBehavior for MultiLine {
         //let pad_top = self.padding_top();
         let pad_bot = self.padding_bottom();
 
-        let mut scroll = self.scroll.get();
+        let mut scroll = self.scroll.load(Ordering::Relaxed);
         let rect_h = self.max_height.get() - pad_bot;
         let cursor_y0 = self.get_cursor_pos().await.y;
         let cursor_h = self.baseline.get() + self.cursor_descent.get();
@@ -181,17 +182,17 @@ impl EditorBehavior for MultiLine {
             //t!("  cursor bottom below rect");
             // We want cursor_y1 = rect_h + scroll by adjusting scroll
             scroll = (cursor_y1 - rect_h).clamp(0., max_scroll);
-            self.scroll.set(atom, scroll);
+            self.scroll.store(scroll, Ordering::Release);
         } else if cursor_y0 < scroll {
             //t!("  cursor top above rect");
             scroll = cursor_y0.max(0.);
             assert!(scroll >= 0.);
-            self.scroll.set(atom, scroll);
+            self.scroll.store(scroll, Ordering::Release);
         }
     }
 
     fn scroll(&self) -> Point {
-        Point::new(0., -self.scroll.get())
+        Point::new(0., -self.scroll.load(Ordering::Relaxed))
     }
 
     /// Maximum allowed scroll value
@@ -200,7 +201,7 @@ impl EditorBehavior for MultiLine {
     /// * `rect_h` then clips the `outer_height` to min/max values.
     /// We only allow scrolling when max clipping has been applied.
     async fn max_scroll(&self) -> f32 {
-        let content_height = self.content_height.get();
+        let content_height = self.content_height.load(Ordering::Relaxed);
         let outer_height = content_height + self.padding_top() + self.padding_bottom();
         let rect_h = self.rect.get_height();
         //t!("max_scroll content_height={content_height}, rect_h={rect_h}");
@@ -213,7 +214,7 @@ impl EditorBehavior for MultiLine {
         let pad_bot = self.padding_bottom();
         let pad_left = self.padding.get_f32(3).unwrap();
 
-        let content_height = self.content_height.get();
+        let content_height = self.content_height.load(Ordering::Relaxed);
         let outer_height = content_height + pad_top + pad_bot;
         let rect_h = self.rect.get_height();
         let mut inner_pos = Point::zero();
@@ -237,13 +238,13 @@ impl EditorBehavior for MultiLine {
 }
 
 pub(super) struct SingleLine {
-    pub content_height: PropertyFloat32,
-    pub scroll: PropertyFloat32,
     pub rect: PropertyRect,
     pub padding: PropertyPtr,
     pub cursor_width: PropertyFloat32,
     pub parent_rect: Arc<SyncMutex<Option<Rectangle>>>,
     pub editor: Arc<AsyncMutex<Option<Editor>>>,
+    pub content_height: AtomicF32,
+    pub scroll: Arc<AtomicF32>,
 }
 
 impl SingleLine {
@@ -261,7 +262,7 @@ impl EditorBehavior for SingleLine {
             editor.refresh().await;
             editor.height()
         };
-        self.content_height.set(atom, content_height);
+        self.content_height.store(content_height, Ordering::Relaxed);
 
         let parent_rect = self.parent_rect.lock().clone().unwrap();
         //self.rect.eval(atom, &parent_rect).unwrap();
@@ -280,7 +281,7 @@ impl EditorBehavior for SingleLine {
     async fn apply_cursor_scroll(&self, atom: &mut PropertyAtomicGuard) {
         let pad_right = self.padding.get_f32(1).unwrap();
         let pad_left = self.padding.get_f32(3).unwrap();
-        let mut scroll = self.scroll.get();
+        let mut scroll = self.scroll.load(Ordering::Relaxed);
         let rect_w = self.rect.get_width() - pad_right;
         let cursor_x0 = self.lock_editor().await.get_cursor_pos().x + pad_left;
         let cursor_x1 = cursor_x0 + self.cursor_width.get();
@@ -288,16 +289,16 @@ impl EditorBehavior for SingleLine {
         if cursor_x0 < scroll {
             assert!(cursor_x0 >= 0.);
             scroll = cursor_x0.max(0.);
-            self.scroll.set(atom, cursor_x0);
+            self.scroll.store(cursor_x0, Ordering::Release);
         } else if cursor_x1 > rect_w + scroll {
             let max_scroll = self.max_scroll().await;
             let scroll = (cursor_x1 - rect_w).clamp(0., max_scroll);
-            self.scroll.set(atom, scroll);
+            self.scroll.store(scroll, Ordering::Release);
         }
     }
 
     fn scroll(&self) -> Point {
-        Point::new(-self.scroll.get(), 0.)
+        Point::new(-self.scroll.load(Ordering::Relaxed), 0.)
     }
 
     async fn max_scroll(&self) -> f32 {
@@ -310,7 +311,7 @@ impl EditorBehavior for SingleLine {
 
     fn inner_pos(&self) -> Point {
         let pad_left = self.padding.get_f32(3).unwrap();
-        let content_height = self.content_height.get();
+        let content_height = self.content_height.load(Ordering::Relaxed);
         let rect_h = self.rect.get_height();
         let mut inner_pos = Point::zero();
         inner_pos.x = pad_left;

+ 35 - 42
bin/app/src/ui/edit/mod.rs

@@ -18,6 +18,7 @@
 
 use async_lock::Mutex as AsyncMutex;
 use async_trait::async_trait;
+use atomic_float::AtomicF32;
 use darkfi::system::msleep;
 use darkfi_serial::Decodable;
 use futures::{select, FutureExt};
@@ -88,12 +89,12 @@ enum TouchStateAction {
 
 struct TouchInfo {
     state: TouchStateAction,
-    scroll: PropertyFloat32,
+    scroll: Arc<AtomicF32>,
     scroll_ctrl: ScrollDir,
 }
 
 impl TouchInfo {
-    fn new(scroll: PropertyFloat32, scroll_ctrl: ScrollDir) -> Self {
+    fn new(scroll: Arc<AtomicF32>, scroll_ctrl: ScrollDir) -> Self {
         Self { state: TouchStateAction::Inactive, scroll, scroll_ctrl }
     }
 
@@ -123,7 +124,7 @@ impl TouchInfo {
                 } else if self.scroll_ctrl.cmp(grad) {
                     // Vertical movement
                     debug!(target: "ui::chatedit::touch", "update touch state: Started -> ScrollVert");
-                    let scroll_start = self.scroll.get();
+                    let scroll_start = self.scroll.load(Ordering::Relaxed);
                     self.state =
                         TouchStateAction::ScrollVert { start_pos: *start_pos, scroll_start };
                 } else {
@@ -214,13 +215,9 @@ pub struct BaseEdit {
 
     is_active: PropertyBool,
     is_focused: PropertyBool,
-    min_height: PropertyFloat32,
-    max_height: PropertyFloat32,
-    content_height: PropertyFloat32,
     rect: PropertyRect,
     baseline: PropertyFloat32,
     lineheight: PropertyFloat32,
-    scroll: PropertyFloat32,
     scroll_speed: PropertyFloat32,
     padding: PropertyPtr,
     font_size: PropertyFloat32,
@@ -251,6 +248,7 @@ pub struct BaseEdit {
     hide_cursor: AtomicBool,
     /// Used to start select and scroll when mouse moves outside widget rect.
     sel_sender: SyncMutex<Option<async_channel::Sender<Option<Point>>>>,
+    scroll: Arc<AtomicF32>,
 
     touch_info: SyncMutex<TouchInfo>,
     is_phone_select: AtomicBool,
@@ -275,16 +273,9 @@ impl BaseEdit {
         let node_ref = &node.upgrade().unwrap();
         let is_active = PropertyBool::wrap(node_ref, Role::Internal, "is_active", 0).unwrap();
         let is_focused = PropertyBool::wrap(node_ref, Role::Internal, "is_focused", 0).unwrap();
-        let min_height =
-            PropertyFloat32::wrap(node_ref, Role::Internal, "height_range", 0).unwrap();
-        let max_height =
-            PropertyFloat32::wrap(node_ref, Role::Internal, "height_range", 1).unwrap();
-        let content_height =
-            PropertyFloat32::wrap(node_ref, Role::Internal, "content_height", 0).unwrap();
         let rect = PropertyRect::wrap(node_ref, Role::Internal, "rect").unwrap();
         let baseline = PropertyFloat32::wrap(node_ref, Role::Internal, "baseline", 0).unwrap();
         let lineheight = PropertyFloat32::wrap(node_ref, Role::Internal, "lineheight", 0).unwrap();
-        let scroll = PropertyFloat32::wrap(node_ref, Role::Internal, "scroll", 0).unwrap();
         let scroll_speed =
             PropertyFloat32::wrap(node_ref, Role::Internal, "scroll_speed", 0).unwrap();
         let padding = node_ref.get_property("padding").unwrap();
@@ -320,28 +311,36 @@ impl BaseEdit {
 
         let parent_rect = Arc::new(SyncMutex::new(None));
         let editor = Arc::new(AsyncMutex::new(None));
+        let scroll = Arc::new(AtomicF32::new(0.));
         let behave: Box<dyn EditorBehavior> = match edit_type {
             BaseEditType::SingleLine => Box::new(SingleLine {
-                content_height: content_height.clone(),
-                scroll: scroll.clone(),
                 rect: rect.clone(),
                 padding: padding.clone(),
                 cursor_width: cursor_width.clone(),
                 parent_rect: parent_rect.clone(),
                 editor: editor.clone(),
-            }),
-            BaseEditType::MultiLine => Box::new(MultiLine {
-                min_height: min_height.clone(),
-                max_height: max_height.clone(),
-                content_height: content_height.clone(),
+                content_height: AtomicF32::new(0.),
                 scroll: scroll.clone(),
-                rect: rect.clone(),
-                baseline: baseline.clone(),
-                padding: padding.clone(),
-                cursor_descent: cursor_descent.clone(),
-                parent_rect: parent_rect.clone(),
-                editor: editor.clone(),
             }),
+            BaseEditType::MultiLine => {
+                let min_height =
+                    PropertyFloat32::wrap(node_ref, Role::Internal, "height_range", 0).unwrap();
+                let max_height =
+                    PropertyFloat32::wrap(node_ref, Role::Internal, "height_range", 1).unwrap();
+
+                Box::new(MultiLine {
+                    min_height: min_height.clone(),
+                    max_height: max_height.clone(),
+                    rect: rect.clone(),
+                    baseline: baseline.clone(),
+                    padding: padding.clone(),
+                    cursor_descent: cursor_descent.clone(),
+                    parent_rect: parent_rect.clone(),
+                    editor: editor.clone(),
+                    content_height: AtomicF32::new(0.),
+                    scroll: scroll.clone(),
+                })
+            }
         };
 
         let self_ = Arc::new(Self {
@@ -360,13 +359,9 @@ impl BaseEdit {
 
             is_active,
             is_focused,
-            min_height,
-            max_height,
-            content_height,
             rect,
             baseline,
             lineheight: lineheight.clone(),
-            scroll: scroll.clone(),
             scroll_speed,
             padding,
             font_size: font_size.clone(),
@@ -395,6 +390,7 @@ impl BaseEdit {
             blink_is_paused: AtomicBool::new(false),
             hide_cursor: AtomicBool::new(false),
             sel_sender: SyncMutex::new(None),
+            scroll: scroll.clone(),
 
             touch_info: SyncMutex::new(TouchInfo::new(scroll, behave.scroll_ctrl())),
             is_phone_select: AtomicBool::new(false),
@@ -861,10 +857,10 @@ impl BaseEdit {
                 let travel_dist = self.behave.scroll_ctrl().travel(*start_pos, touch_pos);
                 let mut scroll = scroll_start + travel_dist;
                 scroll = scroll.clamp(0., self.behave.max_scroll().await);
-                if (self.scroll.get() - scroll).abs() < VERT_SCROLL_UPDATE_INC {
+                if (self.scroll.load(Ordering::Relaxed) - scroll).abs() < VERT_SCROLL_UPDATE_INC {
                     return true
                 }
-                self.scroll.set(atom, scroll);
+                self.scroll.store(scroll, Ordering::Release);
                 self.redraw_scroll(atom.batch_id).await;
             }
             TouchStateAction::SetCursorPos => {
@@ -930,8 +926,8 @@ impl BaseEdit {
 
             let max_scroll = self.behave.max_scroll().await;
             let delta = travel * SELECT_SCROLL_TRAVEL_SPEED;
-            let scroll = (self.scroll.get() + delta).clamp(0., max_scroll);
-            self.scroll.set(atom, scroll);
+            let scroll = (self.scroll.load(Ordering::Relaxed) + delta).clamp(0., max_scroll);
+            self.scroll.store(scroll, Ordering::Release);
 
             self.redraw_scroll(atom.batch_id).await;
         }
@@ -1111,10 +1107,6 @@ impl BaseEdit {
         vec![DrawInstruction::Draw(mesh.alloc(&self.render_api).draw_untextured())]
     }
 
-    fn bounded_height(&self, height: f32) -> f32 {
-        height.clamp(self.min_height.get(), self.max_height.get())
-    }
-
     async fn make_draw_calls(&self, _trace_id: u32, atom: &mut PropertyAtomicGuard) -> DrawUpdate {
         self.behave.eval_rect(atom).await;
         let rect = self.rect.get();
@@ -1367,7 +1359,7 @@ impl UIObject for BaseEdit {
         async fn reset(self_: Arc<BaseEdit>, batch: BatchGuardPtr) {
             let atom = &mut batch.spawn();
             //self_.select_text.set_null(Role::Internal, 0).unwrap();
-            self_.scroll.set(atom, 0.);
+            self_.scroll.store(0., Ordering::Release);
             self_.redraw(atom).await;
         }
         async fn redraw(self_: Arc<BaseEdit>, batch: BatchGuardPtr) {
@@ -1699,10 +1691,11 @@ impl UIObject for BaseEdit {
 
         let atom = &mut self.render_api.make_guard(gfxtag!("BaseEdit::handle_mouse_wheel"));
 
-        let mut scroll = self.scroll.get() - wheel_pos.y * self.scroll_speed.get();
+        let mut scroll =
+            self.scroll.load(Ordering::Relaxed) - wheel_pos.y * self.scroll_speed.get();
         scroll = scroll.clamp(0., self.behave.max_scroll().await);
         t!("handle_mouse_wheel({wheel_pos:?}) [scroll={scroll}]");
-        self.scroll.set(atom, scroll);
+        self.scroll.store(scroll, Ordering::Release);
         self.redraw_scroll(atom.batch_id).await;
 
         true