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

fix: delete file and dir fail #11

Merged
merged 3 commits into from
Aug 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
123 changes: 72 additions & 51 deletions FinderSyncExt/FinderOpen.swift
Original file line number Diff line number Diff line change
Expand Up @@ -21,30 +21,33 @@ class FinderOpen: FIFinderSync {
let finderChannel = FinderCommChannel()
let messager = Messager.shared

var bookmarkItems: [BookmarkFolderItem] = []

override init() {
super.init()

logger.info("---- finderOpen init")
finderChannel.setup(folderStore, menuStore)

messager.on(name: "quit") { _ in
self.isHostAppOpen = false
}
messager.on(name: "running") { _ in
self.isHostAppOpen = true
logger.warning("startt running")
}
}

// MARK: - Primary Finder Sync protocol methods

override func beginObservingDirectory(at url: URL) {
// The user is now seeing the container's contents.
// If they see it in more than one view at a time, we're only told once.
NSLog("beginObservingDirectoryAtURL: %@", url.path as NSString)
// let dirs = FIFinderSyncController.default().directoryURLs.map()
let dirs = FIFinderSyncController.default().directoryURLs!

// for dir in dirs {
// logger.notice("Sync directory set to \(dir.path)")
// }
for dir in dirs {
logger.notice("Sync directory set to \(dir.path)")
}
}

override func endObservingDirectory(at url: URL) {
Expand All @@ -56,9 +59,9 @@ class FinderOpen: FIFinderSync {
NSLog("requestBadgeIdentifierForURL: %@", url.path as NSString)

// For demonstration purposes, this picks one of our two badges, or no badge at all, based on the filename.
let whichBadge = abs(url.path.hash) % 3
let badgeIdentifier = ["", "One", "Two"][whichBadge]
FIFinderSyncController.default().setBadgeIdentifier(badgeIdentifier, for: url)
// let whichBadge = abs(url.path.hash) % 3
// let badgeIdentifier = ["", "One", "Two"][whichBadge]
// FIFinderSyncController.default().setBadgeIdentifier(badgeIdentifier, for: url)
}

// MARK: - Menu and toolbar item support
Expand All @@ -75,6 +78,23 @@ class FinderOpen: FIFinderSync {
return NSImage(systemSymbolName: "computermouse", accessibilityDescription: "RClick Menu")!
}

@MainActor func initMenuDirs() throws {
do {
let bks = try folderStore.getBookmarkItems()
logger.warning("start init fifindersync ")
if bks.isEmpty {
logger.warning("start init fifindersync empty ")
} else {
logger.warning("start init fifindersync else")
for dir in bks {
logger.warning("Sync directory set to \(dir.path) ")
}
}
} catch {
logger.error("Failed to load URLs: \(error)")
}
}

@MainActor override func menu(for menuKind: FIMenuKind) -> NSMenu {
// Produce a menu for the extension.

Expand All @@ -87,12 +107,11 @@ class FinderOpen: FIFinderSync {
for nsmenu in createAppItems() {
applicationMenu.addItem(nsmenu)
}
let fileMenuItem = createFileCreateMenuItem()

fileMenuItem.submenu = createFileCreateMenu()

applicationMenu.addItem(fileMenuItem)

if let fileMenuItem = createFileCreateMenuItem() {
applicationMenu.addItem(fileMenuItem)
}

case .contextualMenuForItems:
NSLog("contextualMenuForItems")

Expand All @@ -116,7 +135,7 @@ class FinderOpen: FIFinderSync {
for item in menuStore.appItems {
let menuItem = NSMenuItem()
menuItem.target = self
menuItem.title = String(localized: "\(item.name)打开")
menuItem.title = String(localized: "Open With \(item.name)")
menuItem.action = #selector(itemAction(_:))
menuItem.toolTip = "\(item.name)"
menuItem.tag = 0
Expand All @@ -132,27 +151,48 @@ class FinderOpen: FIFinderSync {
for item in menuStore.actionItems.filter(\.enabled) {
let menuItem = NSMenuItem()
menuItem.target = self
menuItem.title = item.name
menuItem.title = String(localized: String.LocalizationValue(item.key))
menuItem.action = #selector(itemAction(_:))
menuItem.toolTip = "\(item.name)"
menuItem.tag = 1
menuItem.image = NSImage(systemSymbolName: item.iconName, accessibilityDescription: item.iconName)!
logger.info("item key\(item.key)")
actionMenuitems.append(menuItem)
}
return actionMenuitems
}

@MainActor @objc func itemAction(_ menuItem: NSMenuItem) {
switch menuItem.tag {
case 0:
appOpen(menuItem, isContainer: false)
case 1:
actioning(menuItem, isContainer: false)
case 2:
createFile(menuItem, isContainer: false)
default:
break
}
}

// 创建文件菜单
@objc func createFileCreateMenu() -> NSMenu {
let submenu = NSMenu(title: "文件创建菜单")
for item in menuStore.filetypeItems.filter(\.enabled) {
// 创建文件菜单容器
@objc func createFileCreateMenuItem() -> NSMenuItem? {
let enabledFiletypeItems = menuStore.filetypeItems.filter(\.enabled)
if enabledFiletypeItems.isEmpty {
return nil
}
let menuItem = NSMenuItem()
menuItem.title = String(localized: "New File")
menuItem.image = NSImage(systemSymbolName: "doc.badge.plus", accessibilityDescription: "doc.badge.plus")!
let submenu = NSMenu(title: "file create menu")
for item in enabledFiletypeItems {
let menuItem = NSMenuItem()
menuItem.target = self
menuItem.title = item.name
menuItem.action = #selector(itemAction(_:))
menuItem.toolTip = "\(item.name)"
menuItem.tag = 2

if let img = NSImage(named: item.iconName) {
menuItem.image = img
} else {
Expand All @@ -161,15 +201,7 @@ class FinderOpen: FIFinderSync {

submenu.addItem(menuItem)
}
return submenu
}

// 创建文件菜单容器
@objc func createFileCreateMenuItem() -> NSMenuItem {
let menuItem = NSMenuItem()
menuItem.title = "创建文件"
menuItem.image = NSImage(systemSymbolName: "doc.badge.plus", accessibilityDescription: "doc.badge.plus")!

menuItem.submenu = submenu
return menuItem
}

Expand All @@ -183,19 +215,6 @@ class FinderOpen: FIFinderSync {
}
}

@MainActor @objc func itemAction(_ menuItem: NSMenuItem) {
switch menuItem.tag {
case 0:
appOpen(menuItem, isContainer: false)
case 1:
actioning(menuItem, isContainer: false)
case 2:
createFile(menuItem, isContainer: false)
default:
break
}
}

@MainActor @objc func createFile(_ menuItem: NSMenuItem, isContainer: Bool) {
let item = menuStore.getFileCreateItem(name: menuItem.title)
let url = FIFinderSyncController.default().targetedURL()
Expand All @@ -206,17 +225,19 @@ class FinderOpen: FIFinderSync {
}

@MainActor @objc func actioning(_ menuItem: NSMenuItem, isContainer: Bool) {
let item = menuStore.getActionItem(name: menuItem.title)
let urls = FIFinderSyncController.default().selectedItemURLs()

guard urls != nil else {
guard let item = menuStore.getActionItem(name: menuItem.title) else {
logger.info("not item ad ")
return
}

guard let urls = FIFinderSyncController.default().selectedItemURLs(), !urls.isEmpty else {
logger.info("not urls")
return
}

if let actionName = item?.key {
let urlstr = urls!.map { $0.path }
messager.sendMessage(name: Key.messageFromFinder, data: MessagePayload(action: actionName, target: urlstr))
} else {}
let urlstr = urls.map { $0.path }
logger.info("test \(String(localized: String.LocalizationValue(item.key)))")
messager.sendMessage(name: Key.messageFromFinder, data: MessagePayload(action: item.key, target: urlstr))
}

@objc func appOpen(_ menuItem: NSMenuItem, isContainer: Bool) {
Expand Down
8 changes: 5 additions & 3 deletions FinderSyncExt/FinderSyncExt.entitlements
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
<true/>
<key>com.apple.security.application-groups</key>
<array>
<string>group.cn.wflixu.RClickDebug</string>
<string>group.cn.wflixu.RClick</string>
</array>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.bookmarks.app-scope</key>
<true/>
<key>com.apple.security.files.bookmarks.read-write</key>
<true/>
<key>com.apple.security.files.user-selected.read-write</key>
<true/>
</dict>
Expand Down
16 changes: 0 additions & 16 deletions FinderSyncExt/FinderSyncExtRelease.entitlements

This file was deleted.

Loading