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
This commit is contained in:
Takase
2024-06-23 10:08:08 +08:00
committed by takase1121
parent 214602d17a
commit ce70fcf5dd
3 changed files with 51 additions and 17 deletions
+24 -1
View File
@@ -24,6 +24,7 @@ function RootView:new()
base_color = style.drag_overlay_tab,
color = { table.unpack(style.drag_overlay_tab) } }
self.drag_overlay_tab.to = { x = 0, y = 0, w = 0, h = 0 }
self.first_update_done = false
end
@@ -319,7 +320,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
@@ -363,6 +385,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