From a48c9a11c16c6d60b3b44c1209d9081d58f7e19a Mon Sep 17 00:00:00 2001 From: Hugh Perkins Date: Wed, 4 Jun 2025 16:09:52 -0700 Subject: [PATCH 1/2] update for cocoa --- taichi/ui/CMakeLists.txt | 6 ++++ taichi/ui/ggui/gui.cpp | 13 ++++--- taichi/ui/gui/cocoa.cpp | 34 ++++++------------ taichi/ui/gui/cocoa_keycodes.h | 64 ++++++++++++++++++++++++++++++++++ 4 files changed, 90 insertions(+), 27 deletions(-) create mode 100644 taichi/ui/gui/cocoa_keycodes.h diff --git a/taichi/ui/CMakeLists.txt b/taichi/ui/CMakeLists.txt index d24b53bce8..871f8d0b13 100644 --- a/taichi/ui/CMakeLists.txt +++ b/taichi/ui/CMakeLists.txt @@ -8,6 +8,12 @@ add_library(taichi_ui gui/x11.cpp ) +if (APPLE) + set(CLANG_OSX_FLAGS -DTARGET_OS_OSX -x objective-c++) + target_compile_options(taichi_ui PRIVATE ${CLANG_OSX_FLAGS}) +endif() + + if(TI_WITH_GGUI AND NOT ANDROID) target_sources(taichi_ui PRIVATE common/window_base.cpp) endif() diff --git a/taichi/ui/ggui/gui.cpp b/taichi/ui/ggui/gui.cpp index f5bc108338..5da1fd269e 100644 --- a/taichi/ui/ggui/gui.cpp +++ b/taichi/ui/ggui/gui.cpp @@ -44,7 +44,9 @@ Gui::Gui(AppContext *app_context, SwapChain *swap_chain, TaichiWindow *window) { void Gui::init_render_resources(VkRenderPass render_pass) { ImGui_ImplVulkan_LoadFunctions( - load_vk_function_for_gui); // this is because we're using volk. + VK_API_VERSION_1_0, // or app_context_->config.vk_api_version + load_vk_function_for_gui, // this is because we're using volk. + nullptr); auto &device = static_cast(app_context_->device()); @@ -60,7 +62,9 @@ void Gui::init_render_resources(VkRenderPass render_pass) { init_info.Allocator = VK_NULL_HANDLE; init_info.MinImageCount = swap_chain_->surface().get_image_count(); init_info.ImageCount = swap_chain_->surface().get_image_count(); - ImGui_ImplVulkan_Init(&init_info, render_pass); + // new signature takes only the struct + init_info.RenderPass = render_pass; + ImGui_ImplVulkan_Init(&init_info); render_pass_ = render_pass; // Upload Fonts @@ -73,10 +77,11 @@ void Gui::init_render_resources(VkRenderPass render_pass) { ->vk_command_buffer() ->buffer; - ImGui_ImplVulkan_CreateFontsTexture(command_buffer); + // ≥ 1.90: the helper records its own commands + ImGui_ImplVulkan_CreateFontsTexture(); stream->submit_synced(cmd_list.get()); - ImGui_ImplVulkan_DestroyFontUploadObjects(); + ImGui_ImplVulkan_DestroyFontsTexture(); } prepare_for_next_frame(); diff --git a/taichi/ui/gui/cocoa.cpp b/taichi/ui/gui/cocoa.cpp index 67705a48c0..af53810207 100644 --- a/taichi/ui/gui/cocoa.cpp +++ b/taichi/ui/gui/cocoa.cpp @@ -21,11 +21,17 @@ // Obj-c runtime doc: // https://developer.apple.com/documentation/objectivec/objective-c_runtime?language=objc +#include +#include +#include + +typedef const struct __CFAttributedString *CFAttributedStringRef; #include -#include + #include #include -#include +#include +#include "taichi/ui/gui/cocoa_keycodes.h" namespace { using taichi::mac::call; @@ -113,13 +119,6 @@ std::string lookup_keysym(ushort keycode) { return "Vk" + std::to_string((int)keycode); } -// TODO(k-ye): Define all the magic numbers for Obj-C enums here -constexpr int NSApplicationActivationPolicyRegular = 0; -constexpr int NSEventTypeKeyDown = 10; -constexpr int NSEventTypeKeyUp = 11; -constexpr int NSEventTypeFlagsChanged = 12; -constexpr int NSEventTypeScrollWheel = 22; - struct ModifierFlagsHandler { struct Result { std::vector released; @@ -163,9 +162,6 @@ constexpr char kTaichiViewClassName[] = "TaichiGuiView"; } // namespace -extern id NSApp; -extern id const NSDefaultRunLoopMode; - typedef struct AppDel { Class isa; id window; @@ -182,14 +178,6 @@ class IdComparator { std::map gui_from_id; -enum { - NSBorderlessWindowMask = 0, - NSTitledWindowMask = 1 << 0, - NSClosableWindowMask = 1 << 1, - NSMiniaturizableWindowMask = 1 << 2, - NSResizableWindowMask = 1 << 3, -}; - void updateLayer(id self, SEL _) { using namespace taichi; auto *gui = gui_from_id[self]; @@ -293,7 +281,7 @@ void GUI::create_window() { // call(NSApp, "activateIgnoringOtherApps:", YES); img_data_length = width * height * 4; img_data.resize(img_data_length); - auto appDelObj = clscall("AppDelegate", "alloc"); + id appDelObj = clscall("AppDelegate", "alloc"); appDelObj = call(appDelObj, "init"); call(NSApp, "setDelegate:", appDelObj); window = clscall("NSWindow", "alloc"); @@ -324,7 +312,7 @@ void GUI::process_event() { "runMode:beforeDate:", NSDefaultRunLoopMode, clscall("NSDate", "distantPast")); while (1) { - auto event = call( + id event = call( NSApp, "nextEventMatchingMask:untilDate:inMode:dequeue:", NSUIntegerMax, clscall("NSDate", "distantPast"), NSDefaultRunLoopMode, YES); if (event != nullptr) { @@ -412,7 +400,7 @@ void GUI::process_event() { } void GUI::set_title(std::string title) { - auto str = clscall("NSString", "stringWithUTF8String:", title.c_str()); + id str = clscall("NSString", "stringWithUTF8String:", title.c_str()); call(window, "setTitle:", str); call(str, "release"); } diff --git a/taichi/ui/gui/cocoa_keycodes.h b/taichi/ui/gui/cocoa_keycodes.h new file mode 100644 index 0000000000..cd3b452235 --- /dev/null +++ b/taichi/ui/gui/cocoa_keycodes.h @@ -0,0 +1,64 @@ +// Define key codes that were previously in Carbon +#define kVK_ANSI_0 0x1D +#define kVK_ANSI_1 0x12 +#define kVK_ANSI_2 0x13 +#define kVK_ANSI_3 0x14 +#define kVK_ANSI_4 0x15 +#define kVK_ANSI_5 0x17 +#define kVK_ANSI_6 0x16 +#define kVK_ANSI_7 0x1A +#define kVK_ANSI_8 0x1C +#define kVK_ANSI_9 0x19 +#define kVK_ANSI_A 0x00 +#define kVK_ANSI_B 0x0B +#define kVK_ANSI_C 0x08 +#define kVK_ANSI_D 0x02 +#define kVK_ANSI_E 0x0E +#define kVK_ANSI_F 0x03 +#define kVK_ANSI_G 0x05 +#define kVK_ANSI_H 0x04 +#define kVK_ANSI_I 0x22 +#define kVK_ANSI_J 0x26 +#define kVK_ANSI_K 0x28 +#define kVK_ANSI_L 0x25 +#define kVK_ANSI_M 0x2E +#define kVK_ANSI_N 0x2D +#define kVK_ANSI_O 0x1F +#define kVK_ANSI_P 0x23 +#define kVK_ANSI_Q 0x0C +#define kVK_ANSI_R 0x0F +#define kVK_ANSI_S 0x01 +#define kVK_ANSI_T 0x11 +#define kVK_ANSI_U 0x20 +#define kVK_ANSI_V 0x09 +#define kVK_ANSI_W 0x0D +#define kVK_ANSI_X 0x07 +#define kVK_ANSI_Y 0x10 +#define kVK_ANSI_Z 0x06 + +#define kVK_F1 0x7A +#define kVK_F2 0x78 +#define kVK_F3 0x63 +#define kVK_F4 0x76 +#define kVK_F5 0x60 +#define kVK_F6 0x61 +#define kVK_F7 0x62 +#define kVK_F8 0x64 +#define kVK_F9 0x65 +#define kVK_F10 0x6D +#define kVK_F11 0x67 +#define kVK_F12 0x6F +#define kVK_F13 0x69 +#define kVK_F14 0x6B +#define kVK_F15 0x71 +#define kVK_F16 0x6A + +#define kVK_LeftArrow 0x7B +#define kVK_RightArrow 0x7C +#define kVK_UpArrow 0x7E +#define kVK_DownArrow 0x7D +#define kVK_Tab 0x30 +#define kVK_Return 0x24 +#define kVK_Delete 0x33 +#define kVK_Escape 0x35 +#define kVK_Space 0x31 From 07d692133aba361899dd04f766fc469c5a432bb6 Mon Sep 17 00:00:00 2001 From: Hugh Perkins Date: Wed, 4 Jun 2025 16:47:04 -0700 Subject: [PATCH 2/2] CFAttributedString.h --- taichi/ui/gui/cocoa.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/taichi/ui/gui/cocoa.cpp b/taichi/ui/gui/cocoa.cpp index af53810207..511c276980 100644 --- a/taichi/ui/gui/cocoa.cpp +++ b/taichi/ui/gui/cocoa.cpp @@ -25,9 +25,8 @@ #include #include -typedef const struct __CFAttributedString *CFAttributedStringRef; +#include #include - #include #include #include