Browse Source

retina scaling of bcbutton glyphs

Johann ELSASS 4 years ago
parent
commit
7af04d6092

+ 10 - 3
lazpaint/dialog/uchoosecolorinterface.pas

@@ -747,6 +747,7 @@ procedure TChooseColorInterface.UpdateButtonLayout;
 var
   iconSize: Integer;
   tmpIcon: TBitmap;
+  scaling, invScaling: single;
 begin
   FButtonSize := round(FBarWidth);
   BCButton_AddToPalette.Width := FButtonSize;
@@ -770,17 +771,23 @@ begin
     BCButton_RemoveFromPalette.Left := BCButton_AddToPalette.Left + BCButton_AddToPalette.Width;
     BCButton_RemoveFromPalette.Top := round(FMargin / 2);
   end;
-  iconSize := FButtonSize-4;
-  if not Assigned(BCButton_AddToPalette.Glyph) or (BCButton_AddToPalette.Glyph.Width <> iconSize) then
+  scaling := Container.GetCanvasScaleFactor;
+  iconSize := round((FButtonSize-4)*scaling);
+  invScaling := 1/scaling;
+  if not Assigned(BCButton_AddToPalette.Glyph) or (BCButton_AddToPalette.Glyph.Width <> iconSize)
+     or (BCButton_AddToPalette.GlyphScale <> invScaling) then
   begin
     tmpIcon:= MakeAddIcon(iconSize);
     BCButton_AddToPalette.Glyph.Assign(tmpIcon);
+    BCButton_AddToPalette.GlyphScale:= invScaling;
     tmpIcon.Free;
   end;
-  if not Assigned(BCButton_RemoveFromPalette.Glyph) or (BCButton_RemoveFromPalette.Glyph.Width <> iconSize) then
+  if not Assigned(BCButton_RemoveFromPalette.Glyph) or (BCButton_RemoveFromPalette.Glyph.Width <> iconSize)
+     or (BCButton_RemoveFromPalette.GlyphScale <> invScaling) then
   begin
     tmpIcon:= MakeRemoveIcon(iconSize);
     BCButton_RemoveFromPalette.Glyph.Assign(tmpIcon);
+    BCButton_RemoveFromPalette.GlyphScale:= invScaling;
     tmpIcon.Free;
   end;
 end;

+ 2 - 0
lazpaint/umainformlayout.pas

@@ -767,6 +767,8 @@ begin
   HideToolboxGroups;
   if Assigned(FMenu) then
     FMenu.RepaintToolbar;
+  if Assigned(FPaletteToolbar) then
+    FPaletteToolbar.Arrange;
 end;
 
 procedure TMainFormLayout.CheckDelayedUpdate;

+ 35 - 19
lazpaint/upalettetoolbar.pas

@@ -83,10 +83,12 @@ type
       {%H-}Shift: TShiftState; X, Y: Integer);
     procedure CreatePopupMenu;
     procedure ApplyTheme;
+    procedure ComputeMenuButtonGlyph;
     property PanelPalette: TBGRAVirtualScreen read GetPanelPalette;
   public
     constructor Create;
     destructor Destroy; override;
+    procedure Arrange;
     procedure AddColor(AColor: TBGRAPixel);
     procedure RemoveColor(AColor: TBGRAPixel);
     procedure SetBounds(ALeft,ATop,AWidth,AHeight: integer);
@@ -286,6 +288,34 @@ begin
     DarkThemeInstance.Apply(FMenuButton, DarkTheme);
 end;
 
+procedure TPaletteToolbar.ComputeMenuButtonGlyph;
+var
+  glyphBmp: TBitmap;
+  size: Integer;
+  glyphScale: single;
+begin
+  glyphScale := 1/UGraph.CanvasScale;
+  if Assigned(FMenuButton.Glyph) and not FMenuButton.Glyph.Empty and
+    (FMenuButton.GlyphScale = glyphScale) then exit;
+  glyphBmp := TBitmap.Create;
+  size := DoScaleY(FPaletteItemHeight*3 div 5, OriginalDPI, Screen.PixelsPerInch * UGraph.CanvasScale);
+  if not odd(size) then size += 1;
+  glyphBmp.Width := size;
+  glyphBmp.Height := size;
+  glyphBmp.Canvas.Pen.Color := clBlack;
+  glyphBmp.Canvas.Brush.Color := BGRAToColor(CSSLawnGreen);
+  glyphBmp.Canvas.Rectangle(0,0,glyphBmp.Width div 2+1,glyphBmp.Height div 2+1);
+  glyphBmp.Canvas.Brush.Color := clYellow;
+  glyphBmp.Canvas.Rectangle(glyphBmp.Width div 2,0,glyphBmp.Width,glyphBmp.Height div 2+1);
+  glyphBmp.Canvas.Brush.Color := BGRAToColor(CSSDodgerBlue);
+  glyphBmp.Canvas.Rectangle(0,glyphBmp.Height div 2,glyphBmp.Width div 2+1,glyphBmp.Height);
+  glyphBmp.Canvas.Brush.Color := BGRAToColor(CSSBlue);
+  glyphBmp.Canvas.Rectangle(glyphBmp.Width div 2,glyphBmp.Height div 2,glyphBmp.Width,glyphBmp.Height);
+  FMenuButton.Glyph := glyphBmp;
+  FMenuButton.GlyphScale := glyphScale;
+  glyphBmp.Free;
+end;
+
 procedure TPaletteToolbar.DoClearPalette(Sender: TObject);
 begin
   FColors.Clear;
@@ -467,9 +497,6 @@ begin
 end;
 
 function TPaletteToolbar.GetPanelPalette: TBGRAVirtualScreen;
-var
-  glyphBmp: TBitmap;
-  size: integer;
 begin
   if not Assigned(FPanelPalette) then
   begin
@@ -500,22 +527,6 @@ begin
     DarkThemeInstance.Apply(FMenuButton, DarkTheme);
     FMenuButton.DropDownArrow := true;
     FMenuButton.DropDownArrowSize := DoScaleY(FPaletteItemHeight div 2, OriginalDPI);
-    glyphBmp := TBitmap.Create;
-    size := DoScaleY(FPaletteItemHeight*3 div 5, OriginalDPI);
-    if not odd(size) then size += 1;
-    glyphBmp.Width := size;
-    glyphBmp.Height := size;
-    glyphBmp.Canvas.Pen.Color := clBlack;
-    glyphBmp.Canvas.Brush.Color := BGRAToColor(CSSLawnGreen);
-    glyphBmp.Canvas.Rectangle(0,0,glyphBmp.Width div 2+1,glyphBmp.Height div 2+1);
-    glyphBmp.Canvas.Brush.Color := clYellow;
-    glyphBmp.Canvas.Rectangle(glyphBmp.Width div 2,0,glyphBmp.Width,glyphBmp.Height div 2+1);
-    glyphBmp.Canvas.Brush.Color := BGRAToColor(CSSDodgerBlue);
-    glyphBmp.Canvas.Rectangle(0,glyphBmp.Height div 2,glyphBmp.Width div 2+1,glyphBmp.Height);
-    glyphBmp.Canvas.Brush.Color := BGRAToColor(CSSBlue);
-    glyphBmp.Canvas.Rectangle(glyphBmp.Width div 2,glyphBmp.Height div 2,glyphBmp.Width,glyphBmp.Height);
-    FMenuButton.Glyph := glyphBmp;
-    glyphBmp.Free;
     FMenuButton.DropDownStyle := bdsCommon;
     FMenuButton.Caption := '';
     FMenuButton.SetBounds(2,2,FPanelPalette.Width-4, DoScaleY(FPaletteItemHeight*4 div 3, OriginalDPI));
@@ -855,6 +866,11 @@ begin
   inherited Destroy;
 end;
 
+procedure TPaletteToolbar.Arrange;
+begin
+  ComputeMenuButtonGlyph;
+end;
+
 procedure TPaletteToolbar.AddColor(AColor: TBGRAPixel);
 begin
   if not FTransparentPalette then