+static IMP original_nsview_draw_rect;
+static std::vector<id> plugin_views;
+static uint32_t gandalf_block = 0;
+
+static void add_plugin_view (id view)
+{
+ plugin_views.push_back (view);
+ std::cerr << "Added " << view << " now have " << plugin_views.size() << endl;
+}
+
+static void remove_plugin_view (id view)
+{
+ std::vector<id>::iterator x = find (plugin_views.begin(), plugin_views.end(), view);
+ if (x != plugin_views.end()) {
+ plugin_views.erase (x);
+ std::cerr << "Removed " << view << " now have " << plugin_views.size() << endl;
+ }
+}
+
+static bool is_plugin_view (id view)
+{
+ for (std::vector<id>::const_iterator v = plugin_views.begin(); v != plugin_views.end(); ++v) {
+ /* displayIfNeeded seems to be invoked for some toplevel NSView
+ * that we can't actually access. It is a superview of the
+ * contentView of the NSWindow for the plugin.
+ */
+
+ if (view == [(*v) superview]) {
+ return true;
+ }
+ }
+ return false;
+}
+
+int gandalf_draw (id receiver, SEL selector, NSRect rect)
+{
+ if (gandalf_block && is_plugin_view (receiver)) {
+ gandalf_block--;
+ /* YOU ... SHALL .... NOT ... DRAW!!!! */
+ return 0;
+ }
+ return ((int(*)(id,SEL,NSRect)) original_nsview_draw_rect) (receiver, selector, rect);
+}
+
+@implementation NSView (Tracking)
++ (void) load {
+ static dispatch_once_t once_token;
+
+ dispatch_once (&once_token, ^{
+ Method target = class_getInstanceMethod ([NSView class], @selector(displayIfNeeded));
+ original_nsview_draw_rect = method_setImplementation (target, (IMP) gandalf_draw);
+ }
+ );
+}
+
+@end
+