Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mac] [GUI] Fix bug due to View class name conflict #1012

Merged
merged 2 commits into from
May 18, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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[] = "TaichiGuiClass";

} // 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