feat: promisify app.getFileIcon() (#15742)
This commit is contained in:
parent
cfbea4a0e3
commit
3f15f51615
5 changed files with 72 additions and 68 deletions
|
@ -536,17 +536,15 @@ int ImportIntoCertStore(CertificateManagerModel* model,
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
void OnIconDataAvailable(v8::Isolate* isolate,
|
void OnIconDataAvailable(v8::Isolate* isolate,
|
||||||
const App::FileIconCallback& callback,
|
scoped_refptr<util::Promise> promise,
|
||||||
gfx::Image* icon) {
|
gfx::Image* icon) {
|
||||||
v8::Locker locker(isolate);
|
v8::Locker locker(isolate);
|
||||||
v8::HandleScope handle_scope(isolate);
|
v8::HandleScope handle_scope(isolate);
|
||||||
|
|
||||||
if (icon && !icon->IsEmpty()) {
|
if (icon && !icon->IsEmpty()) {
|
||||||
callback.Run(v8::Null(isolate), *icon);
|
promise->Resolve(*icon);
|
||||||
} else {
|
} else {
|
||||||
v8::Local<v8::String> error_message =
|
promise->RejectWithErrorMessage("Failed to get file icon.");
|
||||||
v8::String::NewFromUtf8(isolate, "Failed to get file icon.");
|
|
||||||
callback.Run(v8::Exception::Error(error_message), gfx::Image());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1125,16 +1123,16 @@ JumpListResult App::SetJumpList(v8::Local<v8::Value> val,
|
||||||
}
|
}
|
||||||
#endif // defined(OS_WIN)
|
#endif // defined(OS_WIN)
|
||||||
|
|
||||||
void App::GetFileIcon(const base::FilePath& path, mate::Arguments* args) {
|
v8::Local<v8::Promise> App::GetFileIcon(const base::FilePath& path,
|
||||||
mate::Dictionary options;
|
mate::Arguments* args) {
|
||||||
IconLoader::IconSize icon_size;
|
|
||||||
FileIconCallback callback;
|
|
||||||
|
|
||||||
v8::Locker locker(isolate());
|
v8::Locker locker(isolate());
|
||||||
v8::HandleScope handle_scope(isolate());
|
v8::HandleScope handle_scope(isolate());
|
||||||
|
|
||||||
|
scoped_refptr<util::Promise> promise = new util::Promise(isolate());
|
||||||
base::FilePath normalized_path = path.NormalizePathSeparators();
|
base::FilePath normalized_path = path.NormalizePathSeparators();
|
||||||
|
|
||||||
|
IconLoader::IconSize icon_size;
|
||||||
|
mate::Dictionary options;
|
||||||
if (!args->GetNext(&options)) {
|
if (!args->GetNext(&options)) {
|
||||||
icon_size = IconLoader::IconSize::NORMAL;
|
icon_size = IconLoader::IconSize::NORMAL;
|
||||||
} else {
|
} else {
|
||||||
|
@ -1143,22 +1141,17 @@ void App::GetFileIcon(const base::FilePath& path, mate::Arguments* args) {
|
||||||
icon_size = GetIconSizeByString(icon_size_string);
|
icon_size = GetIconSizeByString(icon_size_string);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!args->GetNext(&callback)) {
|
|
||||||
args->ThrowError("Missing required callback function");
|
|
||||||
return;
|
|
||||||
}
|
|
||||||
|
|
||||||
auto* icon_manager = AtomBrowserMainParts::Get()->GetIconManager();
|
auto* icon_manager = AtomBrowserMainParts::Get()->GetIconManager();
|
||||||
gfx::Image* icon =
|
gfx::Image* icon =
|
||||||
icon_manager->LookupIconFromFilepath(normalized_path, icon_size);
|
icon_manager->LookupIconFromFilepath(normalized_path, icon_size);
|
||||||
if (icon) {
|
if (icon) {
|
||||||
callback.Run(v8::Null(isolate()), *icon);
|
promise->Resolve(*icon);
|
||||||
} else {
|
} else {
|
||||||
icon_manager->LoadIcon(
|
icon_manager->LoadIcon(normalized_path, icon_size,
|
||||||
normalized_path, icon_size,
|
base::Bind(&OnIconDataAvailable, isolate(), promise),
|
||||||
base::Bind(&OnIconDataAvailable, isolate(), callback),
|
&cancelable_task_tracker_);
|
||||||
&cancelable_task_tracker_);
|
|
||||||
}
|
}
|
||||||
|
return promise->GetHandle();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<mate::Dictionary> App::GetAppMetrics(v8::Isolate* isolate) {
|
std::vector<mate::Dictionary> App::GetAppMetrics(v8::Isolate* isolate) {
|
||||||
|
|
|
@ -198,7 +198,8 @@ class App : public AtomBrowserClient::Delegate,
|
||||||
void ImportCertificate(const base::DictionaryValue& options,
|
void ImportCertificate(const base::DictionaryValue& options,
|
||||||
const net::CompletionCallback& callback);
|
const net::CompletionCallback& callback);
|
||||||
#endif
|
#endif
|
||||||
void GetFileIcon(const base::FilePath& path, mate::Arguments* args);
|
v8::Local<v8::Promise> GetFileIcon(const base::FilePath& path,
|
||||||
|
mate::Arguments* args);
|
||||||
|
|
||||||
std::vector<mate::Dictionary> GetAppMetrics(v8::Isolate* isolate);
|
std::vector<mate::Dictionary> GetAppMetrics(v8::Isolate* isolate);
|
||||||
v8::Local<v8::Value> GetGPUFeatureStatus(v8::Isolate* isolate);
|
v8::Local<v8::Value> GetGPUFeatureStatus(v8::Isolate* isolate);
|
||||||
|
|
|
@ -552,8 +552,29 @@ On _Windows_, there a 2 kinds of icons:
|
||||||
- Icons associated with certain file extensions, like `.mp3`, `.png`, etc.
|
- Icons associated with certain file extensions, like `.mp3`, `.png`, etc.
|
||||||
- Icons inside the file itself, like `.exe`, `.dll`, `.ico`.
|
- Icons inside the file itself, like `.exe`, `.dll`, `.ico`.
|
||||||
|
|
||||||
On _Linux_ and _macOS_, icons depend on the application associated with file
|
On _Linux_ and _macOS_, icons depend on the application associated with file mime type.
|
||||||
mime type.
|
|
||||||
|
**[Deprecated Soon](promisification.md)**
|
||||||
|
|
||||||
|
### `app.getFileIcon(path[, options])`
|
||||||
|
|
||||||
|
* `path` String
|
||||||
|
* `options` Object (optional)
|
||||||
|
* `size` String
|
||||||
|
* `small` - 16x16
|
||||||
|
* `normal` - 32x32
|
||||||
|
* `large` - 48x48 on _Linux_, 32x32 on _Windows_, unsupported on _macOS_.
|
||||||
|
|
||||||
|
Returns `Promise<NativeImage>` - fulfilled with the app's icon, which is a [NativeImage](native-image.md).
|
||||||
|
|
||||||
|
Fetches a path's associated icon.
|
||||||
|
|
||||||
|
On _Windows_, there a 2 kinds of icons:
|
||||||
|
|
||||||
|
- Icons associated with certain file extensions, like `.mp3`, `.png`, etc.
|
||||||
|
- Icons inside the file itself, like `.exe`, `.dll`, `.ico`.
|
||||||
|
|
||||||
|
On _Linux_ and _macOS_, icons depend on the application associated with file mime type.
|
||||||
|
|
||||||
### `app.setPath(name, path)`
|
### `app.setPath(name, path)`
|
||||||
|
|
||||||
|
|
|
@ -40,9 +40,14 @@ Object.assign(app, {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
const nativeFn = app.getAppMetrics
|
const nativeGetFileIcon = app.getFileIcon
|
||||||
|
app.getFileIcon = (path, options = {}, cb) => {
|
||||||
|
return deprecate.promisify(nativeGetFileIcon.call(app, path, options), cb)
|
||||||
|
}
|
||||||
|
|
||||||
|
const nativeAppMetrics = app.getAppMetrics
|
||||||
app.getAppMetrics = () => {
|
app.getAppMetrics = () => {
|
||||||
const metrics = nativeFn.call(app)
|
const metrics = nativeAppMetrics.call(app)
|
||||||
for (const metric of metrics) {
|
for (const metric of metrics) {
|
||||||
if ('memory' in metric) {
|
if ('memory' in metric) {
|
||||||
deprecate.removeProperty(metric, 'memory')
|
deprecate.removeProperty(metric, 'memory')
|
||||||
|
|
|
@ -769,61 +769,45 @@ describe('app module', () => {
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fetches a non-empty icon', done => {
|
it('fetches a non-empty icon', async () => {
|
||||||
app.getFileIcon(iconPath, (err, icon) => {
|
const icon = await app.getFileIcon(iconPath)
|
||||||
expect(err).to.be.null()
|
expect(icon.isEmpty()).to.be.false()
|
||||||
expect(icon.isEmpty()).to.be.false()
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fetches normal icon size by default', done => {
|
it('fetches normal icon size by default', async () => {
|
||||||
app.getFileIcon(iconPath, (err, icon) => {
|
const icon = await app.getFileIcon(iconPath)
|
||||||
const size = icon.getSize()
|
const size = icon.getSize()
|
||||||
|
|
||||||
expect(err).to.be.null()
|
expect(size.height).to.equal(sizes.normal)
|
||||||
expect(size.height).to.equal(sizes.normal)
|
expect(size.width).to.equal(sizes.normal)
|
||||||
expect(size.width).to.equal(sizes.normal)
|
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
describe('size option', () => {
|
describe('size option', () => {
|
||||||
it('fetches a small icon', (done) => {
|
it('fetches a small icon', async () => {
|
||||||
app.getFileIcon(iconPath, { size: 'small' }, (err, icon) => {
|
const icon = await app.getFileIcon(iconPath, { size: 'small' })
|
||||||
const size = icon.getSize()
|
const size = icon.getSize()
|
||||||
expect(err).to.be.null()
|
|
||||||
expect(size.height).to.equal(sizes.small)
|
expect(size.height).to.equal(sizes.small)
|
||||||
expect(size.width).to.equal(sizes.small)
|
expect(size.width).to.equal(sizes.small)
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fetches a normal icon', (done) => {
|
it('fetches a normal icon', async () => {
|
||||||
app.getFileIcon(iconPath, { size: 'normal' }, (err, icon) => {
|
const icon = await app.getFileIcon(iconPath, { size: 'normal' })
|
||||||
const size = icon.getSize()
|
const size = icon.getSize()
|
||||||
expect(err).to.be.null()
|
|
||||||
expect(size.height).to.equal(sizes.normal)
|
expect(size.height).to.equal(sizes.normal)
|
||||||
expect(size.width).to.equal(sizes.normal)
|
expect(size.width).to.equal(sizes.normal)
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
|
|
||||||
it('fetches a large icon', function (done) {
|
it('fetches a large icon', async () => {
|
||||||
// macOS does not support large icons
|
// macOS does not support large icons
|
||||||
if (process.platform === 'darwin') {
|
if (process.platform === 'darwin') return
|
||||||
// FIXME(alexeykuzmin): Skip the test.
|
|
||||||
// this.skip()
|
|
||||||
return done()
|
|
||||||
}
|
|
||||||
|
|
||||||
app.getFileIcon(iconPath, { size: 'large' }, (err, icon) => {
|
const icon = await app.getFileIcon(iconPath, { size: 'large' })
|
||||||
const size = icon.getSize()
|
const size = icon.getSize()
|
||||||
expect(err).to.be.null()
|
|
||||||
expect(size.height).to.equal(sizes.large)
|
expect(size.height).to.equal(sizes.large)
|
||||||
expect(size.width).to.equal(sizes.large)
|
expect(size.width).to.equal(sizes.large)
|
||||||
done()
|
|
||||||
})
|
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
})
|
})
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue