Cocoa: IME: TCocoaCustomControl refactor

This commit is contained in:
rich2014 2023-07-06 21:47:58 +08:00
parent 2cff36a203
commit f605a02231

View File

@ -205,6 +205,8 @@ type
faileddraw : Boolean; faileddraw : Boolean;
_inIME: Boolean; _inIME: Boolean;
private
function getWindowEditor(): NSTextView; message 'getWindowEditor';
public public
callback: ICommonCallback; callback: ICommonCallback;
auxMouseByParent: Boolean; auxMouseByParent: Boolean;
@ -606,6 +608,11 @@ begin
Result:= NSString( aString ); Result:= NSString( aString );
end; end;
function TCocoaCustomControl.getWindowEditor(): NSTextView;
begin
Result:= NSTextView( self.window.fieldEditor_forObject(true,nil) );
end;
procedure TCocoaCustomControl.setStringValue(avalue: NSString); procedure TCocoaCustomControl.setStringValue(avalue: NSString);
begin begin
if Assigned(fstr) then fstr.release; if Assigned(fstr) then fstr.release;
@ -640,16 +647,16 @@ end;
procedure TCocoaCustomControl.keyDown(theEvent: NSEvent); procedure TCocoaCustomControl.keyDown(theEvent: NSEvent);
var var
view: NSView; textView: NSView;
isFirst: Boolean; isFirst: Boolean;
begin begin
isFirst:= not _inIME; isFirst:= not _inIME;
inputContext.handleEvent(theEvent); inputContext.handleEvent(theEvent);
if _inIME and isFirst then if _inIME and isFirst then
begin begin
view:= self.window.fieldEditor_forObject(true, nil); textView:= getWindowEditor();
view.setFrameSize( NSMakeSize(self.frame.size.width,16) ); textView.setFrameSize( NSMakeSize(self.frame.size.width,16) );
self.addSubView( view ); self.addSubView( textView );
end end
else if not _inIME then else if not _inIME then
inputContext.discardMarkedText; inputContext.discardMarkedText;
@ -658,12 +665,12 @@ end;
procedure TCocoaCustomControl.insertText_replacementRange(aString: id; procedure TCocoaCustomControl.insertText_replacementRange(aString: id;
replacementRange: NSRange); replacementRange: NSRange);
var var
textView: NSTextView; textView: NSView;
nsText: NSString; nsText: NSString;
begin begin
if not _inIME then exit; if not _inIME then exit;
textView:= NSTextView(self.window.fieldEditor_forObject(true,nil)); textView:= getWindowEditor();
if Assigned(textView) then if Assigned(textView) then
textView.removeFromSuperview; textView.removeFromSuperview;
@ -714,9 +721,9 @@ end;
function TCocoaCustomControl.selectedRange: NSRange; function TCocoaCustomControl.selectedRange: NSRange;
var var
textView: NSTextView; textView: NSText;
begin begin
textView:= NSTextView(self.window.fieldEditor_forObject(true,nil)); textView:= getWindowEditor();
if not Assigned(textView) then if not Assigned(textView) then
Result:= NSMakeRange( NSNotFound, 0 ) Result:= NSMakeRange( NSNotFound, 0 )
else else
@ -727,7 +734,7 @@ function TCocoaCustomControl.markedRange: NSRange;
var var
textView: NSTextView; textView: NSTextView;
begin begin
textView:= NSTextView(self.window.fieldEditor_forObject(true,nil)); textView:= getWindowEditor();
if not Assigned(textView) then if not Assigned(textView) then
Result:= NSMakeRange( NSNotFound, 0 ) Result:= NSMakeRange( NSNotFound, 0 )
else else