Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

This image is from SystemPreferences > Appearance enter image description here

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 :)

like image 750
nacho4d Avatar asked May 23 '11 15:05

nacho4d


People also ask

What happens when you double-click on the title bar?

Extra features and functions of a title bar Press and hold the mouse button on the title bar to move the window. Double-click the title bar to maximize the window or set the window into window mode.

What displays in the window title bar?

The title bar at the top of a window displays an application-defined icon and line of text. The text specifies the name of the application and indicates the purpose of the window. The title bar also makes it possible for the user to move the window using a mouse or other pointing device.


1 Answers

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.

like image 175
NSGod Avatar answered Sep 29 '22 07:09

NSGod