From 9ca96bc909ce752f0465f81e9feae4441deb56b8 Mon Sep 17 00:00:00 2001 From: Ross Boucher Date: Sat, 31 Oct 2009 09:52:26 -0700 Subject: [PATCH] Add desktop support for recent file list --- AppKit/CPApplication.j | 4 +- AppKit/CPDocumentController.j | 94 +++++++++++++++++- AppKit/CPMenu.j | 16 +++ .../CPWindowStandardCloseButtonUnsaved.png | Bin 720 -> 757 bytes ...wStandardCloseButtonUnsavedHighlighted.png | Bin 716 -> 753 bytes 5 files changed, 111 insertions(+), 3 deletions(-) diff --git a/AppKit/CPApplication.j b/AppKit/CPApplication.j index c5033ed53..386c3d551 100644 --- a/AppKit/CPApplication.j +++ b/AppKit/CPApplication.j @@ -288,7 +288,9 @@ CPRunContinuesResponse = -1002; if (needsUntitled) [_documentController newDocument:self]; - + + [_documentController _updateRecentDocumentsMenu]; + [defaultCenter postNotificationName:CPApplicationDidFinishLaunchingNotification object:self]; diff --git a/AppKit/CPDocumentController.j b/AppKit/CPDocumentController.j index c392d172e..9431155fe 100644 --- a/AppKit/CPDocumentController.j +++ b/AppKit/CPDocumentController.j @@ -114,7 +114,7 @@ var CPSharedDocumentController = nil; [theDocument makeWindowControllers]; [theDocument showWindows]; } - + return theDocument; } @@ -145,10 +145,13 @@ var CPSharedDocumentController = nil; var type = [self typeForContentsOfURL:anAbsoluteURL error:anError]; result = [self makeDocumentWithContentsOfURL:anAbsoluteURL ofType:type delegate:self didReadSelector:@selector(document:didRead:contextInfo:) contextInfo:[CPDictionary dictionaryWithObject:shouldDisplay forKey:@"shouldDisplay"]]; + + if (result) + [self noteNewRecentDocument:result]; } else if (shouldDisplay) [result showWindows]; - + return result; } @@ -358,3 +361,90 @@ var CPSharedDocumentController = nil; } @end + +@implementation CPDocumentController (Recents) + +- (CPArray)recentDocumentURLs +{ + // FIXME move this to CP land + if (typeof window["cpRecentDocumentURLs"] === 'function') + return window.cpRecentDocumentURLs(); + + return []; +} + +- (void)clearRecentDocuments:(id)sender +{ + if (typeof window["cpClearRecentDocuments"] === 'function') + window.cpClearRecentDocuments(); + + [self _updateRecentDocumentsMenu]; +} + +- (void)noteNewRecentDocument:(CPDocument)aDocument +{ + [self noteNewRecentDocumentURL:[[aDocument fileURL] absoluteString]]; +} + +- (void)noteNewRecentDocumentURL:(CPString)aURL +{ + if (typeof window["cpNoteNewRecentDocumentURL"] === 'function') + window.cpNoteNewRecentDocumentURL(aURL); + + [self _updateRecentDocumentsMenu]; +} + +- (void)_removeAllRecentDocumentsFromMenu:(CPMenu)aMenu +{ + var items = [aMenu itemArray], + count = [items count]; + + while (count--) + { + var item = items[count]; + + if ([item action] === @selector(_openRecentDocument:)) + [aMenu removeItemAtIndex:count]; + } +} + +- (void)_updateRecentDocumentsMenu +{ + var menu = [[CPApp mainMenu] _menuWithName:@"_CPRecentDocumentsMenu"], + recentDocuments = [self recentDocumentURLs], + menuItems = [menu itemArray], + documentCount = [recentDocuments count], + menuItemCount = [menuItems count]; + + [self _removeAllRecentDocumentsFromMenu:menu]; + + if (menuItemCount) + { + if (!documentCount) + { + if ([menuItems[0] isSeparatorItem]) + [menu removeItemAtIndex:0]; + } + else + { + if (![menuItems[0] isSeparatorItem]) + [menu insertItem:[CPMenuItem separatorItem] atIndex:0]; + } + } + + while (documentCount--) + { + var path = recentDocuments[documentCount], + item = [[CPMenuItem alloc] initWithTitle:[path lastPathComponent] action:@selector(_openRecentDocument:) keyEquivalent:nil]; + + [item setTag:path]; + [menu insertItem:item atIndex:0]; + } +} + +- (void)_openRecentDocument:(id)sender +{ + [self openDocumentWithContentsOfURL:[sender tag] display:YES error:nil]; +} + +@end diff --git a/AppKit/CPMenu.j b/AppKit/CPMenu.j index ad0e2dad7..45845b168 100644 --- a/AppKit/CPMenu.j +++ b/AppKit/CPMenu.j @@ -865,6 +865,22 @@ var _CPMenuBarVisible = NO, } } +- (void)_menuWithName:(CPString)aName +{ + if (aName === _name) + return self; + + for (var i = 0, count = [_items count]; i < count; i++) + { + var menu = [[_items[i] submenu] _menuWithName:aName]; + + if (menu) + return menu; + } + + return nil; +} + @end diff --git a/AppKit/Resources/CPWindow/Standard/CPWindowStandardCloseButtonUnsaved.png b/AppKit/Resources/CPWindow/Standard/CPWindowStandardCloseButtonUnsaved.png index 2cb0e49295e36af48f14368fef07f3e21ab00452..9bd3566b10cd4a98535a069b570b4b87501c067e 100644 GIT binary patch delta 696 zcmV;p0!RJO1@#4xNq?6~L_t(|+H_J~OH@%5-RIu9U*kB#I1>(PCJg07363KYlMw!Z z{(#YIPl59+A}At0_@ML)eDMb)^kff#VT?8oVx(C{mSciNXzI+p-h0~X;D{6*Sln~> zS!?fg_C8{4Y>et_baYgdhlkrYq#X9D)d#-k-9%hBjBUfVUw_>dVzPU3a(8@uT-G(( z6>(@`WaM$VT3tQ={ria?1f8a?YXJ6$;S3ybFB1qMAU2*)OcV}}kLQ@2G)M{$B0f-Ri8)6P_36a1$IjmT5{O z6jIf3N7($|0>}4#@=2L;lpiM^KT~((uA<83^722zkAEvG>Kp(!+|+c2I2@eDTUx#y z?d)VA4zZKZ#UgpGOX=QT?xCb@{{OzQL9uZ7ayn7gu=WfIes<<^FN&4QEuNKz1dZIx zz13Cvw6vtEJB~w*k%&r;aZ1|`pn(=wIBe|bSmbK!0sp`vCK8LqD49$uM59p^!x=c> zLIbUONPoO(Wjs76m!E!FTg&(-C+dY>UAT(g&=Bx6wo=*bGY%~<_Vfr_%jffG$YCUv z+P~J;M(3K=%vLS8S{0HLIf+CNPp5x%_V+Kw(&;(ozGPIUr>A`}H8rK|6$%CQs2Fz` zeT>_T<_iM{jQ5Np;~nG2%*>2e@n=@`Owf1h)0YM7s(nf5}d#xgy=kT eB{b0f5nuor*%G{tZ>i7#0000{5fJt3T_4*)Eq^SA;?WQ6}YMY zpxrFm=`sieUAb`Ktl9}e`xa41l;AePjrl7_u|+b&Oa(zRI_k4?E2&(MP$;Z)jxm)= z>10!-Qh7KYk6&F~UENm{#cMK|6r<6oVA~6^+h=EIk05@W&1R1qjfO^^0f{7=tJUfu z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