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

Provide richer information when using NSSharingService #1699

Merged
merged 3 commits into from
Jul 8, 2023
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
12 changes: 7 additions & 5 deletions Vienna/Sources/Main window/ArticleListView.m
Original file line number Diff line number Diff line change
Expand Up @@ -1478,12 +1478,13 @@ -(BOOL)copyTableSelection:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)
[pboard declareTypes:@[VNAPasteboardTypeRSSItem, VNAPasteboardTypeWebURLsWithTitles, NSPasteboardTypeString, NSPasteboardTypeHTML]
owner:self];
if (count == 1) {
[pboard addTypes:@[VNAPasteboardTypeURL, VNAPasteboardTypeURLName, NSPasteboardTypeURL]
[pboard addTypes:@[NSPasteboardTypeURL, VNAPasteboardTypeURLName]
owner:self];
}

// Open the HTML string
[fullHTMLText appendString:@"<html><body>"];
[fullHTMLText appendString:@"<html style=\"font-family:sans-serif;\">"
"<head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head><body>"];

// Get all the articles that are being dragged
NSUInteger msgIndex = rowIndexes.firstIndex;
Expand All @@ -1507,13 +1508,14 @@ -(BOOL)copyTableSelection:(NSIndexSet *)rowIndexes toPasteboard:(NSPasteboard *)
[arrayOfArticles addObject:articleDict];

// Plain text
[fullPlainText appendFormat:@"%@\n%@\n\n", msgTitle, msgText];
[fullPlainText appendFormat:@"%@\n%@\n\n", msgTitle, thisArticle.summary];

// Add HTML version too.
[fullHTMLText appendFormat:@"<a href=\"%@\">%@</a><br />%@<br /><br />", msgLink, msgTitle, msgText];
[fullHTMLText appendFormat:@"<div class=\"info\"><a href=\"%@\">%@</a><div>"
"<div class=\"articleBodyStyle\">%@</div><br>", msgLink, msgTitle, msgText];

if (count == 1) {
[pboard setString:msgLink forType:VNAPasteboardTypeURL];
[pboard setString:msgLink forType:NSPasteboardTypeURL];
[pboard setString:msgTitle forType:VNAPasteboardTypeURLName];

// Write the link to the pastboard.
Expand Down
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
12 changes: 7 additions & 5 deletions Vienna/Sources/Main window/UnifiedDisplayView.m
Original file line number Diff line number Diff line change
Expand Up @@ -599,12 +599,13 @@ -(BOOL)copyIndexesSelection:(NSIndexSet*)rowIndexes toPasteboard:(NSPasteboard *
// Set up the pasteboard
[pboard declareTypes:@[VNAPasteboardTypeRSSItem, VNAPasteboardTypeWebURLsWithTitles, NSPasteboardTypeString, NSPasteboardTypeHTML] owner:self];
if (count == 1) {
[pboard addTypes:@[VNAPasteboardTypeURL, VNAPasteboardTypeURLName, NSPasteboardTypeURL]
[pboard addTypes:@[NSPasteboardTypeURL, VNAPasteboardTypeURLName]
owner:self];
}

// Open the HTML string
[fullHTMLText appendString:@"<html><body>"];
[fullHTMLText appendString:@"<html style=\"font-family:sans-serif;\">"
"<head><meta http-equiv=\"content-type\" content=\"text/html; charset=UTF-8\"></head><body>"];

// Get all the articles that are being dragged
NSUInteger msgIndex = rowIndexes.firstIndex;
Expand All @@ -628,13 +629,14 @@ -(BOOL)copyIndexesSelection:(NSIndexSet*)rowIndexes toPasteboard:(NSPasteboard *
[arrayOfArticles addObject:articleDict];

// Plain text
[fullPlainText appendFormat:@"%@\n%@\n\n", msgTitle, msgText];
[fullPlainText appendFormat:@"%@\n%@\n\n", msgTitle, thisArticle.summary];

// Add HTML version too.
[fullHTMLText appendFormat:@"<a href=\"%@\">%@</a><br />%@<br /><br />", msgLink, msgTitle, msgText];
[fullHTMLText appendFormat:@"<div class=\"info\"><a href=\"%@\">%@</a><div>"
"<div class=\"articleBodyStyle\">%@</div><br>", msgLink, msgTitle, msgText];

if (count == 1) {
[pboard setString:msgLink forType:VNAPasteboardTypeURL];
[pboard setString:msgLink forType:NSPasteboardTypeURL];
[pboard setString:msgTitle forType:VNAPasteboardTypeURLName];

// Write the link to the pastboard.
Expand Down
1 change: 0 additions & 1 deletion Vienna/Sources/Shared/Constants.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ extern NSInteger const MA_Default_ConcurrentDownloads;
extern NSPasteboardType const VNAPasteboardTypeRSSItem;
extern NSPasteboardType const VNAPasteboardTypeFolderList;
extern NSPasteboardType const VNAPasteboardTypeRSSSource;
extern NSPasteboardType const VNAPasteboardTypeURL;
extern NSPasteboardType const VNAPasteboardTypeURLName;
extern NSPasteboardType const VNAPasteboardTypeWebURLsWithTitles;

Expand Down
3 changes: 1 addition & 2 deletions Vienna/Sources/Shared/Constants.m
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,5 @@
NSPasteboardType const VNAPasteboardTypeFolderList = @"ViennaFolderType";
NSPasteboardType const VNAPasteboardTypeRSSSource = @"CorePasteboardFlavorType 0x52535373";
NSPasteboardType const VNAPasteboardTypeRSSItem = @"CorePasteboardFlavorType 0x52535369";
NSPasteboardType const VNAPasteboardTypeURL = @"CorePasteboardFlavorType 0x75726C20";
NSPasteboardType const VNAPasteboardTypeURLName = @"CorePasteboardFlavorType 0x75726C6E";
NSPasteboardType const VNAPasteboardTypeURLName = @"public.url-name";
NSPasteboardType const VNAPasteboardTypeWebURLsWithTitles = @"WebURLsWithTitlesPboardType";