Skip to content

Commit ea689eb

Browse files
authored
Merge pull request #76 from Cristian006/develop
v2.1.4
2 parents 8a210c5 + 2e89864 commit ea689eb

8 files changed

Lines changed: 84 additions & 27 deletions

File tree

README.md

Lines changed: 32 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,30 @@ import TitleBar from 'frameless-titlebar'
5252
const currentWindow = remote.getCurrentWindow();
5353

5454
const Example = () => {
55+
// manage window state, default to currentWindow maximized state
56+
const [maximized, setMaximized] = useState(currentWindow.isMaximized());
57+
// add window listeners for currentWindow
58+
useEffect(() => {
59+
const onMaximized = () => setMaximized(true);
60+
const onRestore = () => setMaximized(false);
61+
currentWindow.on("maximize", onMaximized);
62+
currentWindow.on("unmaximize", onRestore);
63+
return () => {
64+
currentWindow.removeListener("maximize", onMaximized);
65+
currentWindow.removeListener("unmaximize", onRestore);
66+
}
67+
}, []);
68+
69+
// used by double click on the titlebar
70+
// and by the maximize control button
71+
const handleMaximize = () => {
72+
if (maximized) {
73+
currentWindow.restore();
74+
} else {
75+
currentWindow.maximize();
76+
}
77+
}
78+
5579
return (
5680
<div>
5781
<TitleBar
@@ -66,9 +90,15 @@ const Example = () => {
6690
title="frameless app"
6791
onClose={() => currentWindow.close()}
6892
onMinimize={() => currentWindow.minimize()}
69-
onMaximize={() => currentWindow.maximize()}
93+
onMaximize={handleMaximize}
7094
// when the titlebar is double clicked
71-
onDoubleClick={() => currentWindow.maximize()}
95+
onDoubleClick={handleMaximize}
96+
// hide minimize windows control
97+
disableMinimize={false}
98+
// hide maximize windows control
99+
disableMaximize={false}
100+
// is the current window maximized?
101+
maximized={maximized}
72102
>
73103
{/* custom titlebar items */}
74104
</TitleBar>

example/src/App.js

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,16 @@ const selections = [
6767
label: 'Show Custom Titlebar Button',
6868
type: 'toggle'
6969
},
70+
{
71+
option: 'disableMinimize',
72+
label: 'Disable Minimize Button',
73+
type: 'toggle',
74+
},
75+
{
76+
option: 'disableMaximize',
77+
label: 'Disable Maximize Button',
78+
type: 'toggle',
79+
}
7080
]
7181

7282
const useStyles = makeStyles((theme) => ({
@@ -87,10 +97,13 @@ const App = ({ theme, setPalette }) => {
8797
platform: 'win32',
8898
menuStyle: 'default',
8999
align: 'center',
100+
maximized: false,
90101
subLabels: true,
91102
showIcon: true,
92103
showTitle: true,
93-
showCustom: true
104+
showCustom: true,
105+
disableMinimize: false,
106+
disableMaximize: false,
94107
})
95108

96109
const currentTheme = {
@@ -174,9 +187,12 @@ const App = ({ theme, setPalette }) => {
174187
)}
175188
platform={state.platform}
176189
title={state.showTitle && 'example-app'}
190+
disableMaximize={state.disableMaximize}
191+
disableMinimize={state.disableMinimize}
192+
maximized={state.maximized}
177193
onClose={() => { enqueueSnackbar('close clicked', { variant: 'error' }) }}
178194
onMinimize={() => { enqueueSnackbar('minimized clicked', { variant: 'success' }) }}
179-
onMaximize={() => { enqueueSnackbar('maximized clicked', { variant: 'success' }) }}
195+
onMaximize={() => { enqueueSnackbar(`window ${state.maximized ? 'maximized' : 'restored'}`, { variant: 'success' }); setState({ ...state, maximized: !state.maximized }); }}
180196
onDoubleClick={() => { enqueueSnackbar('double clicked', { variant: 'success' }) }}
181197
>
182198
{state.showCustom && (
@@ -188,7 +204,7 @@ const App = ({ theme, setPalette }) => {
188204
<Typography variant='h6' className={classes.title}>
189205
Frameless Titlebar
190206
<Typography style={{ marginLeft: 12, fontWeight: 'bolder' }} component='span' variant='h6' color='secondary'>
191-
v2.1.3 <span role='img'>🥳</span>
207+
v2.1.4 <span role='img'>🥳</span>
192208
</Typography>
193209
</Typography>
194210
<Tooltip title='Toggle Dark/Light Mode' >

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "frameless-titlebar",
3-
"version": "2.1.3",
3+
"version": "2.1.4",
44
"description": "Customizable titlebar for frameless Electron desktop applications",
55
"author": "Cristian006",
66
"license": "MIT",
@@ -51,4 +51,4 @@
5151
"rollup-plugin-url": "^2.1.0",
5252
"typescript": "^3.2.4"
5353
}
54-
}
54+
}

src/title-bar/effects/useAccessibility.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ const useAccessibility = (
3434

3535
const callback = useCallback(e => {
3636
if (e.altKey) {
37-
e.preventDefault();
3837
if (!altKey) {
3938
dispatch({
4039
type: 'alt',
@@ -45,6 +44,8 @@ const useAccessibility = (
4544
if (e.keyCode !== 18) {
4645
let firstIndex = menu!.findIndex(x => (!x.disabled && altKeyCodeMatch(e, x.label)));
4746
if (firstIndex >= 0) {
47+
// only prevent default when alt key code match is found
48+
e.preventDefault();
4849
const maxIndex = Math.min(firstIndex, overflow && overflow.hide ? menu.length - 1 : menu.length)
4950
dispatch({
5051
type: 'button-set',

src/title-bar/index.tsx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ const TitleBar = ({
2424
icon,
2525
iconSrc,
2626
title,
27+
maximized,
2728
currentWindow
2829
}: TitleBarProps) => {
2930
const focused = useWindowFocus();
@@ -46,6 +47,7 @@ const TitleBar = ({
4647
focused={focused}
4748
disableMaximize={disableMaximize}
4849
disableMinimize={disableMinimize}
50+
maximized={maximized}
4951
onMinimize={onMinimize}
5052
onMaximize={onMaximize}
5153
onClose={onClose}
@@ -77,6 +79,7 @@ const TitleBar = ({
7779
focused={focused}
7880
disableMaximize={disableMaximize}
7981
disableMinimize={disableMinimize}
82+
maximized={maximized}
8083
onMinimize={onMinimize}
8184
onMaximize={onMaximize}
8285
onClose={onClose}

src/title-bar/menu-bar/menu-button.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ const MenuButton = ({
5555
if (!item.disabled) {
5656
dispatch({ type: 'button-set', depth, selected: idx });
5757
}
58-
}, [idx]);
58+
}, [idx, item.disabled]);
5959

6060
const onHover = useCallback((hovering: boolean) => {
6161
if (currentSelected(selectedPath, depth) >= 0 && hovering) {

src/title-bar/typings/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ export interface ControlProps {
2121
onClose?: () => void;
2222
disableMaximize?: boolean;
2323
disableMinimize?: boolean;
24+
maximized?: boolean;
2425
}
2526

2627
export interface WindowControlsProps extends ControlProps {

src/title-bar/window-controls/index.tsx

Lines changed: 24 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
import React, { useContext } from 'react';
22
import PropTypes from 'prop-types';
33
import styles from '../style.css';
4-
import { MinimizeIcon, MaximizeIcon, CloseIcon } from './icons';
4+
import { MinimizeIcon, MaximizeIcon, CloseIcon, RestoreIcon } from './icons';
55
import { ThemeContext } from '../theme';
66
import WindowButton from './button';
77
import { WindowControlsProps, ControlsTheme } from '../typings';
88

9-
const buttons = (isWin: boolean, onMinimize: () => void, onMaximize: () => void, onClose: () => void) => ([
9+
const buttons = (isWin: boolean, maximized: boolean, onMinimize: () => void, onMaximize: () => void, onClose: () => void) => ([
1010
{
1111
type: 'minimize',
1212
onClick: onMinimize,
@@ -15,19 +15,22 @@ const buttons = (isWin: boolean, onMinimize: () => void, onMaximize: () => void,
1515
{
1616
type: 'maximize',
1717
onClick: onMaximize,
18-
icon: <MaximizeIcon isWin={isWin} />
18+
icon: maximized ? <RestoreIcon isWin={isWin} /> : <MaximizeIcon isWin={isWin} />
1919
},
2020
{
2121
type: 'close',
2222
onClick: onClose,
2323
icon: <CloseIcon isWin={isWin} />
2424
}
25-
])
25+
]);
2626

2727
const WindowControls = ({
2828
onMinimize,
2929
onMaximize,
3030
onClose,
31+
maximized,
32+
disableMinimize,
33+
disableMaximize,
3134
focused
3235
}: WindowControlsProps) => {
3336
const {
@@ -36,7 +39,8 @@ const WindowControls = ({
3639
controls
3740
} = useContext(ThemeContext);
3841
const isWin = platform === 'win32';
39-
const width = platform === 'win32' ? '146px' : '120px';
42+
const itemWidth = isWin ? 48 : 40;
43+
const width = itemWidth * (3 - (disableMaximize ? 1 : 0) - (disableMinimize ? 1 : 0));
4044
return (
4145
<div
4246
className={styles.ControlsWrapper}
@@ -46,19 +50,21 @@ const WindowControls = ({
4650
}}
4751
>
4852
{
49-
buttons(isWin, onMinimize!, onMaximize!, onClose!).map((b) => {
50-
return (
51-
<WindowButton
52-
key={b.type}
53-
platform={platform}
54-
close={b.type === 'close'}
55-
onClick={b.onClick}
56-
controls={controls as Required<ControlsTheme>}
57-
>
58-
{b.icon}
59-
</WindowButton>
60-
)
61-
})
53+
buttons(isWin, maximized ?? false, onMinimize!, onMaximize!, onClose!)
54+
.filter(x => !(disableMaximize && x.type == 'maximize' || disableMinimize && x.type == 'minimize'))
55+
.map((b) => {
56+
return (
57+
<WindowButton
58+
key={b.type}
59+
platform={platform}
60+
close={b.type === 'close'}
61+
onClick={b.onClick}
62+
controls={controls as Required<ControlsTheme>}
63+
>
64+
{b.icon}
65+
</WindowButton>
66+
)
67+
})
6268
}
6369
</div>
6470
);

0 commit comments

Comments
 (0)