Skip to content
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

Select background #1001

Merged
merged 7 commits into from
Sep 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions scss/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -136,3 +136,4 @@ code {
image-rendering: -o-crisp-edges;
image-rendering: pixelated;
}

17 changes: 17 additions & 0 deletions scss/modules/_settings.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// ChatView Background selector

.background-preview {
width: 100pt;
height: 100pt;
border-radius: 5pt;
background-image: url(../images/background_light.svg);
background-color: var(--chatViewBg);
}

.background-options {
display: flex;
flex-direction: column;
padding-left: 8pt;
justify-content: space-evenly;
}
2 changes: 2 additions & 0 deletions scss/modules/index.scss
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
@import 'emoji-mart-overwrites';


@import 'settings';

// Module: Contact Name

.module-contact-name__profile-name {
Expand Down
27 changes: 25 additions & 2 deletions src/main/ipc.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module.exports = { init }

const { app, ipcMain } = require('electron')
const { app, ipcMain, dialog } = require('electron')
const rimraf = require('rimraf')
const path = require('path')
const fs = require('fs')
Expand Down Expand Up @@ -173,11 +173,34 @@ function init (cwd, state, logHandler) {
e.returnValue = app.localeData
})

ipcMain.on('updateDesktopSetting', (e, key, value) => {
const updateDesktopSetting = (e, key, value) => {
Copy link
Member

Choose a reason for hiding this comment

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

Maybe remove e since we don't use it?

const { saved } = app.state
saved[key] = value
app.saveState({ saved })
render()
}
ipcMain.on('updateDesktopSetting', updateDesktopSetting)
Copy link
Member

Choose a reason for hiding this comment

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

Maybe do

 ipcMain.on('updateDesktopSetting', (e, k, v) => updateDesktopSetting(k, v))

Copy link
Member Author

Choose a reason for hiding this comment

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

a new function... maybe more clearer, but us the definition is above it should be fine


ipcMain.on('selectBackgroundImage', (e) => {
dialog.showOpenDialog(undefined, {
title: 'Select Background Image',
filters: [
{ name: 'Images', extensions: ['jpg', 'png', 'gif'] },
{ name: 'All Files', extensions: ['*'] }
],
properties: ['openFile']
}, (filenames) => {
if (!filenames) { return }
log.info('BG-IMG Selected File:', filenames[0])
const newPath = path.join(getConfigPath(), 'background' + path.extname(filenames[0]))
fs.copyFile(filenames[0], newPath, (err) => {
if (err) {
log.error('BG-IMG Copy Failed', err)
return
}
updateDesktopSetting(null, 'chatViewBgImg', `url("${newPath}")`)
Copy link
Member

Choose a reason for hiding this comment

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

.. and if so, remove null here :)

})
})
})

ipcMain.on('updateCredentials', (e, credentials) => {
Expand Down
3 changes: 2 additions & 1 deletion src/main/state.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ function getDefaultState () {
showNotificationContent: true,
locale: 'en',
credentials: null,
enableOnDemandLocationStreaming: false
enableOnDemandLocationStreaming: false,
chatViewBgImgPath: undefined
}
}
}
Expand Down
1 change: 1 addition & 0 deletions src/renderer/ThemeBackend.js
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ export function ThemeDataBuilder (theme) {
themeData = Object.assign(themeData, theme.raw)
log.debug('theme.raw', theme.raw, themeData)
}

return themeData
}

Expand Down
71 changes: 40 additions & 31 deletions src/renderer/components/ChatView.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ const MessageWrapper = require('./MessageWrapper')
const log = require('../../logger').getLogger('renderer/chatView')

const { isDisplayableByRenderMedia } = require('./Attachment')
const SettingsContext = require('../contexts/SettingsContext')

const MutationObserver = window.MutationObserver

Expand All @@ -23,7 +24,7 @@ const ChatViewWrapper = styled.div`
margin-top: 50px;
background-image: ${props => props.theme.chatViewBgImgPath};
background-size: cover;
background-color: ${props => props.theme.chatViewBg};
background-color: var(--chatViewBg);

}
`
Expand Down Expand Up @@ -242,36 +243,44 @@ class ChatView extends React.Component {
render () {
const { onDeadDropClick, chat } = this.props
return (
<ChatViewWrapper
style={{ gridTemplateRows: `auto ${this.state.composerSize}px` }}
ref={this.ChatViewWrapperRef} onDrop={this.onDrop.bind({ props: { chat } })} onDragOver={this.onDragOver} >
<ConversationWrapper>
<div id='the-conversation' ref={this.conversationDiv}>
<ul>
{chat.messages.map(rawMessage => {
const message = MessageWrapper.convert(rawMessage)
message.onReply = () => log.debug('reply to', message)
message.onForward = this.onForward.bind(this, message)
return MessageWrapper.render({
message,
chat,
onClickContactRequest: () => onDeadDropClick(message),
onClickSetupMessage: this.onClickSetupMessage.bind(this, message),
onShowDetail: this.onShowDetail.bind(this, message),
onClickAttachment: this.onClickAttachment.bind(this, message)
})
})}
</ul>
</div>
</ConversationWrapper>
<Composer
ref={this.refComposer}
chatId={chat.id}
draft={chat.draft}
onSubmit={this.writeMessage}
setComposerSize={this.setComposerSize.bind(this)}
/>
</ChatViewWrapper>
<SettingsContext.Consumer>
{(settings) => (
Copy link
Contributor

Choose a reason for hiding this comment

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

Is it possible to use hooks here?

<ChatViewWrapper
style={{
gridTemplateRows: `auto ${this.state.composerSize}px`,
background: settings['chatViewBgImg'],
backgroundSize: 'cover'
}}
ref={this.ChatViewWrapperRef} onDrop={this.onDrop.bind({ props: { chat } })} onDragOver={this.onDragOver} >
<ConversationWrapper>
<div id='the-conversation' ref={this.conversationDiv}>
<ul>
{chat.messages.map(rawMessage => {
const message = MessageWrapper.convert(rawMessage)
message.onReply = () => log.debug('reply to', message)
message.onForward = this.onForward.bind(this, message)
return MessageWrapper.render({
message,
chat,
onClickContactRequest: () => onDeadDropClick(message),
onClickSetupMessage: this.onClickSetupMessage.bind(this, message),
onShowDetail: this.onShowDetail.bind(this, message),
onClickAttachment: this.onClickAttachment.bind(this, message)
})
})}
</ul>
</div>
</ConversationWrapper>
<Composer
ref={this.refComposer}
chatId={chat.id}
draft={chat.draft}
onSubmit={this.writeMessage}
setComposerSize={this.setComposerSize.bind(this)}
/>
</ChatViewWrapper>
)}
</SettingsContext.Consumer>
)
}
}
Expand Down
69 changes: 69 additions & 0 deletions src/renderer/components/dialogs/Settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,10 @@ export default class Settings extends React.Component {
<H5>{this.translate('pref_experimental_features')}</H5>
{ this.renderDTSettingSwitch('enableOnDemandLocationStreaming', this.translate('pref_on_demand_location_streaming')) }
</Card>
<Card elevation={Elevation.ONE}>
<H5>{this.translate('pref_background')}</H5>
<BackgroundSelector onChange={(val) => this.handleDesktopSettingsChange('chatViewBgImg', val)} />
</Card>
</div>
)
} else if (this.state.show === 'login') {
Expand Down Expand Up @@ -363,3 +367,68 @@ export default class Settings extends React.Component {
)
}
}

class BackgroundSelector extends React.Component {
constructor () {
super()
this.fileInput = React.createRef()
this.colorInput = React.createRef()
}

render () {
const tx = window.translate
return (<div style={{ display: 'flex' }}>
<SettingsContext.Consumer>
{(settings) => (
<div
style={{ background: settings['chatViewBgImg'], backgroundSize: 'cover' }}
aria-label={tx('a11y_background_preview_label')}
className={'background-preview'}
/>
)}
</SettingsContext.Consumer>
<div className={'background-options'}>
<button onClick={this.onButton.bind(this, 'def')}>{tx('pref_background_default')}</button>
<button onClick={this.onButton.bind(this, 'def_color')}>{tx('pref_background_default_color')}</button>
<button onClick={this.onButton.bind(this, 'image')}>{tx('pref_background_custom')}</button>
<button onClick={this.onButton.bind(this, 'color')}>{tx('pref_background_custom_color')}</button>
</div>
<div hidden>
<input
type={'color'}
onChange={this.onColor.bind(this)}
ref={this.colorInput} /> // todo put this somewhere where it isn't closed by react rerender imidiately
</div>
</div>
)
}

setValue (val) {
this.props.onChange(val)
}

onButton (type, ev) {
switch (type) {
case 'def':
this.setValue(undefined)
break
case 'def_color':
this.setValue('var(--chatViewBg)')
break
case 'image':
ipcRenderer.send('selectBackgroundImage')
break
case 'color':
this.colorInput.current && this.colorInput.current.click()
break
default:
/* ignore-console-log */
console.error("this shouldn't happen")
}
}

onColor (ev) {
// TODO debounce
this.setValue(ev.target.value)
}
}