Skip to content

Commit

Permalink
Merge pull request #1699 from barijaona/sharingservice
Browse files Browse the repository at this point in the history
  • Loading branch information
barijaona authored Jul 8, 2023
2 parents ced711d + 739392c commit e6aff5f
Show file tree
Hide file tree
Showing 5 changed files with 38 additions and 17 deletions.
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";

0 comments on commit e6aff5f

Please sign in to comment.