Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mac] Fix InputContext event handling #11021

Merged
merged 7 commits into from
Apr 19, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
104 changes: 68 additions & 36 deletions native/Avalonia.Native/src/OSX/AvnView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ @implementation AvnView
{
ComPtr<WindowBaseImpl> _parent;
NSTrackingArea* _area;
NSMutableAttributedString* _markedText;
bool _isLeftPressed, _isMiddlePressed, _isRightPressed, _isXButton1Pressed, _isXButton2Pressed;
AvnInputModifiers _modifierState;
NSEvent* _lastMouseDownEvent;
Expand All @@ -22,8 +21,9 @@ @implementation AvnView
AvnPlatformResizeReason _resizeReason;
AvnAccessibilityElement* _accessibilityChild;
NSRect _cursorRect;
NSMutableString* _text;
NSRange _selection;
NSMutableAttributedString* _text;
NSRange _selectedRange;
NSRange _markedRange;
}

- (void)onClosed
Expand Down Expand Up @@ -59,6 +59,11 @@ -(AvnView*) initWithParent: (WindowBaseImpl*) parent
[self registerForDraggedTypes: @[@"public.data", GetAvnCustomDataType()]];

_modifierState = AvnInputModifiersNone;

_text = [[NSMutableAttributedString alloc] initWithString:@""];
_markedRange = NSMakeRange(0, 0);
_selectedRange = NSMakeRange(0, 0);

return self;
}

Expand Down Expand Up @@ -521,9 +526,13 @@ - (void)flagsChanged:(NSEvent *)event

- (void)keyDown:(NSEvent *)event
{
[self keyboardEvent:event withType:KeyDown];
_lastKeyHandled = [[self inputContext] handleEvent:event];
[super keyDown:event];
_lastKeyHandled = false;

[[self inputContext] handleEvent:event];

if(!_lastKeyHandled){
[self keyboardEvent:event withType:KeyDown];
}
}

- (void)keyUp:(NSEvent *)event
Expand All @@ -532,6 +541,10 @@ - (void)keyUp:(NSEvent *)event
[super keyUp:event];
}

- (void) doCommandBySelector:(SEL)selector{

}

- (AvnInputModifiers)getModifiers:(NSEventModifierFlags)mod
{
unsigned int rv = 0;
Expand Down Expand Up @@ -561,50 +574,52 @@ - (AvnInputModifiers)getModifiers:(NSEventModifierFlags)mod

- (BOOL)hasMarkedText
{
return [_markedText length] > 0;
return _markedRange.length > 0;
}

- (NSRange)markedRange
{
if([_markedText length] > 0)
return NSMakeRange(0, [_markedText length] - 1);
return NSMakeRange(NSNotFound, 0);
return _markedRange;
}

- (NSRange)selectedRange
{
return _selection;
return _selectedRange;
}

- (void)setMarkedText:(id)string selectedRange:(NSRange)selectedRange replacementRange:(NSRange)replacementRange
{
_lastKeyHandled = true;

NSString* markedText;

if([string isKindOfClass:[NSAttributedString class]])
{
_markedText = [[NSMutableAttributedString alloc] initWithAttributedString:string];
markedText = [string string];
}
else
{
_markedText = [[NSMutableAttributedString alloc] initWithString:string];
markedText = (NSString*) string;
}

if(!_parent->InputMethod->IsActive()){
return;
_markedRange = NSMakeRange(_selectedRange.location, [markedText length]);

if(_parent->InputMethod->IsActive()){
_parent->InputMethod->Client->SetPreeditText((char*)[markedText UTF8String]);
}

_parent->InputMethod->Client->SetPreeditText((char*)[_markedText.string UTF8String]);
}

- (void)unmarkText
{
[[_markedText mutableString] setString:@""];
if(_parent->InputMethod->IsActive()){
_parent->InputMethod->Client->SetPreeditText(nullptr);
}

[[self inputContext] discardMarkedText];
_markedRange = NSMakeRange(_selectedRange.location, 0);

if(!_parent->InputMethod->IsActive()){
return;
if([self inputContext]) {
[[self inputContext] discardMarkedText];
}

_parent->InputMethod->Client->SetPreeditText(nullptr);
}

- (NSArray<NSString *> *)validAttributesForMarkedText
Expand All @@ -614,19 +629,38 @@ - (void)unmarkText

- (NSAttributedString *)attributedSubstringForProposedRange:(NSRange)range actualRange:(NSRangePointer)actualRange
{
return nullptr;
if(actualRange){
range = *actualRange;
}

NSAttributedString* subString = [_text attributedSubstringFromRange:range];

return subString;
}

- (void)insertText:(id)string replacementRange:(NSRange)replacementRange
{
[self unmarkText];

if(_parent != nullptr)
if(_parent == nullptr){
return;
}

NSString* text;

if([string isKindOfClass:[NSAttributedString class]])
{
_lastKeyHandled = _parent->BaseEvents->RawTextInputEvent(0, [string UTF8String]);
text = [string string];
}
else
{
text = (NSString*) string;
}

[[self inputContext] invalidateCharacterCoordinates];
[self unmarkText];

uint32_t timestamp = static_cast<uint32_t>([NSDate timeIntervalSinceReferenceDate] * 1000);

_lastKeyHandled = _parent->BaseEvents->RawTextInputEvent(timestamp, [text UTF8String]);

}

- (NSUInteger)characterIndexForPoint:(NSPoint)point
Expand Down Expand Up @@ -746,15 +780,11 @@ - (id)accessibilityFocusedUIElement
}

- (void) setText:(NSString *)text{
[_text setString:text];

[[self inputContext] discardMarkedText];
[[_text mutableString] setString:text];
}

- (void) setSelection:(int)start :(int)end{
_selection = NSMakeRange(start, end - start);

[[self inputContext] invalidateCharacterCoordinates];
_selectedRange = NSMakeRange(start, end - start);
}

- (void) setCursorRect:(AvnRect)rect{
Expand All @@ -766,7 +796,9 @@ - (void) setCursorRect:(AvnRect)rect{

_cursorRect = windowRectOnScreen;

[[self inputContext] invalidateCharacterCoordinates];
if([self inputContext]) {
[[self inputContext] invalidateCharacterCoordinates];
}
}

@end