From b1e9c419a6e810c5f70a009795f17ea9aad30299 Mon Sep 17 00:00:00 2001 From: Nico Weber Date: Tue, 3 Oct 2023 10:24:23 -0400 Subject: [PATCH] MacPDF: Add a split view for a togglable sidebar It can be toggled with the (only) toolbar button, or via the menu bar, or the standard shortcut Cmd-Ctrl-S. --- .../Contrib/MacPDF/MacPDFWindowController.mm | 38 ++++++++++++++++++- 1 file changed, 37 insertions(+), 1 deletion(-) diff --git a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm index 585be0fd02..6e54bbed40 100644 --- a/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm +++ b/Meta/Lagom/Contrib/MacPDF/MacPDFWindowController.mm @@ -29,9 +29,22 @@ return nil; _pdfView = [[MacPDFView alloc] initWithFrame:NSZeroRect]; - window.contentView = _pdfView; [_pdfView setDelegate:self]; + NSSplitViewController* split_view = [[NSSplitViewController alloc] initWithNibName:nil bundle:nil]; + [split_view addSplitViewItem:[self makeSidebarSplitItem]]; + [split_view addSplitViewItem:[NSSplitViewItem splitViewItemWithViewController:[self viewControllerForView:_pdfView]]]; + + // Autosave if the sidebar is open or not, and how far. + // autosaveName only works if identifier is set too. + // identifier docs: "For programmatically created views, you typically set this value + // after creating the item but before adding it to a window. [...] For views and controls + // in a window, the value you specify for this string must be unique on a per-window basis." + split_view.splitView.autosaveName = @"MacPDFSplitView"; + split_view.splitView.identifier = @"MacPDFSplitViewId"; + + window.contentViewController = split_view; + NSToolbar* toolbar = [[NSToolbar alloc] initWithIdentifier:@"MacPDFToolbar"]; toolbar.delegate = self; toolbar.displayMode = NSToolbarDisplayModeIconOnly; @@ -41,6 +54,27 @@ return self; } +- (NSViewController*)viewControllerForView:(NSView*)view +{ + NSViewController* view_controller = [[NSViewController alloc] initWithNibName:nil bundle:nil]; + view_controller.view = view; + return view_controller; +} + +- (NSSplitViewItem*)makeSidebarSplitItem +{ + // FIXME: Use an NSOutlineView with the document's outline. + NSView* side_view = [[NSView alloc] initWithFrame:NSZeroRect]; + NSSplitViewItem* item = [NSSplitViewItem sidebarWithViewController:[self viewControllerForView:side_view]]; + item.collapseBehavior = NSSplitViewItemCollapseBehaviorPreferResizingSplitViewWithFixedSiblings; + + // This only has an effect on the very first run. + // Later, the collapsed state is loaded from the sidebar's autosave data. + item.collapsed = YES; + + return item; +} + - (void)pdfDidInitialize { [_pdfView setDocument:_pdfDocument.pdf->make_weak_ptr()]; @@ -87,6 +121,8 @@ - (NSArray*)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar { + // NSToolbarToggleSidebarItemIdentifier sends toggleSidebar: along the responder chain, + // which NSSplitViewController conveniently implements. return @[ NSToolbarToggleSidebarItemIdentifier ]; }