Skip to content

Commit dbb8af9

Browse files
hyunq21MrJul
andauthored
[macOS] IME preedit lost on focus change (AvaloniaUI#21601)
* fix(macOS): commit IME preedit when switching focus between textboxes Fixes AvaloniaUI#19964 * remove comment --------- Co-authored-by: Julien Lebosquain <julien@lebosquain.net>
1 parent 5495737 commit dbb8af9

3 files changed

Lines changed: 25 additions & 1 deletion

File tree

native/Avalonia.Native/src/OSX/AvnTextInputMethod.mm

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
}
3030

3131
void AvnTextInputMethod::Reset() {
32+
[_inputMethodDelegate resetInputMethod];
3233
}
3334

3435
void AvnTextInputMethod::SetSurroundingText(char* text, int start, int end) {

native/Avalonia.Native/src/OSX/AvnTextInputMethodDelegate.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
-(void) setText:(NSString* _Nonnull) text;
1515
-(void) setCursorRect:(AvnRect) cursorRect;
1616
-(void) setSelection: (int) start : (int) end;
17+
-(void) resetInputMethod;
1718

1819
@end
1920

native/Avalonia.Native/src/OSX/AvnView.mm

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -375,7 +375,15 @@ - (void)mouseEvent:(NSEvent *)event withType:(AvnRawMouseEventType) type
375375
[self becomeFirstResponder];
376376
}
377377
}
378-
378+
379+
if([self hasMarkedText] &&
380+
(type == LeftButtonDown || type == RightButtonDown || type == MiddleButtonDown ||
381+
type == XButton1Down || type == XButton2Down) &&
382+
[self inputContext] != nil)
383+
{
384+
[[self inputContext] handleEvent:event];
385+
}
386+
379387
auto parent = _parent.tryGet();
380388
if(parent != nullptr)
381389
{
@@ -1035,6 +1043,20 @@ - (void) setSelection:(int)start :(int)end{
10351043
_selectedRange = NSMakeRange(start, end - start);
10361044
}
10371045

1046+
- (void) resetInputMethod{
1047+
auto parent = _parent.tryGet();
1048+
1049+
if(parent != nullptr && parent->InputMethod->IsActive()){
1050+
parent->InputMethod->Client->SetPreeditText(nullptr);
1051+
}
1052+
1053+
_markedRange = NSMakeRange(_selectedRange.location, 0);
1054+
1055+
if([self inputContext]) {
1056+
[[self inputContext] discardMarkedText];
1057+
}
1058+
}
1059+
10381060
- (void) setCursorRect:(AvnRect)rect{
10391061
NSRect cursorRect = ToNSRect(rect);
10401062
NSRect windowRectOnScreen = [[self window] convertRectToScreen:self.frame];

0 commit comments

Comments
 (0)