-
-
Notifications
You must be signed in to change notification settings - Fork 7
Preset app icon #61 #64
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,8 +1,35 @@ | ||
| const { InstanceStatus } = require('@companion-module/base') | ||
| const entities = require('entities') | ||
| const http = require('http') | ||
|
|
||
| const Client = require('node-rest-client').Client | ||
|
|
||
| function fetchAndEncodeBase64(url) { | ||
| return new Promise((resolve, reject) => { | ||
| http | ||
| .get(url, (res) => { | ||
| if (res.statusCode !== 200) { | ||
| reject(new Error(`Request Failed. Status Code: ${res.statusCode}`)) | ||
| res.resume() | ||
| return | ||
| } | ||
|
|
||
| const chunks = [] | ||
| res.on('data', (chunk) => { | ||
| chunks.push(chunk) | ||
| }) | ||
|
|
||
| res.on('end', () => { | ||
| const base64 = Buffer.concat(chunks).toString('base64') | ||
| resolve(base64) | ||
| }) | ||
| }) | ||
| .on('error', (err) => { | ||
| reject(err) | ||
| }) | ||
| }) | ||
| } | ||
|
|
||
| module.exports = { | ||
| initConnection: function () { | ||
| let self = this | ||
|
|
@@ -178,12 +205,25 @@ module.exports = { | |
|
|
||
| buildAppList: function () { | ||
| let self = this | ||
| const promises = [] | ||
|
|
||
| self.CHOICES_APPS = [] | ||
|
|
||
| for (let i = 0; i < self.DATA.apps.length; i++) { | ||
| let app = self.DATA.apps[i] | ||
| self.CHOICES_APPS.push({ id: app.uri, label: entities.decodeHTML(app.title) }) | ||
| if (app.icon) { | ||
| promises.push( | ||
| fetchAndEncodeBase64(app.icon).then((png64) => { | ||
| self.CHOICES_APPS[i].png64 = png64 | ||
| }), | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| ;(async () => { | ||
| await Promise.allSettled(promises) | ||
| self.initPresets() | ||
| })() | ||
|
Comment on lines
+224
to
+227
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not hot enough on my async/promise stuff; will this function block until the promise is resolved and not allow program flow to continue, or will it do it in the background as it's async? Are we fine because the await is inside an async? I was just thinking it would be good to initPresets beforehand too, but that's irrelevant if it runs in the background...
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Once all promises are resolved, the presets will be updated. Since this is local communication, it is assumed that all promises resolve immediately, allowing the presets to be updated in bulk. |
||
| }, | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -262,7 +262,7 @@ module.exports = { | |
|
|
||
| for (let i = 0; i < (this.CHOICES_APPS || []).length; i++) { | ||
| let app = this.CHOICES_APPS[i] | ||
| presets[`select_app_${i}`] = { | ||
| const preset = { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, the mutable Javascript const! Out of curiosity, what's the benefit of the preset over just putting the properties directly into the one we were creating before? |
||
| category: 'App', | ||
| name: `Select app ${app.label}`, | ||
| type: 'button', | ||
|
|
@@ -271,6 +271,7 @@ module.exports = { | |
| size: '10', | ||
| color: foregroundColor, | ||
| bgcolor: foregroundColorBlack, | ||
| png64: app.png64, | ||
|
itota marked this conversation as resolved.
Outdated
|
||
| }, | ||
| steps: [ | ||
| { | ||
|
|
@@ -287,6 +288,11 @@ module.exports = { | |
| }, | ||
| ], | ||
| } | ||
| if (app.png64) { | ||
| preset.style.png64 = app.png64 | ||
| preset.style.alignment = 'center:bottom' | ||
| } | ||
| presets[`select_app_${i}`] = preset | ||
| } | ||
|
|
||
| self.setPresetDefinitions(presets) | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is the leading semicolon some weird prettier quirk (I've seen similar before)?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes