def window_should_close(self, window): from editxt.application import DocumentSavingDelegate # this method is called after the window controller has prompted the # user to save the current document (if it is dirty). This causes some # wierdness with the window and subsequent sheets. Technically we # do not need to prompt to save the current document a second time. # However, we will because it is easier... THIS IS UGLY! but there # doesn't seem to be a way to prevent the window controller from # prompting to save the current document when the window's close button # is clicked. UPDATE: the window controller seems to only prompt to save # the current document if the document is new (untitled). def iter_dirty_docs(): app = self.app for proj in self.projects: eds = app.find_editors_with_project(proj) if eds == [self]: for dv in proj.dirty_documents(): doc = dv.document editors = app.iter_editors_with_view_of_document(doc) if list(editors) == [self]: yield dv yield proj def callback(should_close): if should_close: window.close() saver = DocumentSavingDelegate.alloc(). \ init_callback_(iter_dirty_docs(), callback) saver.save_next_document() return False
def test_DocumentSavingDelegate_init(): docs = object() ctrl = object() cbck = object() saver = DocumentSavingDelegate.alloc().init_callback_(docs, cbck) assert saver is DocumentSavingDelegate.registry[id(saver)] assert saver.documents is docs assert saver.callback is cbck assert saver.should_close
def do_test(called_back): m = Mocker() saver = DocumentSavingDelegate.alloc().init_callback_(m.mock(), m.mock()) saver.document_called_back = called_back notif = m.mock(fn.NSNotification) win = m.mock(ak.NSWindow) notif.object() >> win note_ctr = m.replace(fn, 'NSNotificationCenter') note_ctr.defaultCenter().removeObserver_name_object_( saver, ak.NSWindowDidEndSheetNotification, win) save_next_document = m.method(saver.save_next_document) if called_back: save_next_document() with m: saver.windowDidEndSheet_(notif) assert saver.sheet_did_end
def perform_close(self, editor): from editxt.application import DocumentSavingDelegate if app.find_editors_with_project(self) == [editor]: def dirty_docs(): for dv in self.dirty_documents(): itr = app.iter_editors_with_view_of_document(dv.document) if list(itr) == [editor]: yield dv yield self def callback(should_close): if should_close: editor.discard_and_focus_recent(self) saver = DocumentSavingDelegate.alloc().\ init_callback_(dirty_docs(), callback) saver.save_next_document() else: editor.discard_and_focus_recent(self)
def do_test(doctype, doc_window_is_front=True): m = Mocker() app = m.replace(editxt, 'app') docs = [] dc = m.mock(DocumentController) note_ctr = m.replace(fn, 'NSNotificationCenter') controller = m.mock() callback = m.mock() context = 0 saver = DocumentSavingDelegate.alloc().init_callback_( iter(docs), callback) def do_stop_routine(): callback(saver.should_close) if doctype is None: do_stop_routine() else: doc = m.mock(doctype) docs.append(doc) if doctype is Project: doc.save() do_stop_routine() elif doctype is TextDocumentView: app.set_current_document_view(doc) win = m.mock() doc.window() >> win note_ctr.defaultCenter().addObserver_selector_name_object_( saver, "windowDidEndSheet:", ak.NSWindowDidEndSheetNotification, win) document = doc.document >> m.mock(TextDocument) wcs = m.mock(list) (document.windowControllers() << wcs).count(1, 2) wcs[0].window() >> (win if doc_window_is_front else m.mock()) if not doc_window_is_front: wcs.sort(key=ANY) document.canCloseDocumentWithDelegate_shouldCloseSelector_contextInfo_( saver, "document:shouldClose:contextInfo:", context) with m: saver.save_next_document() if doctype is TextDocumentView: assert not saver.document_called_back assert not saver.sheet_did_end else: eq_(saver.documents, None) assert id(saver) not in saver.registry
def do_test(called_back): m = Mocker() saver = DocumentSavingDelegate.alloc().init_callback_( m.mock(), m.mock()) saver.document_called_back = called_back notif = m.mock(fn.NSNotification) win = m.mock(ak.NSWindow) notif.object() >> win note_ctr = m.replace(fn, 'NSNotificationCenter') note_ctr.defaultCenter().removeObserver_name_object_( saver, ak.NSWindowDidEndSheetNotification, win) save_next_document = m.method(saver.save_next_document) if called_back: save_next_document() with m: saver.windowDidEndSheet_(notif) assert saver.sheet_did_end
def do_test(doctype, doc_window_is_front=True): m = Mocker() app = m.replace("editxt.app", type=Application) docs = m.mock() dc = m.mock(DocumentController) controller = m.mock() callback = m.mock() context = 0 saver = DocumentSavingDelegate.alloc().init_callback_(docs, callback) def do_stop_routine(): expect(docs.next()).throw(StopIteration) callback(saver.should_close) if doctype is None: do_stop_routine() else: doc = m.mock(doctype) docs.next() >> doc if doctype is Project: doc.save() do_stop_routine() elif doctype is TextDocumentView: app.set_current_document_view(doc) win = m.mock() doc.window() >> win note_ctr = m.replace("editxt.application.NSNotificationCenter") note_ctr.defaultCenter().addObserver_selector_name_object_( saver, "windowDidEndSheet:", NSWindowDidEndSheetNotification, win) document = doc.document >> m.mock(TextDocument) wcs = m.mock(list) (document.windowControllers() << wcs).count(1, 2) wcs[0].window() >> (win if doc_window_is_front else m.mock()) if not doc_window_is_front: wcs.sort(key=ANY) document.canCloseDocumentWithDelegate_shouldCloseSelector_contextInfo_( saver, "document:shouldClose:contextInfo:", context) with m: saver.save_next_document() if doctype is TextDocumentView: assert not saver.document_called_back assert not saver.sheet_did_end else: eq_(saver.documents, None) assert id(saver) not in saver.registry
def do_test(should_close, sheet_did_end): context = 0 m = Mocker() saver = DocumentSavingDelegate.alloc().init_callback_(m.mock(), m.mock()) save_next_document = m.method(saver.save_next_document) saver.sheet_did_end = sheet_did_end if sheet_did_end: save_next_document() doc = m.mock(TextDocument) with m: saver.document_shouldClose_contextInfo_(doc, should_close, context) assert saver.document_called_back if not should_close: assert not saver.should_close try: next(saver.documents) raise AssertionError("next(saver.documents) should raise StopIteration") except StopIteration: pass else: assert saver.should_close
def do_test(should_close, sheet_did_end): context = 0 m = Mocker() saver = DocumentSavingDelegate.alloc().init_callback_( m.mock(), m.mock()) save_next_document = m.method(saver.save_next_document) saver.sheet_did_end = sheet_did_end if sheet_did_end: save_next_document() doc = m.mock(TextDocument) with m: saver.document_shouldClose_contextInfo_(doc, should_close, context) assert saver.document_called_back if not should_close: assert not saver.should_close try: next(saver.documents) raise AssertionError( "next(saver.documents) should raise StopIteration") except StopIteration: pass else: assert saver.should_close