覆盖窗口关闭行为

时间:2011-09-12 01:50:39

标签: objective-c cocoa pyobjc

我想抓住所有尝试关闭一些特定的现有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

然而,这似乎不太正确。 (它崩溃了。)

2 个答案:

答案 0 :(得分:1)

关闭窗口小部件不是关闭窗口的唯一方法。有一个公共API来获取小部件,所以你不需要遍历框架视图的子视图,但无论如何这都是错误的路径。

正确的方法是使一个对象成为窗口的委托,并在那里interfere with the window's closure。理想情况下,您应该在创建窗口和订购窗口之间设置窗口的委托。

答案 1 :(得分:0)

我现在要走另一条路了。这部分与Chrome有关,但很容易在其他地方采用。我想尽早抓住几个关闭窗口的操作,以避免任何其他清理或导致窗口处于奇怪状态。

def check_close_callback(obj):
    # check ...
    return True # or:
    return False

import objc
BrowserWindowController = objc.lookUpClass("BrowserWindowController")

# copied from objc.signature to avoid warning
def my_signature(signature, **kw):
    from objc._objc import selector
    kw['signature'] = signature
    def makeSignature(func):
        return selector(func, **kw)
    return makeSignature

windowWillCloseSig = "c12@0:4@8" # BrowserWindowController.windowWillClose_.signature
commandDispatchSig = "v12@0:4@8"
class BrowserWindowController(objc.Category(BrowserWindowController)):
    @my_signature(windowWillCloseSig)
    def myWindowShouldClose_(self, sender):
        print "myWindowShouldClose", self, sender
        if not check_close_callback(self): return objc.NO
        return self.myWindowShouldClose_(sender) # this is no recursion when we exchanged the methods

    @my_signature(commandDispatchSig)
    def myCommandDispatch_(self, cmd):
        try: print "myCommandDispatch_", self, cmd
        except: pass # like <type 'exceptions.UnicodeEncodeError'>: 'ascii' codec can't encode character u'\u2026' in position 37: ordinal not in range(128)
        if cmd.tag() == 34015: # IDC_CLOSE_TAB
            if not check_close_callback(self): return           
        self.myCommandDispatch_(cmd)

from ctypes import *
capi = pythonapi

# id objc_getClass(const char *name)
capi.objc_getClass.restype = c_void_p
capi.objc_getClass.argtypes = [c_char_p]

# SEL sel_registerName(const char *str)
capi.sel_registerName.restype = c_void_p
capi.sel_registerName.argtypes = [c_char_p]

def capi_get_selector(name):
    return c_void_p(capi.sel_registerName(name))

# Method class_getInstanceMethod(Class aClass, SEL aSelector)
# Will also search superclass for implementations.
capi.class_getInstanceMethod.restype = c_void_p
capi.class_getInstanceMethod.argtypes = [c_void_p, c_void_p]

# void method_exchangeImplementations(Method m1, Method m2)
capi.method_exchangeImplementations.restype = None
capi.method_exchangeImplementations.argtypes = [c_void_p, c_void_p]

def method_exchange(className, origSelName, newSelName):
    clazz = capi.objc_getClass(className)
    origMethod = capi.class_getInstanceMethod(clazz, capi_get_selector(origSelName))
    newMethod = capi.class_getInstanceMethod(clazz, capi_get_selector(newSelName))
    capi.method_exchangeImplementations(origMethod, newMethod)

def hook_into_windowShouldClose():
    method_exchange("BrowserWindowController", "windowShouldClose:", "myWindowShouldClose:")

def hook_into_commandDispatch():
    method_exchange("BrowserWindowController", "commandDispatch:", "myCommandDispatch:")

此代码来自herehere