1
1
import { fromEvent } from "rxjs" ;
2
2
import { filter } from "rxjs/operators" ;
3
- import { BrowserWindow , dialog , ipcMain } from "electron" ;
3
+ import { BrowserWindow , ipcMain } from "electron" ;
4
4
5
5
import {
6
6
CLOSE_TAB ,
7
7
DECREASE_FONT ,
8
8
INCREASE_FONT ,
9
9
NEW_TAB ,
10
- OPEN_FILE ,
11
10
SAVE_BUFFER ,
12
11
SET_ACTIVE_TAB ,
13
12
WINDOW_CLOSED ,
14
13
WINDOW_READY
15
14
} from "../constants/messages" ;
16
15
import SessionManager from "./session-manager" ;
17
16
import createTabSession from "./tab" ;
18
- import showOpenDialog from "../dialogs/open" ;
19
17
20
18
class WindowSession extends SessionManager {
21
19
window = null ;
@@ -47,7 +45,7 @@ class WindowSession extends SessionManager {
47
45
windowId === this . windowId && this . setActiveTab ( tabId )
48
46
) ;
49
47
50
- this . subscribeToEvent ( this . window , ' close' , this . closeAllTabs ) ;
48
+ this . subscribeToEvent ( this . window , " close" , this . closeAllTabs ) ;
51
49
this . subscribeToEvent ( this . window , "closed" , this . dispose ) ;
52
50
53
51
this . setupMenuListeners ( ) ;
@@ -59,7 +57,6 @@ class WindowSession extends SessionManager {
59
57
this . handleMenuEvent ( NEW_TAB , this . openTab ) ;
60
58
this . handleMenuEvent ( CLOSE_TAB , this . closeTab ) ;
61
59
62
- this . handleMenuEvent ( OPEN_FILE , this . showOpenFileDialog ) ;
63
60
this . handleMenuEvent ( SAVE_BUFFER , this . saveActiveBuffer ) ;
64
61
65
62
this . handleMenuEvent ( INCREASE_FONT , ( ) =>
@@ -104,17 +101,21 @@ class WindowSession extends SessionManager {
104
101
} ) ;
105
102
} ;
106
103
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
+ ) ;
115
108
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
+ }
118
119
119
120
saveActiveBuffer = ( ) => {
120
121
this . activeTabSession . save ( ) ;
@@ -139,9 +140,13 @@ class WindowSession extends SessionManager {
139
140
return Object . values ( this . tabSessions ) . find ( s => s . isActive ) ;
140
141
}
141
142
143
+ hasFileOpen ( filename ) {
144
+ return Object . values ( this . tabSessions ) . some ( t => t . filename === filename ) ;
145
+ }
146
+
142
147
handleMenuEvent ( eventName , ...handlers ) {
143
148
return this . subscribeTo (
144
- fromEvent ( this . menu , eventName ) . pipe (
149
+ this . eventsFrom ( this . menu , eventName ) . pipe (
145
150
filter ( ( { browserWindow } ) => browserWindow === this . window )
146
151
) ,
147
152
...handlers
0 commit comments