Activate a context menu, or the file menu of a pane
Hit escape
Expected result
Menu should close
Actual result
Menu remains open
Environment
Operating system: Windows
Obsidian version: 0.10.11
Additional information
I’m really accustomed to escape closing popup menus in other programs, so this is a bit of an irritation, as I keep having to figure out what part of the UI I can safely click to make the menu go away… after I first hit escape a few times and feel confused.
ATM I’m hotfixing this by wrapping Menu.prototype.load with a call to register a document-level keydown event handler that looks for escape and hides the menu. (It’s nice that the framework makes this so easy to do!) But an actual fix would probably be better than my monkeypatching.
Which part, the hotfixing part, or the keydown handler? To hotfix something like that, I have my plugin modify the class during plugin load, and then restore the original version on plugin unload. It’s better if it can be done on instances (like app or workspace) than on classes, because then you can just delete the wrapping function from the instance instead of having to save and restore the original.
As for the method, it’s basically:
onKeydown(e) {
if (e.key==="Escape") {
e.preventDefault();
this.hide();
}
}
For the hotfixing, it generally looks like this in a plugin onload:
const proto = Someclass.prototype, oldMethod = proto.someMethod;
this.register( () => proto.someMethod = oldMethod );
proto.someMethod = function(...) {
// possibly call `oldMethod.call(this, ...)` to invoke the original
}
This is not a terribly robust way of doing it; if I were going to make it compatible with other plugins possibly hotpatching the same method, I would wrap the new method in a function that would switch to calling the original method at exit time. That way, if another plugin hotpatched the same method, it would just end up calling the unpatched version if my plugin were unloaded.
But for the moment, I have very few hotfixes like this and as far as I know no other plugins are doing this sort of thing at the moment.
Part of one of my own personal plugins, yes. I actually had a more extensive one for a while with a lot of hotfixes for Obsidian’s pane/focus/setActiveLeaf issues, but I’ve been able to ditch most of those hotfixes recently. It still has some stuff to let commands work on panes docked in a sidebar, though, as that’s something that is still an issue in Obsidian core.