From 27d57e4c7d0f108df1d55d752b7e440b750294d4 Mon Sep 17 00:00:00 2001
From: Jonathan Wilkes <jon.w.wilkes@gmail.com>
Date: Mon, 19 Sep 2016 20:44:38 -0400
Subject: [PATCH] fix #119: store canvas title for later use. This will
 properly display the title for a dirty canvas in the case where the Pd patch
 is mutated with a loadbang (and properly other cases, too)

---
 pd/nw/pd_canvas.js | 16 ++++++++++++++++
 pd/nw/pdgui.js     | 19 ++++++++++++++++++-
 pd/src/g_canvas.c  |  2 +-
 3 files changed, 35 insertions(+), 2 deletions(-)

diff --git a/pd/nw/pd_canvas.js b/pd/nw/pd_canvas.js
index 74a93d90c..54dcace4e 100644
--- a/pd/nw/pd_canvas.js
+++ b/pd/nw/pd_canvas.js
@@ -927,6 +927,7 @@ function translate_form() {
 // It provides us with our canvas id from the C side.  Once we have it
 // we can create the menu and register event callbacks
 function register_window_id(cid, attr_array) {
+    var kludge_title;
     // We create the window menus and popup menu before doing anything else
     // to ensure that we don't try to set the svg size before these are done.
     // Otherwise we might set the svg size to the window viewport, only to have
@@ -950,6 +951,21 @@ function register_window_id(cid, attr_array) {
     // default. But if this changes we need to set its menu item checkbox
     // accordingly here
     //set_cord_inspector_checkbox();
+
+    // One final kludge-- because window creation is asyncronous, we may
+    // have gotten a dirty flag before the window was created. In that case
+    // we check the title_queue to see if our title now contains an asterisk
+    // (which is the visual cue for "dirty")
+
+    // Two possibilities for handling this better:
+    // have a representation of canvas attys in pdgui.js (editmode, dirty, etc.)
+    // or
+    // send those attys from Pd after mapping the canvas
+    kludge_title = pdgui.query_title_queue(cid);
+    if (kludge_title) {
+        nw.Window.get().title = kludge_title;
+    }
+    pdgui.free_title_queue(cid);
 }
 
 function create_popup_menu(name) {
diff --git a/pd/nw/pdgui.js b/pd/nw/pdgui.js
index f962121b8..204b62883 100644
--- a/pd/nw/pdgui.js
+++ b/pd/nw/pdgui.js
@@ -892,6 +892,7 @@ var scroll = {},
     doscroll = {},
     last_loaded,
     loading = {},
+    title_queue= {}, // ugly kluge to work around an ugly race condition
     popup_menu = {};
 
     var patchwin = {}; // object filled with cid: [Window object] pairs
@@ -995,9 +996,25 @@ exports.format_window_title = format_window_title;
 // (and maybe in other situations)
 function gui_canvas_set_title(cid, name, args, dir, dirty_flag) {
     var title = format_window_title(name, dirty_flag, args, dir);
-    patchwin[cid].title = title;
+    if (patchwin[cid]) {
+        patchwin[cid].title = title;
+    } else {
+        title_queue[cid] = title;
+    }
+}
+
+function query_title_queue(cid) {
+    return title_queue[cid];
 }
 
+exports.query_title_queue = query_title_queue;
+
+function free_title_queue(cid) {
+    title_queue[cid] = null;
+}
+
+exports.free_title_queue = free_title_queue;
+
 function window_is_loading(cid) {
     return loading[cid];
 }
diff --git a/pd/src/g_canvas.c b/pd/src/g_canvas.c
index 79cb8badb..632913107 100644
--- a/pd/src/g_canvas.c
+++ b/pd/src/g_canvas.c
@@ -690,7 +690,7 @@ void canvas_dirty(t_canvas *x, t_floatarg n)
     if ((unsigned)n != x2->gl_dirty)
     {
         x2->gl_dirty = n;
-        if (x2->gl_havewindow && glist_isvisible(x2))
+        if (x2->gl_havewindow)
             canvas_reflecttitle(x2);
     }
 }
-- 
GitLab