Przeglądaj źródła

app: editor add insert() method which works on android too

darkfi 1 rok temu
rodzic
commit
b8226ec94d

+ 17 - 0
bin/app/java/MainActivity.java

@@ -128,6 +128,23 @@ public boolean setSelection(int id, int start, int end) {
     iv.inputConnection.endBatchEdit();
     iv.inputConnection.endBatchEdit();
     return true;
     return true;
 }
 }
+public boolean commitText(int id, String txt) {
+    //Log.d("darkfi", "setText(" + id + ", " + txt + ")");
+    InvisibleInputView iv = editors.get(id);
+    if (iv == null) {
+        return false;
+    }
+
+    if (iv.inputConnection == null) {
+        return false;
+    }
+
+    iv.inputConnection.beginBatchEdit();
+    iv.inputConnection.finishComposingText();
+    iv.inputConnection.commitText(txt, 1);
+    iv.inputConnection.endBatchEdit();
+    return true;
+}
 
 
 /*
 /*
 // Editable string with the spans displayed inline
 // Editable string with the spans displayed inline

+ 33 - 2
bin/app/src/android.rs

@@ -201,15 +201,18 @@ pub fn set_text(id: usize, text: &str) -> Option<()> {
 
 
         let new_string_utf = (**env).NewStringUTF.unwrap();
         let new_string_utf = (**env).NewStringUTF.unwrap();
         let jtext = new_string_utf(env, ctext.as_ptr());
         let jtext = new_string_utf(env, ctext.as_ptr());
+        let delete_local_ref = (**env).DeleteLocalRef.unwrap();
 
 
-        ndk_utils::call_bool_method!(
+        let res = ndk_utils::call_bool_method!(
             env,
             env,
             android::ACTIVITY,
             android::ACTIVITY,
             "setText",
             "setText",
             "(ILjava/lang/String;)Z",
             "(ILjava/lang/String;)Z",
             id as i32,
             id as i32,
             jtext
             jtext
-        )
+        );
+        delete_local_ref(env, jtext);
+        res
     };
     };
     if is_success == 0u8 {
     if is_success == 0u8 {
         None
         None
@@ -239,6 +242,34 @@ pub fn set_selection(id: usize, select_start: usize, select_end: usize) -> Optio
     }
     }
 }
 }
 
 
+pub fn commit_text(id: usize, text: &str) -> Option<()> {
+    let ctext = std::ffi::CString::new(text).unwrap();
+    let is_success = unsafe {
+        let env = android::attach_jni_env();
+
+        let new_string_utf = (**env).NewStringUTF.unwrap();
+        let delete_local_ref = (**env).DeleteLocalRef.unwrap();
+
+        let jtext = new_string_utf(env, ctext.as_ptr());
+
+        let res = ndk_utils::call_bool_method!(
+            env,
+            android::ACTIVITY,
+            "commitText",
+            "(ILjava/lang/String;)Z",
+            id as i32,
+            jtext
+        );
+        delete_local_ref(env, jtext);
+        res
+    };
+    if is_success == 0u8 {
+        None
+    } else {
+        Some(())
+    }
+}
+
 pub struct Editable {
 pub struct Editable {
     pub buffer: String,
     pub buffer: String,
     pub select_start: usize,
     pub select_start: usize,

+ 6 - 1
bin/app/src/text2/editor/android.rs

@@ -226,7 +226,12 @@ impl Editor {
         Point::new(cursor_rect.x0 as f32, cursor_rect.y0 as f32)
         Point::new(cursor_rect.x0 as f32, cursor_rect.y0 as f32)
     }
     }
 
 
-    pub async fn driver<'a>(
+    pub async fn insert(&mut self, txt: &str, atom: &mut PropertyAtomicGuard) {
+        android::commit_text(self.composer_id, txt);
+        self.on_buffer_changed(atom).await;
+    }
+
+    pub fn driver<'a>(
         &'a mut self,
         &'a mut self,
         txt_ctx: &'a mut TextContext,
         txt_ctx: &'a mut TextContext,
     ) -> Option<parley::PlainEditorDriver<'a, Color>> {
     ) -> Option<parley::PlainEditorDriver<'a, Color>> {

+ 9 - 1
bin/app/src/text2/editor/parley.rs

@@ -102,7 +102,15 @@ impl Editor {
         cursor_pos
         cursor_pos
     }
     }
 
 
-    pub async fn driver<'a>(
+    pub async fn insert(&mut self, txt: &str, atom: &mut PropertyAtomicGuard) {
+        let mut txt_ctx = TEXT_CTX.get().await;
+        let (font_ctx, layout_ctx) = txt_ctx.borrow();
+        let mut drv = self.editor.driver(font_ctx, layout_ctx);
+        drv.insert_or_replace_selection(&txt);
+        self.on_buffer_changed(atom).await;
+    }
+
+    pub fn driver<'a>(
         &'a mut self,
         &'a mut self,
         txt_ctx: &'a mut TextContext,
         txt_ctx: &'a mut TextContext,
     ) -> Option<parley::PlainEditorDriver<'a, Color>> {
     ) -> Option<parley::PlainEditorDriver<'a, Color>> {

+ 5 - 9
bin/app/src/ui/chatedit.rs

@@ -539,7 +539,7 @@ impl ChatEdit {
 
 
         let mut txt_ctx = text2::TEXT_CTX.get().await;
         let mut txt_ctx = text2::TEXT_CTX.get().await;
         let mut editor = self.lock_editor().await;
         let mut editor = self.lock_editor().await;
-        let mut drv = editor.driver(&mut txt_ctx).await.unwrap();
+        let mut drv = editor.driver(&mut txt_ctx).unwrap();
 
 
         match key {
         match key {
             'a' => {
             'a' => {
@@ -587,7 +587,7 @@ impl ChatEdit {
 
 
         let mut txt_ctx = text2::TEXT_CTX.get().await;
         let mut txt_ctx = text2::TEXT_CTX.get().await;
         let mut editor = self.lock_editor().await;
         let mut editor = self.lock_editor().await;
-        let mut drv = editor.driver(&mut txt_ctx).await.unwrap();
+        let mut drv = editor.driver(&mut txt_ctx).unwrap();
 
 
         match key {
         match key {
             KeyCode::Left => {
             KeyCode::Left => {
@@ -1268,12 +1268,8 @@ impl ChatEdit {
     }
     }
 
 
     async fn insert(&self, txt: &str, atom: &mut PropertyAtomicGuard) {
     async fn insert(&self, txt: &str, atom: &mut PropertyAtomicGuard) {
-        let mut txt_ctx = text2::TEXT_CTX.get().await;
         let mut editor = self.lock_editor().await;
         let mut editor = self.lock_editor().await;
-        let mut drv = editor.driver(&mut txt_ctx).await.unwrap();
-
-        drv.insert_or_replace_selection(&txt);
-        editor.on_buffer_changed(atom).await;
+        editor.insert(txt, atom).await;
     }
     }
 
 
     async fn process_insert_text_method(me: &Weak<Self>, sub: &MethodCallSub) -> bool {
     async fn process_insert_text_method(me: &Weak<Self>, sub: &MethodCallSub) -> bool {
@@ -1667,7 +1663,7 @@ impl UIObject for ChatEdit {
         {
         {
             let mut txt_ctx = text2::TEXT_CTX.get().await;
             let mut txt_ctx = text2::TEXT_CTX.get().await;
             let mut editor = self.lock_editor().await;
             let mut editor = self.lock_editor().await;
-            let mut drv = editor.driver(&mut txt_ctx).await.unwrap();
+            let mut drv = editor.driver(&mut txt_ctx).unwrap();
             drv.move_to_point(mouse_pos.x, mouse_pos.y);
             drv.move_to_point(mouse_pos.x, mouse_pos.y);
         }
         }
 
 
@@ -1717,7 +1713,7 @@ impl UIObject for ChatEdit {
         let seltext = {
         let seltext = {
             let mut txt_ctx = text2::TEXT_CTX.get().await;
             let mut txt_ctx = text2::TEXT_CTX.get().await;
             let mut editor = self.lock_editor().await;
             let mut editor = self.lock_editor().await;
-            let mut drv = editor.driver(&mut txt_ctx).await.unwrap();
+            let mut drv = editor.driver(&mut txt_ctx).unwrap();
             drv.extend_selection_to_point(mouse_pos.x, mouse_pos.y);
             drv.extend_selection_to_point(mouse_pos.x, mouse_pos.y);
             editor.selected_text()
             editor.selected_text()
         };
         };