Override "Edited" in window title for NSDocument

How to prevent the window title from showing "Edited" for an infected NSDocument?

I manage saving and autosaving myself using a web service, and just don't want to get distracted in the title bar.

I tried overriding:

  • NSDocument -isDocumentEdited and -hasUnautosavedChanges always return NO .
  • -[NSWindowController setDocumentEdited] do nothing or always use NO regardless of the actual value of the parameter.
  • -[NSWindowController synchronizeWindowTitleWithDocumentName] do nothing.
  • -[NSWindow setDocumentEdited] do nothing or always use NO regardless of the actual value of the parameter.

In all cases, the title bar still changes to β€œEdited” when I make changes to the saved document.

If I override -[NSDocument updateChangeCount:] and -[NSDocument updateChangeCountWithToken:forSaveOperation:] to do nothing, I can prevent this, but it also affects saving, autosaving and other types of documents.

I also tried this:

 [[self.window standardWindowButton: NSWindowDocumentVersionsButton] setTitle:nil]; 

This displayed an empty line instead of Edited, but a dash appeared - one that usually separates the document name and Edited.

Any idea how to separate this part of the window from the document?

+6
source share
3 answers

Several parameters:

  • To get a pointer to a dash, find the TextField in [window.contentView.superview.subviews] with stringValue equal to "-". You can also set its text to an empty string.

     @implementation NSWindow (DashRetrivalMethod) - (NSTextField*)versionsDashTextField { NSTextField* res = nil; NSView* themeFrame = [self.contentView superview]; for (NSView* tmp in [themeFrame subviews]) { if ([tmp isKindOfClass:[NSTextField class]]) { if ([[(NSTextField*)tmp stringValue] isEqualToString:@"β€”"]) { res = (NSTextField*)tmp; break; } } } return res; } @end 
  • You can override NSWindow -setRepresentedURL :. This will also affect the NSWindowDocumentIconButton and the popup menu, but you can manually create it if you want: [NSWindow standardWindowButton: NSWindowDocumentIconButton].

  • Undo one of these three undocumented NSDocument methods:

     // Always return here NO if you don't want the version button to appear. // This seems to be the cleanest options, besides the fact that you are /// overriding a private method. - (BOOL)_shouldShowAutosaveButtonForWindow:(NSWindow*)window; // Call super with NO - (void)_setShowAutosaveButton:(BOOL)flag; // Here the button and the dash are actually created - (void)_endVersionsButtonUpdates; // Here Cocoa hide or unhide the edited button - (void)_updateDocumentEditedAndAnimate:(BOOL)flag 
+6
source

Have you tried to override NSDocuments - (BOOL)hasUnautosavedChanges in addition to overriding - (BOOL) isDocumentEdited ?

+1
source

Although this is a late answer, you can easily determine what will be the title of your NSDocument window by overriding

 - (NSString *)windowTitleForDocumentDisplayName:(NSString *)displayName 

in your NSWindowController and return the appropriate header.

You can do this as well by overriding the property of your NSDocument:

 - (NSString *)displayName 

but this is not recommended by Apple because it is commonly used by OS error handlers.

I added this answer because none of the other answers really set me on the right path.

0
source

Source: https://habr.com/ru/post/914284/


All Articles