覆盖窗口关闭行为
我想捕获所有关闭某些特定现有 Cocoa 窗口并添加一些自己的处理程序的尝试(这可能确实会关闭它或执行不同的操作)。
我想到了不同的解决方案来做到这一点。一是:
我想在运行时用自己的关闭小部件替换现有 Cocoa 窗口的窗口关闭按钮,我可以在其中添加一些自己的代码。
现在,我有这段代码:
import objc
_NSThemeCloseWidget = objc.lookUpClass("_NSThemeCloseWidget")
def find_close_widget(window):
contentView = window.contentView()
grayFrame = contentView.superview()
for i in range(len(grayFrame.subviews())):
v = grayFrame.subviews()[i]
if isinstance(v, _NSThemeCloseWidget):
return v, i, grayFrame
class CustomCloseWidget(_NSThemeCloseWidget):
pass
def replace_close_widget(window, clazz=CustomCloseWidget):
v, i, grayFrame = find_close_widget(window)
newv = clazz.alloc().init()
grayFrame.subviews()[i] = newv
但是,这似乎不太正确。 (它崩溃了。)
I want to catch all tries to close some specific existing Cocoa window and add some own handler (which might indeed really close it or do something different).
I had different solutions in mind to do this. One was:
I want to replace the window close button of an existing Cocoa window at runtime with an own close widget where I can add some own code.
Right now, I have this code:
import objc
_NSThemeCloseWidget = objc.lookUpClass("_NSThemeCloseWidget")
def find_close_widget(window):
contentView = window.contentView()
grayFrame = contentView.superview()
for i in range(len(grayFrame.subviews())):
v = grayFrame.subviews()[i]
if isinstance(v, _NSThemeCloseWidget):
return v, i, grayFrame
class CustomCloseWidget(_NSThemeCloseWidget):
pass
def replace_close_widget(window, clazz=CustomCloseWidget):
v, i, grayFrame = find_close_widget(window)
newv = clazz.alloc().init()
grayFrame.subviews()[i] = newv
However, this doesn't seem quite right. (It crashes.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
关闭小部件并不是关闭窗口的唯一方法。有一个公共 API 可以获取该小部件,因此您无需仔细检查框架视图的子视图,但无论如何这是错误的路径。
正确的方法是让一个对象成为窗口的委托,并且 干扰窗口关闭 那里。理想情况下,您应该在创建窗口和订购窗口之间设置窗口的委托。
The close widget isn't the only way to close the window. There's a public API to obtain the widget, so you don't need to go rifling through the frame view's subviews, but that's the wrong path anyway.
The right way is to make an object to be the window's delegate, and interfere with the window's closure there. Ideally, you should set the window's delegate in between creating the window and ordering it in.
我现在要走另一条路。这部分与 Chrome 相关,但它可以很容易地在其他地方采用。我想尽早捕获关闭窗口的几个操作,以避免任何其他清理或其他导致窗口处于奇怪状态的操作。
此代码来自此处和这里。
I am going another route now. This is partly Chrome related but it can easily be adopted elsewhere. I wanted to catch several actions for closing the window as early as possible to avoid any other cleanups or so which resulted in the window being in a strange state.
This code is from here and here.