From 5f849396505039cf32a769ab67de5ed88439743d Mon Sep 17 00:00:00 2001 From: rich2014 Date: Fri, 16 Aug 2024 11:47:19 +0800 Subject: [PATCH] Cocoa: Focus Ring config for Button applied --- lcl/interfaces/cocoa/cocoabuttons.pas | 7 +++-- lcl/interfaces/cocoa/cocoawschecklst.pas | 2 +- lcl/interfaces/cocoa/cocoawsstdctrls.pas | 37 +++++++++++++++--------- 3 files changed, 29 insertions(+), 17 deletions(-) diff --git a/lcl/interfaces/cocoa/cocoabuttons.pas b/lcl/interfaces/cocoa/cocoabuttons.pas index 83109ab67e..83225eb5f8 100644 --- a/lcl/interfaces/cocoa/cocoabuttons.pas +++ b/lcl/interfaces/cocoa/cocoabuttons.pas @@ -23,8 +23,8 @@ unit CocoaButtons; interface uses - Types, Classes, SysUtils, Graphics, - MacOSAll, CocoaAll, CocoaConst, CocoaPrivate, CocoaCallback; + Types, Classes, SysUtils, Graphics, Controls, + MacOSAll, CocoaAll, CocoaConst, CocoaWSCommon, CocoaPrivate, CocoaCallback; const @@ -92,6 +92,8 @@ type procedure setState(astate: NSInteger); override; end; + TCocoaButtonNeedFocusRing = objcclass(TCocoaButton) + end; IStepperCallback = interface(ICommonCallback) procedure BeforeChange(var Allowed: Boolean); @@ -360,6 +362,7 @@ begin begin setTarget(Self); setAction(objcselector('actionButtonClick:')); + UpdateControlFocusRing( self, TWinControl(lclGetTarget) ); // todo: find a way to release notifications below // NSNotificationCenter.defaultCenter.addObserver_selector_name_object(Self, objcselector('boundsDidChange:'), NSViewBoundsDidChangeNotification, Result); // NSNotificationCenter.defaultCenter.addObserver_selector_name_object(Self, objcselector('frameDidChange:'), NSViewFrameDidChangeNotification, Result); diff --git a/lcl/interfaces/cocoa/cocoawschecklst.pas b/lcl/interfaces/cocoa/cocoawschecklst.pas index a4be59438e..d4a77905d7 100644 --- a/lcl/interfaces/cocoa/cocoawschecklst.pas +++ b/lcl/interfaces/cocoa/cocoawschecklst.pas @@ -245,7 +245,7 @@ begin scroll.setAutohidesScrollers(true); ScrollViewSetBorderStyle(scroll, TCustomCheckListBox(AWinControl).BorderStyle); - UpdateFocusRing(list, TCustomCheckListBox(AWinControl).BorderStyle); + UpdateControlFocusRing(list, AWinControl); Result := TLCLHandle(scroll); end; diff --git a/lcl/interfaces/cocoa/cocoawsstdctrls.pas b/lcl/interfaces/cocoa/cocoawsstdctrls.pas index 88fd09b45b..79e5b1a9cb 100644 --- a/lcl/interfaces/cocoa/cocoawsstdctrls.pas +++ b/lcl/interfaces/cocoa/cocoawsstdctrls.pas @@ -399,9 +399,17 @@ implementation function AllocButton(const ATarget: TWinControl; const ACallBackClass: TLCLButtonCallBackClass; const AParams: TCreateParams; btnBezel: NSBezelStyle; btnType: NSButtonType): TCocoaButton; begin - Result := TCocoaButton.alloc.lclInitWithCreateParams(AParams); - if Assigned(Result) then - begin + case btnType of + NSMomentaryLightButton, + NSMomentaryChangeButton, + NSMomentaryPushInButton: + Result:= TCocoaButton.alloc; + else + Result:= TCocoaButtonNeedFocusRing.alloc; + end; + + if Assigned(Result) then begin + Result:= Result.lclInitWithCreateParams(AParams); TCocoaButton(Result).callback := ACallBackClass.Create(Result, ATarget); Result.setTitle(ControlTitleToNSStr(AParams.Caption)); @@ -848,7 +856,7 @@ class function TCocoaWSButton.CreateHandle(const AWinControl: TWinControl; var btn: TCocoaButton; begin - btn := AllocButton(AWinControl, TLCLButtonCallback, AParams, NSRoundedBezelStyle, NSMomentaryPushInButton); + btn := AllocButton(AWinControl, TLCLButtonCallback, AParams, NSRoundedBezelStyle, NSMomentaryLightButton); btn.smallHeight := PUSHBTN_SMALL_HEIGHT; btn.miniHeight := PUSHBTN_MINI_HEIGHT; btn.adjustFontToControlSize:=true; @@ -1082,12 +1090,12 @@ end; class function TCocoaWSCustomEdit.CreateHandle(const AWinControl: TWinControl; const AParams: TCreateParams): TLCLHandle; var - field : NSTextField; + field : TCocoaTextField; cell : NSTextFieldCell; begin if TCustomEdit(AWinControl).PasswordChar=#0 - then field:=NSTextField(AllocTextField(AWinControl, AParams)) - else field:=NSTextField(AllocSecureTextField(AWinControl, AParams)); + then field:=TCocoaTextField(AllocTextField(AWinControl, AParams)) + else field:=TCocoaTextField(AllocSecureTextField(AWinControl, AParams)); if (field.respondsToSelector(ObjCSelector('cell'))) and Assigned(field.cell) then begin cell := NSTextFieldCell(field.cell); @@ -1097,7 +1105,8 @@ begin if NOT TCocoaTextField(field).fixedBorderStyle then TextFieldSetBorderStyle(field, TCustomEdit(AWinControl).BorderStyle); TextFieldSetAllignment(field, TCustomEdit(AWinControl).Alignment); - UpdateFocusRing(field, TCustomEdit(AWinControl).BorderStyle); + if NOT field.fixedBorderStyle then + UpdateControlFocusRing( field, AWinControl ); Result:=TLCLHandle(field); end; @@ -1142,7 +1151,7 @@ begin field.setBordered( ABorderStyle <> bsNone ); field.setBezeled( ABorderStyle <> bsNone ); {$endif} - UpdateFocusRing(field, ABorderStyle); + UpdateControlFocusRing( field, AWinControl ); end; class function TCocoaWSCustomEdit.GetSelStart(const ACustomEdit: TCustomEdit): integer; @@ -1637,7 +1646,7 @@ begin scr.setDrawsBackground(false); ScrollViewSetBorderStyle(scr, TCustomMemo(AWinControl).BorderStyle); - UpdateFocusRing(txt, TCustomMemo(AWinControl).BorderStyle); + scr.setFocusRingType( NSFocusRingTypeExterior ); nr:=scr.documentVisibleRect; txt.setFrame(nr); @@ -1660,7 +1669,7 @@ begin // This makes NSTextView to be responsive to theme color change (Mojave 10.14) txt.setTextColor(NSColor.textColor); txt.setBackgroundColor(NSColor.textBackgroundColor); - scr.setFocusRingType(NSFocusRingTypeExterior); + UpdateControlFocusRing(txt, AWinControl); lcl := TLCLCommonCallback.Create(txt, AWinControl); lcl.ForceReturnKeyDown := true; @@ -1730,7 +1739,7 @@ begin if not Assigned(sv) then Exit; ScrollViewSetBorderStyle(sv, ABorderStyle); - UpdateFocusRing(NSView(sv.documentView), ABorderStyle); + UpdateControlFocusRing(sv.documentView, AWinControl); end; class function TCocoaWSCustomMemo.GetCaretPos(const ACustomEdit: TCustomEdit): TPoint; @@ -2512,7 +2521,7 @@ begin scroll.setHasHorizontalScroller(true); scroll.setAutohidesScrollers(true); ScrollViewSetBorderStyle(scroll, lclListBox.BorderStyle); - UpdateFocusRing(list, lclListBox.BorderStyle); + UpdateControlFocusRing(list, lclListBox); Result := TLCLHandle(scroll); end; @@ -2634,7 +2643,7 @@ begin if not Assigned(list) then Exit; ScrollViewSetBorderStyle(list.enclosingScrollView, ABorderStyle); - UpdateFocusRing(list, ABorderStyle); + UpdateControlFocusRing(list, AWinControl); end; class procedure TCocoaWSCustomListBox.SetItemIndex(const ACustomListBox: TCustomListBox; const AIndex: integer);