From 674e4a9fdde4ab690b68ea4aef2b6643aa915990 Mon Sep 17 00:00:00 2001 From: Shelley Vohr Date: Tue, 17 Mar 2020 18:06:52 -0700 Subject: [PATCH] chore: more modules to dual prop/fn support (#22688) --- docs/api/download-item.md | 4 -- docs/api/native-image.md | 4 -- docs/api/system-preferences.md | 8 +-- lib/browser/api/system-preferences.ts | 25 +++++---- lib/browser/api/web-contents.js | 2 +- lib/common/api/native-image.js | 5 +- .../api/electron_api_system_preferences.cc | 11 ++-- shell/common/api/electron_api_native_image.cc | 4 +- spec-main/api-system-preferences-spec.ts | 45 ++++++++++++++-- spec/api-native-image-spec.js | 51 +++++++++++-------- 10 files changed, 91 insertions(+), 68 deletions(-) diff --git a/docs/api/download-item.md b/docs/api/download-item.md index a52a9b978a63..866b9a57fbea 100644 --- a/docs/api/download-item.md +++ b/docs/api/download-item.md @@ -82,16 +82,12 @@ The API is only available in session's `will-download` callback function. If user doesn't set the save path via the API, Electron will use the original routine to determine the save path; this usually prompts a save dialog. -**[Deprecated](modernization/property-updates.md): use the `savePath` property instead.** - #### `downloadItem.getSavePath()` Returns `String` - The save path of the download item. This will be either the path set via `downloadItem.setSavePath(path)` or the path selected from the shown save dialog. -**[Deprecated](modernization/property-updates.md): use the `savePath` property instead.** - #### `downloadItem.setSaveDialogOptions(options)` * `options` SaveDialogOptions - Set the save file dialog options. This object has the same diff --git a/docs/api/native-image.md b/docs/api/native-image.md index edd466f56fe0..05c86ffb0a2c 100644 --- a/docs/api/native-image.md +++ b/docs/api/native-image.md @@ -276,14 +276,10 @@ Returns [`Size`](structures/size.md) Marks the image as a template image. -**[Deprecated](modernization/property-updates.md)** - #### `image.isTemplateImage()` Returns `Boolean` - Whether the image is a template image. -**[Deprecated](modernization/property-updates.md)** - #### `image.crop(rect)` * `rect` [Rectangle](structures/rectangle.md) - The area of the image to crop. diff --git a/docs/api/system-preferences.md b/docs/api/system-preferences.md index 9816acdadc10..64b446cbea63 100644 --- a/docs/api/system-preferences.md +++ b/docs/api/system-preferences.md @@ -360,7 +360,7 @@ Returns `Boolean` - `true` if an inverted color scheme (a high contrast color sc Returns `Boolean` - `true` if a high contrast theme is active, `false` otherwise. -**Depreacted:** Should use the new [`nativeTheme.shouldUseHighContrastColors`](native-theme.md#nativethemeshouldusehighcontrastcolors-macos-windows-readonly) API. +**Deprecated:** Should use the new [`nativeTheme.shouldUseHighContrastColors`](native-theme.md#nativethemeshouldusehighcontrastcolors-macos-windows-readonly) API. ### `systemPreferences.getEffectiveAppearance()` _macOS_ @@ -369,8 +369,6 @@ Returns `String` - Can be `dark`, `light` or `unknown`. Gets the macOS appearance setting that is currently applied to your application, maps to [NSApplication.effectiveAppearance](https://developer.apple.com/documentation/appkit/nsapplication/2967171-effectiveappearance?language=objc) -**[Deprecated](modernization/property-updates.md)** - ### `systemPreferences.getAppLevelAppearance()` _macOS_ _Deprecated_ Returns `String` | `null` - Can be `dark`, `light` or `unknown`. @@ -379,8 +377,6 @@ Gets the macOS appearance setting that you have declared you want for your application, maps to [NSApplication.appearance](https://developer.apple.com/documentation/appkit/nsapplication/2967170-appearance?language=objc). You can use the `setAppLevelAppearance` API to set this value. -**[Deprecated](modernization/property-updates.md)** - ### `systemPreferences.setAppLevelAppearance(appearance)` _macOS_ _Deprecated_ * `appearance` String | null - Can be `dark` or `light` @@ -388,8 +384,6 @@ You can use the `setAppLevelAppearance` API to set this value. Sets the appearance setting for your application, this should override the system default and override the value of `getEffectiveAppearance`. -**[Deprecated](modernization/property-updates.md)** - ### `systemPreferences.canPromptTouchID()` _macOS_ Returns `Boolean` - whether or not this device has the ability to use Touch ID. diff --git a/lib/browser/api/system-preferences.ts b/lib/browser/api/system-preferences.ts index 3de4732de05c..e052a016bc93 100644 --- a/lib/browser/api/system-preferences.ts +++ b/lib/browser/api/system-preferences.ts @@ -6,21 +6,20 @@ const { systemPreferences, SystemPreferences } = process.electronBinding('system Object.setPrototypeOf(SystemPreferences.prototype, EventEmitter.prototype) EventEmitter.call(systemPreferences) -if ('appLevelAppearance' in systemPreferences) { - deprecate.fnToProperty( - SystemPreferences.prototype, - 'appLevelAppearance', - '_getAppLevelAppearance', - '_setAppLevelAppearance' - ) +if ('getAppLevelAppearance' in systemPreferences) { + const nativeALAGetter = systemPreferences.getAppLevelAppearance + const nativeALASetter = systemPreferences.setAppLevelAppearance + Object.defineProperty(SystemPreferences.prototype, 'appLevelAppearance', { + get: () => nativeALAGetter.call(systemPreferences), + set: (appearance) => nativeALASetter.call(systemPreferences, appearance) + }) } -if ('effectiveAppearance' in systemPreferences) { - deprecate.fnToProperty( - SystemPreferences.prototype, - 'effectiveAppearance', - '_getEffectiveAppearance' - ) +if ('getEffectiveAppearance' in systemPreferences) { + const nativeEAGetter = systemPreferences.getAppLevelAppearance + Object.defineProperty(SystemPreferences.prototype, 'effectiveAppearance', { + get: () => nativeEAGetter.call(systemPreferences) + }) } SystemPreferences.prototype.isDarkMode = deprecate.moveAPI( diff --git a/lib/browser/api/web-contents.js b/lib/browser/api/web-contents.js index 04e308bfff56..a77c23d858e6 100644 --- a/lib/browser/api/web-contents.js +++ b/lib/browser/api/web-contents.js @@ -5,7 +5,7 @@ const { EventEmitter } = require('events') const electron = require('electron') const path = require('path') const url = require('url') -const { app, ipcMain, session, deprecate } = electron +const { app, ipcMain, session } = electron const { internalWindowOpen } = require('@electron/internal/browser/guest-window-manager') const NavigationController = require('@electron/internal/browser/navigation-controller') diff --git a/lib/common/api/native-image.js b/lib/common/api/native-image.js index 7c7737c7f1b8..f1f2d51d102e 100644 --- a/lib/common/api/native-image.js +++ b/lib/common/api/native-image.js @@ -1,8 +1,5 @@ 'use strict' -const { deprecate } = require('electron') -const { NativeImage, nativeImage } = process.electronBinding('native_image') - -deprecate.fnToProperty(NativeImage.prototype, 'isMacTemplateImage', '_isTemplateImage', '_setTemplateImage') +const { nativeImage } = process.electronBinding('native_image') module.exports = nativeImage diff --git a/shell/browser/api/electron_api_system_preferences.cc b/shell/browser/api/electron_api_system_preferences.cc index f7855d77a653..7fe7a02e0bbd 100644 --- a/shell/browser/api/electron_api_system_preferences.cc +++ b/shell/browser/api/electron_api_system_preferences.cc @@ -99,17 +99,12 @@ void SystemPreferences::BuildPrototype( .SetMethod("removeUserDefault", &SystemPreferences::RemoveUserDefault) .SetMethod("isSwipeTrackingFromScrollEventsEnabled", &SystemPreferences::IsSwipeTrackingFromScrollEventsEnabled) - .SetMethod("_getEffectiveAppearance", + .SetMethod("getEffectiveAppearance", &SystemPreferences::GetEffectiveAppearance) - .SetMethod("_getAppLevelAppearance", + .SetMethod("getAppLevelAppearance", &SystemPreferences::GetAppLevelAppearance) - .SetMethod("_setAppLevelAppearance", + .SetMethod("setAppLevelAppearance", &SystemPreferences::SetAppLevelAppearance) - .SetProperty("appLevelAppearance", - &SystemPreferences::GetAppLevelAppearance, - &SystemPreferences::SetAppLevelAppearance) - .SetProperty("effectiveAppearance", - &SystemPreferences::GetEffectiveAppearance) .SetMethod("getSystemColor", &SystemPreferences::GetSystemColor) .SetMethod("canPromptTouchID", &SystemPreferences::CanPromptTouchID) .SetMethod("promptTouchID", &SystemPreferences::PromptTouchID) diff --git a/shell/common/api/electron_api_native_image.cc b/shell/common/api/electron_api_native_image.cc index 7c37234636f3..fc265b1f5e55 100644 --- a/shell/common/api/electron_api_native_image.cc +++ b/shell/common/api/electron_api_native_image.cc @@ -510,8 +510,8 @@ void NativeImage::BuildPrototype(v8::Isolate* isolate, .SetMethod("toDataURL", &NativeImage::ToDataURL) .SetMethod("isEmpty", &NativeImage::IsEmpty) .SetMethod("getSize", &NativeImage::GetSize) - .SetMethod("_setTemplateImage", &NativeImage::SetTemplateImage) - .SetMethod("_isTemplateImage", &NativeImage::IsTemplateImage) + .SetMethod("setTemplateImage", &NativeImage::SetTemplateImage) + .SetMethod("isTemplateImage", &NativeImage::IsTemplateImage) .SetProperty("isMacTemplateImage", &NativeImage::IsTemplateImage, &NativeImage::SetTemplateImage) .SetMethod("resize", &NativeImage::Resize) diff --git a/spec-main/api-system-preferences-spec.ts b/spec-main/api-system-preferences-spec.ts index ea0d53c528fa..4e85fb22842c 100644 --- a/spec-main/api-system-preferences-spec.ts +++ b/spec-main/api-system-preferences-spec.ts @@ -187,12 +187,47 @@ describe('systemPreferences module', () => { }) ifdescribe(process.platform === 'darwin')('systemPreferences.appLevelAppearance', () => { - it('has an appLevelAppearance property', () => { - expect(systemPreferences).to.have.property('appLevelAppearance') + const options = ['dark', 'light', 'unknown', null] + describe('with properties', () => { + it('returns a valid appearance', () => { + const appearance = systemPreferences.appLevelAppearance + expect(options).to.include(appearance) + }) - // TODO(codebytere): remove when propertyification is complete - expect(systemPreferences.setAppLevelAppearance).to.be.a('function') - expect(() => { systemPreferences.getAppLevelAppearance() }).to.not.throw() + it('can be changed', () => { + systemPreferences.appLevelAppearance = 'dark' + expect(systemPreferences.appLevelAppearance).to.eql('dark') + }) + }) + + describe('with functions', () => { + it('returns a valid appearance', () => { + const appearance = systemPreferences.getAppLevelAppearance() + expect(options).to.include(appearance) + }) + + it('can be changed', () => { + systemPreferences.setAppLevelAppearance('dark') + const appearance = systemPreferences.getAppLevelAppearance() + expect(appearance).to.eql('dark') + }) + }) + }) + + ifdescribe(process.platform === 'darwin')('systemPreferences.effectiveAppearance', () => { + const options = ['dark', 'light', 'unknown'] + describe('with properties', () => { + it('returns a valid appearance', () => { + const appearance = systemPreferences.effectiveAppearance + expect(options).to.include(appearance) + }) + }) + + describe('with functions', () => { + it('returns a valid appearance', () => { + const appearance = systemPreferences.getEffectiveAppearance() + expect(options).to.include(appearance) + }) }) }) diff --git a/spec/api-native-image-spec.js b/spec/api-native-image-spec.js index e515a3c89ef7..7d772bdbcdf1 100644 --- a/spec/api-native-image-spec.js +++ b/spec/api-native-image-spec.js @@ -2,6 +2,7 @@ const { expect } = require('chai') const { nativeImage } = require('electron') +const { ifdescribe, ifit } = require('./spec-helpers') const path = require('path') describe('nativeImage module', () => { @@ -101,31 +102,41 @@ describe('nativeImage module', () => { return matchingImage } - describe('isMacTemplateImage property', () => { - before(function () { - if (process.platform !== 'darwin') this.skip() + ifdescribe(process.platform === 'darwin')('isMacTemplateImage state', () => { + describe('with properties', () => { + it('correctly recognizes a template image', () => { + const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png')) + expect(image.isMacTemplateImage).to.be.false() + + 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() + }) }) - it('returns whether the image is a template image', () => { - const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png')) + describe('with functions', () => { + it('correctly recognizes a template image', () => { + const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png')) + expect(image.isTemplateImage()).to.be.false() - expect(image.isMacTemplateImage).to.be.a('boolean') + const templateImage = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo_Template.png')) + expect(templateImage.isTemplateImage()).to.be.true() + }) - expect(image.isTemplateImage).to.be.a('function') - expect(image.setTemplateImage).to.be.a('function') - }) + it('sets a template image', function () { + const image = nativeImage.createFromPath(path.join(__dirname, 'fixtures', 'assets', 'logo.png')) + expect(image.isTemplateImage()).to.be.false() - 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() + image.setTemplateImage(true) + expect(image.isTemplateImage()).to.be.true() + }) }) })