Created
October 29, 2018 05:45
-
-
Save sanjeevbishnoi/aedb3dc766c57365e64cc2013ad77082 to your computer and use it in GitHub Desktop.
To set the default printer out of list
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import React, { Component } from 'react'; | |
import { RadioButton, RadioButtonGroup } from 'material-ui/RadioButton'; | |
import RaisedButton from 'material-ui/RaisedButton'; | |
import DropDownMenu from 'material-ui/DropDownMenu'; | |
import MenuItem from 'material-ui/MenuItem'; | |
const storage = require('electron-json-storage'); | |
const styles = { | |
headline: { | |
fontSize: 24, | |
paddingTop: 16, | |
marginBottom: 12, | |
fontWeight: 400, | |
}, | |
tabStyle: { | |
background: 'white', | |
color: 'black' | |
}, | |
container: { | |
padding: 20 | |
}, | |
margin: 12, | |
block: { | |
maxWidth: 250, | |
}, | |
radioButton: { | |
marginBottom: 16, | |
}, | |
}; | |
export default class Home extends Component { | |
constructor(props) { | |
super(props) | |
this.state = { | |
value: '', | |
list:[], | |
printer:'', | |
pageSize:'1' | |
}; | |
} | |
componentDidMount(){ | |
this.handlePrint(); | |
} | |
handleChange = (event, index, value) => this.setState({ value }); | |
handlePageSizeChange=(event,index)=>{ | |
//alert(index); | |
this.setState({pageSize:index}) | |
}; | |
saveChanges=()=>{ | |
storage.set('printer_settings', { printer: this.state.value, pageSize: this.state.pageSize }, (error) => { | |
if (error) { | |
alert('Error occured'); | |
}else{ | |
alert('Settings saved successfully'); | |
} | |
}); | |
} | |
render() { | |
return ( | |
<div style={{ padding: 20 }}> | |
<div style={{ minHeight: 300 }}> | |
<h4>Printer to send print job to</h4> | |
<DropDownMenu value={this.state.value} onChange={this.handleChange}> | |
{this.state.list.map((item,index)=>{ | |
//alert(JSON.stringify(item)); | |
return <MenuItem value={item.name} primaryText={item.name} /> | |
}) | |
} | |
</DropDownMenu> | |
<h4>Print copies divider</h4> | |
<RadioButtonGroup style={{ marginTop: 20 }} name="printerdivide" defaultSelected={this.state.pageSize} valueSelected={this.state.pageSize} onChange={this.handlePageSizeChange}> | |
<RadioButton | |
value="1" | |
label="Divide by 1" | |
style={styles.radioButton} | |
/> | |
<RadioButton | |
value="2" | |
label="Divide by 2" | |
style={styles.radioButton} | |
/> | |
<RadioButton | |
value="4" | |
label="Divide by 4" | |
style={styles.radioButton} | |
/> | |
</RadioButtonGroup> | |
</div> | |
<div style={{ position: 'absolute', right: 20, bottom: 20 }}> | |
<RaisedButton label="Save" primary={true} style={styles.margin} onClick={this.saveChanges} /> | |
</div> | |
</div> | |
); | |
} | |
handlePrint() { | |
const { remote } = require('electron'); | |
const { BrowserWindow, dialog, shell } = remote; | |
let printWindow = new BrowserWindow({ 'auto-hide-menu-bar': true,show:false }); | |
printWindow.loadURL("www.google.com"); | |
let list = printWindow.webContents.getPrinters(); | |
this.setState({list}); | |
storage.get('printer_settings', (error, data) => { | |
if (data){ | |
this.setState({ value: data.printer?data.printer:this.state.printer,pageSize:data.pageSize?data.pageSize:this.state.pageSize}); | |
} | |
}); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I think
loadURL()
isn't necessary. I tried omitting this and it just works fine.