Skip to content

Commit 7163a81

Browse files
committed
Activate existing open files
1 parent fe26eae commit 7163a81

File tree

6 files changed

+76
-23
lines changed

6 files changed

+76
-23
lines changed

src/components/app.js

+23-2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,27 @@ class AppComponent extends React.Component {
6161
});
6262
};
6363

64+
handleTabActivation = (e) => {
65+
const { windowId, tabId } = e;
66+
console.log('Got tab activation event?!', e);
67+
68+
if (windowId !== this.windowId) {
69+
return;
70+
}
71+
72+
const { tabs } = this.state;
73+
74+
this.setState({
75+
activeTabId: tabId,
76+
tabs: Object.values(tabs).reduce((allTabs, tab) => Object.assign(allTabs, {
77+
[tab.tabId]: {
78+
...tab,
79+
isActive: tab.tabId === tabId
80+
}
81+
}), {})
82+
})
83+
};
84+
6485
handleCloseTab = ({ tabId, windowId }) => {
6586
if (windowId !== this.windowId) {
6687
return;
@@ -230,9 +251,9 @@ class AppComponent extends React.Component {
230251
[NEW_TAB]: this.handleNewTab,
231252
[OPEN_FILE]: this.handleOpenFile,
232253
[RENDER_RESULT]: this.handleRender,
233-
[SAVE_DOT_FILE]: this.handleSave,
234254
[SAVE_COMPLETED]: this.handleSaveCompleted,
235-
[CLOSE_TAB]: this.handleCloseTab
255+
[CLOSE_TAB]: this.handleCloseTab,
256+
[SET_ACTIVE_TAB]: this.handleTabActivation
236257
}}
237258
/>
238259
);

src/dialogs/open.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export default function showOpenDialog() {
99
filters,
1010
properties: ["openFile"]
1111
},
12-
filePaths => resolve(filePaths[0])
12+
filePaths => resolve(filePaths && filePaths[0])
1313
);
1414
});
1515
}

src/menu/index.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ function setupMenu() {
1111
const emitter = new EventEmitter();
1212

1313
const emit = (eventName, payload) => (menuItem, browserWindow, event) => {
14-
emitter.emit(eventName, { menuItem, browserWindow, event, payload });
14+
emitter.emit(eventName, event, { menuItem, browserWindow, ...payload });
1515
};
1616

1717
const menu = Menu.buildFromTemplate([

src/sessions/app.js

+25-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import createMenu from "../menu/index";
22
import createWindowSession from "./window";
33
import SessionManager from "./session-manager";
4-
import { NEW_WINDOW, WINDOW_CLOSED } from "../constants/messages";
4+
import { NEW_WINDOW, OPEN_FILE, WINDOW_CLOSED } from "../constants/messages";
5+
import showOpenDialog from "../dialogs/open";
56

67
class AppSession extends SessionManager {
78
menu = null;
@@ -13,7 +14,8 @@ class AppSession extends SessionManager {
1314
this.menu = createMenu();
1415
this.openWindow();
1516

16-
this.subscribeToEvent(this.menu, NEW_WINDOW, this.openWindow);
17+
this.subscribeToMenuEvent(NEW_WINDOW, this.openWindow);
18+
this.subscribeToMenuEvent(OPEN_FILE, this.showOpenDialog);
1719
}
1820

1921
openWindow = () => {
@@ -23,11 +25,32 @@ class AppSession extends SessionManager {
2325
this.subscribeToEvent(windowSession, WINDOW_CLOSED, () =>
2426
this.handleWindowClosed(windowSession)
2527
);
28+
29+
return windowSession;
30+
};
31+
32+
showOpenDialog = async ({ browserWindow }) => {
33+
const filename = await showOpenDialog();
34+
if (!filename) {
35+
return;
36+
}
37+
38+
const windowSessions = Object.values(this.windowSessions);
39+
const target = windowSessions.find(w => w.hasFileOpen(filename))
40+
|| windowSessions.find(w => w.window === browserWindow)
41+
|| this.openWindow();
42+
43+
await target.openFile(filename);
44+
target.window.focus();
2645
};
2746

2847
handleWindowClosed(windowSession) {
2948
delete this.windowSessions[windowSession.id];
3049
}
50+
51+
subscribeToMenuEvent(eventName, ...handlers) {
52+
return this.subscribeToEvent(this.menu, eventName, ...handlers);
53+
}
3154
}
3255

3356
export default function startAppSession() {

src/sessions/tab.js

+5-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
NEW_TAB,
1919
OPEN_FILE,
2020
RENDER_RESULT,
21-
SAVE_COMPLETED,
21+
SAVE_COMPLETED, SET_ACTIVE_TAB,
2222
SOURCE_CHANGED
2323
} from "../constants/messages";
2424
import showSaveDialog from "../dialogs/save";
@@ -85,6 +85,10 @@ class TabSession extends SessionManager {
8585

8686
setIsActive(isActive) {
8787
this.isActive = isActive;
88+
89+
if (isActive) {
90+
this.sendTabEvent(SET_ACTIVE_TAB);
91+
}
8892
}
8993

9094
async open(filename) {

src/sessions/window.js

+21-16
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,19 @@
11
import { fromEvent } from "rxjs";
22
import { filter } from "rxjs/operators";
3-
import { BrowserWindow, dialog, ipcMain } from "electron";
3+
import { BrowserWindow, ipcMain } from "electron";
44

55
import {
66
CLOSE_TAB,
77
DECREASE_FONT,
88
INCREASE_FONT,
99
NEW_TAB,
10-
OPEN_FILE,
1110
SAVE_BUFFER,
1211
SET_ACTIVE_TAB,
1312
WINDOW_CLOSED,
1413
WINDOW_READY
1514
} from "../constants/messages";
1615
import SessionManager from "./session-manager";
1716
import createTabSession from "./tab";
18-
import showOpenDialog from "../dialogs/open";
1917

2018
class WindowSession extends SessionManager {
2119
window = null;
@@ -47,7 +45,7 @@ class WindowSession extends SessionManager {
4745
windowId === this.windowId && this.setActiveTab(tabId)
4846
);
4947

50-
this.subscribeToEvent(this.window, 'close', this.closeAllTabs);
48+
this.subscribeToEvent(this.window, "close", this.closeAllTabs);
5149
this.subscribeToEvent(this.window, "closed", this.dispose);
5250

5351
this.setupMenuListeners();
@@ -59,7 +57,6 @@ class WindowSession extends SessionManager {
5957
this.handleMenuEvent(NEW_TAB, this.openTab);
6058
this.handleMenuEvent(CLOSE_TAB, this.closeTab);
6159

62-
this.handleMenuEvent(OPEN_FILE, this.showOpenFileDialog);
6360
this.handleMenuEvent(SAVE_BUFFER, this.saveActiveBuffer);
6461

6562
this.handleMenuEvent(INCREASE_FONT, () =>
@@ -104,17 +101,21 @@ class WindowSession extends SessionManager {
104101
});
105102
};
106103

107-
showOpenFileDialog = async () => {
108-
const filename = await showOpenDialog();
109-
if (!filename) {
110-
return;
111-
}
112-
113-
let tab = this.activeTabSession;
114-
if (tab.filename || tab.isDirty) tab = this.openTab();
104+
async openFile(filename) {
105+
const existingTab = Object.values(this.tabSessions).find(
106+
t => t.filename === filename
107+
);
115108

116-
await tab.open(filename);
117-
};
109+
if (existingTab) {
110+
this.setActiveTab(existingTab.id);
111+
} else {
112+
const activeTab = this.activeTabSession;
113+
const targetTab = (activeTab.filename || activeTab.isDirty)
114+
? this.openTab()
115+
: activeTab;
116+
await targetTab.open(filename);
117+
}
118+
}
118119

119120
saveActiveBuffer = () => {
120121
this.activeTabSession.save();
@@ -139,9 +140,13 @@ class WindowSession extends SessionManager {
139140
return Object.values(this.tabSessions).find(s => s.isActive);
140141
}
141142

143+
hasFileOpen(filename) {
144+
return Object.values(this.tabSessions).some(t => t.filename === filename);
145+
}
146+
142147
handleMenuEvent(eventName, ...handlers) {
143148
return this.subscribeTo(
144-
fromEvent(this.menu, eventName).pipe(
149+
this.eventsFrom(this.menu, eventName).pipe(
145150
filter(({ browserWindow }) => browserWindow === this.window)
146151
),
147152
...handlers

0 commit comments

Comments
 (0)