Skip to content

Commit

Permalink
Use titles as sharing service's subject
Browse files Browse the repository at this point in the history
Especially useful for the e-mail sharing service
  • Loading branch information
barijaona committed Jul 8, 2023
1 parent ada77a5 commit 9f85685
Showing 1 changed file with 23 additions and 4 deletions.
27 changes: 23 additions & 4 deletions Vienna/Sources/Main window/MainWindowController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -175,15 +175,23 @@ final class MainWindowController: NSWindowController {
}
}

var shareableItemsSubject = String()

private var shareableItems: [NSPasteboardWriting] {
var items = [URL]()
if let activeTab = browser.activeTab, let url = activeTab.tabUrl {
items.append(url)
shareableItemsSubject = activeTab.title ?? NSLocalizedString("URL", comment: "URL")
} else {
if let articles = articleListView?.markedArticleRange as? [Article] {
let links = articles.compactMap { $0.link }
let urls = links.compactMap { URL(string: $0) }
items = urls
if articles.count == 1 {
shareableItemsSubject = articles[0].title ?? ""
} else {
shareableItemsSubject = String(format: NSLocalizedString("%u articles", comment: ""), articles.count)
}
}
}
return items as [NSURL]
Expand Down Expand Up @@ -229,16 +237,21 @@ final class MainWindowController: NSWindowController {

@IBAction private func performSharingService(_ sender: Any) {
if (sender as? SharingServiceMenuItem)?.name == "emailLink" {
let sharingService = NSSharingService(named: .composeEmail)
sharingService?.perform(withItems: shareableItems)
if let sharingService = NSSharingService(named: .composeEmail) {
sharingService.delegate = self
sharingService.perform(withItems: shareableItems)
}
}

if (sender as? SharingServiceMenuItem)?.name == "safariReadingList" {
let sharingService = NSSharingService(named: .addToSafariReadingList)
sharingService?.perform(withItems: shareableItems)
if let sharingService = NSSharingService(named: .addToSafariReadingList) {
sharingService.delegate = self
sharingService.perform(withItems: shareableItems)
}
}

if let sharingService = (sender as? SharingServiceToolbarItem)?.service {
sharingService.delegate = self
sharingService.perform(withItems: shareableItems)
}
}
Expand Down Expand Up @@ -490,6 +503,12 @@ extension MainWindowController: NSSharingServiceDelegate {
return window
}

func sharingService(
_ sharingService: NSSharingService,
willShareItems items: [Any]
) {
sharingService.subject = shareableItemsSubject
}
}

// MARK: - NSSharingServicePickerDelegate
Expand Down

0 comments on commit 9f85685

Please sign in to comment.