双击标题栏时如何知道窗口是否可以最小化?

发布于 2024-11-09 09:01:50 字数 1489 浏览 0 评论 0原文

此图片来自“系统偏好设置”>外貌 在此处输入图像描述

我想知道如何以编程方式获取该值?

我问这个问题是因为我正在绘制一个带有自定义标题栏的窗口,并且我希望它(在行为上)尽可能类似于普通(非自定义)可可窗口。

也许我可以通过管道传输终端命令,或者是否有可可 API 可以执行此操作?

编辑:

回答(感谢 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];
        }
    }
}

后来我发现可以找到外观(水绿色/石墨):

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"];
}

这也很有帮助:)

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无力看清 2024-11-16 09:01:50

我这样做的方法可能是从用户默认值中读取值。

NSString * const MDAppleMiniaturizeOnDoubleClickKey = @"AppleMiniaturizeOnDoubleClick";

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

NSNumber *miniaturize = [userDefaults
                     objectForKey:MDAppleMiniaturizeOnDoubleClickKey];

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

(此首选项设置存储在 ~/Library/Preferences/ 文件夹中不可见的 .GlobalPreferences.plist 中)。

请注意,默认情况下,“双击最小化”选项是关闭的,因此如果您检查 AppleMiniaturizeOnDoubleClick 是否存在并且它返回 nil,则意味着它已关闭。 (用户默认值仅在与默认值不同时才开始存储值)。

该键在 Leopard 中与在 Snow Leopard 中是相同的。 (在《狮子》或《山狮》中也没有改变)。

当然,NSWindow 中有一个秘密(私有)方法,-(BOOL)_shouldMiniaturizeOnDoubleClick,但我不建议使用私有方法。

[更新] 关于 Catfish_Man 的评论:您是正确的,因为 [userDefaults addSuiteNamed:NSGlobalDomain]; 行是不必要的,因为 NSUserDefaults 已经能够读取全局首选项。 (我修改了上面的代码以反映这一点)。

“此外,NSGlobalDomain 未翻译为
该方法的.GlobalPreferences.plist。”

我不确定我是否明白你的意思。 NSUserDefaults 构建在 CFPreferences 之上,它定义了以下 6 个常量:

Application:

 kCFPreferencesAnyApplication,
 kCFPreferencesCurrentApplication

Host:

 kCFPreferencesAnyHost,
 kCFPreferencesCurrentHost

User:

 kCFPreferencesAnyUser,
 kCFPreferencesCurrentUser

给定一个虚构的应用程序包标识符“com.markdouma .App”和单个主机(基于您当前的网络位置,在此示例中不会更改),通常有 8 个位置可以在您的磁盘上存储首选项信息。(注意:显示的路径适用于仅用于演示;确切的文件路径位置可能会发生变化)。这 8 个不同的位置由 CFPreferences 常量的不同组合产生:

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

虽然 NSUserDefaults 只能写入斜体显示的域组合,但它会自动拥有对粗体显示的域组合的读取访问权限。换句话说,无需执行任何操作,我就可以自动运行以下代码并打印结果:

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"));

在我的系统上运行该代码会打印以下结果:

 AppleMiniaturizeOnDoubleClick == YES
 MultipleSessionEnabled == YES

这是有道理的,因为我同时具有快速用户切换和双击最小化选项已启用。 MultipleSessionsEnabled 存储在本地域中的 /Library/Preferences/.GlobalPreferences.plist 中,AppleMiniaturizeOnDoubleClick 存储在用户域中的 >~/Library/Preferences/.GlobalPreferences.plist

示例项目: NSUserDefaultsFinagler.zip

“此外,这很慢。请不要这样做。”

抱歉,但这没有任何意义(假设我们同意不再使用 addSuiteNamed:)。用户默认值由应用程序缓存,使得调用只需几毫秒。向用户询问代表本地应用程序值的键的值或代表全局值的键的默认值之间不会有什么明显的区别吗?

AFAIK,这是实现 OP 目标的唯一“合法”(应用程序商店兼容)方式。如果还有其他更有效的方法,请详细说明。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文