Skip to content

Commit 2424025

Browse files
committed
IMKSwift & SessionCtl // Lambda-assign all protocol implementations.
1 parent 336975e commit 2424025

3 files changed

Lines changed: 233 additions & 162 deletions

File tree

Packages/vChewing_IMKUtils/Sources/IMKSwiftModernHeaders/IMKSwift.m

Lines changed: 96 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -8,16 +8,14 @@
88
/// This file provides the `@implementation` block for `IMKInputSessionController`,
99
/// which is declared in `IMKSwift.h` as a subclass of `IMKInputController`.
1010
///
11-
/// Because `IMKInputSessionController` re-declares inherited methods from
12-
/// `IMKInputController` (with refined nullability and `@MainActor` annotations),
13-
/// the compiler would normally emit `-Wincomplete-implementation` warnings for
14-
/// each method that lacks an explicit body. The `#pragma clang diagnostic`
15-
/// block suppresses these warnings — the actual implementations are inherited
16-
/// from `IMKInputController` at runtime via the Objective-C message dispatch.
11+
/// **All** `IMKInputController` overrides are implemented here; each invokes the
12+
/// corresponding block property set by the Swift subclass. The blocks receive
13+
/// raw memory addresses (`uintptr_t`) instead of object references — no
14+
/// retain/release is performed on the client or the controller itself.
1715
///
18-
/// **Only** `-activateServer:` and `-deactivateServer:` are implemented here;
19-
/// they invoke the block properties (`onActivateServer` / `onDeactivateServer`)
20-
/// set by the Swift subclass and manage the deferred-dealloc timer.
16+
/// The `-Wincomplete-implementation` pragma is still needed for the remaining
17+
/// methods that are inherited from `IMKInputController` at runtime without
18+
/// explicit bodies (e.g. `candidates:`, `doCommandBy:`).
2119

2220
#import <Foundation/Foundation.h>
2321
#import <InputMethodKit/InputMethodKit.h>
@@ -32,42 +30,46 @@ @implementation IMKInputSessionController {
3230
void (^_onActivateServer)(uintptr_t, uintptr_t);
3331
void (^_onDeactivateServer)(uintptr_t, uintptr_t);
3432
void (^_onDealloc)(uintptr_t);
33+
void (^_onShowingPreferences)(uintptr_t, uintptr_t);
34+
void (^_onHidingPallettes)(uintptr_t);
35+
void (^_onInputControllerWillClose)(uintptr_t);
36+
NSRange (^_onProvidingSelectionRange)(uintptr_t);
37+
NSMenu * _Nullable (^_onProvidingIMEMenu)(uintptr_t);
38+
id _Nullable (^_onProvidingComposedString)(uintptr_t, uintptr_t);
39+
void (^_onAutoCommittingComposition)(uintptr_t, uintptr_t);
40+
NSUInteger (^_onProvidingRecognizedEvents)(uintptr_t, uintptr_t);
41+
BOOL (^_onHandlingGivenNullableEvent)(uintptr_t, uintptr_t, uintptr_t);
42+
void (^_onSettingObjCValue)(uintptr_t, intptr_t, uintptr_t, uintptr_t);
3543
}
3644

3745
// MARK: - Block Properties (MRC)
3846

39-
- (void)setOnActivateServer:(nullable void (^)(uintptr_t, uintptr_t))block {
40-
if (_onActivateServer != block) {
41-
[_onActivateServer release];
42-
_onActivateServer = [block copy];
47+
#define MRC_BLOCK_PROPERTY(_name, _sig) \
48+
- (void)set##_name:(nullable _sig)block { \
49+
if (_##_name != block) { \
50+
[_##_name release]; \
51+
_##_name = [block copy]; \
52+
} \
53+
} \
54+
- (nullable _sig)_name { \
55+
return [[_##_name retain] autorelease]; \
4356
}
44-
}
45-
46-
- (nullable void (^)(uintptr_t, uintptr_t))onActivateServer {
47-
return [[_onActivateServer retain] autorelease];
48-
}
49-
50-
- (void)setOnDeactivateServer:(nullable void (^)(uintptr_t, uintptr_t))block {
51-
if (_onDeactivateServer != block) {
52-
[_onDeactivateServer release];
53-
_onDeactivateServer = [block copy];
54-
}
55-
}
56-
57-
- (nullable void (^)(uintptr_t, uintptr_t))onDeactivateServer {
58-
return [[_onDeactivateServer retain] autorelease];
59-
}
6057

61-
- (void)setOnDealloc:(nullable void (^)(uintptr_t))block {
62-
if (_onDealloc != block) {
63-
[_onDealloc release];
64-
_onDealloc = [block copy];
65-
}
66-
}
67-
68-
- (nullable void (^)(uintptr_t))onDealloc {
69-
return [[_onDealloc retain] autorelease];
70-
}
58+
MRC_BLOCK_PROPERTY(onActivateServer, void (^)(uintptr_t, uintptr_t))
59+
MRC_BLOCK_PROPERTY(onDeactivateServer, void (^)(uintptr_t, uintptr_t))
60+
MRC_BLOCK_PROPERTY(onDealloc, void (^)(uintptr_t))
61+
MRC_BLOCK_PROPERTY(onShowingPreferences, void (^)(uintptr_t, uintptr_t))
62+
MRC_BLOCK_PROPERTY(onHidingPallettes, void (^)(uintptr_t))
63+
MRC_BLOCK_PROPERTY(onInputControllerWillClose, void (^)(uintptr_t))
64+
MRC_BLOCK_PROPERTY(onProvidingSelectionRange, NSRange (^)(uintptr_t))
65+
MRC_BLOCK_PROPERTY(onProvidingIMEMenu, NSMenu * _Nullable (^)(uintptr_t))
66+
MRC_BLOCK_PROPERTY(onProvidingComposedString, id _Nullable (^)(uintptr_t, uintptr_t))
67+
MRC_BLOCK_PROPERTY(onAutoCommittingComposition, void (^)(uintptr_t, uintptr_t))
68+
MRC_BLOCK_PROPERTY(onProvidingRecognizedEvents, NSUInteger (^)(uintptr_t, uintptr_t))
69+
MRC_BLOCK_PROPERTY(onHandlingGivenNullableEvent, BOOL (^)(uintptr_t, uintptr_t, uintptr_t))
70+
MRC_BLOCK_PROPERTY(onSettingObjCValue, void (^)(uintptr_t, intptr_t, uintptr_t, uintptr_t))
71+
72+
#undef MRC_BLOCK_PROPERTY
7173

7274
// MARK: - Lifecycle
7375

@@ -76,6 +78,16 @@ - (void)dealloc {
7678
[_onDealloc release];
7779
[_onActivateServer release];
7880
[_onDeactivateServer release];
81+
[_onShowingPreferences release];
82+
[_onHidingPallettes release];
83+
[_onInputControllerWillClose release];
84+
[_onProvidingSelectionRange release];
85+
[_onProvidingIMEMenu release];
86+
[_onProvidingComposedString release];
87+
[_onAutoCommittingComposition release];
88+
[_onProvidingRecognizedEvents release];
89+
[_onHandlingGivenNullableEvent release];
90+
[_onSettingObjCValue release];
7991
[self IMKSwift_cancelDelayedDealloc];
8092
[super dealloc];
8193
}
@@ -92,6 +104,51 @@ - (void)deactivateServer:(id)sender {
92104
[self IMKSwift_scheduleDelayedDeallocAfterDelay:3.0];
93105
}
94106

107+
- (void)showPreferences:(nullable id)sender {
108+
if (_onShowingPreferences) _onShowingPreferences((uintptr_t)sender, (uintptr_t)self);
109+
}
110+
111+
- (void)hidePalettes {
112+
if (_onHidingPallettes) _onHidingPallettes((uintptr_t)self);
113+
}
114+
115+
- (void)inputControllerWillClose {
116+
if (_onInputControllerWillClose) _onInputControllerWillClose((uintptr_t)self);
117+
}
118+
119+
- (NSRange)selectionRange {
120+
if (_onProvidingSelectionRange) return _onProvidingSelectionRange((uintptr_t)self);
121+
return NSMakeRange(NSNotFound, 0);
122+
}
123+
124+
- (nullable NSMenu *)menu {
125+
if (_onProvidingIMEMenu) return _onProvidingIMEMenu((uintptr_t)self);
126+
return [[NSMenu new] autorelease];
127+
}
128+
129+
- (nullable id)composedString:(id)sender {
130+
if (_onProvidingComposedString) return _onProvidingComposedString((uintptr_t)sender, (uintptr_t)self);
131+
return nil;
132+
}
133+
134+
- (void)commitComposition:(id)sender {
135+
if (_onAutoCommittingComposition) _onAutoCommittingComposition((uintptr_t)sender, (uintptr_t)self);
136+
}
137+
138+
- (NSUInteger)recognizedEvents:(id)sender {
139+
if (_onProvidingRecognizedEvents) return _onProvidingRecognizedEvents((uintptr_t)sender, (uintptr_t)self);
140+
return 0;
141+
}
142+
143+
- (BOOL)handleEvent:(nullable NSEvent *)event client:(id)sender {
144+
if (_onHandlingGivenNullableEvent) return _onHandlingGivenNullableEvent((uintptr_t)event, (uintptr_t)sender, (uintptr_t)self);
145+
return NO;
146+
}
147+
148+
- (void)setValue:(nullable id)value forTag:(NSInteger)tag client:(id)sender {
149+
if (_onSettingObjCValue) _onSettingObjCValue((uintptr_t)value, (intptr_t)tag, (uintptr_t)sender, (uintptr_t)self);
150+
}
151+
95152
// MARK: - Private: Deferred Dealloc
96153

97154
/// Schedules a delayed dealloc of this controller after `delay` seconds.

Packages/vChewing_IMKUtils/Sources/IMKSwiftModernHeaders/include/IMKSwift.h

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -900,7 +900,7 @@
900900
/// @return An array of candidate objects, or `nil` if there are no candidates.
901901
- (nullable NSArray *)candidates:(nonnull id<IMKTextInput>)sender;
902902

903-
// MARK: Deferred Dealloc Callbacks
903+
// MARK: Deferred Dealloc & IME Lifecycle Callbacks
904904

905905
/// Block invoked from `-activateServer:` after cancelling any pending delayed
906906
/// dealloc. Receives raw memory addresses (`uintptr_t`) of both the
@@ -916,6 +916,36 @@
916916
/// that must run even if the Swift subclass's `deinit` is skipped.
917917
@property (copy, nullable) void (^onDealloc)(uintptr_t selfAddr);
918918

919+
/// Block invoked from `-showPreferences:`.
920+
@property (copy, nullable) void (^onShowingPreferences)(uintptr_t givenClientAddr, uintptr_t selfAddr);
921+
922+
/// Block invoked from `-hidePalettes`.
923+
@property (copy, nullable) void (^onHidingPallettes)(uintptr_t selfAddr);
924+
925+
/// Block invoked from `-inputControllerWillClose`.
926+
@property (copy, nullable) void (^onInputControllerWillClose)(uintptr_t selfAddr);
927+
928+
/// Block invoked from `-selectionRange`.
929+
@property (copy, nullable) NSRange (^onProvidingSelectionRange)(uintptr_t selfAddr);
930+
931+
/// Block invoked from `-menu`.
932+
@property (copy, nullable) NSMenu * _Nullable (^onProvidingIMEMenu)(uintptr_t selfAddr);
933+
934+
/// Block invoked from `-composedString:`.
935+
@property (copy, nullable) id _Nullable (^onProvidingComposedString)(uintptr_t givenClientAddr, uintptr_t selfAddr);
936+
937+
/// Block invoked from `-commitComposition:`.
938+
@property (copy, nullable) void (^onAutoCommittingComposition)(uintptr_t givenClientAddr, uintptr_t selfAddr);
939+
940+
/// Block invoked from `-recognizedEvents:`.
941+
@property (copy, nullable) NSUInteger (^onProvidingRecognizedEvents)(uintptr_t givenClientAddr, uintptr_t selfAddr);
942+
943+
/// Block invoked from `-handleEvent:client:`. `nsEventPtr` may be 0 (nil event).
944+
@property (copy, nullable) BOOL (^onHandlingGivenNullableEvent)(uintptr_t nsEventPtr, uintptr_t givenClientAddr, uintptr_t selfAddr);
945+
946+
/// Block invoked from `-setValue:forTag:client:`. `valuePtr` may be 0 (nil value).
947+
@property (copy, nullable) void (^onSettingObjCValue)(uintptr_t valuePtr, intptr_t intTag, uintptr_t givenClientAddr, uintptr_t selfAddr);
948+
919949
@end
920950

921951
#pragma clang attribute pop

0 commit comments

Comments
 (0)