English 中文(简体)
3. 闭门窗户的密切行为
原标题:Override window close behavior

我想赶上所有努力关闭某些现有的可可窗户,并增加一些自有手手(可能实际上关闭或做一些不同的事情)。

我对此有不同的解决办法。 一是:

我想用自己的近重植被取代目前可可窗口的窗户,我可以补充一些自己的代码。

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.)

最佳回答

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.

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:")

该代码来自here

问题回答




相关问题
Asynchronous request to the server from background thread

I ve got the problem when I tried to do asynchronous requests to server from background thread. I ve never got results of those requests. Simple example which shows the problem: @protocol ...

objective-c: Calling a void function from another controller

i have a void, like -(void) doSomething in a specific controller. i can call it in this controller via [self doSomething], but i don t know how to call this void from another .m file. I want to call ...

ABPersonViewController Usage for displaying contact

Created a View based Project and added a contact to the AddressBook using ABAddressBookRef,ABRecordRef now i wanted to display the added contact ABPersonViewController is the method but how to use in ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

NSUndoManager and runModalForWindow:

I have a simple Core Data app which displays a list of entities in the main window. To create or add new entities, I use a second modal window with a separate managed object context so changes can be ...

NSMutableArray values becoming "invalid"

I m trying to show a database information in a tableview and then the detailed information in a view my problem is as follow: I created a NSMutableArray: NSMutableArray *myArray = [[NSMutableArray ...

iPhone numberpad with decimal point

I am writing an iPhone application which requires the user to enter several values that may contain a decimal point (currency values, percentages etc.). The number of decimal places in the values ...

热门标签