Просмотр исходного кода

app: fix broken editor for desktop builds due to recent android changes

darkfi 1 год назад
Родитель
Сommit
8fbdc0627d

+ 1 - 0
bin/app/src/app/schema/chat.rs

@@ -165,6 +165,7 @@ mod ui_consts {
     pub const CMD_HELP_NICK_CMD_WIDTH: f32 = 140.;
     pub const CMD_HELP_NICK_DESC_WIDTH: f32 = 500.;
     pub const CMD_HELP_NICK_DESC_X: f32 = 160.;
+    pub const CMD_HELP_LABEL_Y: f32 = 10.;
 
     pub const ACTION_POPUP_Y_OFF: f32 = 100.;
     pub const ACTION_COPY_RECT: Rectangle = Rectangle::new(0., 0., 100., 80.);

+ 6 - 0
bin/app/src/gfx/mod.rs

@@ -934,6 +934,12 @@ impl EventHandler for Stage {
         let god = GOD.get().unwrap();
         god.stop_app();
     }
+
+    fn force_reload(&mut self) {
+        let god = GOD.get().unwrap();
+        god.stop_app();
+        god.start_app();
+    }
 }
 
 pub fn run_gui() {

+ 5 - 4
bin/app/src/text2/editor/android.rs

@@ -122,7 +122,7 @@ impl Editor {
         t!("Initialized composer [{}]", self.composer_id);
     }
 
-    pub async fn on_text_changed(&mut self) {
+    pub async fn on_text_prop_changed(&mut self) {
         // Get modified text property
         let txt = self.text.get();
         // Update Android text buffer
@@ -131,12 +131,13 @@ impl Editor {
         // Refresh our layout
         self.refresh().await;
     }
-    pub async fn on_buffer_changed(&mut self) {
+    pub async fn on_buffer_changed(&mut self, atom: &mut PropertyAtomicGuard) {
         // Refresh the layout using the Android buffer
         self.refresh().await;
-        // Now update the text attribute
+
+        // Update the text attribute
         let edit = android::get_editable(self.composer_id).unwrap();
-        self.text.set(&mut PropertyAtomicGuard::new(), &edit.buffer);
+        self.text.set(atom, &edit.buffer);
     }
 
     /// Can only be called after AndroidSuggestEvent::Init.

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

@@ -36,7 +36,7 @@ pub struct Editor {
 }
 
 impl Editor {
-    pub async fn new(
+    pub fn new(
         text: PropertyStr,
         font_size: PropertyFloat32,
         text_color: PropertyColor,
@@ -46,17 +46,28 @@ impl Editor {
         let mut editor = parley::PlainEditor::new(1.);
         //let atxt = "A berry is a small, pulpy, and often edible fruit. Typically, berries are juicy, rounded, brightly colored, sweet, sour or tart, and do not have a stone or pit, although many pips or seeds may be present. Common examples of berries in the culinary sense are strawberries, raspberries, blueberries, blackberries, white currants, blackcurrants, and redcurrants. In Britain, soft fruit is a horticultural term for such fruits. The common usage of the term berry is different from the scientific or botanical definition of a berry, which refers to a fruit produced from the ovary of a single flower where the outer layer of the ovary wall develops into an edible fleshy portion (pericarp). The botanical definition includes many fruits that are not commonly known or referred to as berries, such as grapes, tomatoes, cucumbers, eggplants, bananas, and chili peppers.";
         //editor.set_text(atxt);
-        let mut self_ = Self { text, editor, font_size, text_color, window_scale, lineheight };
-        self_.refresh_layout().await;
-        self_
+        Self { text, editor, font_size, text_color, window_scale, lineheight }
     }
 
+    // These are android specific
     pub fn init(&mut self) {}
     pub fn setup(&mut self) {}
     pub fn focus(&self) {}
     pub fn unfocus(&self) {}
 
-    async fn refresh_layout(&mut self) {
+    pub async fn on_text_prop_changed(&mut self) {
+        // Get modified text property
+        let txt = self.text.get();
+        // Update Parley text buffer
+        self.editor.set_text(&txt);
+        // Refresh our layout
+        self.refresh().await;
+    }
+    pub async fn on_buffer_changed(&mut self, atom: &mut PropertyAtomicGuard) {
+        self.text.set(atom, self.editor.raw_text());
+    }
+
+    pub async fn refresh(&mut self) {
         let font_size = self.font_size.get();
         let text_color = self.text_color.get();
         let window_scale = self.window_scale.get();
@@ -73,10 +84,6 @@ impl Editor {
         let (font_ctx, layout_ctx) = txt_ctx.borrow();
         self.editor.refresh_layout(font_ctx, layout_ctx);
     }
-    pub async fn refresh(&mut self, atom: &mut PropertyAtomicGuard) {
-        self.refresh_layout().await;
-        self.text.set(atom, self.editor.raw_text());
-    }
 
     pub fn layout(&self) -> &parley::Layout<Color> {
         self.editor.try_layout().unwrap()

+ 24 - 15
bin/app/src/ui/chatedit.rs

@@ -523,17 +523,6 @@ impl ChatEdit {
         self.redraw().await;
     }
 
-    async fn insert_char(&self, key: char) {
-        t!("insert_char({key})");
-        let mut tmp = [0; 4];
-        let key_str = key.encode_utf8(&mut tmp);
-
-        let mut txt_ctx = text2::TEXT_CTX.get().await;
-        let mut editor = self.lock_editor().await;
-        let mut drv = editor.driver(&mut txt_ctx).await.unwrap();
-        drv.insert_or_replace_selection(&key_str);
-    }
-
     async fn handle_shortcut(
         &self,
         key: char,
@@ -644,6 +633,7 @@ impl ChatEdit {
             KeyCode::Enter | KeyCode::KpEnter => {
                 if mods.shift {
                     drv.insert_or_replace_selection("\n");
+                    editor.on_buffer_changed(atom);
                 }
             }
             KeyCode::Delete => {
@@ -652,6 +642,7 @@ impl ChatEdit {
                 } else {
                     drv.delete();
                 }
+                editor.on_buffer_changed(atom);
             }
             KeyCode::Backspace => {
                 if action_mod {
@@ -659,6 +650,7 @@ impl ChatEdit {
                 } else {
                     drv.backdelete();
                 }
+                editor.on_buffer_changed(atom);
             }
             KeyCode::Home => {
                 if action_mod {
@@ -1365,7 +1357,8 @@ impl ChatEdit {
             }
         }
 
-        editor.on_buffer_changed().await;
+        let atom = &mut PropertyAtomicGuard::new();
+        editor.on_buffer_changed(atom).await;
         drop(editor);
 
         self.redraw().await;
@@ -1434,7 +1427,7 @@ impl UIObject for ChatEdit {
             self_.redraw().await;
         }
         async fn set_text(self_: Arc<ChatEdit>) {
-            self_.lock_editor().await.on_text_changed().await;
+            self_.lock_editor().await.on_text_prop_changed().await;
             self_.redraw().await;
         }
 
@@ -1568,11 +1561,27 @@ impl UIObject for ChatEdit {
             return self.handle_shortcut(key, &mods, atom).await
         }
 
-        let atom = &mut PropertyAtomicGuard::new();
+        // Do nothing
+        if actions == 0 {
+            return true
+        }
+
+        let mut txt_ctx = text2::TEXT_CTX.get().await;
+        let mut editor = self.lock_editor().await;
+        let mut drv = editor.driver(&mut txt_ctx).await.unwrap();
+
         t!("Key {:?} has {} actions", key, actions);
         for _ in 0..actions {
-            self.insert_char(key).await;
+            let mut tmp = [0; 4];
+            let key_str = key.encode_utf8(&mut tmp);
+
+            drv.insert_or_replace_selection(&key_str);
         }
+        drop(drv);
+        drop(txt_ctx);
+        editor.on_buffer_changed(atom);
+        drop(editor);
+
         self.redraw().await;
         true
     }