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"
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
@interface PreferencesWindow ()
|
2009-03-08 02:38:25 +00:00
|
|
|
|
|
|
|
- (NSString *)lastPaneDefaultsKey;
|
|
|
|
- (BOOL)loadPaneNamed:(NSString *)name display:(BOOL)display;
|
|
|
|
- (void)createToolbar;
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
2021-01-08 05:25:30 +00:00
|
|
|
@implementation PreferencesWindow {
|
2022-02-07 05:49:27 +00:00
|
|
|
NSMutableArray<NSToolbarItemIdentifier> *preferencePaneOrder;
|
|
|
|
NSMutableDictionary<NSToolbarItemIdentifier, id<PreferencePane>> *preferencePanes;
|
|
|
|
NSMutableDictionary<NSToolbarItemIdentifier, NSToolbarItem *> *toolbarItems;
|
2021-01-08 05:25:30 +00:00
|
|
|
}
|
2009-03-08 02:38:25 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (instancetype)initWithPreferencePanes:(NSArray<id<PreferencePane>> *)panes {
|
|
|
|
NSWindowStyleMask windowStyleMask =
|
|
|
|
(NSWindowStyleMaskClosable | NSWindowStyleMaskTitled);
|
2022-07-13 11:37:16 +00:00
|
|
|
self = [super initWithContentRect:NSMakeRect(0, 0, 640, 300)
|
2022-02-07 05:49:27 +00:00
|
|
|
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];
|
2022-06-18 23:10:18 +00:00
|
|
|
[self setTitle:NSLocalizedString(@"PreferencesTitle", @"")];
|
2022-02-07 05:49:27 +00:00
|
|
|
[self center];
|
|
|
|
|
2022-06-23 05:54:32 +00:00
|
|
|
if(@available(macOS 11.0, *)) {
|
2022-02-07 05:49:27 +00:00
|
|
|
[self setToolbarStyle:NSWindowToolbarStylePreference];
|
|
|
|
}
|
|
|
|
|
|
|
|
[self createToolbar];
|
|
|
|
}
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2009-03-08 02:38:25 +00:00
|
|
|
return self;
|
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSString *)lastPaneDefaultsKey {
|
|
|
|
return @"LastPreferencePane";
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)setContentView:(NSView *)view animate:(BOOL)animate {
|
|
|
|
NSSize newSize = view.bounds.size;
|
|
|
|
NSSize oldSize = [self contentView].bounds.size;
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
CGFloat diff = newSize.height - oldSize.height;
|
|
|
|
NSRect newFrame = [self frame];
|
|
|
|
newFrame.size.height += diff;
|
|
|
|
newFrame.origin.y -= diff;
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
if(animate) {
|
|
|
|
[self setContentView:nil];
|
|
|
|
}
|
|
|
|
[self setFrame:newFrame display:animate animate:animate];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[self setContentView:view];
|
|
|
|
[self setContentSize:newSize];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (BOOL)loadPaneNamed:(NSString *)name display:(BOOL)display {
|
|
|
|
id<PreferencePane> paneController = preferencePanes[name];
|
|
|
|
if(!paneController) {
|
|
|
|
return NO;
|
|
|
|
}
|
|
|
|
|
|
|
|
NSView *paneView = [paneController view];
|
|
|
|
if(!paneView || [self contentView] == paneView) {
|
|
|
|
return NO;
|
|
|
|
}
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[self setContentView:paneView animate:display];
|
2022-07-25 06:14:52 +00:00
|
|
|
if([paneController respondsToSelector:@selector(refreshPathList:)]) {
|
|
|
|
[paneController refreshPathList:self];
|
|
|
|
}
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
// Update defaults
|
|
|
|
[[NSUserDefaults standardUserDefaults] setObject:name forKey:[self lastPaneDefaultsKey]];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[[self toolbar] setSelectedItemIdentifier:name];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
return YES;
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)createToolbar {
|
|
|
|
toolbarItems = [[NSMutableDictionary alloc] init];
|
|
|
|
for(NSString *name in preferencePaneOrder) {
|
|
|
|
id<PreferencePane> pane = preferencePanes[name];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[item setToolTip:name];
|
|
|
|
[item setImage:[pane icon]];
|
2009-03-08 02:38:25 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[item setTarget:self];
|
|
|
|
[item setAction:@selector(toolbarItemClicked:)]; // action called when item is clicked
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
toolbarItems[name] = item;
|
|
|
|
}
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
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];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[toolbar setSizeMode:NSToolbarSizeModeDefault];
|
2021-01-08 05:25:30 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
[self setToolbar:toolbar];
|
2021-01-08 05:25:30 +00:00
|
|
|
}
|
2009-03-08 02:38:25 +00:00
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)show {
|
|
|
|
NSString *lastPane = [[NSUserDefaults standardUserDefaults] objectForKey:[self lastPaneDefaultsKey]];
|
|
|
|
// Previous pane names migrations.
|
|
|
|
if([lastPane isEqualToString:NSLocalizedPrefString(@"Growl")]) {
|
|
|
|
lastPane = NSLocalizedPrefString(@"Notifications");
|
|
|
|
}
|
2022-06-20 10:35:29 +00:00
|
|
|
if([lastPane isEqualToString:NSLocalizedPrefString(@"Last.fm")] ||
|
|
|
|
[lastPane isEqualToString:NSLocalizedPrefString(@"Scrobble")]) {
|
|
|
|
lastPane = NSLocalizedPrefString(@"General");
|
2022-02-07 05:49:27 +00:00
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2022-06-24 07:29:50 +00:00
|
|
|
- (void)showPathSuggester {
|
|
|
|
NSString *name = NSLocalizedPrefString(@"General");
|
|
|
|
|
|
|
|
[self loadPaneNamed:name display:NO];
|
|
|
|
|
|
|
|
[self makeKeyAndOrderFront:self];
|
|
|
|
|
|
|
|
id<PreferencePane> pane = preferencePanes[name];
|
|
|
|
if(pane && [pane respondsToSelector:@selector(showPathSuggester:)]) {
|
|
|
|
[pane showPathSuggester:self];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-08 05:25:30 +00:00
|
|
|
// Close on Esc pressed.
|
|
|
|
- (void)cancelOperation:(id)sender {
|
2022-02-07 05:49:27 +00:00
|
|
|
[self close];
|
2021-01-08 05:25:30 +00:00
|
|
|
}
|
2009-03-08 02:38:25 +00:00
|
|
|
|
|
|
|
#pragma mark Delegate methods
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSArray *)toolbarAllowedItemIdentifiers:(NSToolbar *)toolbar {
|
|
|
|
return preferencePaneOrder;
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSArray *)toolbarSelectableItemIdentifiers:(NSToolbar *)toolbar {
|
|
|
|
return preferencePaneOrder;
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSArray *)toolbarDefaultItemIdentifiers:(NSToolbar *)toolbar {
|
|
|
|
return preferencePaneOrder;
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag {
|
|
|
|
return toolbarItems[itemIdentifier];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
2022-02-07 05:49:27 +00:00
|
|
|
- (void)toolbarItemClicked:(NSToolbarItem *)item {
|
|
|
|
[self loadPaneNamed:[item itemIdentifier] display:YES];
|
2009-03-08 02:38:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|