Remove MiniBrowser custom "Process Swap" menu item which does not work
[WebKit-https.git] / Tools / MiniBrowser / mac / SettingsController.m
1 /*
2  * Copyright (C) 2014-2016 Apple Inc. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  *    notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  *    notice, this list of conditions and the following disclaimer in the
11  *    documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY APPLE INC. AND ITS CONTRIBUTORS ``AS IS''
14  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
15  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR ITS CONTRIBUTORS
17  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
18  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
19  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
21  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
22  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
23  * THE POSSIBILITY OF SUCH DAMAGE.
24  */
25
26 #import "SettingsController.h"
27
28 #import "AppDelegate.h"
29 #import "AppKitCompatibilityDeclarations.h"
30 #import "BrowserWindowController.h"
31 #import <WebKit/WKPreferencesPrivate.h>
32
33 #if WK_API_ENABLED
34 #import <WebKit/_WKExperimentalFeature.h>
35 #import <WebKit/_WKInternalDebugFeature.h>
36 #endif
37
38 static NSString * const defaultURL = @"http://www.webkit.org/";
39 static NSString * const DefaultURLPreferenceKey = @"DefaultURL";
40
41 static NSString * const UseWebKit2ByDefaultPreferenceKey = @"UseWebKit2ByDefault";
42 static NSString * const CreateEditorByDefaultPreferenceKey = @"CreateEditorByDefault";
43 static NSString * const LayerBordersVisiblePreferenceKey = @"LayerBordersVisible";
44 static NSString * const SimpleLineLayoutEnabledPreferenceKey = @"SimpleLineLayoutEnabled";
45 static NSString * const SimpleLineLayoutDebugBordersEnabledPreferenceKey = @"SimpleLineLayoutDebugBordersEnabled";
46 static NSString * const TiledScrollingIndicatorVisiblePreferenceKey = @"TiledScrollingIndicatorVisible";
47 static NSString * const ReserveSpaceForBannersPreferenceKey = @"ReserveSpaceForBanners";
48
49 static NSString * const ResourceUsageOverlayVisiblePreferenceKey = @"ResourceUsageOverlayVisible";
50 static NSString * const LoadsAllSiteIconsKey = @"LoadsAllSiteIcons";
51 static NSString * const UsesGameControllerFrameworkKey = @"UsesGameControllerFramework";
52 static NSString * const IncrementalRenderingSuppressedPreferenceKey = @"IncrementalRenderingSuppressed";
53 static NSString * const AcceleratedDrawingEnabledPreferenceKey = @"AcceleratedDrawingEnabled";
54 static NSString * const DisplayListDrawingEnabledPreferenceKey = @"DisplayListDrawingEnabled";
55 static NSString * const SubpixelAntialiasedLayerTextEnabledPreferenceKey = @"SubpixelAntialiasedLayerTextEnabled";
56 static NSString * const ResourceLoadStatisticsEnabledPreferenceKey = @"ResourceLoadStatisticsEnabled";
57
58 static NSString * const NonFastScrollableRegionOverlayVisiblePreferenceKey = @"NonFastScrollableRegionOverlayVisible";
59 static NSString * const WheelEventHandlerRegionOverlayVisiblePreferenceKey = @"WheelEventHandlerRegionOverlayVisible";
60
61 static NSString * const UseTransparentWindowsPreferenceKey = @"UseTransparentWindows";
62 static NSString * const UsePaginatedModePreferenceKey = @"UsePaginatedMode";
63 static NSString * const EnableSubPixelCSSOMMetricsPreferenceKey = @"EnableSubPixelCSSOMMetrics";
64
65 static NSString * const VisualViewportEnabledPreferenceKey = @"VisualViewportEnabled";
66 static NSString * const LargeImageAsyncDecodingEnabledPreferenceKey = @"LargeImageAsyncDecodingEnabled";
67 static NSString * const AnimatedImageAsyncDecodingEnabledPreferenceKey = @"AnimatedImageAsyncDecodingEnabled";
68 static NSString * const AppleColorFilterEnabledPreferenceKey = @"AppleColorFilterEnabled";
69 static NSString * const PunchOutWhiteBackgroundsInDarkModePreferenceKey = @"PunchOutWhiteBackgroundsInDarkMode";
70 static NSString * const UseSystemAppearancePreferenceKey = @"UseSystemAppearance";
71
72 // This default name intentionally overlaps with the key that WebKit2 checks when creating a view.
73 static NSString * const UseRemoteLayerTreeDrawingAreaPreferenceKey = @"WebKit2UseRemoteLayerTreeDrawingArea";
74
75 static NSString * const PerWindowWebProcessesDisabledKey = @"PerWindowWebProcessesDisabled";
76 static NSString * const NetworkCacheSpeculativeRevalidationDisabledKey = @"NetworkCacheSpeculativeRevalidationDisabled";
77 static NSString * const ProcessSwapOnWindowOpenWithOpenerKey = @"ProcessSwapOnWindowOpenWithOpener";
78
79 typedef NS_ENUM(NSInteger, DebugOverylayMenuItemTag) {
80     NonFastScrollableRegionOverlayTag = 100,
81     WheelEventHandlerRegionOverlayTag,
82 #if WK_API_ENABLED
83     ExperimentalFeatureTag,
84     InternalDebugFeatureTag,
85 #endif
86 };
87
88 @implementation SettingsController
89
90 @synthesize menu=_menu;
91
92 + (instancetype)shared
93 {
94     static SettingsController *sharedSettingsController;
95     static dispatch_once_t onceToken;
96     dispatch_once(&onceToken, ^{
97         sharedSettingsController = [[super alloc] init];
98     });
99
100     return sharedSettingsController;
101 }
102
103 - (instancetype)init
104 {
105     self = [super init];
106     if (!self)
107         return nil;
108
109     NSArray *onByDefaultPrefs = @[
110         UseWebKit2ByDefaultPreferenceKey,
111         AcceleratedDrawingEnabledPreferenceKey,
112         SimpleLineLayoutEnabledPreferenceKey,
113 #if __MAC_OS_X_VERSION_MIN_REQUIRED >= 101300
114         SubpixelAntialiasedLayerTextEnabledPreferenceKey,
115 #endif
116         VisualViewportEnabledPreferenceKey,
117         LargeImageAsyncDecodingEnabledPreferenceKey,
118         AnimatedImageAsyncDecodingEnabledPreferenceKey,
119     ];
120     
121     NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
122     for (NSString *prefName in onByDefaultPrefs) {
123         if (![userDefaults objectForKey:prefName])
124             [userDefaults setBool:YES forKey:prefName];
125     }
126
127     return self;
128 }
129
130 - (NSMenu *)menu
131 {
132     if (!_menu)
133         [self _populateMenu];
134
135     return _menu;
136 }
137
138 - (void)_addItemWithTitle:(NSString *)title action:(SEL)action indented:(BOOL)indented
139 {
140     NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:title action:action keyEquivalent:@""];
141     [item setTarget:self];
142     if (indented)
143         [item setIndentationLevel:1];
144     [_menu addItem:item];
145     [item release];
146 }
147
148 - (void)_addHeaderWithTitle:(NSString *)title
149 {
150     [_menu addItem:[NSMenuItem separatorItem]];
151     [_menu addItem:[[[NSMenuItem alloc] initWithTitle:title action:nil keyEquivalent:@""] autorelease]];
152 }
153
154 - (void)_populateMenu
155 {
156     _menu = [[NSMenu alloc] initWithTitle:@"Settings"];
157
158     [self _addItemWithTitle:@"Use WebKit2 By Default" action:@selector(toggleUseWebKit2ByDefault:) indented:NO];
159     [self _addItemWithTitle:@"Create Editor By Default" action:@selector(toggleCreateEditorByDefault:) indented:NO];
160     [self _addItemWithTitle:@"Set Default URL to Current URL" action:@selector(setDefaultURLToCurrentURL:) indented:NO];
161
162     [_menu addItem:[NSMenuItem separatorItem]];
163
164     [self _addItemWithTitle:@"Use Transparent Windows" action:@selector(toggleUseTransparentWindows:) indented:NO];
165     [self _addItemWithTitle:@"Use Paginated Mode" action:@selector(toggleUsePaginatedMode:) indented:NO];
166     [self _addItemWithTitle:@"Show Layer Borders" action:@selector(toggleShowLayerBorders:) indented:NO];
167     [self _addItemWithTitle:@"Disable Simple Line Layout" action:@selector(toggleSimpleLineLayoutEnabled:) indented:NO];
168     [self _addItemWithTitle:@"Show Simple Line Layout Borders" action:@selector(toggleSimpleLineLayoutDebugBordersEnabled:) indented:NO];
169     [self _addItemWithTitle:@"Suppress Incremental Rendering in New Windows" action:@selector(toggleIncrementalRenderingSuppressed:) indented:NO];
170     [self _addItemWithTitle:@"Enable Accelerated Drawing" action:@selector(toggleAcceleratedDrawingEnabled:) indented:NO];
171     [self _addItemWithTitle:@"Enable Display List Drawing" action:@selector(toggleDisplayListDrawingEnabled:) indented:NO];
172     [self _addItemWithTitle:@"Enable Subpixel-antialiased Layer Text" action:@selector(toggleSubpixelAntialiasedLayerTextEnabled:) indented:NO];
173     [self _addItemWithTitle:@"Enable Visual Viewport" action:@selector(toggleVisualViewportEnabled:) indented:NO];
174     [self _addItemWithTitle:@"Enable Resource Load Statistics" action:@selector(toggleResourceLoadStatisticsEnabled:) indented:NO];
175     [self _addItemWithTitle:@"Enable Large Image Async Decoding" action:@selector(toggleLargeImageAsyncDecodingEnabled:) indented:NO];
176     [self _addItemWithTitle:@"Enable Animated Image Async Decoding" action:@selector(toggleAnimatedImageAsyncDecodingEnabled:) indented:NO];
177     [self _addItemWithTitle:@"Enable color-filter" action:@selector(toggleAppleColorFilterEnabled:) indented:NO];
178     [self _addItemWithTitle:@"Punch Out White Backgrounds in Dark Mode" action:@selector(togglePunchOutWhiteBackgroundsInDarkMode:) indented:NO];
179     [self _addItemWithTitle:@"Use System Appearance" action:@selector(toggleUseSystemAppearance:) indented:NO];
180     [self _addHeaderWithTitle:@"WebKit2-only Settings"];
181
182     [self _addItemWithTitle:@"Reserve Space For Banners" action:@selector(toggleReserveSpaceForBanners:) indented:YES];
183     [self _addItemWithTitle:@"Show Tiled Scrolling Indicator" action:@selector(toggleShowTiledScrollingIndicator:) indented:YES];
184     [self _addItemWithTitle:@"Use UI-Side Compositing" action:@selector(toggleUseUISideCompositing:) indented:YES];
185     [self _addItemWithTitle:@"Disable Per-Window Web Processes" action:@selector(togglePerWindowWebProcessesDisabled:) indented:YES];
186     [self _addItemWithTitle:@"Show Resource Usage Overlay" action:@selector(toggleShowResourceUsageOverlay:) indented:YES];
187     [self _addItemWithTitle:@"Load All Site Icons Per-Page" action:@selector(toggleLoadsAllSiteIcons:) indented:YES];
188     [self _addItemWithTitle:@"Use GameController.framework on macOS (Restart required)" action:@selector(toggleUsesGameControllerFramework:) indented:YES];
189     [self _addItemWithTitle:@"Disable network cache speculative revalidation" action:@selector(toggleNetworkCacheSpeculativeRevalidationDisabled:) indented:YES];
190     [self _addItemWithTitle:@"Enable Process Swap on window.open() with an opener" action:@selector(toggleProcessSwapOnWindowOpenWithOpener:) indented:YES];
191
192     NSMenuItem *debugOverlaysSubmenuItem = [[NSMenuItem alloc] initWithTitle:@"Debug Overlays" action:nil keyEquivalent:@""];
193     NSMenu *debugOverlaysMenu = [[NSMenu alloc] initWithTitle:@"Debug Overlays"];
194     [debugOverlaysSubmenuItem setSubmenu:debugOverlaysMenu];
195
196     NSMenuItem *nonFastScrollableRegionItem = [[NSMenuItem alloc] initWithTitle:@"Non-fast Scrollable Region" action:@selector(toggleDebugOverlay:) keyEquivalent:@""];
197     [nonFastScrollableRegionItem setTag:NonFastScrollableRegionOverlayTag];
198     [nonFastScrollableRegionItem setTarget:self];
199     [debugOverlaysMenu addItem:[nonFastScrollableRegionItem autorelease]];
200
201     NSMenuItem *wheelEventHandlerRegionItem = [[NSMenuItem alloc] initWithTitle:@"Wheel Event Handler Region" action:@selector(toggleDebugOverlay:) keyEquivalent:@""];
202     [wheelEventHandlerRegionItem setTag:WheelEventHandlerRegionOverlayTag];
203     [wheelEventHandlerRegionItem setTarget:self];
204     [debugOverlaysMenu addItem:[wheelEventHandlerRegionItem autorelease]];
205     [debugOverlaysMenu release];
206     
207     [_menu addItem:debugOverlaysSubmenuItem];
208     [debugOverlaysSubmenuItem release];
209
210 #if WK_API_ENABLED
211     NSMenuItem *experimentalFeaturesSubmenuItem = [[NSMenuItem alloc] initWithTitle:@"Experimental Features" action:nil keyEquivalent:@""];
212     NSMenu *experimentalFeaturesMenu = [[NSMenu alloc] initWithTitle:@"Experimental Features"];
213     [experimentalFeaturesSubmenuItem setSubmenu:experimentalFeaturesMenu];
214
215     NSArray<_WKExperimentalFeature *> *experimentalFeatures = [WKPreferences _experimentalFeatures];
216
217     for (_WKExperimentalFeature *feature in experimentalFeatures) {
218         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:feature.name action:@selector(toggleExperimentalFeature:) keyEquivalent:@""];
219         item.toolTip = feature.details;
220         item.representedObject = feature;
221
222         [item setTag:ExperimentalFeatureTag];
223         [item setTarget:self];
224         [experimentalFeaturesMenu addItem:[item autorelease]];
225     }
226
227     [_menu addItem:experimentalFeaturesSubmenuItem];
228     [experimentalFeaturesSubmenuItem release];
229     [experimentalFeaturesMenu release];
230
231     NSMenuItem *internalDebugFeaturesSubmenuItem = [[NSMenuItem alloc] initWithTitle:@"Internal Features" action:nil keyEquivalent:@""];
232     NSMenu *internalDebugFeaturesMenu = [[NSMenu alloc] initWithTitle:@"Internal Features"];
233     [internalDebugFeaturesSubmenuItem setSubmenu:internalDebugFeaturesMenu];
234
235     NSArray<_WKInternalDebugFeature *> *internalDebugFeatures = [WKPreferences _internalDebugFeatures];
236
237     for (_WKInternalDebugFeature *feature in internalDebugFeatures) {
238         NSMenuItem *item = [[NSMenuItem alloc] initWithTitle:feature.name action:@selector(toggleInternalDebugFeature:) keyEquivalent:@""];
239         item.toolTip = feature.details;
240         item.representedObject = feature;
241
242         [item setTag:InternalDebugFeatureTag];
243         [item setTarget:self];
244         [internalDebugFeaturesMenu addItem:[item autorelease]];
245     }
246
247     [_menu addItem:internalDebugFeaturesSubmenuItem];
248     [internalDebugFeaturesSubmenuItem release];
249     [internalDebugFeaturesMenu release];
250 #endif // WK_API_ENABLED
251
252     [self _addHeaderWithTitle:@"WebKit1-only Settings"];
253     [self _addItemWithTitle:@"Enable Subpixel CSSOM Metrics" action:@selector(toggleEnableSubPixelCSSOMMetrics:) indented:YES];
254 }
255
256 - (BOOL)validateMenuItem:(NSMenuItem *)menuItem
257 {
258     SEL action = [menuItem action];
259
260     if (action == @selector(toggleUseWebKit2ByDefault:))
261         [menuItem setState:[self useWebKit2ByDefault] ? NSControlStateValueOn : NSControlStateValueOff];
262     else if (action == @selector(toggleCreateEditorByDefault:))
263         [menuItem setState:[self createEditorByDefault] ? NSControlStateValueOn : NSControlStateValueOff];
264     else if (action == @selector(toggleUseTransparentWindows:))
265         [menuItem setState:[self useTransparentWindows] ? NSControlStateValueOn : NSControlStateValueOff];
266     else if (action == @selector(toggleUsePaginatedMode:))
267         [menuItem setState:[self usePaginatedMode] ? NSControlStateValueOn : NSControlStateValueOff];
268     else if (action == @selector(toggleShowLayerBorders:))
269         [menuItem setState:[self layerBordersVisible] ? NSControlStateValueOn : NSControlStateValueOff];
270     else if (action == @selector(toggleSimpleLineLayoutEnabled:))
271         [menuItem setState:[self simpleLineLayoutEnabled] ? NSControlStateValueOff : NSControlStateValueOn];
272     else if (action == @selector(toggleSimpleLineLayoutDebugBordersEnabled:))
273         [menuItem setState:[self simpleLineLayoutDebugBordersEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
274     else if (action == @selector(toggleIncrementalRenderingSuppressed:))
275         [menuItem setState:[self incrementalRenderingSuppressed] ? NSControlStateValueOn : NSControlStateValueOff];
276     else if (action == @selector(toggleAcceleratedDrawingEnabled:))
277         [menuItem setState:[self acceleratedDrawingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
278     else if (action == @selector(toggleDisplayListDrawingEnabled:))
279         [menuItem setState:[self displayListDrawingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
280     else if (action == @selector(toggleSubpixelAntialiasedLayerTextEnabled:))
281         [menuItem setState:[self subpixelAntialiasedLayerTextEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
282     else if (action == @selector(toggleResourceLoadStatisticsEnabled:))
283         [menuItem setState:[self resourceLoadStatisticsEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
284     else if (action == @selector(toggleLargeImageAsyncDecodingEnabled:))
285         [menuItem setState:[self largeImageAsyncDecodingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
286     else if (action == @selector(toggleAnimatedImageAsyncDecodingEnabled:))
287         [menuItem setState:[self animatedImageAsyncDecodingEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
288     else if (action == @selector(toggleAppleColorFilterEnabled:))
289         [menuItem setState:[self appleColorFilterEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
290     else if (action == @selector(togglePunchOutWhiteBackgroundsInDarkMode:))
291         [menuItem setState:[self punchOutWhiteBackgroundsInDarkMode] ? NSControlStateValueOn : NSControlStateValueOff];
292     else if (action == @selector(toggleUseSystemAppearance:))
293         [menuItem setState:[self useSystemAppearance] ? NSControlStateValueOn : NSControlStateValueOff];
294     else if (action == @selector(toggleVisualViewportEnabled:))
295         [menuItem setState:[self visualViewportEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
296     else if (action == @selector(toggleReserveSpaceForBanners:))
297         [menuItem setState:[self isSpaceReservedForBanners] ? NSControlStateValueOn : NSControlStateValueOff];
298     else if (action == @selector(toggleShowTiledScrollingIndicator:))
299         [menuItem setState:[self tiledScrollingIndicatorVisible] ? NSControlStateValueOn : NSControlStateValueOff];
300     else if (action == @selector(toggleShowResourceUsageOverlay:))
301         [menuItem setState:[self resourceUsageOverlayVisible] ? NSControlStateValueOn : NSControlStateValueOff];
302     else if (action == @selector(toggleLoadsAllSiteIcons:))
303         [menuItem setState:[self loadsAllSiteIcons] ? NSControlStateValueOn : NSControlStateValueOff];
304     else if (action == @selector(toggleUsesGameControllerFramework:))
305         [menuItem setState:[self usesGameControllerFramework] ? NSControlStateValueOn : NSControlStateValueOff];
306     else if (action == @selector(toggleNetworkCacheSpeculativeRevalidationDisabled:))
307         [menuItem setState:[self networkCacheSpeculativeRevalidationDisabled] ? NSControlStateValueOn : NSControlStateValueOff];
308     else if (action == @selector(toggleProcessSwapOnWindowOpenWithOpener:))
309         [menuItem setState:[self processSwapOnWindowOpenWithOpenerEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
310     else if (action == @selector(toggleUseUISideCompositing:))
311         [menuItem setState:[self useUISideCompositing] ? NSControlStateValueOn : NSControlStateValueOff];
312     else if (action == @selector(togglePerWindowWebProcessesDisabled:))
313         [menuItem setState:[self perWindowWebProcessesDisabled] ? NSControlStateValueOn : NSControlStateValueOff];
314     else if (action == @selector(toggleEnableSubPixelCSSOMMetrics:))
315         [menuItem setState:[self subPixelCSSOMMetricsEnabled] ? NSControlStateValueOn : NSControlStateValueOff];
316     else if (action == @selector(toggleDebugOverlay:))
317         [menuItem setState:[self debugOverlayVisible:menuItem] ? NSControlStateValueOn : NSControlStateValueOff];
318
319 #if WK_API_ENABLED
320     if (menuItem.tag == ExperimentalFeatureTag) {
321         _WKExperimentalFeature *feature = menuItem.representedObject;
322         [menuItem setState:[defaultPreferences() _isEnabledForExperimentalFeature:feature] ? NSControlStateValueOn : NSControlStateValueOff];
323     }
324     if (menuItem.tag == InternalDebugFeatureTag) {
325         _WKInternalDebugFeature *feature = menuItem.representedObject;
326         [menuItem setState:[defaultPreferences() _isEnabledForInternalDebugFeature:feature] ? NSControlStateValueOn : NSControlStateValueOff];
327     }
328 #endif
329
330     return YES;
331 }
332
333 - (void)_toggleBooleanDefault:(NSString *)defaultName
334 {
335     NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
336     [defaults setBool:![defaults boolForKey:defaultName] forKey:defaultName];
337
338     [(BrowserAppDelegate *)[[NSApplication sharedApplication] delegate] didChangeSettings];
339 }
340
341 - (void)toggleUseWebKit2ByDefault:(id)sender
342 {
343     [self _toggleBooleanDefault:UseWebKit2ByDefaultPreferenceKey];
344 }
345
346 - (BOOL)useWebKit2ByDefault
347 {
348     return [[NSUserDefaults standardUserDefaults] boolForKey:UseWebKit2ByDefaultPreferenceKey];
349 }
350
351 - (void)toggleCreateEditorByDefault:(id)sender
352 {
353     [self _toggleBooleanDefault:CreateEditorByDefaultPreferenceKey];
354 }
355
356 - (BOOL)createEditorByDefault
357 {
358     return [[NSUserDefaults standardUserDefaults] boolForKey:CreateEditorByDefaultPreferenceKey];
359 }
360
361 - (void)toggleUseTransparentWindows:(id)sender
362 {
363     [self _toggleBooleanDefault:UseTransparentWindowsPreferenceKey];
364 }
365
366 - (BOOL)useTransparentWindows
367 {
368     return [[NSUserDefaults standardUserDefaults] boolForKey:UseTransparentWindowsPreferenceKey];
369 }
370
371 - (void)toggleUsePaginatedMode:(id)sender
372 {
373     [self _toggleBooleanDefault:UsePaginatedModePreferenceKey];
374 }
375
376 - (BOOL)usePaginatedMode
377 {
378     return [[NSUserDefaults standardUserDefaults] boolForKey:UsePaginatedModePreferenceKey];
379 }
380
381 - (void)toggleUseUISideCompositing:(id)sender
382 {
383     [self _toggleBooleanDefault:UseRemoteLayerTreeDrawingAreaPreferenceKey];
384 }
385
386 - (BOOL)useUISideCompositing
387 {
388     return [[NSUserDefaults standardUserDefaults] boolForKey:UseRemoteLayerTreeDrawingAreaPreferenceKey];
389 }
390
391 - (void)togglePerWindowWebProcessesDisabled:(id)sender
392 {
393     NSAlert *alert = [[NSAlert alloc] init];
394     [alert setMessageText:self.perWindowWebProcessesDisabled ? @"Are you sure you want to switch to per-window web processes?" : @"Are you sure you want to switch to a single web process?"];
395     [alert setInformativeText:@"This requires quitting and relaunching MiniBrowser. I'll do the quitting. You will have to do the relaunching."];
396     [alert addButtonWithTitle:@"Switch and Quit"];
397     [alert addButtonWithTitle:@"Cancel"];
398
399     NSModalResponse response = [alert runModal];    
400     [alert release];
401
402     if (response != NSAlertFirstButtonReturn)
403         return;
404
405     [self _toggleBooleanDefault:PerWindowWebProcessesDisabledKey];
406     [NSApp terminate:self];
407 }
408
409 - (BOOL)perWindowWebProcessesDisabled
410 {
411     return [[NSUserDefaults standardUserDefaults] boolForKey:PerWindowWebProcessesDisabledKey];
412 }
413
414 - (void)toggleIncrementalRenderingSuppressed:(id)sender
415 {
416     [self _toggleBooleanDefault:IncrementalRenderingSuppressedPreferenceKey];
417 }
418
419 - (BOOL)incrementalRenderingSuppressed
420 {
421     return [[NSUserDefaults standardUserDefaults] boolForKey:IncrementalRenderingSuppressedPreferenceKey];
422 }
423
424 - (void)toggleShowLayerBorders:(id)sender
425 {
426     [self _toggleBooleanDefault:LayerBordersVisiblePreferenceKey];
427 }
428
429 - (BOOL)layerBordersVisible
430 {
431     return [[NSUserDefaults standardUserDefaults] boolForKey:LayerBordersVisiblePreferenceKey];
432 }
433
434 - (void)toggleSimpleLineLayoutEnabled:(id)sender
435 {
436     [self _toggleBooleanDefault:SimpleLineLayoutEnabledPreferenceKey];
437 }
438
439 - (BOOL)simpleLineLayoutEnabled
440 {
441     return [[NSUserDefaults standardUserDefaults] boolForKey:SimpleLineLayoutEnabledPreferenceKey];
442 }
443
444 - (void)toggleSimpleLineLayoutDebugBordersEnabled:(id)sender
445 {
446     [self _toggleBooleanDefault:SimpleLineLayoutDebugBordersEnabledPreferenceKey];
447 }
448
449 - (BOOL)simpleLineLayoutDebugBordersEnabled
450 {
451     return [[NSUserDefaults standardUserDefaults] boolForKey:SimpleLineLayoutDebugBordersEnabledPreferenceKey];
452 }
453
454 - (void)toggleAcceleratedDrawingEnabled:(id)sender
455 {
456     [self _toggleBooleanDefault:AcceleratedDrawingEnabledPreferenceKey];
457 }
458
459 - (BOOL)acceleratedDrawingEnabled
460 {
461     return [[NSUserDefaults standardUserDefaults] boolForKey:AcceleratedDrawingEnabledPreferenceKey];
462 }
463
464 - (void)toggleDisplayListDrawingEnabled:(id)sender
465 {
466     [self _toggleBooleanDefault:DisplayListDrawingEnabledPreferenceKey];
467 }
468
469 - (BOOL)displayListDrawingEnabled
470 {
471     return [[NSUserDefaults standardUserDefaults] boolForKey:DisplayListDrawingEnabledPreferenceKey];
472 }
473
474 - (void)toggleSubpixelAntialiasedLayerTextEnabled:(id)sender
475 {
476     [self _toggleBooleanDefault:SubpixelAntialiasedLayerTextEnabledPreferenceKey];
477 }
478
479 - (BOOL)subpixelAntialiasedLayerTextEnabled
480 {
481     return [[NSUserDefaults standardUserDefaults] boolForKey:SubpixelAntialiasedLayerTextEnabledPreferenceKey];
482 }
483
484 - (void)toggleReserveSpaceForBanners:(id)sender
485 {
486     [self _toggleBooleanDefault:ReserveSpaceForBannersPreferenceKey];
487 }
488
489 - (void)toggleShowTiledScrollingIndicator:(id)sender
490 {
491     [self _toggleBooleanDefault:TiledScrollingIndicatorVisiblePreferenceKey];
492 }
493
494 - (void)toggleShowResourceUsageOverlay:(id)sender
495 {
496     [self _toggleBooleanDefault:ResourceUsageOverlayVisiblePreferenceKey];
497 }
498
499 - (BOOL)loadsAllSiteIcons
500 {
501     return [[NSUserDefaults standardUserDefaults] boolForKey:LoadsAllSiteIconsKey];
502 }
503
504 - (void)toggleLoadsAllSiteIcons:(id)sender
505 {
506     [self _toggleBooleanDefault:LoadsAllSiteIconsKey];
507 }
508
509 - (BOOL)usesGameControllerFramework
510 {
511     return [[NSUserDefaults standardUserDefaults] boolForKey:UsesGameControllerFrameworkKey];
512 }
513
514 - (void)toggleUsesGameControllerFramework:(id)sender
515 {
516     [self _toggleBooleanDefault:UsesGameControllerFrameworkKey];
517 }
518
519 - (BOOL)networkCacheSpeculativeRevalidationDisabled
520 {
521     return [[NSUserDefaults standardUserDefaults] boolForKey:NetworkCacheSpeculativeRevalidationDisabledKey];
522 }
523
524 - (void)toggleNetworkCacheSpeculativeRevalidationDisabled:(id)sender
525 {
526     [self _toggleBooleanDefault:NetworkCacheSpeculativeRevalidationDisabledKey];
527 }
528
529 - (BOOL)processSwapOnWindowOpenWithOpenerEnabled
530 {
531     return [[NSUserDefaults standardUserDefaults] boolForKey:ProcessSwapOnWindowOpenWithOpenerKey];
532 }
533
534 - (void)toggleProcessSwapOnWindowOpenWithOpener:(id)sender
535 {
536     [self _toggleBooleanDefault:ProcessSwapOnWindowOpenWithOpenerKey];
537 }
538
539 - (BOOL)isSpaceReservedForBanners
540 {
541     return [[NSUserDefaults standardUserDefaults] boolForKey:ReserveSpaceForBannersPreferenceKey];
542 }
543
544 - (BOOL)tiledScrollingIndicatorVisible
545 {
546     return [[NSUserDefaults standardUserDefaults] boolForKey:TiledScrollingIndicatorVisiblePreferenceKey];
547 }
548
549 - (BOOL)resourceUsageOverlayVisible
550 {
551     return [[NSUserDefaults standardUserDefaults] boolForKey:ResourceUsageOverlayVisiblePreferenceKey];
552 }
553
554 - (void)toggleResourceLoadStatisticsEnabled:(id)sender
555 {
556     [self _toggleBooleanDefault:ResourceLoadStatisticsEnabledPreferenceKey];
557 }
558
559 - (BOOL)visualViewportEnabled
560 {
561     return [[NSUserDefaults standardUserDefaults] boolForKey:VisualViewportEnabledPreferenceKey];
562 }
563
564 - (void)toggleVisualViewportEnabled:(id)sender
565 {
566     [self _toggleBooleanDefault:VisualViewportEnabledPreferenceKey];
567 }
568
569 - (BOOL)resourceLoadStatisticsEnabled
570 {
571     return [[NSUserDefaults standardUserDefaults] boolForKey:ResourceLoadStatisticsEnabledPreferenceKey];
572 }
573
574 - (void)toggleLargeImageAsyncDecodingEnabled:(id)sender
575 {
576     [self _toggleBooleanDefault:LargeImageAsyncDecodingEnabledPreferenceKey];
577 }
578
579 - (BOOL)largeImageAsyncDecodingEnabled
580 {
581     return [[NSUserDefaults standardUserDefaults] boolForKey:LargeImageAsyncDecodingEnabledPreferenceKey];
582 }
583
584 - (void)toggleAnimatedImageAsyncDecodingEnabled:(id)sender
585 {
586     [self _toggleBooleanDefault:AnimatedImageAsyncDecodingEnabledPreferenceKey];
587 }
588
589 - (BOOL)animatedImageAsyncDecodingEnabled
590 {
591     return [[NSUserDefaults standardUserDefaults] boolForKey:AnimatedImageAsyncDecodingEnabledPreferenceKey];
592 }
593
594 - (void)toggleAppleColorFilterEnabled:(id)sender
595 {
596     [self _toggleBooleanDefault:AppleColorFilterEnabledPreferenceKey];
597 }
598
599 - (BOOL)appleColorFilterEnabled
600 {
601     return [[NSUserDefaults standardUserDefaults] boolForKey:AppleColorFilterEnabledPreferenceKey];
602 }
603
604 - (void)togglePunchOutWhiteBackgroundsInDarkMode:(id)sender
605 {
606     [self _toggleBooleanDefault:PunchOutWhiteBackgroundsInDarkModePreferenceKey];
607 }
608
609 - (BOOL)punchOutWhiteBackgroundsInDarkMode
610 {
611     return [[NSUserDefaults standardUserDefaults] boolForKey:PunchOutWhiteBackgroundsInDarkModePreferenceKey];
612 }
613
614 - (void)toggleUseSystemAppearance:(id)sender
615 {
616     [self _toggleBooleanDefault:UseSystemAppearancePreferenceKey];
617 }
618
619 - (BOOL)useSystemAppearance
620 {
621     return [[NSUserDefaults standardUserDefaults] boolForKey:UseSystemAppearancePreferenceKey];
622 }
623
624 - (void)toggleEnableSubPixelCSSOMMetrics:(id)sender
625 {
626     [self _toggleBooleanDefault:EnableSubPixelCSSOMMetricsPreferenceKey];
627 }
628
629 - (BOOL)subPixelCSSOMMetricsEnabled
630 {
631     return [[NSUserDefaults standardUserDefaults] boolForKey:EnableSubPixelCSSOMMetricsPreferenceKey];
632 }
633
634 - (BOOL)nonFastScrollableRegionOverlayVisible
635 {
636     return [[NSUserDefaults standardUserDefaults] boolForKey:NonFastScrollableRegionOverlayVisiblePreferenceKey];
637 }
638
639 - (BOOL)wheelEventHandlerRegionOverlayVisible
640 {
641     return [[NSUserDefaults standardUserDefaults] boolForKey:WheelEventHandlerRegionOverlayVisiblePreferenceKey];
642 }
643
644 - (NSString *)preferenceKeyForRegionOverlayTag:(NSUInteger)tag
645 {
646     switch (tag) {
647     case NonFastScrollableRegionOverlayTag:
648         return NonFastScrollableRegionOverlayVisiblePreferenceKey;
649
650     case WheelEventHandlerRegionOverlayTag:
651         return WheelEventHandlerRegionOverlayVisiblePreferenceKey;
652     }
653     return nil;
654 }
655
656 - (void)toggleDebugOverlay:(id)sender
657 {
658     NSString *preferenceKey = [self preferenceKeyForRegionOverlayTag:[sender tag]];
659     if (preferenceKey)
660         [self _toggleBooleanDefault:preferenceKey];
661 }
662
663 #if WK_API_ENABLED
664 - (void)toggleExperimentalFeature:(id)sender
665 {
666     _WKExperimentalFeature *feature = ((NSMenuItem *)sender).representedObject;
667     WKPreferences *preferences = defaultPreferences();
668
669     BOOL currentlyEnabled = [preferences _isEnabledForExperimentalFeature:feature];
670     [preferences _setEnabled:!currentlyEnabled forExperimentalFeature:feature];
671
672     [[NSUserDefaults standardUserDefaults] setBool:!currentlyEnabled forKey:feature.key];
673 }
674
675 - (void)toggleInternalDebugFeature:(id)sender
676 {
677     _WKInternalDebugFeature *feature = ((NSMenuItem *)sender).representedObject;
678     WKPreferences *preferences = defaultPreferences();
679
680     BOOL currentlyEnabled = [preferences _isEnabledForInternalDebugFeature:feature];
681     [preferences _setEnabled:!currentlyEnabled forInternalDebugFeature:feature];
682
683     [[NSUserDefaults standardUserDefaults] setBool:!currentlyEnabled forKey:feature.key];
684 }
685 #endif
686
687 - (BOOL)debugOverlayVisible:(NSMenuItem *)menuItem
688 {
689     NSString *preferenceKey = [self preferenceKeyForRegionOverlayTag:[menuItem tag]];
690     if (preferenceKey)
691         return [[NSUserDefaults standardUserDefaults] boolForKey:preferenceKey];
692
693     return NO;
694 }
695
696 - (NSString *)defaultURL
697 {
698     NSString *customDefaultURL = [[NSUserDefaults standardUserDefaults] stringForKey:DefaultURLPreferenceKey];
699     if (customDefaultURL)
700         return customDefaultURL;
701     return defaultURL;
702 }
703
704 - (void)setDefaultURLToCurrentURL:(id)sender
705 {
706     NSWindowController *windowController = [[NSApp keyWindow] windowController];
707     NSString *customDefaultURL = nil;
708
709     if ([windowController isKindOfClass:[BrowserWindowController class]])
710         customDefaultURL = [[(BrowserWindowController *)windowController currentURL] absoluteString];
711
712     if (customDefaultURL)
713         [[NSUserDefaults standardUserDefaults] setObject:customDefaultURL forKey:DefaultURLPreferenceKey];
714 }
715
716 @end