Skip to content

Commit

Permalink
[Mac] [GUI] Fix bug due to View class name conflict (#1012)
Browse files Browse the repository at this point in the history
* [Mac] [GUI] Fix bug due to View class name conflict

* [skip ci] rename
  • Loading branch information
k-ye authored May 18, 2020
1 parent a1b397a commit a397912
Showing 1 changed file with 10 additions and 5 deletions.
15 changes: 10 additions & 5 deletions taichi/gui/cocoa.cpp
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
#include "taichi/util/bit.h"
#include "taichi/common/task.h"
#include "taichi/gui/gui.h"
#include "taichi/util/bit.h"

#if defined(TI_GUI_COCOA)

#include "taichi/platform/mac/objc_api.h"

#include <algorithm>
#include <optional>
#include <string>
#include <unordered_map>

#include "taichi/platform/mac/objc_api.h"

// https://stackoverflow.com/questions/4356441/mac-os-cocoa-draw-a-simple-pixel-on-a-canvas
// http://cocoadevcentral.com/d/intro_to_quartz/
// Modified based on
Expand Down Expand Up @@ -134,6 +134,10 @@ constexpr int NSApplicationActivationPolicyRegular = 0;
constexpr int NSEventTypeKeyDown = 10;
constexpr int NSEventTypeKeyUp = 11;

// We need to give the View class a somewhat unique name, so that it won't
// conflict with other modules (e.g. matplotlib). See issue#998.
constexpr char kTaichiViewClassName[] = "TaichiGuiView";

} // namespace

extern id NSApp;
Expand Down Expand Up @@ -204,7 +208,8 @@ Class ViewClass;
Class AppDelClass;

__attribute__((constructor)) static void initView() {
ViewClass = objc_allocateClassPair((Class)objc_getClass("NSView"), "View", 0);
ViewClass = objc_allocateClassPair((Class)objc_getClass("NSView"),
kTaichiViewClassName, 0);
// There are two ways to update NSView's content, either via "drawRect:" or
// "updateLayer". Updating via layer can be a lot faster, so we use this
// method. See also:
Expand Down Expand Up @@ -258,7 +263,7 @@ void GUI::create_window() {
(NSTitledWindowMask | NSClosableWindowMask | NSResizableWindowMask |
NSMiniaturizableWindowMask),
0, false);
view = call(clscall("View", "alloc"), "initWithFrame:", rect);
view = call(clscall(kTaichiViewClassName, "alloc"), "initWithFrame:", rect);
gui_from_id[view] = this;
// Use layer to speed up the draw
// https://developer.apple.com/documentation/appkit/nsview/1483695-wantslayer?language=objc
Expand Down

0 comments on commit a397912

Please sign in to comment.