Cocoa: improve ComboBox with OwnerDraw (especially TColorBox in Dark)

This commit is contained in:
rich2014 2024-11-01 18:55:11 +08:00
parent 3a9ae5b7ee
commit 9af9ef8a87
2 changed files with 18 additions and 15 deletions

View File

@ -47,7 +47,8 @@ const
COMBOBOX_RO_SMALL_HEIGHT = 17;
COMBOBOX_RO_MINI_HEIGHT = 15;
COMBOBOX_RO_BUTTON_WIDTH = 20;
COMBOBOX_RO_ROUND_SIZE = 7;
COMBOBOX_RO_BUTTON_WIDTH = 18;
type
@ -214,7 +215,7 @@ type
procedure GetRowHeight(rowidx: integer; var h: Integer);
procedure ComboBoxDrawItem(itemIndex: Integer; ctx: TCocoaContext;
const r: TRect; isSelected: Boolean);
const r: TRect; isSelected: Boolean; backgroundPainted: Boolean );
end;
{ TCocoaComboBoxItemCell }
@ -702,7 +703,7 @@ begin
try
ctxRect:= NSRectToRect( bounds );
ctx.InitDraw( ctxRect.Width, ctxRect.Height );
combobox.callback.ComboBoxDrawItem(itemIndex, ctx, ctxRect, isHighlighted);
combobox.callback.ComboBoxDrawItem(itemIndex, ctx, ctxRect, isHighlighted, false);
finally
ctx.Free;
end;
@ -1738,19 +1739,20 @@ begin
// (however, one should be careful and take layout offsets into account!)
// on the other hand, "cells" themselves are being deprecated...
dr := lclFrame;
dr.Width:= dr.Width - COMBOBOX_RO_BUTTON_WIDTH;
// crop the drawing rectangle according to the
// rounded corners and popup button of the ComboBox.
// the APP can get better effect by drawing in the cropped rectangle.
// the APP can also expand the rectangle if it knows what it is doing.
Types.OffsetRect(dr, -dr.Left, -dr.Top);
//SubLayoutFromFrame( lclGetFrameToLayoutDelta, dr);
inc( dr.Left, COMBOBOX_RO_ROUND_SIZE );
inc( dr.Top, 2 );
dec( dr.Right, COMBOBOX_RO_BUTTON_WIDTH );
inc( dr.Bottom, 1 );
ctx.InitDraw(dr.Width, dr.Height);
// magic offsets are based on the macOS 10.13.6 visual style
// but hard-coding is never reliable
//inc(dr.Left, 11);
//inc(dr.Top, 5);
//dec(dr.Right,18);
//dec(dr.Bottom, 2);
callback.ComboBoxDrawItem(lastSelectedItemIndex, ctx, dr, false);
callback.ComboBoxDrawItem(lastSelectedItemIndex, ctx, dr, False, True);
finally
ctx.Free;
end;

View File

@ -88,7 +88,7 @@ type
procedure GetRowHeight(rowidx: integer; var h: Integer);
procedure ComboBoxDrawItem(itemIndex: Integer; ctx: TCocoaContext;
const r: TRect; isSelected: Boolean);
const r: TRect; isSelected: Boolean; backgroundPainted: Boolean);
end;
{ TCocoaWSCustomComboBox }
@ -860,7 +860,7 @@ begin
end;
procedure TLCLComboboxCallback.ComboBoxDrawItem(itemIndex: Integer;
ctx: TCocoaContext; const r: TRect; isSelected: Boolean);
ctx: TCocoaContext; const r: TRect; isSelected: Boolean; backgroundPainted: Boolean);
var
itemstruct: TDrawListItemStruct;
begin
@ -871,6 +871,7 @@ begin
if isSelected then Include(itemstruct.ItemState, odSelected);
// we don't distingiush at the moment
if isSelected then Include(itemstruct.ItemState, odFocused);
if backgroundPainted then Include(itemstruct.ItemState, odBackgroundPainted);
LCLSendDrawListItemMsg(Target, @itemstruct);
end;