Browse Source

app: add prop.unlink() and call it when we delete chats

darkfi 5 months ago
parent
commit
7e3ada5bd2

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

@@ -377,7 +377,7 @@ pub async fn make(
     prop.set_expr(atom, Role::App, 2, expr::load_var("w")).unwrap();
     prop.set_f32(atom, Role::App, 3, CHATEDIT_HEIGHT).unwrap();
     node.set_property_f32(atom, Role::App, "font_size", FONTSIZE).unwrap();
-    node.set_property_str(atom, Role::App, "text", &("#".to_string() + channel)).unwrap();
+    node.set_property_str(atom, Role::App, "text", channel).unwrap();
     //node.set_property_bool(atom, Role::App, "debug", true).unwrap();
     //node.set_property_str(atom, Role::App, "text", "anon1").unwrap();
     let prop = node.get_property("text_color").unwrap();

+ 13 - 10
bin/app/src/app/schema/menu.rs

@@ -311,12 +311,12 @@ pub async fn make(app: &App, content: SceneNodePtr, i18n_fish: &I18nBabelFish) {
 
     let prop = node.get_property("items").unwrap();
     for channel in CHANNELS {
-        let label = "#".to_string() + channel;
-        prop.push_str(atom, Role::App, label).unwrap();
+        prop.push_str(atom, Role::App, *channel).unwrap();
     }
-    for channel in
-        ["john", "stacy", "barry", "steve", "obombo", "xyz", "lunar", "fren", "anon", "anon1"]
-    {
+    for channel in [
+        "@john", "@stacy", "@barry", "@steve", "@obombo", "@xyz", "@lunar", "@fren", "@anon",
+        "@anon1",
+    ] {
         prop.push_str(atom, Role::App, channel).unwrap();
     }
 
@@ -327,14 +327,13 @@ pub async fn make(app: &App, content: SceneNodePtr, i18n_fish: &I18nBabelFish) {
     let renderer = app.renderer.clone();
     let listen_click = app.ex.spawn(async move {
         while let Ok(data) = recvr.recv().await {
-            let item_name: String = deserialize(&data).unwrap();
-            let Some(channel) = item_name.strip_prefix('#') else { continue };
-            let chatview_path = format!("/window/content/{}_chat_layer", channel);
-            let chatview_node = sg_root.lookup_node(chatview_path).unwrap();
+            let channel: String = deserialize(&data).unwrap();
+            let path = format!("/window/content/{}_chat_layer", channel);
+            let node = sg_root.lookup_node(path).unwrap();
 
             let atom = &mut renderer.make_guard(gfxtag!("channel_clicked"));
             info!(target: "app::menu", "clicked: {channel}!");
-            chatview_node.set_property_bool(atom, Role::App, "is_visible", true).unwrap();
+            node.set_property_bool(atom, Role::App, "is_visible", true).unwrap();
             menu_is_visible.set(atom, false);
         }
     });
@@ -362,11 +361,15 @@ pub async fn make(app: &App, content: SceneNodePtr, i18n_fish: &I18nBabelFish) {
     // Subscribe to edit_done signal to log deleted items
     let (edit_done_slot, edit_done_recvr) = Slot::new("edit_done");
     menu_node.register("edit_done", edit_done_slot).unwrap();
+    let sg_root = app.sg_root.clone();
     let edit_done_listen = app.ex.spawn(async move {
         while let Ok(data) = edit_done_recvr.recv().await {
             let deleted_items: Vec<String> = deserialize(&data).unwrap();
             for item in deleted_items {
+                let path = format!("/window/content/{}_chat_layer", item);
+                let node = sg_root.lookup_node(path).unwrap();
                 debug!(target: "app::menu", "deleted item: {item}");
+                node.unlink();
             }
         }
     });

+ 1 - 1
bin/app/src/app/schema/mod.rs

@@ -116,7 +116,7 @@ mod ui_consts {
 pub use ui_consts::*;
 
 pub static CHANNELS: &'static [&str] =
-    &["dev", "media", "hackers", "memes", "philosophy", "markets", "math", "random"];
+    &["#dev", "#media", "#hackers", "#memes", "#philosophy", "#markets", "#math", "#random"];
 
 #[derive(PartialEq)]
 enum ColorScheme {

+ 22 - 0
bin/app/src/scene.rs

@@ -200,6 +200,22 @@ impl SceneNode {
         children.push(child);
     }
 
+    pub fn unlink(&self) {
+        // Try to read the parent
+        let Some(parent_weak) = self.parent.read().unwrap().clone() else { return };
+        // Try to upgrade to an Arc
+        let Some(parent) = parent_weak.upgrade() else { return };
+        // Delete ourself from the parent
+        let mut children = parent.children.write().unwrap();
+        for i in 0..children.len() {
+            if children[i].id == self.id {
+                children.remove(i);
+                return
+            }
+        }
+        panic!("child not found");
+    }
+
     pub fn get_children(&self) -> Vec<SceneNodePtr> {
         self.children.read().unwrap().clone()
     }
@@ -445,6 +461,12 @@ impl std::fmt::Debug for SceneNode {
     }
 }
 
+impl Drop for SceneNode {
+    fn drop(&mut self) {
+        t!("Drop {}:{}", self.name, self.id);
+    }
+}
+
 #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
 pub enum CallArgType {
     Uint32,