const React = require('react') const remote = require('electron').remote const dialog = remote.dialog const path = require('path') const {dispatch} = require('../lib/dispatcher') module.exports = class Preferences extends React.Component { render () { var state = this.props.state return (
{renderGeneralSection(state)} {renderPlaybackSection(state)}
) } } function renderGeneralSection (state) { return renderSection({ key: 'general', title: 'General', description: '', icon: 'settings' }, [ renderDownloadPathSelector(state), renderFileHandlers(state), renderExternalPlayerSelector(state) ]) } function renderPlaybackSection (state) { return renderSection({ title: 'Playback', description: '', icon: 'settings' }, [ renderPlayInVlcSelector(state) ]) } function renderPlayInVlcSelector (state) { return renderCheckbox({ key: 'play-in-vlc', label: 'Play in VLC', description: 'Media will play in VLC', property: 'playInVlc', value: state.saved.prefs.playInVlc }, state.unsaved.prefs.playInVlc, function (value) { dispatch('updatePreferences', 'playInVlc', value) }) } function renderDownloadPathSelector (state) { return renderFileSelector({ key: 'download-path', label: 'Download Path', description: 'Data from torrents will be saved here', property: 'downloadPath', options: { title: 'Select download directory', properties: [ 'openDirectory' ] } }, state.unsaved.prefs.downloadPath, function (filePath) { dispatch('updatePreferences', 'downloadPath', filePath) }) } function renderFileHandlers (state) { var definition = { key: 'file-handlers', label: 'Handle Torrent Files' } var buttonText = state.unsaved.prefs.isFileHandler ? 'Remove default app for torrent files' : 'Make WebTorrent the default app for torrent files' var controls = [( )] return renderControlGroup(definition, controls) function toggleFileHandlers () { var isFileHandler = state.unsaved.prefs.isFileHandler dispatch('updatePreferences', 'isFileHandler', !isFileHandler) } } function renderExternalPlayerSelector (state) { return renderFileSelector({ label: 'External Media Player', description: 'Progam that will be used to play media externally', property: 'externalPlayerPath', options: { title: 'Select media player executable', properties: [ 'openFile' ] } }, state.unsaved.prefs.externalPlayerPath || '', // TODO: should we get/store vlc path instead? function (filePath) { if (path.extname(filePath) === '.app') { // Get executable in packaged mac app var name = path.basename(filePath, '.app') filePath += '/Contents/MacOS/' + name } dispatch('updatePreferences', 'externalPlayerPath', filePath) }) } // Renders a prefs section. // - definition should be {icon, title, description} // - controls should be an array of vdom elements function renderSection (definition, controls) { var helpElem = !definition.description ? null : (
help_outline{definition.description}
) return (
{definition.icon}{definition.title}
{helpElem}
{controls}
) } function renderCheckbox (definition, value, callback) { var iconClass = 'icon clickable' if (value) iconClass += ' enabled' return (
) function handleClick () { callback(!value) } } // Creates a file chooser // - defition should be {label, description, options} // options are passed to dialog.showOpenDialog // - value should be the current pref, a file or folder path // - callback takes a new file or folder path function renderFileSelector (definition, value, callback) { var controls = [( ), ( )] return renderControlGroup(definition, controls) function handleClick () { dialog.showOpenDialog(remote.getCurrentWindow(), definition.options, function (filenames) { if (!Array.isArray(filenames)) return callback(filenames[0]) }) } } function renderControlGroup (definition, controls) { return (
{controls}
) }