import React, { useState, useEffect } from 'react'
import PropTypes from 'prop-types'
import _ from 'lodash'
import CX from 'classnames'
import './index.less'
function WindowOperaButtonsForMac(props) {
const { style, enableResize } = props
const {
WindowStateAction, listenToWindowStateChange, getWindowState, WINDOW_STATE,
} = require('~/shared/utils/windowState')
const currentWindow = require('electron').remote.getCurrentWindow()
const [isMaximized, setIsMaximized] = useState(currentWindow.isMaximized())
const wrapperStyle = _.assign({}, style)
const handleMaximize = () => {
setIsMaximized(currentWindow.isMaximized())
}
useEffect(() => {
return listenToWindowStateChange(handleMaximize)
})
return (
<div className="window-opera-buttons-for-mac-component-wrap" style={wrapperStyle}>
<span
className="btn btn-close"
onClick={() => {
WindowStateAction.close()
}}
role="button"
tabIndex={0}
/>
<span
className="btn btn-minimize"
onClick={() => {
WindowStateAction.minimize()
}}
role="button"
tabIndex={0}
/>
<span
className={CX({
btn: true,
'btn-maximize': isMaximized === false,
'btn-unmaximize': isMaximized === true,
'btn-maximize-disabled': enableResize === false,
})}
onClick={() => {
if (enableResize === false) {
return
}
const state = getWindowState(currentWindow)
if (state === WINDOW_STATE.MAXIMIZED) {
WindowStateAction.unmaximize()
} else if (state === WINDOW_STATE.NORMAL) {
WindowStateAction.maximize()
}
}}
role="button"
tabIndex={0}
/>
</div>
)
}
WindowOperaButtonsForMac.propTypes = {
style: PropTypes.object,
enableResize: PropTypes.bool,
}
WindowOperaButtonsForMac.defaultProps = {
style: {},
enableResize: false,
}
export default WindowOperaButtonsForMac
关于useState useEffect
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。
推荐阅读更多精彩内容
- ①遇到有趣的人是一件欢喜的事,对方先同我说了一句话,你是个爱花的姑娘。 微微点头,因为我也喜欢树,草,云,山,水,...
- 1.我写了封长长的信 没有地址 没有邮印 字迹很轻 言语深情 那是我的心 刻着你的名 在每个夜深人静 念给岁月听 ...
- 成长记录-连载(三十六) ——我的第一篇五千字长文,说了什么,你一定想不到 并不是不想每天写公众号,而是之前思考怎...