Cocoa: TBitBtn retina support. Issue #33713

git-svn-id: trunk@57928 -
This commit is contained in:
ondrej 2018-05-13 20:57:16 +00:00
parent e5ab08f99b
commit fda9deecd3
2 changed files with 19 additions and 2 deletions

View File

@ -26,6 +26,14 @@ uses
// Libs
MacOSAll, CocoaAll;
type
NSImageScaling = NSUInteger;
const // NSImageScaling values
NSImageScaleProportionallyDown = 0;
NSImageScaleAxesIndependently = 1;
NSImageScaleNone = 2;
NSImageScaleProportionallyUpOrDown = 3;
type
NSMenuFix = objccategory external (NSMenu)
function itemAtIndex(index: NSInteger): NSMenuItem; message 'itemAtIndex:';
@ -38,6 +46,7 @@ type
NSButtonSoundExtensionsCategory = objccategory external (NSButton)
function intrinsicContentSize(): NSSize; message 'intrinsicContentSize';
procedure setImageScaling(aScaling: NSImageScaling); message 'setImageScaling:';
end;
// The following dummy categories fix bugs in the Cocoa bindings available in FPC

View File

@ -125,6 +125,7 @@ var
AIndex: Integer;
AEffect: TGraphicsDrawEffect;
AImgRes: TScaledImageListResolution;
ImgSize: NSSize;
begin
//WriteLn('[TCocoaWSBitBtn.SetGlyph]');
Img := nil;
@ -132,13 +133,20 @@ begin
begin
AGlyph := TBitmap.Create;
AValue.GetImageIndexAndEffect(bsUp, ABitBtn.Font.PixelsPerInch,
1{To-Do: ABitBtn.GetCanvasScaleFactor}, AImgRes, AIndex, AEffect);
ABitBtn.GetCanvasScaleFactor, AImgRes, AIndex, AEffect);
AImgRes.GetBitmap(AIndex, AGlyph, AEffect);
Img := TCocoaBitmap(AGlyph.Handle).image;
if AImgRes.Resolution.ImageList.Scaled then // resize only if the image list is scaled
begin
ImgSize := Img.size;
ImgSize.height := ImgSize.height / ABitBtn.GetCanvasScaleFactor;
ImgSize.width := ImgSize.width / ABitBtn.GetCanvasScaleFactor;
Img.setSize(ImgSize);
end;
lButtonHandle := TCocoaButton(ABitBtn.Handle);
lButtonHandle.setImage(Img);
lButtonHandle.setImagePosition(LCLGlyphPosToCocoa(ABitBtn.Layout));
//To-Do: tell Cocoa lButtonHandle should not scale the image (and enable ABitBtn.GetCanvasScaleFactor above)
lButtonHandle.setImageScaling(NSImageScaleNone); // do not scale - retina scaling is done above with Img.setSize
if Assigned(lButtonHandle.Glyph) then
FreeAndNil(lButtonHandle.Glyph);
lButtonHandle.Glyph := AGlyph;