Construct and manage a graphical, event-driven user interface for your macOS app using AppKit.

AppKit Documentation

Posts under AppKit tag

228 Posts
Sort by:
Post not yet marked as solved
1 Replies
74 Views
Hello. I'm trying to change my SwiftUI Mac app icon's menu in the Dock, using the NSApplicationDelegate's applicationDockMenu(_ : ) function. However, it does not work: The delegate function is only called once, randomly after launch, and then never again, and it will not show any items in that menu I return. When I right-click the app in the Dock, only the macOS-supplied items are shown, and my App delegate function is not called. Here's the code I use inside my NSApplicationDelegate: func applicationDockMenu(_ sender: NSApplication) -> NSMenu? { let men = NSMenu() print("applicationDockMenu called") var it = NSMenuItem(title: "Test1", action: #selector(test(_:)), keyEquivalent: "") it.target = self men.addItem(it) it = NSMenuItem(title: "Test2", action: #selector(test(_:)), keyEquivalent: "") it.target = self men.addItem(it) return men } @objc func test(_ sender: NSMenuItem) { print("application dock menu custom item called") } Is there a SwiftUI modifier I should be using instead of the NSApp delegate method, or is this just not supported at this time in SwiftUI Mac apps? Thank you, - Matthias
Posted Last updated
.
Post not yet marked as solved
0 Replies
90 Views
After popping up a NSMenu a few times, NSResponder's mouseExited event is called even when the cursor is inside the tracking area, returning the main app window's max X value, which makes the mouseEntered/Exited methods unusable for tracking whether or not cursor is currently inside the view's frame. Steps Create a NSMenuItem with a custom view inside Add a tracking area to the views frame Add a background color that changes on mouseEntered/Exited Open and close NSMenu a few times until the color starts flickering when moving the cursor inside the view's frame If possible, print out NSEvent's locationInWindow on both mouseEntered and Exited and see that the location for exited method is not being reported correctly, while the flickering from the bg color being set is happening. Expected Result mouseExited should only be called the cursor leaves the tracking area. locationInWindow values should not exceed the NSPopUpWindow's frame. Tested on macOS Sonoma 14.3.1 and 14.4.1; Xcode 15.2; Feedback ID https://feedbackassistant.apple.com/feedback/13698735
Posted Last updated
.
Post marked as solved
2 Replies
128 Views
Using NSMenuItem's delegate method menu(_ menu: NSMenu, willHighlight item: NSMenuItem?) should return a nil item when the mouse location is moved out of the NSMenu window or if any of the items should not be highlighted. As per Apple Developer website: Only one item per menu can be highlighted at a time. If item is nil, it means that all items in the menu are about to be unhighlighted. But, on macOS Sonoma, when moving the mouse over a .sectionHeader and/or a Separator item, willHighlight won't be called, which makes it not return any item (neither nil, nor the section header). This behavior makes it impossible to use willHighlight method to determine whether custom views should be highlighted or not. Is this a bug or expected behavior?
Posted Last updated
.
Post not yet marked as solved
0 Replies
84 Views
On a submenu (NSMenu), items that use custom views as part of their layout, do not call NSMenu's delegate willHighlight method. The method is called correctly when placing the same items with custom views inside the root/parent NSMenu. Steps Create a NSMenu with at least two NSMenuItem; One with a custom view and another without it. Add a submenu to the item without a custom view; make sure to set the submenu's delegate Add multiple items to the submenu; Make sure some of the items are using custom views Using one of NSMenu's delegate method, willHighlight, check if it will return an item when hovering over the custom view items inside the submenu Notice it will only return when hovering over items that do not use a custom view. Expected Result NSMenu's willHighlight method should be called and inform with item with custom view should be highlighted inside the submenu. Tests done on macOS 14.3.1 and 14.4.1. Xcode 15.2 and 15.3.
Posted Last updated
.
Post not yet marked as solved
1 Replies
111 Views
Hello, When running an iPad app on Apple Silicon (M1) the expirationDate OptionKey of the UIPasteboard has no impact, i.e. the copied value is never removed from the clipboard. The following code works perfectly on iOS, however, behaves how I described above on macOS. let currentDate = Date() let expireDate = currentDate.addingTimeInterval(TimeInterval(90)) UIPasteboard.general.setItems([[UIPasteboard.typeAutomatic: myString]], options: [UIPasteboard.OptionsKey.expirationDate: expireDate]) Is this a bug? I am on Swift 5.10. Thank you!
Posted
by bmmcc_13.
Last updated
.
Post not yet marked as solved
1 Replies
61 Views
Hi, I think the title says it: my application needs to obtain a list of all applications that are configured as potential editors of a certain file type, for example jpeg or tiff. I've found LSCopyAllRoleHandlersForContentType which appears to do what I need, but it is deprecated since macos 12.0. What's the modern alternative? My app is built in c++ with some objective-c. Thanks Joost
Posted
by joostn.
Last updated
.
Post not yet marked as solved
0 Replies
74 Views
Specs: macOS Sonoma 14.3.1 Using NSMenu's submenus with a list of NSMenuItems (with or without custom views) will produce a hang for the first time they are presented. The larger the list, the more time the UI will be frozen before presenting the submenu. Is there something that can be done to mitigate this behavior?
Posted Last updated
.
Post not yet marked as solved
0 Replies
81 Views
-1 I am trying to write a MacOS app which switch input methods by previously assigned shortcut(command+space in here). Switching input methods preoperly works so that the language icon at the status bar(top right) immediately changes as I put the shortcut. The problem I got in here is that the actual input method does not change. For example, if I run my app when the selected input method is Korean, then although the status bar is showing the selected input method is Japanese after command+space, what I can only type is Korean characters. However, after I change focus to another text app(e.g. from sublime text to xcode), only then the selected input method is reflected well. I am using MacOS Monterey 12.6 and Xcode 13.1. My project contains two source files. The code in the file AppDelegate.swift is as follows: import Cocoa @NSApplicationMain class AppDelegate: NSObject, NSApplicationDelegate { var switcher = Switcher() } And the code in the file Switcher.swift is as follows: import Cocoa import MASShortcut class Switcher{ var lang: Int = 0 var kr: TISInputSource? var jp: TISInputSource? var en: TISInputSource? init(){ let inputSourceNSArray = TISCreateInputSourceList(nil, false).takeRetainedValue() as NSArray let inputSourceList = inputSourceNSArray as! [TISInputSource] for inputSource in inputSourceList { if inputSource.id == "com.apple.inputmethod.Korean.2SetKorean" { self.kr = inputSource } if inputSource.id == "com.apple.inputmethod.Kotoeri.RomajiTyping.Japanese" { self.jp = inputSource } if inputSource.id == "com.apple.keylayout.ABC" { self.en = inputSource } } self.register() } func switchLang(){ self.lang = (self.lang + 1) % 3 switch lang { case 0: TISSelectInputSource(self.kr) case 1: TISSelectInputSource(self.jp) case 2: TISSelectInputSource(self.en) default: print("error") } } func register() { let langShortcut = MASShortcut(keyCode: kVK_Space, modifierFlags: [.command]) MASShortcutMonitor.shared()?.register(langShortcut, withAction: { self.switchLang() }) } } I wrote these codes by referring KAWA, but KAWA does not make this issue. I have analyzed all codes of KAWA several times, I couldn't find out why the same problem does not occur in KAWA. I am quite new to Swift, and I have no idea to approach. Could you help me....? Thank you.
Posted Last updated
.
Post not yet marked as solved
1 Replies
105 Views
I have a document class that makes a main window for showing the data. Pieces of that data can be opened in separate subwindows for editing. When the user closes the main window and the document is dirty and the subwindows are dirty, I would like to present UI that asks the user if they want to save the changes in the subwindows (and possibly the main window if I decide to turn off autoSavesInPlace for the document). I've tried a number of possible methods of doing this, but always run into some roadblock: -Overriding shouldCloseWindowController:delegate:shouldCloseSelector:contextInfo: where I would go through the open subwindows and asking and tell them to do their own UI for asking if they should be saved. This is no good because everything returns back to the run loop and the doc would close, leaving the subwindows open with their Save? sheets up. -Making the subwindows inherit from NSEditor and registering them with the document. This looked like it would work, but it caused an infinite loop in my override of commitEditingWithDelegate:didCommitSelector:contextInfo: that I don't understand. I'm probably calling the didCommitSelector wrong because the docs aren't clear and provide no example. -Adding each subwindow's NSWindowController to the document's windowControllers list. I don't recall the problems this caused. Any sage advice about this? Possible examples? The Document is Swift, the subwindows are Cocoa, so examples in either language is fine.
Posted
by sjmills.
Last updated
.
Post not yet marked as solved
0 Replies
81 Views
I have a fairly robust MacOS application that has an NSScrollView that contains a canvas with various subviews (including web views and text views that contain scroll views), and a couple of peer views that track items in the scroll view (eg: screen space controls). Some of these views interrupt two finger scrolling. Every scroll view, and one of the peer views (essentially a stack view with buttons in it). I have written an additional bare bones application which does roughly the same thing, and my bare bones application works perfectly: Start two-finger dragging, scroll any of these other things under the cursor, I can continue to drag (and start dragging in any of those, and they drag without interfering with the parent scroll view). I have tried everything to recreate the interruption, including drag gestures attached to these various ancillary views, and I cannot figure out why dragging some of these views under the cursor interrupts two finger drag in our application, but not in my testbed. Does anyone have suggestions for how to debug this? I can see that there is a gesture recognizer in the NSScrollView hierarchy, but I don't see it in any of my gesture recognizer handling. I have breakpoints on every variation of hit testing and mouse motion, and none of them are getting hit in unexpected ways. I'm at my wit's end. Thanks.
Posted
by Dan Lyke.
Last updated
.
Post not yet marked as solved
2 Replies
435 Views
as I open the pop-up menu and move the mouse before that opened, MouseEntered Event and MouseExited Event are called when mouse moved. The following trackingAreas options are inclued in the view in pop-up area. NSTrackingInVisibleRect, NSTrackingMouseEnteredAndExited, NSTrackingMouseMoved, NSTrackingActiveInKeyWindow LocationInWindow of MouseExitedEvent seem to be incorrect. This problems does not occur in the following cases. Do not move the mouse until the popup is fully opened. Left mouse button down on pop-up area. Move the mouse out of the pop-up area. This issue occurs in Sonoma(MacOS14.0) and later. I would like to know if this is a code issue or a bug in the OS Version. AppDelegate.h #import <Cocoa/Cocoa.h> @interface ViewInPopup : NSView { NSString* resultStr; NSUInteger enteredCount; NSPoint lastEnteredPos; NSUInteger exitedCount; NSPoint lastExitedPos; NSUInteger movedCount; NSPoint lastMovedPos; NSTrackingArea* trackingArea; } @end @interface AppDelegate : NSObject <NSApplicationDelegate> { NSMenu* myMenu; ViewInPopup* viewInPopup; } - (IBAction)onClickButton:(id)sender; @end AppDelegate.mm #import "AppDelegate.h" @interface ViewInPopup () - (void)showResult:(NSEvent*)event; @end @implementation ViewInPopup - (id)initWithFrame:(NSRect)frameRect { self = [super initWithFrame:frameRect]; [self setWantsLayer:TRUE]; [[self layer] setBackgroundColor:[NSColor redColor].CGColor]; return self; } - (void)drawRect:(NSRect)dirtyRect { [super drawRect:dirtyRect]; [resultStr drawInRect:[self bounds] withAttributes:nil]; } - (void)updateTrackingAreas { if (trackingArea) { [self removeTrackingArea:trackingArea]; } NSTrackingAreaOptions options = NSTrackingInVisibleRect | NSTrackingMouseEnteredAndExited | NSTrackingMouseMoved | NSTrackingActiveInKeyWindow; trackingArea = [[NSTrackingArea alloc] initWithRect:[self bounds] options:options owner:self userInfo:nil]; [self addTrackingArea:trackingArea]; [super updateTrackingAreas]; } - (void)mouseEntered:(NSEvent *)event { [self showResult:event]; [super mouseEntered:event]; } - (void)mouseExited:(NSEvent *)event { [self showResult:event]; [super mouseExited:event]; } - (void)mouseMoved:(NSEvent *)event { [self showResult:event]; [super mouseMoved:event]; } - (void)showResult:(NSEvent*)event { NSString* eventTypeStr = @""; switch (event.type) { case NSEventTypeMouseEntered: eventTypeStr = @"Entered"; [[self layer] setBackgroundColor:[NSColor redColor].CGColor]; if (enteredCount >= NSUIntegerMax) { enteredCount = 0; } else { enteredCount++; } lastEnteredPos = event.locationInWindow; break; case NSEventTypeMouseExited: eventTypeStr = @"Exited"; [[self layer] setBackgroundColor:[NSColor blueColor].CGColor]; if (exitedCount >= NSUIntegerMax) { exitedCount = 0; } else { exitedCount++; } lastExitedPos = event.locationInWindow; break; case NSEventTypeMouseMoved: eventTypeStr = @"Moved"; [[self layer] setBackgroundColor:[NSColor greenColor].CGColor]; if (movedCount >= NSUIntegerMax) { movedCount = 0; } else { movedCount++; } lastMovedPos = event.locationInWindow; break; default: return; } resultStr = [NSString stringWithFormat:@"LastEventType:%@\n\nEnteredCount:%ld\nLastEnteredPosition:(%f, %f)\n\nExitedCount:%ld\nLastExitedPosition:(%f %f)\n\nMovedCount:%ld\nLastMovedPosition:(%f, %f)", eventTypeStr, enteredCount, lastEnteredPos.x, lastEnteredPos.y, exitedCount, lastExitedPos.x, lastExitedPos.y, movedCount, lastMovedPos.x, lastMovedPos.y]; [self setNeedsDisplay:YES]; } @end @interface AppDelegate () @property (strong) IBOutlet NSWindow *window; @end @implementation AppDelegate - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { // Insert code here to initialize your application myMenu = [[NSMenu alloc] init]; NSMenuItem* item = [[NSMenuItem alloc] init]; [myMenu addItem:item]; viewInPopup = [[ViewInPopup alloc] initWithFrame:NSMakeRect(0, 0, 300, 300)]; [item setView:viewInPopup]; } - (void)applicationWillTerminate:(NSNotification *)aNotification { // Insert code here to tear down your application } - (BOOL)applicationSupportsSecureRestorableState:(NSApplication *)app { return YES; } - (IBAction)onClickButton:(id)sender { [myMenu popUpMenuPositioningItem:nil atLocation:NSZeroPoint inView:(NSView*)sender]; } @end
Posted
by nwj.
Last updated
.
Post not yet marked as solved
0 Replies
88 Views
Does "natural scrolling" system preference affect NSEvent.scrollingDeltaY? BTW, I find that "natural scrolling" on my Sonoma (Mac mini m1) stops working; turning on/off has no effect when I scroll in Finder app.
Posted
by imneo.
Last updated
.
Post not yet marked as solved
1 Replies
105 Views
I have a tableview with a column which has an inner tableview. I want to change height of the outer tableview to match the height of the inner tableview. outerTableView.reloadData() let range = outerTableView.rows(in: summaryTableView.superview!.visibleRect) outerTableView.noteHeightOfRows(withIndexesChanged: IndexSet(integersIn: range.lowerBound..<range.upperBound)) The above code will cause an exception: WARNING: NSTableView detected a rowView was requested from inside of the -heightOfRow delegate method. That is not supported! ( 0 CoreFoundation 0x000000019093eccc __exceptionPreprocess + 176 1 libobjc.A.dylib 0x0000000190426788 objc_exception_throw + 60 2 AppKit 0x000000019420be98 -[NSTableRowData _availableRowViewWhileUpdatingAtRow:] + 0 3 AppKit 0x000000019425a470 -[NSTableView viewAtColumn:row:makeIfNecessary:] + 32
Posted
by imneo.
Last updated
.
Post not yet marked as solved
1 Replies
178 Views
I have an uncommon scenario here. outer tableview +--------------------------+ | column 1| inner tableview| +--------------------------+ Now most often the out tableview has many rows and vertical scrollbar visible. When user try to scroll vertically in the inner tableview but it has no vertical scrollbar (because it has only a few items), I want the scroll event sink into its parent view or better outer tableview, so that user does not have to move cursor to first column in outer tableview and scrolls. Is this possible?
Posted
by imneo.
Last updated
.
Post not yet marked as solved
0 Replies
99 Views
To me, the documentation for this method is not at all clear and needs to provide sample code. I tried searching the dev site for sample projects, but they're no longer where they lived for decades before. And where's the TextEdit sample project?! I need to override shouldCloseWindowController so I can make sure some sub-windows can be closed before closing the document's one and only window. I've done that, but then have no idea what I'm supposed to do to all the document to close. I tried this, which is what I think the NSDocument version header says to do: if ((self.windowControllers.first?.shouldCloseDocument) != nil){ self.canClose(withDelegate:delegate as Any, shouldClose:shouldCloseSelector, contextInfo:contextInfo); } But the document never gets the close() method called - only the window goes away.
Posted
by sjmills.
Last updated
.
Post not yet marked as solved
3 Replies
744 Views
Sonoma beta release notes mention that NSMenu was rewritten from scratch using AppKit, however, it seems like a lot of behavior was removed along the way which breaks applications. I've filed several requests using Feedback Assistant, but none of them were fixed in the 3 following betas. FB12867496: NSMenu no longer receives keyboard events from GetEventDispatcherTarget (there is a workaround) FB12867573: NSMenuItem custom view window is nil FB12887219 : NSMenu performSelector highlightItem doesn't highlight menu item FB12938907: NSMenu not properly updated when adding/removing NSMenuItem I wonder if anyone else has experienced similar problems and can share workarounds for them:
Posted
by p0deje.
Last updated
.
Post not yet marked as solved
4 Replies
956 Views
Filed as rdar://FB11975037 When macOS Ventura is run as a guest OS within the virtualization framework, the main menu bar items will not be displayed correctly if VZMacGraphicsDisplayConfiguration defines a large resolution. The menu bar titles appear to be using the same color as the menu bar itself. When the Appearance is set to Light, the menu bar items are effectively invisible. When the Appearance is set to Dark, the menu bar items are drawn in what looks like a disabled state. This only affects the menu bar item titles on the left-hand side. The date-time and menu bar icons on the right side are always displayed in the correct color. This appears to be a regression in macOS Ventura as this issue is not present in macOS 12 running as a guest. This bug can be easily reproduced using Apple's own Virtualization sample code titled: "Running macOS in a Virtual Machine on Apple Silicon Macs" Steps to reproduce: Follow the sample code instructions for building and installing a VM.bundle. Before running 'macOSVirtualMachineSampleApp', change the VZMacGraphicsDisplayConfiguration to use: width = 5120, height = 2880, ppi = 144. Run 'macOSVirtualMachineSampleApp' and notice that the menu bar titles on the left side of the screen are not correctly drawn in the guest instance. This has been tested on: Host: macOS 13.1 Guest: macOS 13.x (All versions) Hardware: MBP 14" M1 Pro 32GB/2TB Is there anything that can be done to resolve this issue?
Posted
by kennyc.
Last updated
.