vlc/modules/gui/macosx/library/VLCLibraryWindowToolbarDele...

185 lines
7.1 KiB
Objective-C
Raw Blame History

This file contains invisible Unicode characters

This file contains invisible Unicode characters that are indistinguishable to humans but may be processed differently by a computer. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*****************************************************************************
* VLCLibraryWindowToolbarDelegate.m: MacOS X interface module
*****************************************************************************
* Copyright (C) 2023 VLC authors and VideoLAN
*
* Authors: Claudio Cambra <developer@claudiocambra.com>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
*****************************************************************************/
#import "VLCLibraryWindowToolbarDelegate.h"
#import "library/VLCLibraryWindow.h"
#import "library/VLCLibraryWindowPlaylistSidebarViewController.h"
#import "library/VLCLibraryWindowSplitViewController.h"
NSString * const VLCLibraryWindowTrackingSeparatorToolbarItemIdentifier = @"VLCLibraryWindowTrackingSeparatorToolbarItemIdentifier";
@implementation VLCLibraryWindowToolbarDelegate
- (void)awakeFromNib
{
self.toolbar.allowsUserCustomization = NO;
if (@available(macOS 11.0, *)) {
const NSInteger navSidebarToggleToolbarItemIndex =
[self.toolbar.items indexOfObject:self.toggleNavSidebarToolbarItem];
NSAssert(navSidebarToggleToolbarItemIndex != NSNotFound,
@"Could not find navigation sidebar toggle toolbar item!");
const NSInteger trackingSeparatorItemIndex = navSidebarToggleToolbarItemIndex + 1;
[self.toolbar
insertItemWithItemIdentifier:VLCLibraryWindowTrackingSeparatorToolbarItemIdentifier
atIndex:trackingSeparatorItemIndex];
self.trackingSeparatorToolbarItem =
[self.toolbar.items objectAtIndex:trackingSeparatorItemIndex];
}
}
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar
itemForItemIdentifier:(NSToolbarItemIdentifier)itemIdentifier
willBeInsertedIntoToolbar:(BOOL)flag
{
if ([itemIdentifier isEqualToString:VLCLibraryWindowTrackingSeparatorToolbarItemIdentifier]) {
if (@available(macOS 11.0, *)) {
return [NSTrackingSeparatorToolbarItem trackingSeparatorToolbarItemWithIdentifier:itemIdentifier splitView:self.libraryWindow.mainSplitView dividerIndex:VLCLibraryWindowNavigationSidebarSplitViewDividerIndex];
}
}
return nil;
}
- (void)hideToolbarItem:(NSToolbarItem *)toolbarItem
{
const NSInteger toolbarItemIndex = [self.toolbar.items indexOfObject:toolbarItem];
if (toolbarItemIndex != NSNotFound) {
[self.toolbar removeItemAtIndex:toolbarItemIndex];
}
}
/*
* Try to insert the toolbar item ahead of a group of possible toolbar items.
* "items" should contain items sorted from the trailing edge of the toolbar to leading edge.
* "toolbarItem" will be inserted as close to the trailing edge as possible.
*
* If you have: | item1 | item2 | item3 | item4 |
* and the "items" parameter is an array containing @[item6, item5, item2, item1]
* then the "toolbarItem" provided to this function will place toolbarItem thus:
* | item1 | item2 | toolbarItem | item3 | item4 |
*/
- (void)insertToolbarItem:(NSToolbarItem *)toolbarItem inFrontOf:(NSArray<NSToolbarItem *> *)items
{
NSParameterAssert(toolbarItem != nil && items != nil && toolbarItem.itemIdentifier.length > 0);
const NSInteger toolbarItemIndex = [self.toolbar.items indexOfObject:toolbarItem];
if (toolbarItemIndex != NSNotFound) {
return;
}
for (NSToolbarItem * const item in items) {
const NSInteger itemIndex = [self.toolbar.items indexOfObject:item];
if (itemIndex != NSNotFound) {
[self.toolbar insertItemWithItemIdentifier:toolbarItem.itemIdentifier
atIndex:itemIndex + 1];
return;
}
}
[self.toolbar insertItemWithItemIdentifier:toolbarItem.itemIdentifier atIndex:0];
}
- (void)setForwardsBackwardsToolbarItemsVisible:(BOOL)visible
{
if (!visible) {
[self hideToolbarItem:self.forwardsToolbarItem];
[self hideToolbarItem:self.backwardsToolbarItem];
return;
}
[self insertToolbarItem:self.backwardsToolbarItem
inFrontOf:@[self.trackingSeparatorToolbarItem,
self.toggleNavSidebarToolbarItem]];
[self insertToolbarItem:self.forwardsToolbarItem
inFrontOf:@[self.backwardsToolbarItem,
self.trackingSeparatorToolbarItem,
self.toggleNavSidebarToolbarItem]];
}
- (void)setSortOrderToolbarItemVisible:(BOOL)visible
{
if (!visible) {
[self hideToolbarItem:self.sortOrderToolbarItem];
return;
}
[self insertToolbarItem:self.sortOrderToolbarItem
inFrontOf:@[self.libraryViewModeToolbarItem,
self.forwardsToolbarItem,
self.backwardsToolbarItem,
self.trackingSeparatorToolbarItem,
self.toggleNavSidebarToolbarItem]];
}
- (void)setLibrarySearchToolbarItemVisible:(BOOL)visible
{
if (!visible) {
[self hideToolbarItem:self.librarySearchToolbarItem];
[self.libraryWindow clearFilterString];
return;
}
// Display as far to the right as possible, but not in front of the playlist toggle button
NSMutableArray<NSToolbarItem *> * const currentToolbarItems =
[NSMutableArray arrayWithArray:self.toolbar.items];
if (currentToolbarItems.lastObject == self.togglePlaylistToolbarItem) {
[currentToolbarItems removeLastObject];
}
NSArray * const reversedCurrentToolbarItems =
currentToolbarItems.reverseObjectEnumerator.allObjects;
[self insertToolbarItem:self.librarySearchToolbarItem
inFrontOf:reversedCurrentToolbarItems];
}
- (void)setViewModeToolbarItemVisible:(BOOL)visible
{
if (!visible) {
[self hideToolbarItem:self.libraryViewModeToolbarItem];
return;
}
[self insertToolbarItem:self.libraryViewModeToolbarItem
inFrontOf:@[self.toggleNavSidebarToolbarItem,
self.trackingSeparatorToolbarItem,
self.forwardsToolbarItem,
self.backwardsToolbarItem]];
}
- (void)updatePlayqueueToggleState
{
NSView * const playlistView =
self.libraryWindow.splitViewController.playlistSidebarViewController.view;
self.libraryWindow.playQueueToggle.state =
[self.libraryWindow.mainSplitView isSubviewCollapsed:playlistView] ?
NSControlStateValueOff : NSControlStateValueOn;
}
@end