Skip to content

Add an API to fetch file icon - #7851

Merged
kevinsawicki merged 32 commits into
electron:masterfrom
YurySolovyov:image-from-icon
Feb 7, 2017
Merged

kevinsawicki merged 32 commits into
electron:masterfrom
YurySolovyov:image-from-icon

Conversation

@YurySolovyov

@YurySolovyov YurySolovyov commented Nov 2, 2016 •

Copy link
Copy Markdown
Contributor

Fixes #6397
Huge thanks to @deepak1556 and @MarshallOfSound who did most of the work and helped a lot.

TODO:

  • Add some tests on JS side
  • Make callback node-like with error-first signature
  • Make size argument an options object and use normal size by default.
  • Write docs
  • Try building it on macOS and fix if needed (need help, no Apple devices available for me)
  • Make it compile under/for Windows

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

Note to self: optional args handling

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

@deepak1556 I'm having troubles trying to understand how to detect when icon is empty for implementing node-style callbacks.
Can you please take a look at image-from-icon-nodeback branch?

It seems on linux/kde5 it always returns this icon for non-existent files:
image

@YurySolovyov

YurySolovyov commented Nov 5, 2016 •

Copy link
Copy Markdown
Contributor Author

Faced the problem with extraction of icons from executable files.
On windows:
API call:
image
Chrome Downloads page:
image

It seems like API call only uses mime type to get the "generic" icon

/cc @deepak1556

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

Found the problem: path should be normalized:
.getFileIcon('D:/DOWNLOAD/N1Setup.exe', ... gives generic one
and
.getFileIcon('D:\\DOWNLOAD\\N1Setup.exe'... gives one embedded into executable

@MarshallOfSound

MarshallOfSound commented Nov 5, 2016 •

Copy link
Copy Markdown
Member

@YurySolovyov Pass the path through path.normalize 👍 It will do that transformation for you 😄 I'm sure there is a Chromium FilePath Util to do the same thing

Comment thread docs/api/app.md Outdated
* `options` Object (optional)
* `size` String - Defaults to `normal`. Can be `small`, `normal`, `large`
* `callback` Function
* `Icon` [NativeImage](native-image.md)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Icon --> icon

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

@MarshallOfSound problem with normalize is that it is in js and my code is in C++, so I have to state it in docs or expose only the binding to js and do normalization there

@MarshallOfSound

Copy link
Copy Markdown
Member

Comment thread atom/browser/api/atom_api_app.cc Outdated
v8::Isolate* isolate = v8::Isolate::GetCurrent();
if (icon && !icon->IsEmpty()) {
v8::Locker locker(isolate);
v8::HandleScope handle_scope(isolate);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

locker and scope should be moved to top level of the function.

Comment thread atom/browser/api/atom_api_app.h Outdated
#include "atom/browser/api/event_emitter.h"
#include "atom/browser/atom_browser_client.h"
#include "atom/browser/browser_observer.h"
#include "atom/common/api/atom_api_native_image.h"

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

header not required.

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

Tests(not only the added ones) are somewhat unstable, even locally they pass and fail time-to-time

return dict.GetHandle();
}
};

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Kill the random new line plz 👍

Comment thread docs/api/app.md Outdated

* `path` String
* `options` Object (optional)
* `size` String - Defaults to `normal`. Can be `small`, `normal`, `large`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can be small, normal or large

Comment thread docs/api/app.md Outdated
* `videos` Directory for a user's videos.
* `pepperFlashSystemPlugin` Full path to the system version of the Pepper Flash plugin.

### `app.getFileIcon(path, [options], callback)`

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When did this move from NativeImage.from to an app API. Personally I liked it better the other way round. The app module is already polluted as it is and IMO it shouldn't just become a dumping ground for new methods 👍

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I chatted about it with @deepak1556 and he suggested doing it under app namespace

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is that discussion on this PR somewhere or was it private?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Comment thread spec/api-app-spec.js Outdated

it('fetches large icons', function (done) {
if (process.platform === 'darwin') {
return this.skip() // macOS does not support large icons

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this is the case it should also be in the docs

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

I kind of agree that placing this API to NativeImage is more natural, especially w.r.t. running from both renderer and main processes and I'm happy to move it, but it would be nice to hear from @zcbenz.

@kevinsawicki kevinsawicki left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Left a few minor comments.

Also vendor/brigthray is showing up in the diff, it doesn't need to change as part of this pull request right? Might want to revert that change if it was unintended.

Comment thread atom/browser/api/atom_api_app.cc Outdated
}

if (!args->GetNext(&callback)) {
args->ThrowError();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could a helpful message be added to this error? Maybe "Missing required callback function"?

Comment thread spec/api-app-spec.js Outdated
large: 48
}

it('fetches non-empty icon', function (done) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

icon -> icons

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is one icon per API call, so not sure it is plural here

@kevinsawicki kevinsawicki Nov 10, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The other specs are fetches small icons and fetches normal icons though, so either this should be fetches non-empty icons or fetches a non-empty icon. Currently the grammar is a little off.

Comment thread spec/api-app-spec.js Outdated
})

describe('getFileIcon() API', function () {
const iconPath = path.join(process.cwd(), 'fixtures/assets/icon.ico')

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably want to use __dirname instead of process.cwd().

const iconPath = path.join(__dirname, 'fixtures', 'assets', 'icon.ico')

Comment thread docs/api/app.md Outdated

* `path` String
* `options` Object (optional)
* `size` String - Can be `small`, `normal`, `large`. Except for `large` on _macOS_

@kevinsawicki kevinsawicki Nov 10, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Except for large on macOS

What do you think about:

The large size is not supported on macOS

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm fine with it.

Comment thread docs/api/app.md
* `callback` Function
* `error` Error
* `icon` [NativeImage](native-image.md)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This API could use a sentence (or two) description about what it does.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I guess I can take it from one of the c++ headers, and reword it a bit.

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

@kevinsawicki Thanks, I'll address these as I get home. Any thoughts on where this API should belong?

@kevinsawicki

Copy link
Copy Markdown
Contributor

Any thoughts on where this API should belong?

I'm not 100% sure, both nativeImage and app seem fine to me.

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

I would personally prefer having it under NativeImage. Are you ok with it @deepak1556 ?

@deepak1556

Copy link
Copy Markdown
Member

I am fine with it, but there are certain things you need to take care of. The IconManager class cannot be used as such if the api is called from render process, since it involves usage of browser process threads and the system classes created on the browser process. You would have to use IPC in that scenario.

Comment thread docs/api/app.md Outdated

* `path` String
* `options` Object (optional)
* `size` String - Can be `small`, `normal`, `large`. The `large` size is not supported on _macOS_

@levrik levrik Nov 11, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if I call this API with size: 'large' on macOS? Is there something like a fallback to normal or what could be the best behaviour?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I have a no idea tbh, and no Apple device to test it.

what could be the best behaviour?

I think the best would be to fetch normal one. This is what happens on windows too, where normal and large icons are equal.

I'll see what I can do to test it

@levrik levrik Nov 11, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it should be noted in the docs that there is no difference between normal and large on Windows (and macOS).

Comment thread spec/api-app-spec.js
assert.equal(err, null)
assert.equal(size.height, sizes.normal)
assert.equal(size.width, sizes.normal)
done()

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

normal gets passed instead of large

@levrik levrik Nov 11, 2016 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Strange... after submitting the review the position of the comment moves up.
image
image

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

@deepak1556 any examples in existing APIs of how this IPC messaging could/should look like?

@deepak1556

Copy link
Copy Markdown
Member

@YurySolovyov none of the common apis currently are dependent on ipc for implementation. You could probably define the api in lib/common/api/native-image.js and depending on the process either call app.GetFileIcon directly or send ipc, which is something users can do. I am not sure about the location of this api, will defer to @zcbenz

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

@deepak1556 maybe I can expose API as binding to App, and then call it directly or via IPC depending on the process as you suggested.

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

How about adding it to shell? Seems more appropriate than app and native-image

@alexstrat

Copy link
Copy Markdown
Contributor

Hey, I'm interested in this API: how can I help? What is remaining to be done?

@YurySolovyov

Copy link
Copy Markdown
Contributor Author

@alexstrat it is awaiting for some advice form @zcbenz to decide where this API belongs.

@vutran

vutran commented Feb 4, 2017

Copy link
Copy Markdown

Any updates on which API this belongs to? Looking forward to implement this on my app.

@kevinsawicki

Copy link
Copy Markdown
Contributor

Thanks for this @YurySolovyov, @deepak1556, and @MarshallOfSound 👍 🚢 🚀

@kevinsawicki
kevinsawicki merged commit 4630f44 into electron:master Feb 7, 2017
@YurySolovyov

Copy link
Copy Markdown
Contributor Author

Huge thanks to @MarshallOfSound, @deepak1556 for help and @kevinsawicki for finishing this up.
Can't wait to see this in the release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow fetching chrome://fileicon/<path> endpoint or expose it via API.

7 participants