gpt4 book ai didi

cocoa - 隐藏停靠栏图标而不隐藏菜单栏

转载 作者:行者123 更新时间:2023-12-03 17:11:38 25 4
gpt4 key购买 nike

我使用 this thread 中的想法隐藏我的应用程序的停靠图标可选。如果最终显示了停靠栏图标,那么菜单栏也应该显示。仅适用于 Jiulong's answer我没能完成这项工作。菜单栏仍然隐藏。

所以基本上“应用程序是代理”在 InfoPList 中设置为“1”,并使用以下代码:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchAsAgentApp"]) {   
ProcessSerialNumber psn = { 0, kCurrentProcess };
TransformProcessType(&psn, kProcessTransformToForegroundApplication);
SetSystemUIMode(kUIModeNormal, 0);
[[NSWorkspace sharedWorkspace] launchAppWithBundleIdentifier:@"com.apple.dock" options:NSWorkspaceLaunchDefault additionalEventParamDescriptor:nil launchIdentifier:nil];
[[NSApplication sharedApplication] activateIgnoringOtherApps:TRUE];
}

那么为什么菜单栏不显示,直到我隐藏并重新聚焦应用程序?有什么解决办法吗?我看到'Quick Search Box' for mac app启动时也不显示菜单栏...

<小时/>

编辑:我联系了苹果,他们给了我碳和非碳解决方案。假设有一个新项目,在 Plist 文件中将“Application is Agent”设置为“YES”,则可以在 AppDelegate 类中使用此代码:

#define USE_CARBON  0

//
// Note: NSLogDebug is defined in the projects pre-compiled (.pch) file
//

@implementation AppDelegate
{
BOOL show_icon;
}

// Application will finish launching
- (void)applicationWillFinishLaunching:(NSNotification *)notification {
NSLogDebug();
NSURL *receiptUrl = [[NSBundle mainBundle] appStoreReceiptURL];
if (![[NSFileManager defaultManager] fileExistsAtPath:[receiptUrl path]]) {
// exit(173);
}

#if 1
show_icon = YES;
#else
NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults];
NSString *hasDockIconDefaultsKey = @"Has Dock Icon?";
// note: toggles value on each run (normally set from user pref pannel)
show_icon = [userDefaults boolForKey:hasDockIconDefaultsKey];
[userDefaults setBool:!show_icon forKey:hasDockIconDefaultsKey];
#endif // if 1
if (show_icon) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setPresentationOptions:NSApplicationPresentationDefault];
[NSMenu setMenuBarVisible:NO];
[NSMenu setMenuBarVisible:YES];
}

[NSApp activateIgnoringOtherApps:YES];
} // applicationWillFinishLaunching

// Application did finish launching
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
NSLogDebug();
// Insert code here to initialize your application
if (show_icon) {
#if USE_CARBON
ProcessSerialNumber psn = {0, kCurrentProcess};
OSStatus returnCode = TransformProcessType(&psn, kProcessTransformToForegroundApplication);
if (noErr != returnCode) {
NSLog(@"TransformProcessType error: %d (0x%0X)", returnCode, returnCode);
}

ProcessSerialNumber psnx = {0, kNoProcess};
GetNextProcess(&psnx);
SetFrontProcess(&psnx);
#else // if 0
NSWorkspace *sharedWorkspace = [NSWorkspace sharedWorkspace];
NSRunningApplication * menuBarOwningApplication = [sharedWorkspace menuBarOwningApplication];
(void) [menuBarOwningApplication activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif
[self performSelector:@selector(setFront) withObject:nil afterDelay:0.];
}
} // applicationDidFinishLaunching

// Close app when main window is closed
- (BOOL)applicationShouldTerminateAfterLastWindowClosed:(NSApplication *)sender {
return (YES);
}

- (void)setFront;
{
#if USE_CARBON
ProcessSerialNumber psn = {0, kCurrentProcess};
SetFrontProcess(&psn);
#else // if USE_CARBON
[[NSRunningApplication currentApplication] activateWithOptions:NSApplicationActivateIgnoringOtherApps];
#endif // USE_CARBON
}

@end

请注意,我也提交了错误报告。

这是非碳解决方案的 Swift 版本:

func applicationWillFinishLaunching(_ notification: Notification) {
if showIcon {
NSApp.setActivationPolicy(.regular)
NSApp.presentationOptions = []
NSMenu.setMenuBarVisible(false)
NSMenu.setMenuBarVisible(true)
}
NSApp.activate(ignoringOtherApps: true)
}

func applicationDidFinishLaunching(_ aNotification: Notification) {
NSApplication.shared.activate(ignoringOtherApps: true)
if showIcon {
let workspace = NSWorkspace.shared
let application = workspace.menuBarOwningApplication
application?.activate(options: .activateIgnoringOtherApps)
self.perform(#selector(activate), with: nil, afterDelay: 0.0)
}
}

@objc private func activate() {
NSRunningApplication.current.activate(options: .activateIgnoringOtherApps)
}

最佳答案

首先,您应该使用-[NSApplication setActivationPolicy:]而不是TransformProcessType()-[NSApplication setPresentationOptions:]而不是SetSystemUIMode()在现代代码中。如果切换到这些不足以解决菜单栏不更新的问题,我建议您尝试使用 -setPresentationOptions:+[NSMenu setMenuBarVisible:]隐藏菜单栏,然后​​立即反转该操作。

此外,请放弃尝试激活 Dock 的麻烦。

所以,类似:

if (![[NSUserDefaults standardUserDefaults] boolForKey:@"LaunchAsAgentApp"]) {
[NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
[NSApp setPresentationOptions:NSApplicationPresentationDefault]; // probably not necessary since it's the default
[NSMenu setMenuBarVisible:NO]; // these two lines may not be necessary, either; using -setActivationPolicy: instead of TransformProcessType() may be enough
[NSMenu setMenuBarVisible:YES];
}

关于cocoa - 隐藏停靠栏图标而不隐藏菜单栏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23313571/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com