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-22 12:23:06 +08:00 committed by GitHub
parent 290c7bc27f
commit 687aeda956
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 51 additions and 17 deletions

View File

@ -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

View File

@ -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

View File

@ -8,8 +8,32 @@
<string>lite-xl</string>
<key>CFBundleIconFile</key>
<string>icon.icns</string>
<key>CFBundleIdentifier</key>
<string>com.lite-xl</string>
<key>CFBundleDocumentTypes</key>
<array>
<dict>
<key>CFBundleTypeExtensions</key>
<array>
<string>*</string>
</array>
<key>CFBundleTypeRole</key>
<string>Editor</string>
<key>NSDocumentClass</key>
<string>NSDocument</string>
<key>CFBundleTypeName</key>
<string>All Files</string>
</dict>
</array>
<key>LSItemContentTypes</key>
<array>
<string>public.text</string>
<string>public.folder</string>
</array>
<key>NSExportableTypes</key>
<array>
<string>public.text</string>
</array>
<key>CFBundleIdentifier</key>
<string>com.lite-xl</string>
<key>CFBundleName</key>
<string>Lite XL</string>
<key>CFBundlePackageType</key>