Refactoring Status Menu
This commit is contained in:
@@ -22,101 +22,225 @@
|
||||
|
||||
#import "NSMenu+Ext.h"
|
||||
#import "StoreCoordinator.h"
|
||||
#import "UserPrefs.h"
|
||||
#import "Feed+Ext.h"
|
||||
#import "FeedGroup+Ext.h"
|
||||
#import "Constants.h"
|
||||
|
||||
typedef NS_ENUM(NSInteger, MenuItemTag) {
|
||||
/// Used in @c allowDisplayOfHeaderItem: to identify and enable items
|
||||
TagMarkAllRead = 1,
|
||||
TagMarkAllUnread = 2,
|
||||
TagOpenAllUnread = 3,
|
||||
/// Delimiter item between default header and core data items
|
||||
TagHeaderDelimiter = 8,
|
||||
/// Indicator whether unread count is currently shown in menu item title or not
|
||||
TagTitleCountVisible = 16,
|
||||
};
|
||||
|
||||
|
||||
@implementation NSMenu (Ext)
|
||||
|
||||
#pragma mark - Generator -
|
||||
|
||||
/// @return New main menu with target delegate.
|
||||
+ (instancetype)menuWithDelegate:(id<NSMenuDelegate>)target {
|
||||
NSMenu *menu = [[NSMenu alloc] initWithTitle:@"M"];
|
||||
menu.autoenablesItems = NO;
|
||||
menu.delegate = target;
|
||||
return menu;
|
||||
}
|
||||
|
||||
/// @return New menu with old title and delegate. Index path in title is appended.
|
||||
- (instancetype)submenuWithIndex:(int)index isFeed:(BOOL)flag {
|
||||
NSMenu *menu = [NSMenu menuWithDelegate:self.delegate];
|
||||
menu.title = [NSString stringWithFormat:@"%c%@.%d", (flag ? 'F' : 'G'), self.title, index];
|
||||
return menu;
|
||||
}
|
||||
|
||||
/// @return New menu with old title and delegate.
|
||||
- (instancetype)cleanInstanceCopy {
|
||||
NSMenu *menu = [NSMenu menuWithDelegate:self.delegate];
|
||||
menu.title = self.title;
|
||||
return menu;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Properties -
|
||||
|
||||
/// @return Dot separated list of @c sortIndex of each @c FeedGroup parent. Empty string if main menu.
|
||||
- (NSString*)titleIndexPath {
|
||||
if (self.title.length <= 2) return @"";
|
||||
return [self.title substringFromIndex:2];
|
||||
}
|
||||
|
||||
/// @return The menu item in the super menu. Or @c nil if there is no super menu.
|
||||
- (NSMenuItem*)parentItem {
|
||||
if (!self.supermenu) return nil;
|
||||
//return self.itemArray.firstObject.parentItem; // wont work without items
|
||||
//return self.supermenu.highlightedItem; // is highlight guaranteed?
|
||||
return [self.supermenu itemAtIndex:[self.supermenu indexOfItemWithSubmenu:self]];
|
||||
}
|
||||
|
||||
/// @return @c YES if menu is status bar menu.
|
||||
- (BOOL)isMainMenu {
|
||||
return [self.title isEqualToString:@"M"];
|
||||
}
|
||||
- (BOOL)isMainMenu { return (self.supermenu == nil); }
|
||||
|
||||
/// @return @c YES if menu contains feed articles only.
|
||||
- (BOOL)isFeedMenu {
|
||||
return [self.title characterAtIndex:0] == 'F';
|
||||
}
|
||||
- (BOOL)isFeedMenu { return ([self.title characterAtIndex:0] == 'F'); }
|
||||
|
||||
/// @return Either @c ScopeGlobal, @c ScopeGroup or @c ScopeFeed.
|
||||
- (MenuItemTag)scope {
|
||||
if ([self isFeedMenu]) return ScopeFeed;
|
||||
if ([self isMainMenu]) return ScopeGlobal;
|
||||
return ScopeGroup;
|
||||
}
|
||||
|
||||
/// @return Index offset of the first core data feed item (may be separator), skipping default header and main menu header.
|
||||
- (NSInteger)feedDataOffset {
|
||||
for (NSInteger i = 0; i < self.numberOfItems; i++) {
|
||||
if ([[[self itemAtIndex:i] representedObject] isKindOfClass:[NSManagedObjectID class]])
|
||||
return i;
|
||||
#pragma mark - Generator -
|
||||
|
||||
/// Create new @c NSMenuItem with empty submenu and append it to the menu. @return Inserted item.
|
||||
- (NSMenuItem*)insertFeedGroupItem:(FeedGroup*)fg {
|
||||
unichar chr = '-';
|
||||
NSMenuItem *item = nil;
|
||||
switch (fg.type) {
|
||||
case GROUP: item = [fg newMenuItem]; chr = 'G'; break;
|
||||
case FEED: item = [fg.feed newMenuItem]; chr = 'F'; break;
|
||||
case SEPARATOR: item = [NSMenuItem separatorItem]; break;
|
||||
}
|
||||
return 0;
|
||||
if (!item.isSeparatorItem) {
|
||||
NSString *t = [NSString stringWithFormat:@"%c%@.%d", chr, [self.title substringFromIndex:1], fg.sortIndex];
|
||||
item.submenu = [[NSMenu alloc] initWithTitle:t];
|
||||
}
|
||||
[self addItem:item];
|
||||
return item;
|
||||
}
|
||||
|
||||
/// Perform core data fetch request and return unread count for all descendent items.
|
||||
- (NSInteger)coreDataUnreadCount {
|
||||
NSUInteger loc = [self.title rangeOfString:@"."].location;
|
||||
NSString *path = nil;
|
||||
if (loc != NSNotFound)
|
||||
path = [self.title substringFromIndex:loc + 1];
|
||||
return [StoreCoordinator unreadCountForIndexPathString:path];
|
||||
/// Insert items 'Open all unread', 'Mark all read' and 'Mark all unread'.
|
||||
- (void)insertDefaultHeader {
|
||||
self.autoenablesItems = NO;
|
||||
NSMenuItem *itm = [self addItemIfAllowed:TagOpenAllUnread title:NSLocalizedString(@"Open all unread", nil)];
|
||||
if (itm) {
|
||||
[self addItem:[itm alternateWithTitle:[NSString stringWithFormat:NSLocalizedString(@"Open a few unread (%lu)", nil), [UserPrefs openFewLinksLimit]]]];
|
||||
}
|
||||
[self addItemIfAllowed:TagMarkAllRead title:NSLocalizedString(@"Mark all read", nil)];
|
||||
[self addItemIfAllowed:TagMarkAllUnread title:NSLocalizedString(@"Mark all unread", nil)];
|
||||
if (self.numberOfItems > 0) {
|
||||
// in case someone has disabled all header items. Else, during articles menu rebuild it will stay on top.
|
||||
NSMenuItem *sep = [NSMenuItem separatorItem];
|
||||
sep.tag = TagHeaderDelimiter;
|
||||
[self addItem:sep];
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Modify Menu -
|
||||
#pragma mark - Update Menu
|
||||
|
||||
/// Replace this menu with a clean @c NSMenu. Copy old @c title and @c delegate to new menu. @b Won't work without supermenu!
|
||||
- (void)cleanup {
|
||||
NSMenu *m = [[NSMenu alloc] initWithTitle:self.title];
|
||||
m.delegate = self.delegate;
|
||||
self.parentItem.submenu = m;
|
||||
}
|
||||
|
||||
/// Loop over default header and enable 'OpenAllUnread' and 'TagMarkAllRead' based on unread count.
|
||||
- (void)autoEnableMenuHeader:(BOOL)hasUnread {
|
||||
for (NSMenuItem *item in self.itemArray) {
|
||||
if (item.representedObject)
|
||||
return; // default menu has no represented object
|
||||
switch (item.tag & TagMaskType) {
|
||||
case TagOpenAllUnread: case TagMarkAllRead:
|
||||
item.enabled = hasUnread;
|
||||
default: break;
|
||||
- (void)setHeaderHasUnread:(BOOL)hasUnread hasRead:(BOOL)hasRead {
|
||||
NSInteger i = [self indexOfItemWithTag:TagHeaderDelimiter] - 1;
|
||||
for (; i >= 0; i--) {
|
||||
NSMenuItem *item = [self itemAtIndex:i];
|
||||
switch (item.tag) {
|
||||
case TagOpenAllUnread: // incl. alternate item
|
||||
case TagMarkAllRead:
|
||||
item.enabled = hasUnread; break;
|
||||
case TagMarkAllUnread:
|
||||
item.enabled = hasRead; break;
|
||||
}
|
||||
//[item applyUserSettingsDisplay]; // should not change while menu is open
|
||||
}
|
||||
}
|
||||
|
||||
/// Loop over menu and replace all separator items (text) with actual separator.
|
||||
- (void)replaceSeparatorStringsWithActualSeparator {
|
||||
for (NSInteger i = 0; i < self.numberOfItems; i++) {
|
||||
NSMenuItem *oldItem = [self itemAtIndex:i];
|
||||
if ([oldItem.title isEqualToString:kSeparatorItemTitle]) {
|
||||
NSMenuItem *newItem = [NSMenuItem separatorItem];
|
||||
newItem.representedObject = oldItem.representedObject;
|
||||
[self removeItemAtIndex:i];
|
||||
[self insertItem:newItem atIndex:i];
|
||||
/**
|
||||
Iterate over all menu items in @c self.itemArray and find the item where @c submenu.title matches
|
||||
the first @c sortIndex in @c path. Recursively repeat the process for the items of this submenu and so on.
|
||||
|
||||
@param path Dot separated list of @c sortIndex. E.g., @c Feed.indexPath.
|
||||
@return Either @c NSMenuItem that exactly matches @c path or one of the parent @c NSMenuItem if a submenu isn't open.
|
||||
*/
|
||||
- (NSMenuItem*)deepestItemWithPath:(nonnull NSString*)path {
|
||||
NSUInteger loc = [path rangeOfString:@"."].location;
|
||||
BOOL isLast = (loc == NSNotFound);
|
||||
NSString *indexStr = (isLast ? path : [path substringToIndex:loc]);
|
||||
for (NSMenuItem *item in self.itemArray) {
|
||||
if (item.hasSubmenu && [item.submenu.title hasSuffix:indexStr]) {
|
||||
if (!isLast && item.submenu.numberOfItems > 0)
|
||||
return [item.submenu deepestItemWithPath:[path substringFromIndex:loc+1]];
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
|
||||
#pragma mark - Helper
|
||||
|
||||
/// Check user preferences for preferred display style.
|
||||
- (BOOL)allowDisplayOfHeaderItem:(MenuItemTag)tag {
|
||||
static const char * A[] = {"", "global", "feed", "group"};
|
||||
static const char * B[] = {"", "MarkRead", "MarkUnread", "OpenUnread"};
|
||||
int idx = (self.isMainMenu ? 1 : (self.isFeedMenu ? 2 : 3));
|
||||
return [UserPrefs defaultYES:[NSString stringWithFormat:@"%s%s", A[idx], B[tag & 3]]]; // first 2 bits
|
||||
}
|
||||
|
||||
/// Check user preferences if item should be displayed in menu. If so, add it to the menu and set callback to @c self.
|
||||
- (NSMenuItem*)addItemIfAllowed:(MenuItemTag)tag title:(NSString*)title {
|
||||
if ([self allowDisplayOfHeaderItem:tag]) {
|
||||
NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:@selector(headerMenuItemCallback:) keyEquivalent:@""];
|
||||
item.target = [self class];
|
||||
item.tag = tag;
|
||||
item.representedObject = self.title;
|
||||
[self addItem:item];
|
||||
return item;
|
||||
}
|
||||
return nil;
|
||||
}
|
||||
|
||||
/// Prepare @c userInfo dictionary and send @c NSNotification. Callback for every default header menu item.
|
||||
+ (void)headerMenuItemCallback:(NSMenuItem*)sender {
|
||||
BOOL openLinks = NO;
|
||||
NSUInteger limit = 0;
|
||||
if (sender.tag == TagOpenAllUnread) {
|
||||
if (sender.isAlternate)
|
||||
limit = [UserPrefs openFewLinksLimit];
|
||||
openLinks = YES;
|
||||
} else if (sender.tag != TagMarkAllRead && sender.tag != TagMarkAllUnread) {
|
||||
return; // other menu item clicked. abort and return.
|
||||
}
|
||||
BOOL markRead = (sender.tag != TagMarkAllUnread);
|
||||
BOOL isFeedMenu = NO;
|
||||
NSString *path = sender.representedObject;
|
||||
if (path.length > 2) {
|
||||
isFeedMenu = ([path characterAtIndex:0] == 'F');
|
||||
path = [path substringFromIndex:2];
|
||||
} else { // main menu
|
||||
path = nil;
|
||||
}
|
||||
NSManagedObjectContext *moc = [StoreCoordinator createChildContext];
|
||||
NSArray<FeedArticle*> *list = [StoreCoordinator articlesAtPath:path isFeed:isFeedMenu sorted:openLinks unread:markRead inContext:moc limit:limit];
|
||||
|
||||
NSNumber *countDiff = [NSNumber numberWithUnsignedInteger:list.count];
|
||||
if (markRead) countDiff = [NSNumber numberWithInteger: -1 * countDiff.integerValue];
|
||||
|
||||
NSMutableArray<NSURL*> *urls = [NSMutableArray arrayWithCapacity:list.count];
|
||||
for (FeedArticle *fa in list) {
|
||||
fa.unread = !markRead;
|
||||
if (openLinks && fa.link.length > 0)
|
||||
[urls addObject:[NSURL URLWithString:fa.link]];
|
||||
}
|
||||
[StoreCoordinator saveContext:moc andParent:YES];
|
||||
[moc reset];
|
||||
if (openLinks)
|
||||
[UserPrefs openURLsWithPreferredBrowser:urls];
|
||||
[[NSNotificationCenter defaultCenter] postNotificationName:kNotificationTotalUnreadCountChanged object:countDiff];
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
|
||||
|
||||
#pragma mark - NSMenuItem Category
|
||||
|
||||
@implementation NSMenuItem (Ext)
|
||||
|
||||
/// Create a copy of an existing menu item and set it's option key modifier.
|
||||
- (instancetype)alternateWithTitle:(NSString*)title {
|
||||
NSMenuItem *alt = [self copy];
|
||||
alt.title = title;
|
||||
alt.keyEquivalentModifierMask = NSEventModifierFlagOption;
|
||||
if (!alt.hidden) { // hidden will be ignored if alternate is YES
|
||||
alt.hidden = YES; // force hidden to hide if menu is already open (background update)
|
||||
alt.alternate = YES;
|
||||
}
|
||||
return alt;
|
||||
}
|
||||
|
||||
/// Remove & append new unread count to title
|
||||
- (void)setTitleCount:(NSUInteger)count {
|
||||
if (self.tag == TagTitleCountVisible) {
|
||||
self.tag = 0; // clear mask
|
||||
NSUInteger loc = [self.title rangeOfString:@" (" options:NSLiteralSearch | NSBackwardsSearch].location;
|
||||
if (loc != NSNotFound)
|
||||
self.title = [self.title substringToIndex:loc];
|
||||
}
|
||||
if (count > 0 && [UserPrefs defaultYES:(self.submenu.isFeedMenu ? @"feedUnreadCount" : @"groupUnreadCount")]) {
|
||||
self.tag = TagTitleCountVisible; // apply new mask
|
||||
self.title = [self.title stringByAppendingFormat:@" (%ld)", count];
|
||||
}
|
||||
}
|
||||
|
||||
@end
|
||||
|
||||
Reference in New Issue
Block a user