I'm doing preliminary tests with the "close" method in order to support multiple Leo files in leoInteg.
def closeFile(self, p_paramUnused):'''Closes a leo file. A file can then be opened with "openFile"'''print("Trying to close opened file " + str(self.commander.changed))if self.commander:if self.commander.changed:return self.sendLeoBridgePackage('closed', False)else:self.commander.close()return self.sendLeoBridgePackage('closed', True)return self.sendLeoBridgePackage() # Just send empty as 'did nothing'After going through the second branch and actually calling the .close() method on the controller that was made with the leoBridge, since I didnt change anything else to support a 'closed' commander, I expected the leoInteg view to fatally crash because all subsequent calls to 'getChildren' etc. would obviously fail because the commander was closed.Surprising thing is : There is no crash and I can seemingly do any browsing and editing as I could before the .close() call...I suspect it's me who's not understanding what the self.commander.close method is actually expected to do, or maybe I should be calling something like "self.commander.file.close()" or something like that... Or maybe that just lowered an "opened" flag somewhere and all objects are still valid pointers until python does garbage collecting... i dunno :/If anyone can shed some light on this it would be greatly appreciated! :)
EDIT / ADDENDUM WHILE STEPPING THROUGH WITH THE DEBUGGERFirst it goes through this:@g.commander_command('close-window')def close(self, event=None, new_c=None):"""Close the Leo window, prompting to save it if it has been changed."""g.app.closeLeoWindow(self.frame, new_c=new_c)
Again, since I've got no gui and the commander I'm closing was opened through leoBridge, I guess I should just then 'pop' the commander from the array I was keeping it in, and consider it "closed" and its 'ressources' will be freed eventually? ( as this reference seems to indicate? https://stackoverflow.com/questions/49065803/python-delete-objects-and-free-up-space )
if frame in g.app.windowList:
g.app.destroyWindow(frame)
g.app.windowList.remove(frame)
else:
# #69.
g.app.forgetOpenFile(fn=c.fileName(), force=True)def destroyWindow(self, frame):
"""Destroy all ivars in a Leo frame."""
if 'shutdown' in g.app.debug:
g.pr(f"destroyWindow: {frame.c.shortFileName()}")
if g.app.externalFilesController:
g.app.externalFilesController.destroy_frame(frame)
if frame in g.app.windowList:
# g.pr('destroyWindow', (g.app.windowList)
g.app.forgetOpenFile(frame.c.fileName())
# force the window to go away now.
# Important: this also destroys all the objects of the commander.
frame.destroySelf()My advice is not to worry about whether c has been garbage collected. You can safely access c if and only if c.frame exists in g.app.windowList.
I was trying to make working 'close' and 'save as' methods last night and got caught up in trying to also make multiple opened files a reality also while at it.
I will show the name of the currently chosen file as the status bar indicator, and clicking it will unfold the list of currently opened leo files to choose from.
I'll get back to this later have a great and sunny Saturday everybody!