Skip to content

Conversation

@flocked
Copy link
Contributor

@flocked flocked commented May 16, 2025

I updated the access to the hooking methods by using

  • ObjectHook: Hooks methods of an object.
  • ClassInstanceHook: Hooks methods of all instances of a class.
  • ClassHook: Hooks class methods.

Like this the hooking methods are always named the same for object, class and class instances hooking:

ObjectHook(textfield).hookBefore(selector, closure: closure)
ClassInstanceHook(NSTextField.self).hookBefore(selector, closure: closure)
ClassInstance(NSTextField.self).hookBefore(selector, closure: closure)

It also provides easier hooking of NSObject properties using keyPaths:

  • Hook getting a property: func hook<Value>(_ keyPath: KeyPath<T, Value>, closure: @escaping (_ object: T, _ original: Value)->(Value)) throws -> Token. Example:
ObjectHook(textField).hook(\.stringValue, closure: { object, value in
	return value.lowercased()
})
  • Hook setting a property:func hook<Value>(set keyPath: WritableKeyPath<T, Value>, closure: @escaping (_ object: T, _ value: Value, _ original: (Value)->())->()) throws -> Token. Example:
ObjectHook(textField).hook(set: \.stringValue, closure: { object, value, original in
	// original is a closure to the original property setter that can be called optionally to set the property.
	original(value.lowercased())
})
  • Hook after getting a property: func hookAfter<Value>(_ keyPath: WritableKeyPath<T, Value>, closure: @escaping (_ object: T,_ value: Value)->()) throws -> Token. Example:
ObjectHook(textField).hookAfter(\.stringValue, closure: { object, value in
	
})
  • Hook after setting a property: func hookAfter<Value>(set keyPath: WritableKeyPath<T, Value>, closure: @escaping (_ object: T,_ value: Value)->()) throws -> Token. Example:
ObjectHook(textField).hookAfter(set: \.stringValue, closure: { object, newValue in
	
})
  • Hook before getting a property: func hookBefore<Value>(_ keyPath: WritableKeyPath<T, Value>, closure: @escaping (_ object: T,_ value: Value)->()) throws -> Token.
  • Hook before setting a property: func hookBefore <Value>(set keyPath: WritableKeyPath<T, Value>, closure: @escaping (_ object: T,_ value: Value)->()) throws -> Token.

@623637646
Copy link
Owner

Hi @flocked .Thank you so much for the contribution again. This PR is a huge one for some small improvement of user experience. Currently I don't have time to review it. Now only bug fixing and important features are acceptable. Sorry about it. Let's keep this PR, will consider it in the future if needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants