refactor: make templateImage a property on nativeImage (#18124)

* refactor: make templateImage a property on nativeImage

* Update docs/api/native-image.md

Co-Authored-By: codebytere <codebytere@github.com>

* fix nativeImage prototype deprecation

* update for new property name

* Update docs/api/native-image.md

Co-Authored-By: codebytere <codebytere@github.com>
This commit is contained in:
Shelley Vohr 2019-05-07 06:52:07 -07:00 committed by John Kleinschmidt
parent cfb6e847a0
commit 02710ef574
6 changed files with 50 additions and 6 deletions

View file

@ -638,8 +638,10 @@ void NativeImage::BuildPrototype(v8::Isolate* isolate,
.SetMethod("toDataURL", &NativeImage::ToDataURL) .SetMethod("toDataURL", &NativeImage::ToDataURL)
.SetMethod("isEmpty", &NativeImage::IsEmpty) .SetMethod("isEmpty", &NativeImage::IsEmpty)
.SetMethod("getSize", &NativeImage::GetSize) .SetMethod("getSize", &NativeImage::GetSize)
.SetMethod("setTemplateImage", &NativeImage::SetTemplateImage) .SetMethod("_setTemplateImage", &NativeImage::SetTemplateImage)
.SetMethod("isTemplateImage", &NativeImage::IsTemplateImage) .SetMethod("_isTemplateImage", &NativeImage::IsTemplateImage)
.SetProperty("isMacTemplateImage", &NativeImage::IsTemplateImage,
&NativeImage::SetTemplateImage)
.SetMethod("resize", &NativeImage::Resize) .SetMethod("resize", &NativeImage::Resize)
.SetMethod("crop", &NativeImage::Crop) .SetMethod("crop", &NativeImage::Crop)
.SetMethod("getAspectRatio", &NativeImage::GetAspectRatio) .SetMethod("getAspectRatio", &NativeImage::GetAspectRatio)

View file

@ -30,8 +30,6 @@ The Electron team is currently undergoing an initiative to convert separate gett
* `DownloadItem` class * `DownloadItem` class
* `savePath` * `savePath`
* `paused` * `paused`
* `NativeImage`
* `templateImage`
* `Session` module * `Session` module
* `preloads` * `preloads`
* `SystemPreferences` module * `SystemPreferences` module
@ -58,3 +56,5 @@ The Electron team is currently undergoing an initiative to convert separate gett
* `applicationMenu` * `applicationMenu`
* `badgeCount` * `badgeCount`
* `name` * `name`
* `NativeImage`
* `isMacTemplateImage`

View file

@ -71,7 +71,6 @@ images/
└── icon@3x.png └── icon@3x.png
``` ```
```javascript ```javascript
const { Tray } = require('electron') const { Tray } = require('electron')
let appIcon = new Tray('/Users/somebody/images/icon.png') let appIcon = new Tray('/Users/somebody/images/icon.png')
@ -276,10 +275,14 @@ Returns [`Size`](structures/size.md)
Marks the image as a template image. Marks the image as a template image.
**[Deprecated Soon](modernization/property-updates.md)**
#### `image.isTemplateImage()` #### `image.isTemplateImage()`
Returns `Boolean` - Whether the image is a template image. Returns `Boolean` - Whether the image is a template image.
**[Deprecated Soon](modernization/property-updates.md)**
#### `image.crop(rect)` #### `image.crop(rect)`
* `rect` [Rectangle](structures/rectangle.md) - The area of the image to crop. * `rect` [Rectangle](structures/rectangle.md) - The area of the image to crop.
@ -324,3 +327,11 @@ to explicitly add different scale factor representations to an image. This
can be called on empty images. can be called on empty images.
[buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer [buffer]: https://nodejs.org/api/buffer.html#buffer_class_buffer
## Properties
### `nativeImage.isMacTemplateImage` _macOS_
A `Boolean` property that determines whether the image is considered a [template image](https://developer.apple.com/documentation/appkit/nsimage/1520017-template).
Please note that this property only has an effect on macOS.

View file

@ -1,5 +1,8 @@
'use strict' 'use strict'
const { nativeImage } = process.electronBinding('native_image') const { deprecate } = require('electron')
const { NativeImage, nativeImage } = process.electronBinding('native_image')
deprecate.fnToProperty(NativeImage.prototype, 'isMacTemplateImage', '_isTemplateImage', '_setTemplateImage')
module.exports = nativeImage module.exports = nativeImage

View file

@ -105,6 +105,34 @@ describe('nativeImage module', () => {
return matchingImage return matchingImage
} }
describe('isMacTemplateImage property', () => {
before(function () {
if (process.platform !== 'darwin') this.skip()
})
it('returns whether the image is a template image', () => {
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
expect(image.isMacTemplateImage).to.be.a('boolean')
expect(image.isTemplateImage).to.be.a('function')
expect(image.setTemplateImage).to.be.a('function')
})
it('correctly recognizes a template image', () => {
const templateImage = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo_Template.png'))
expect(templateImage.isMacTemplateImage).to.be.true()
})
it('sets a template image', function () {
const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png'))
expect(image.isMacTemplateImage).to.be.false()
image.isMacTemplateImage = true
expect(image.isMacTemplateImage).to.be.true()
})
})
describe('createEmpty()', () => { describe('createEmpty()', () => {
it('returns an empty image', () => { it('returns an empty image', () => {
const empty = nativeImage.createEmpty() const empty = nativeImage.createEmpty()

BIN
spec/fixtures/assets/logo_Template.png vendored Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 14 KiB