How to know if window is minimizable when titlebar was double clicked?

梦想的初衷 提交于 2019-12-08 17:24:32

问题


This image is from SystemPreferences > Appearance

I want to know How do I get that value programmatically?

I ask because I am drawing a window with a customized titlebar and I want it to resemble (in behavior) as much as possible to normal (non-customized) cocoa windows.

Maybe a terminal command I can pipe or is there an cocoa API that does this?

EDIT:

Answer (thanks to NSGod)

- (void)mouseUp:(NSEvent *)event{

    if ([event clickCount] == 2) {
        //Get settings from "System Preferences" >  "Appearance" > "Double-click on windows title bar to minimize"
        NSString *const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick";
        NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
        // [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary
        BOOL shouldMiniaturize = [[userDefaults objectForKey:MDAppleMiniaturizeOnDoubleClickKey] boolValue];
        if (shouldMiniaturize) {
            [self miniaturize:self];
        }
    }
}

Later I found that Appearance (Aqua/Graphite) can be found:

NSString * const kAppleAquaColorVariant = @"AppleAquaColorVariant";
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary  
NSNumber *color = [userDefaults objectForKey:kAppleAquaColorVariant];
if ([color intValue] == 6) {//graphite is 6 
    imageName = [imageName stringByAppendingFormat:@"_graphite"];
}else{//defaults to aqua, (aqua is 1)
    imageName = [imageName stringByAppendingFormat:@"_colorsryg"];
}

Which can be helpful too :)


回答1:


The way I would do it is probably read the value in from user defaults.

NSString * const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick";

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
// [userDefaults addSuiteNamed:NSGlobalDomain]; // unnecessary

NSNumber *miniaturize = [userDefaults
                     objectForKey:MDAppleMiniaturizeOnDoubleClickKey];

NSLog(@"AppleMiniaturizeOnDoubleClick == %@",
                ([miniaturize boolValue] ? @"YES" : @"NO"));

(This preference setting is stored in the invisible .GlobalPreferences.plist in your ~/Library/Preferences/ folder).

Note that by default, the "double-click to minimize" option is turned off, so if you check for the presence of the AppleMiniaturizeOnDoubleClick and it returns nil, that means it is off. (User defaults only start to store values if they differ from the defaults).

This key is the same in Leopard as it is in Snow Leopard. (It hasn't changed in Lion or Mountain Lion either).

Of course, there is a secret (private) method in NSWindow, -(BOOL)_shouldMiniaturizeOnDoubleClick, but I wouldn't recommend using private methods.

[UPDATE] Regarding Catfish_Man's comment: you are correct in that the line [userDefaults addSuiteNamed:NSGlobalDomain]; is unnecessary, as NSUserDefaults already has the ability to read global preferences. (I modified the code above to reflect this).

"Additionally, NSGlobalDomain is not translated to .GlobalPreferences.plist for that method."

I'm not sure I follow you there. NSUserDefaults is built on top of CFPreferences which defines the following 6 constants:

Application:

 kCFPreferencesAnyApplication,
 kCFPreferencesCurrentApplication

Host:

 kCFPreferencesAnyHost,
 kCFPreferencesCurrentHost

User:

 kCFPreferencesAnyUser,
 kCFPreferencesCurrentUser

Given a fictional application bundle identifier of "com.markdouma.App" and a single host (based on your current network location that won't change for this example), there are generally 8 locations where preference information could be stored on your disk. (NOTE: The paths shown are for demonstration purposes only; the exact file path locations are subject to change). The 8 different locations arise from the different combination of the CFPreferences constants:

/Library/Preferences/.GlobalPreferences.plist (kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost)

/Library/Preferences/com.markdouma.App.plist
(kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesAnyHost)

/Library/Preferences/ByHost/.GlobalPreferences.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesAnyApplication, kCFPreferencesAnyUser, kCFPreferencesCurrentHost)

/Library/Preferences/ByHost/com.markdouma.App.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesCurrentApplication, kCFPreferencesAnyUser, kCFPreferencesCurrentHost)

~/Library/Preferences/.GlobalPreferences.plist (kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)

~/Library/Preferences/com.markdouma.App.plist (kCFPreferencesCurrentApplication, kCFPreferencesCurrentUser, kCFPreferencesAnyHost)

~/Library/Preferences/ByHost/.GlobalPreferences.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesAnyApplication, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)

~/Library/Preferences/ByHost/com.markdouma.App.UNIQUE_HOST_IDENTIFIER.plist (kCFPreferencesCurrentApplication, kCFPreferencesCurrentUser, kCFPreferencesCurrentHost)

While NSUserDefaults can only write to the domain combination shown in italics, it automatically has read access to the domain combinations shown in bold. In other words, without having to do anything, I can automatically run the following code and print the results:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];

NSNumber *miniaturize = [userDefaults
                 objectForKey:@"AppleMiniaturizeOnDoubleClick"];

NSNumber *fastUserSwitching = [userDefaults
                 objectForKey:@"MultipleSessionEnabled"];

NSLog(@"AppleMiniaturizeOnDoubleClick == %@",
            ([miniaturize boolValue] ? @"YES" : @"NO"));

NSLog(@"MultipleSessionEnabled == %@",
            ([fastUserSwitching boolValue] ? @"YES" : @"NO"));

Running that code on my system prints the following results:

 AppleMiniaturizeOnDoubleClick == YES
 MultipleSessionEnabled == YES

This makes sense, since I have both Fast User Switching and Double-click to minimize options enabled. MultipleSessionsEnabled is stored in the local domain at /Library/Preferences/.GlobalPreferences.plist, and AppleMiniaturizeOnDoubleClick is stored in the user domain at ~/Library/Preferences/.GlobalPreferences.plist.

Sample project: NSUserDefaultsFinagler.zip

"Additionally additionally, that's slow. Please don't do this."

Sorry, but that makes no sense (assuming that we've agreed that we're no longer using addSuiteNamed:). User defaults are cached by the application, making calls take in the matter of milliseconds. Wouldn't there be little noticeable difference between asking user defaults for the value for a key that represents a local application value or one that represents a global value?

AFAIK, this is the only "legal" (App-store-compatible) way to achieve the OP's goal. If there's another more efficient means, then please elaborate.



来源:https://stackoverflow.com/questions/6099338/how-to-know-if-window-is-minimizable-when-titlebar-was-double-clicked

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!