2009-03-08 02:38:25 +00:00
|
|
|
//
|
|
|
|
// PreferencesWindowController.m
|
|
|
|
// Cog
|
|
|
|
//
|
|
|
|
// Created by Vincent Spader on 3/7/09.
|
|
|
|
// Copyright 2009 __MyCompanyName__. All rights reserved.
|
|
|
|
//
|
|
|
|
|
|
|
|
#import "PreferencesWindow.h"
|
|
|
|
#import "PreferencePanePlugin.h"
|
|
|
|
|
2013-10-11 12:03:55 +00:00
|
|
|
#import "Logging.h"
|
|
|
|
|
2009-03-08 02:38:25 +00:00
|
|
|
@interface PreferencesWindow()
|
|
|
|
|
|
|
|
- (NSString *)lastPaneDefaultsKey;
|
|
|
|
- (BOOL)loadPaneNamed:(NSString *)name display:(BOOL)display;
|
|
|
|
- (void)createToolbar;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
|
2021-01-08 05:25:30 +00:00
|
|
|
@implementation PreferencesWindow {
|
|
|
|
NSMutableArray<NSToolbarItemIdentifier> *preferencePaneOrder;
|
|
|
|
NSMutableDictionary<NSToolbarItemIdentifier, id<PreferencePane>> *preferencePanes;
|
|
|
|
NSMutableDictionary<NSToolbarItemIdentifier, NSToolbarItem *> *toolbarItems;
|
|
|
|
}
|
2009-03-08 02:38:25 +00:00
|
|
|
|
2021-01-08 05:25:30 +00:00
|
|
|
- (instancetype)initWithPreferencePanes:(NSArray<id<PreferencePane>> *)panes
|
2009-03-08 02:38:25 +00:00
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
NSWindowStyleMask windowStyleMask =
|
|
|
|
(NSWindowStyleMaskClosable | NSWindowStyleMaskTitled);
|
|
|
|
self = [super initWithContentRect:NSMakeRect(0, 0, 530, 300)
|
|
|
|
styleMask:windowStyleMask
|
|
|
|
backing:NSBackingStoreBuffered
|
|
|
|
defer:NO];
|
|
|
|
if (self)
|
|
|
|
{
|
|
|
|
preferencePaneOrder = [[NSMutableArray alloc] init];
|
|
|
|
preferencePanes = [[NSMutableDictionary alloc] init];
|
|
|
|
|
|
|
|
for (id<PreferencePane> pane in panes) {
|
|
|
|
[preferencePaneOrder addObject:[pane title]];
|
|
|
|
[preferencePanes setObject:pane forKey:[pane title]];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self setReleasedWhenClosed:NO];
|
|
|
|
[self setTitle:@"Preferences"];
|
|
|
|
[self center];
|
|
|
|
|
2020-11-20 00:24:33 +00:00
|
|
|
if (@available(macOS 11, *)) {
|
|
|
|
[self setToolbarStyle:NSWindowToolbarStylePreference];
|
|
|
|
}
|
2021-01-08 05:25:30 +00:00
|
|
|
|
|
|
|
[self createToolbar];
|
|
|
|
}
|
|
|
|
|
2009-03-08 02:38:25 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)lastPaneDefaultsKey
|
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
return @"LastPreferencePane";
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)setContentView:(NSView *)view animate:(BOOL)animate
|
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
NSSize newSize = view.bounds.size;
|
|
|
|
NSSize oldSize = [self contentView].bounds.size;
|
|
|
|
|
|
|
|
CGFloat diff = newSize.height - oldSize.height;
|
|
|
|
NSRect newFrame = [self frame];
|
|
|
|
newFrame.size.height += diff;
|
|
|
|
newFrame.origin.y -= diff;
|
|
|
|
|
2009-03-08 02:38:25 +00:00
|
|
|
if (animate) {
|
2021-01-08 05:25:30 +00:00
|
|
|
[self setContentView:nil];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
2021-01-08 05:25:30 +00:00
|
|
|
[self setFrame:newFrame display:animate animate:animate];
|
|
|
|
|
|
|
|
[self setContentView:view];
|
|
|
|
[self setContentSize:newSize];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (BOOL)loadPaneNamed:(NSString *)name display:(BOOL)display
|
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
id<PreferencePane> paneController = preferencePanes[name];
|
2009-03-08 02:38:25 +00:00
|
|
|
if (!paneController) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSView *paneView = [paneController view];
|
2021-01-08 05:25:30 +00:00
|
|
|
if (!paneView || [self contentView] == paneView) {
|
2009-03-08 02:38:25 +00:00
|
|
|
return NO;
|
|
|
|
}
|
2021-01-08 05:25:30 +00:00
|
|
|
|
|
|
|
[self setContentView:paneView animate:display];
|
|
|
|
|
2009-03-08 02:38:25 +00:00
|
|
|
// Update defaults
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:name forKey:[self lastPaneDefaultsKey]];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2009-03-08 02:38:25 +00:00
|
|
|
[[self toolbar] setSelectedItemIdentifier:name];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2009-03-08 02:38:25 +00:00
|
|
|
return YES;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void)createToolbar
|
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
toolbarItems = [[NSMutableDictionary alloc] init];
|
|
|
|
for (NSString *name in preferencePaneOrder) {
|
|
|
|
id<PreferencePane> pane = preferencePanes[name];
|
|
|
|
|
|
|
|
NSToolbarItem *item = [[NSToolbarItem alloc] initWithItemIdentifier:name];
|
|
|
|
[item setPaletteLabel:name]; // item's label in the "Customize Toolbar" sheet (not relevant here, but we set it anyway)
|
|
|
|
[item setLabel:name]; // item's label in the toolbar
|
2009-03-08 02:38:25 +00:00
|
|
|
|
2021-01-08 05:25:30 +00:00
|
|
|
[item setToolTip:name];
|
|
|
|
[item setImage:[pane icon]];
|
2009-03-08 02:38:25 +00:00
|
|
|
|
2021-01-08 05:25:30 +00:00
|
|
|
[item setTarget:self];
|
|
|
|
[item setAction:@selector(toolbarItemClicked:)]; // action called when item is clicked
|
|
|
|
|
|
|
|
toolbarItems[name] = item;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSString *bundleIdentifier = [[NSBundle mainBundle] bundleIdentifier];
|
|
|
|
NSToolbar *toolbar = [[NSToolbar alloc] initWithIdentifier:[bundleIdentifier stringByAppendingString:@" Preferences Toolbar"]];
|
|
|
|
[toolbar setDelegate:self];
|
|
|
|
[toolbar setAllowsUserCustomization:NO];
|
|
|
|
[toolbar setAutosavesConfiguration:NO];
|
|
|
|
[toolbar setDisplayMode:NSToolbarDisplayModeIconAndLabel];
|
|
|
|
|
|
|
|
[toolbar setSizeMode:NSToolbarSizeModeDefault];
|
|
|
|
|
|
|
|
[self setToolbar:toolbar];
|
|
|
|
}
|
2009-03-08 02:38:25 +00:00
|
|
|
|
|
|
|
- (void)show
|
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
NSString *lastPane = [[NSUserDefaults standardUserDefaults] objectForKey:[self lastPaneDefaultsKey]];
|
|
|
|
// Previous pane names migrations.
|
|
|
|
if ([lastPane isEqualToString:NSLocalizedPrefString(@"Growl")]) {
|
|
|
|
lastPane = NSLocalizedPrefString(@"Notifications");
|
|
|
|
}
|
|
|
|
if ([lastPane isEqualToString:NSLocalizedPrefString(@"Last.fm")]) {
|
|
|
|
lastPane = NSLocalizedPrefString(@"Scrobble");
|
|
|
|
}
|
|
|
|
if (nil == lastPane) {
|
|
|
|
if (0 >= [preferencePaneOrder count]) {
|
|
|
|
ALog(@"Error: Preference panes not found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
lastPane = preferencePaneOrder[0];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self loadPaneNamed:lastPane display:NO];
|
|
|
|
|
|
|
|
[self makeKeyAndOrderFront:self];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2021-01-08 05:25:30 +00:00
|
|
|
// Close on Esc pressed.
|
|
|
|
- (void)cancelOperation:(id)sender {
|
|
|
|
[self close];
|
|
|
|
}
|
2009-03-08 02:38:25 +00:00
|
|
|
|
|
|
|
#pragma mark Delegate methods
|
|
|
|
|
|
|
|
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar*)toolbar
|
|
|
|
{
|
|
|
|
return preferencePaneOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar
|
|
|
|
{
|
|
|
|
return preferencePaneOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar*)toolbar
|
|
|
|
{
|
|
|
|
return preferencePaneOrder;
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
|
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
return toolbarItems[itemIdentifier];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void)toolbarItemClicked:(NSToolbarItem *)item
|
|
|
|
{
|
2021-01-08 05:25:30 +00:00
|
|
|
[self loadPaneNamed:[item itemIdentifier] display:YES];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|