Spec icon indicating copy to clipboard operation
Spec copied to clipboard

The border color specified in a stylesheet is not applied to an image presenter

Open koendehondt opened this issue 1 year ago • 3 comments

Try this in a Playground:

application := SpApplication new
	addStyleSheetFromString: '.application [
		.yellowBorder [
			Draw { #backgroundColor: #green},
			Container { #borderColor: #yellow, #borderWidth: 3 } ]
	]';
	yourself.
presenter := SpPresenter newApplication: application.
image := presenter newImage
	image: (self iconNamed: #grayCircle);
	addStyle: 'yellowBorder';
	yourself.
layout := SpBoxLayout newTopToBottom
	vAlignCenter;
	hAlignCenter;
	add: image;
	yourself.
presenter layout: layout.
presenter open

This snippet opens this window:

Screenshot 2024-11-13 at 12 46 18

Expected and ok:

  • Green background color
  • Border width of 3 pixels.

Expected, but not ok:

  • The border should be yellow. It is color B5B5B5.

koendehondt avatar Nov 13 '24 11:11 koendehondt

The root cause of the issue is the implementation of ImageMorph>>#adoptPaneColor::

adoptPaneColor: paneColor
	"Change our border color too."

	super adoptPaneColor: paneColor.
	paneColor ifNil: [^self].
	self borderStyle baseColor: paneColor twiceDarker

This method sets the baseColor of self borderStyle. Let's look at Morph>>#borderStyle:

borderStyle

	^ extension
		ifNil: [BorderStyle default trackColorFrom: self]
		ifNotNil: [:ext | (ext borderStyle ifNil: [BorderStyle default]) trackColorFrom: self]

Note that this method answers a new BorderStyle if there is no extension or if the extension does not have a borderStyle. In those situations, self borderStyle baseColor: paneColor twiceDarker in ImageMorph>>#adoptPaneColor: has no visual effect (because the new border style is not set in the morph).

However, if the morph does have an extension with a borderStyle, then self borderStyle baseColor: paneColor twiceDarker changes the baseColor, which will be used when drawing the morph. That is what we see in the screenshot: the border is gray. In the example code of the reported issue, the morph has a borderStyle which holds a width of 3 pixels and a yellow baseColor because the style Container { #borderColor: #yellow, #borderWidth: 3 } defines the border style.

So the implementation of ImageMorph>>#adoptPaneColor: is wrong. In case no style has been applied to a SpImagePresenter, the method has no effect. In case a style applies a borderColor, the method does not apply the style's borderColor, but changes it to paneColor twiceDarker instead.

In conclusion, the method can be removed. It will fix the issue and it has no consequences if no style has been applied.

koendehondt avatar Nov 29 '24 14:11 koendehondt

This is the result of the code snippet after removing ImageMorph>>#adoptPaneColor:, which is what we expect with the given stylesheet:

Screenshot 2024-11-29 at 15 01 54

koendehondt avatar Nov 29 '24 14:11 koendehondt

Thanks Koen for the analysis and the fix!

Ducasse avatar Dec 01 '24 12:12 Ducasse