From 687aeda956fb282e55781525a580aaba4d440ad6 Mon Sep 17 00:00:00 2001 From: Takase <20792268+takase1121@users.noreply.github.com> Date: Sat, 22 Jun 2024 12:23:06 +0800 Subject: [PATCH] macos: support drag-and-drop and default file associations (#1822) * macos: support drag-and-drop and default file associations * resources/macos: use LSItemContentTypes to narrow down files * macos: support opening folders * rootview: workaround macos weird dnd event timing * core/rootview: rename variable and refactor if statement --- data/core/init.lua | 15 +-------------- data/core/rootview.lua | 25 ++++++++++++++++++++++++- resources/macos/Info.plist.in | 28 ++++++++++++++++++++++++++-- 3 files changed, 51 insertions(+), 17 deletions(-) diff --git a/data/core/init.lua b/data/core/init.lua index 910c24a3..ec6cebd5 100644 --- a/data/core/init.lua +++ b/data/core/init.lua @@ -1313,20 +1313,7 @@ function core.on_event(type, ...) elseif type == "minimized" or type == "maximized" or type == "restored" then core.window_mode = type == "restored" and "normal" or type elseif type == "filedropped" then - if not core.root_view:on_file_dropped(...) then - local filename, mx, my = ... - local info = system.get_file_info(filename) - if info and info.type == "dir" then - system.exec(string.format("%q %q", EXEFILE, filename)) - else - local ok, doc = core.try(core.open_doc, filename) - if ok then - local node = core.root_view.root_node:get_child_overlapping_point(mx, my) - node:set_active_view(node.active_view) - core.root_view:open_doc(doc) - end - end - end + core.root_view:on_file_dropped(...) elseif type == "focuslost" then core.root_view:on_focus_lost(...) elseif type == "quit" then diff --git a/data/core/rootview.lua b/data/core/rootview.lua index ec9d5e0e..965203fe 100644 --- a/data/core/rootview.lua +++ b/data/core/rootview.lua @@ -27,6 +27,7 @@ function RootView:new() self.grab = nil -- = {view = nil, button = nil} self.overlapping_view = nil self.touched_view = nil + self.first_update_done = false end @@ -372,7 +373,28 @@ end ---@return boolean function RootView:on_file_dropped(filename, x, y) local node = self.root_node:get_child_overlapping_point(x, y) - return node and node.active_view:on_file_dropped(filename, x, y) + local result = node and node.active_view:on_file_dropped(filename, x, y) + if result then return result end + local info = system.get_file_info(filename) + if info and info.type == "dir" then + if self.first_update_done then + -- first update done, open in new window + system.exec(string.format("%q %q", EXEFILE, filename)) + else + -- DND event before first update, this is sent by macOS when folder is dropped into the dock + core.confirm_close_docs(core.docs, function(dirpath) + core.open_folder_project(dirpath) + end, system.absolute_path(filename)) + end + else + local ok, doc = core.try(core.open_doc, filename) + if ok then + local node = core.root_view.root_node:get_child_overlapping_point(x, y) + node:set_active_view(node.active_view) + core.root_view:open_doc(doc) + end + end + return true end @@ -460,6 +482,7 @@ function RootView:update() self:update_drag_overlay() self:interpolate_drag_overlay(self.drag_overlay) self:interpolate_drag_overlay(self.drag_overlay_tab) + self.first_update_done = true end diff --git a/resources/macos/Info.plist.in b/resources/macos/Info.plist.in index a23d2a79..17d7408d 100644 --- a/resources/macos/Info.plist.in +++ b/resources/macos/Info.plist.in @@ -8,8 +8,32 @@ lite-xl CFBundleIconFile icon.icns - CFBundleIdentifier - com.lite-xl + CFBundleDocumentTypes + + + CFBundleTypeExtensions + + * + + CFBundleTypeRole + Editor + NSDocumentClass + NSDocument + CFBundleTypeName + All Files + + + LSItemContentTypes + + public.text + public.folder + + NSExportableTypes + + public.text + + CFBundleIdentifier + com.lite-xl CFBundleName Lite XL CFBundlePackageType