Skip to content

Commit 9a82530

Browse files
Chugunov RomanChugunov Roman
authored andcommitted
Added closing a tab on the middle mouse click
1 parent 7886ff1 commit 9a82530

4 files changed

Lines changed: 34 additions & 11 deletions

File tree

src/main/webContent/Tabs.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ class Tabs implements ITabs {
2828
});
2929
tab.setBounds(options);
3030
tab.webContents.loadURL(url);
31-
isDev && tab.webContents.toggleDevTools();
31+
// isDev && tab.webContents.toggleDevTools();
3232

3333
Tabs.tabs.push(tab);
3434

src/main/webContent/WindowManager.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,8 @@ class WindowManager implements IWindowManager {
7878
}, 600);
7979

8080
if (isDev) this.devtools();
81-
if (isDev) this.mainWindow.webContents.toggleDevTools();
81+
if (isDev) this.mainWindow.webContents.openDevTools({ mode: 'detach' });
82+
// if (isDev) this.mainWindow.webContents.toggleDevTools();
8283
});
8384
E.app.on('window-all-closed', this.onWindowAllClosed);
8485

src/renderer/components/Tabs/index.tsx

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
1+
/// <reference path="../../../../@types/common/index.d.ts" />
12
/// <reference path="../../../../@types/renderer/stores/index.d.ts" />
3+
24
import { ipcRenderer } from "electron";
35
import { Component } from "react";
46
import { h } from 'preact';
@@ -23,7 +25,7 @@ class Tabs extends Component<TabsProps, {}> {
2325
this.props = props;
2426
}
2527

26-
private close = (e: React.MouseEvent<HTMLDivElement> & Event, id: number) => {
28+
private close = (e: MouseEvent & Event, id: number) => {
2729
e.stopPropagation();
2830
e.stopImmediatePropagation();
2931

@@ -44,12 +46,29 @@ class Tabs extends Component<TabsProps, {}> {
4446
1
4547
);
4648
}
47-
49+
4850
private newTab = () => {
4951
ipcRenderer.send('newtab');
5052
}
5153

52-
private focus = (e: React.MouseEvent<HTMLDivElement> & Event, id: number) => {
54+
private clickTab = (event: MouseEvent & Event, tab: Tab) => {
55+
switch(event.button) {
56+
// Handle left click, set focuse on the target tab
57+
case 0: {
58+
this.focus(event, tab.id);
59+
} break;
60+
// Handle middle click, close tab
61+
case 1: {
62+
this.close(event, tab.id);
63+
} break;
64+
// Handle right click, invoke the popup menu
65+
case 2: {
66+
// console.log('clickTab right click, ', event.button);
67+
} break;
68+
}
69+
}
70+
71+
private focus = (e: MouseEvent & Event, id: number) => {
5372
e.stopPropagation();
5473
e.stopImmediatePropagation();
5574

@@ -63,10 +82,9 @@ class Tabs extends Component<TabsProps, {}> {
6382
tabs={toJS(this.props.tabs) as ITabsStore}
6483
close={this.close}
6584
newTab={this.newTab}
66-
focus={this.focus}
85+
clickTab={this.clickTab}
6786
/>
6887
)
69-
7088
}
7189
}
7290

src/renderer/components/Tabs/tabs.tsx

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
1+
/// <reference path="../../../../@types/common/index.d.ts" />
12
/// <reference path="../../../../@types/renderer/stores/index.d.ts" />
23
import { h } from 'preact';
34

45
interface Props {
56
tabs: ITabsStore;
67

7-
close(e: React.MouseEvent<HTMLDivElement> & Event, id: number): void
8-
focus(e: React.MouseEvent<HTMLDivElement> & Event, id: number): void
8+
close(e: MouseEvent & Event, id: number): void
9+
clickTab(e: MouseEvent & Event, tab: Tab): void
910
newTab(): void
1011
}
1112

@@ -18,9 +19,12 @@ function Tabs(props: Props) {
1819
</svg>
1920
</div>
2021
{props.tabs.tabs.map(t => (
21-
<div className={`tab ${props.tabs.current === t.id ? 'active' : ''}`} onClick={(e) => props.focus(e as React.MouseEvent<HTMLDivElement> & Event, t.id)}>
22+
<div
23+
className={`tab ${props.tabs.current === t.id ? 'active' : ''}`}
24+
onMouseDown={(e) => props.clickTab(e, t)}
25+
>
2226
<span>{t.title}</span>
23-
<div className="tabClose" onClick={(e) => props.close(e as React.MouseEvent<HTMLDivElement> & Event, t.id)}>
27+
<div className="tabClose" onClick={(e) => props.close(e, t.id)}>
2428
<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 21.9 21.9" enable-background="new 0 0 21.9 21.9" width="8px" height="8px">
2529
<path d="M14.1,11.3c-0.2-0.2-0.2-0.5,0-0.7l7.5-7.5c0.2-0.2,0.3-0.5,0.3-0.7s-0.1-0.5-0.3-0.7l-1.4-1.4C20,0.1,19.7,0,19.5,0 c-0.3,0-0.5,0.1-0.7,0.3l-7.5,7.5c-0.2,0.2-0.5,0.2-0.7,0L3.1,0.3C2.9,0.1,2.6,0,2.4,0S1.9,0.1,1.7,0.3L0.3,1.7C0.1,1.9,0,2.2,0,2.4 s0.1,0.5,0.3,0.7l7.5,7.5c0.2,0.2,0.2,0.5,0,0.7l-7.5,7.5C0.1,19,0,19.3,0,19.5s0.1,0.5,0.3,0.7l1.4,1.4c0.2,0.2,0.5,0.3,0.7,0.3 s0.5-0.1,0.7-0.3l7.5-7.5c0.2-0.2,0.5-0.2,0.7,0l7.5,7.5c0.2,0.2,0.5,0.3,0.7,0.3s0.5-0.1,0.7-0.3l1.4-1.4c0.2-0.2,0.3-0.5,0.3-0.7 s-0.1-0.5-0.3-0.7L14.1,11.3z" fill="#FFFFFF"/>
2630
</svg>

0 commit comments

Comments
 (0)