mirror of
https://github.com/SerenityOS/serenity
synced 2024-07-21 10:05:32 +00:00
MacPDF: Add an NSOutlineViewDataSource for the PDF outline
Not used yet.
This commit is contained in:
parent
18dfc61280
commit
79bba20efc
|
@ -17,6 +17,7 @@ add_executable(MacPDF MACOSX_BUNDLE
|
|||
main.mm
|
||||
AppDelegate.mm
|
||||
MacPDFDocument.mm
|
||||
MacPDFOutlineViewDataSource.mm
|
||||
MacPDFView.mm
|
||||
MacPDFWindowController.mm
|
||||
)
|
||||
|
|
28
Meta/Lagom/Contrib/MacPDF/MacPDFOutlineViewDataSource.h
Normal file
28
Meta/Lagom/Contrib/MacPDF/MacPDFOutlineViewDataSource.h
Normal file
|
@ -0,0 +1,28 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "CocoaWrapper.h"
|
||||
|
||||
#include <LibPDF/Document.h>
|
||||
|
||||
NS_ASSUME_NONNULL_BEGIN
|
||||
|
||||
// Objective-C wrapper of PDF::OutlineItem, to launder it through the NSOutlineViewDataSource protocol.
|
||||
@interface OutlineItemWrapper : NSObject
|
||||
|
||||
- (Optional<u32>)page;
|
||||
|
||||
@end
|
||||
|
||||
@interface MacPDFOutlineViewDataSource : NSObject <NSOutlineViewDataSource>
|
||||
|
||||
- (instancetype)initWithOutline:(RefPtr<PDF::OutlineDict>)outline;
|
||||
|
||||
@end
|
||||
|
||||
NS_ASSUME_NONNULL_END
|
89
Meta/Lagom/Contrib/MacPDF/MacPDFOutlineViewDataSource.mm
Normal file
89
Meta/Lagom/Contrib/MacPDF/MacPDFOutlineViewDataSource.mm
Normal file
|
@ -0,0 +1,89 @@
|
|||
/*
|
||||
* Copyright (c) 2023, Nico Weber <thakis@chromium.org>
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#import "MacPDFOutlineViewDataSource.h"
|
||||
|
||||
@interface OutlineItemWrapper ()
|
||||
{
|
||||
RefPtr<PDF::OutlineItem> _item;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation OutlineItemWrapper
|
||||
- (instancetype)initWithItem:(NonnullRefPtr<PDF::OutlineItem>)item
|
||||
{
|
||||
if (self = [super init]; !self)
|
||||
return nil;
|
||||
_item = move(item);
|
||||
return self;
|
||||
}
|
||||
|
||||
- (Optional<u32>)page
|
||||
{
|
||||
return _item->dest.page.map([](u32 page_index) { return page_index + 1; });
|
||||
}
|
||||
|
||||
- (OutlineItemWrapper*)child:(NSInteger)index
|
||||
{
|
||||
return [[OutlineItemWrapper alloc] initWithItem:_item->children[index]];
|
||||
}
|
||||
|
||||
- (NSInteger)numberOfChildren
|
||||
{
|
||||
return _item->children.size();
|
||||
}
|
||||
|
||||
- (NSString*)objectValue
|
||||
{
|
||||
return [NSString stringWithFormat:@"%s", _item->title.characters()]; // FIXME: encoding?
|
||||
}
|
||||
@end
|
||||
|
||||
@interface MacPDFOutlineViewDataSource ()
|
||||
{
|
||||
RefPtr<PDF::OutlineDict> _outline;
|
||||
}
|
||||
@end
|
||||
|
||||
@implementation MacPDFOutlineViewDataSource
|
||||
|
||||
- (instancetype)initWithOutline:(RefPtr<PDF::OutlineDict>)outline
|
||||
{
|
||||
if (self = [super init]; !self)
|
||||
return nil;
|
||||
_outline = move(outline);
|
||||
return self;
|
||||
}
|
||||
|
||||
#pragma mark - NSOutlineViewDataSource
|
||||
|
||||
- (id)outlineView:(NSOutlineView*)outlineView child:(NSInteger)index ofItem:(nullable id)item
|
||||
{
|
||||
if (item)
|
||||
return [(OutlineItemWrapper*)item child:index];
|
||||
|
||||
return [[OutlineItemWrapper alloc] initWithItem:_outline->children[index]];
|
||||
}
|
||||
|
||||
- (BOOL)outlineView:(NSOutlineView*)outlineView isItemExpandable:(id)item
|
||||
{
|
||||
return [self outlineView:outlineView numberOfChildrenOfItem:item] > 0;
|
||||
}
|
||||
|
||||
- (NSInteger)outlineView:(NSOutlineView*)outlineView numberOfChildrenOfItem:(nullable id)item
|
||||
{
|
||||
if (item)
|
||||
return [(OutlineItemWrapper*)item numberOfChildren];
|
||||
|
||||
return _outline ? _outline->children.size() : 0;
|
||||
}
|
||||
|
||||
- (id)outlineView:(NSOutlineView*)outlineView objectValueForTableColumn:(nullable NSTableColumn*)tableColumn byItem:(nullable id)item
|
||||
{
|
||||
return [(OutlineItemWrapper*)item objectValue];
|
||||
}
|
||||
|
||||
@end
|
Loading…
Reference in a new issue