Skip to content

Commit

Permalink
MacPF: Add a small group header in front of the outline
Browse files Browse the repository at this point in the history
This has to be part of the data source, which makes things a bit
annoying.

For PDFs that have no outline, it says "(No outline)".
  • Loading branch information
nico committed Oct 9, 2023
1 parent 4382f67 commit 6f776a8
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
1 change: 1 addition & 0 deletions Meta/Lagom/Contrib/MacPDF/MacPDFOutlineViewDataSource.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ NS_ASSUME_NONNULL_BEGIN
// Objective-C wrapper of PDF::OutlineItem, to launder it through the NSOutlineViewDataSource protocol.
@interface OutlineItemWrapper : NSObject

- (BOOL)isGroupItem;
- (Optional<u32>)page;

@end
Expand Down
31 changes: 29 additions & 2 deletions Meta/Lagom/Contrib/MacPDF/MacPDFOutlineViewDataSource.mm
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,9 @@

@interface OutlineItemWrapper ()
{
// Only one of those two is set.
RefPtr<PDF::OutlineItem> _item;
NSString* _groupName;
}
@end

Expand All @@ -18,11 +20,27 @@ - (instancetype)initWithItem:(NonnullRefPtr<PDF::OutlineItem>)item
if (self = [super init]; !self)
return nil;
_item = move(item);
_groupName = nil;
return self;
}

- (instancetype)initWithGroupName:(nonnull NSString*)groupName
{
if (self = [super init]; !self)
return nil;
_groupName = groupName;
return self;
}

- (BOOL)isGroupItem
{
return _groupName != nil;
}

- (Optional<u32>)page
{
if ([self isGroupItem])
return {};
return _item->dest.page.map([](u32 page_index) { return page_index + 1; });
}

Expand All @@ -33,11 +51,15 @@ - (OutlineItemWrapper*)child:(NSInteger)index

- (NSInteger)numberOfChildren
{
if ([self isGroupItem])
return 0;
return _item->children.size();
}

- (NSString*)objectValue
{
if (_groupName)
return _groupName;
return [NSString stringWithFormat:@"%s", _item->title.characters()]; // FIXME: encoding?
}
@end
Expand Down Expand Up @@ -65,7 +87,12 @@ - (id)outlineView:(NSOutlineView*)outlineView child:(NSInteger)index ofItem:(nul
if (item)
return [(OutlineItemWrapper*)item child:index];

return [[OutlineItemWrapper alloc] initWithItem:_outline->children[index]];
if (index == 0) {
bool has_outline = _outline && !_outline->children.is_empty();
// FIXME: Maybe put filename here instead?
return [[OutlineItemWrapper alloc] initWithGroupName:has_outline ? @"Outline" : @"(No outline)"];
}
return [[OutlineItemWrapper alloc] initWithItem:_outline->children[index - 1]];
}

- (BOOL)outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
Expand All @@ -78,7 +105,7 @@ - (NSInteger)outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(nul
if (item)
return [(OutlineItemWrapper*)item numberOfChildren];

return _outline ? _outline->children.size() : 0;
return 1 + (_outline ? _outline->children.size() : 0);
}

- (id)outlineView:(NSOutlineView*)outlineView objectValueForTableColumn:(nullable NSTableColumn*)tableColumn byItem:(nullable id)item
Expand Down
11 changes: 11 additions & 0 deletions Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ - (NSSplitViewItem*)makeSidebarSplitItem
_outlineView = [[NSOutlineView alloc] initWithFrame:NSZeroRect];

_outlineView.style = NSTableViewStyleSourceList;
_outlineView.floatsGroupRows = NO;
_outlineView.focusRingType = NSFocusRingTypeNone;
_outlineView.headerView = nil;

Expand Down Expand Up @@ -180,6 +181,16 @@ - (NSToolbarItem*)toolbar:(NSToolbar*)toolbar

#pragma mark - NSOutlineViewDelegate

- (BOOL)outlineView:(NSOutlineView*)outlineView isGroupItem:(id)item
{
return [item isGroupItem];
}

- (BOOL)outlineView:(NSOutlineView*)outlineView shouldSelectItem:(id)item
{
return ![self outlineView:outlineView isGroupItem:item];
}

// "This method is required if you wish to turn on the use of NSViews instead of NSCells."
- (NSView*)outlineView:(NSOutlineView*)outlineView viewForTableColumn:(NSTableColumn*)tableColumn item:(id)item
{
Expand Down

0 comments on commit 6f776a8

Please sign in to comment.