@@ -135,6 +135,10 @@ export class AppWindow extends BrowserWindow {
135135 this . selectContainer ( this . containers . find ( ( x ) => x . id === id ) ) ;
136136 } ) ;
137137
138+ ipcMain . on ( 'new-tab' , ( ) => {
139+ this . newTab ( ) ;
140+ } ) ;
141+
138142 ipcMain . on ( 'detach-window' , ( e , id : number ) => {
139143 for ( const container of this . containers ) {
140144 container . removeWindow ( id , true ) ;
@@ -241,29 +245,7 @@ export class AppWindow extends BrowserWindow {
241245 this . draggedWindow . resizing = false ;
242246
243247 if ( this . willAttachWindow ) {
244- const win = this . draggedWindow ;
245- const container = this . draggedContainer ;
246-
247- if ( platform ( ) === 'win32' ) {
248- const handle = this . getNativeWindowHandle ( ) . readInt32LE ( 0 ) ;
249- win . setOwner ( handle ) ;
250- }
251-
252- if ( platform ( ) === 'darwin' && this . containers . length === 0 ) {
253- this . setBounds ( { height : TOOLBAR_HEIGHT } as any ) ;
254- this . setMaximumSize ( 0 , TOOLBAR_HEIGHT ) ;
255- }
256-
257- this . containers . push ( this . draggedContainer ) ;
258- this . willAttachWindow = false ;
259-
260- this . draggedContainer . rearrangeWindows ( ) ;
261-
262- setTimeout ( ( ) => {
263- this . selectContainer ( container ) ;
264- } , 50 ) ;
265-
266- this . controlShortcuts ( win . id ) ;
248+ this . attachNewContainer ( this . draggedContainer , this . draggedWindow ) ;
267249 } else if ( this . willSplitWindow && ! this . detached ) {
268250 this . willSplitWindow = false ;
269251
@@ -420,34 +402,9 @@ export class AppWindow extends BrowserWindow {
420402 this . selectedContainer . removeWindow ( win . id , e . type === 'mouseup' ) ;
421403 }
422404
423- const container = new Container ( this , win ) ;
424-
425- const title = this . draggedWindow . getTitle ( ) ;
405+ const container = this . prepareContainer ( this . draggedWindow ) ;
426406
427407 this . draggedContainer = container ;
428- win . lastTitle = title ;
429-
430- const icon = fileIcon ( win . path , 16 ) ;
431-
432- this . webContents . send ( 'add-tab' , {
433- id : container . id ,
434- title,
435- icon,
436- } ) ;
437-
438- try {
439- Vibrant . from ( icon )
440- . getPalette ( )
441- . then ( ( palette ) => {
442- this . webContents . send (
443- 'tab-background' ,
444- container . id ,
445- palette . Vibrant . hex ,
446- ) ;
447- } ) ;
448- } catch ( e ) {
449- console . error ( e ) ;
450- }
451408
452409 this . draggedIn = true ;
453410 this . willAttachWindow = true ;
@@ -461,6 +418,92 @@ export class AppWindow extends BrowserWindow {
461418 }
462419 }
463420
421+ public prepareContainer ( window : ProcessWindow , dragged = true ) {
422+ const container = new Container ( this , window , dragged ) ;
423+
424+ const title = window . getTitle ( ) ;
425+ const icon = fileIcon ( window . path , 16 ) ;
426+
427+ window . lastTitle = title ;
428+
429+ this . webContents . send ( 'add-tab' , {
430+ id : container . id ,
431+ title,
432+ icon,
433+ } ) ;
434+
435+ try {
436+ Vibrant . from ( icon )
437+ . getPalette ( )
438+ . then ( ( palette ) => {
439+ this . webContents . send (
440+ 'tab-background' ,
441+ container . id ,
442+ palette . Vibrant . hex ,
443+ ) ;
444+ } ) ;
445+ } catch ( e ) {
446+ console . error ( e ) ;
447+ }
448+
449+ return container ;
450+ }
451+
452+ public attachNewContainer ( container : Container , win : ProcessWindow ) {
453+ if ( platform ( ) === 'win32' ) {
454+ const handle = this . getNativeWindowHandle ( ) . readInt32LE ( 0 ) ;
455+ win . setOwner ( handle ) ;
456+ }
457+
458+ if ( platform ( ) === 'darwin' && this . containers . length === 0 ) {
459+ this . setBounds ( { height : TOOLBAR_HEIGHT } as any ) ;
460+ this . setMaximumSize ( 0 , TOOLBAR_HEIGHT ) ;
461+ }
462+
463+ this . containers . push ( container ) ;
464+ this . willAttachWindow = false ;
465+
466+ container . rearrangeWindows ( ) ;
467+
468+ setTimeout ( ( ) => {
469+ this . selectContainer ( container ) ;
470+ } , 50 ) ;
471+
472+ this . controlShortcuts ( win . id ) ;
473+ }
474+
475+ public async newTab ( ) {
476+ if ( this . selectedContainer ?. windows ?. length === 1 ) {
477+ const window = await this . createNewWindow (
478+ this . selectedContainer . windows [ 0 ] . path ,
479+ ) ;
480+
481+ this . addTabWithWindow ( window ) ;
482+ }
483+ }
484+
485+ public createNewWindow ( path : string ) : Promise < ProcessWindow > {
486+ return new Promise ( ( resolve , reject ) => {
487+ windowManager . createProcess ( path ) ;
488+
489+ const timeout = setTimeout ( ( ) => {
490+ reject ( new Error ( 'Creating new window timed out' ) ) ;
491+ } , 5000 ) ;
492+
493+ windowManager . once ( 'window-activated' , ( win : ProcessWindow ) => {
494+ if ( win . path === path ) {
495+ clearTimeout ( timeout ) ;
496+ resolve ( new ProcessWindow ( win . id , this ) ) ;
497+ }
498+ } ) ;
499+ } ) ;
500+ }
501+
502+ public addTabWithWindow ( win : ProcessWindow ) {
503+ const container = this . prepareContainer ( win , false ) ;
504+ this . attachNewContainer ( container , win ) ;
505+ }
506+
464507 public getContentArea ( ) {
465508 const bounds = this . getContentBounds ( ) ;
466509
0 commit comments