@@ -6,18 +6,19 @@ const fs = require('fs');
66const icpChannel = require ( '../../lib/icpChannel' ) ;
77const Env = require ( '../../env' ) ;
88const { getPageTitle } = require ( '../../lib/utils' ) ;
9+ const BaseModule = require ( '../../lib/BaseModule' ) ;
10+ const kleur = require ( 'kleur' ) ;
11+ const ipcChannel = require ( '../../lib/icpChannel' ) ;
912
1013// Sample Module. Plase copy-paste this file into new module's main folder
11- class Toolbar extends require ( '../../lib/ BaseModule' )
14+ class Toolbar extends BaseModule
1215{
1316 MODULE_NAME = "toolbar" ; // MUST be the same as the 'extension' field in config.json
1417 static toolbar_tab ;
1518 tabs_count = 0 ;
1619 track_active_tab = false ;
1720 first_avoided = false ;
1821
19- // required_modules = ['window-events'];
20-
2122 onNewTabCreated ( new_tab , old_tab )
2223 {
2324 new_tab . webContents . setWindowOpenHandler ( ( details ) => {
@@ -33,48 +34,67 @@ class Toolbar extends require('../../lib/BaseModule')
3334 {
3435 if ( this . __conf . hidden != true )
3536 {
36- Toolbar . toolbar_tab = new WebContentsView ( {
37+ this . tab = new WebContentsView ( {
3738 webPreferences : {
3839 preload : path . join ( __dirname , '../single_preload.js' ) ,
3940 additionalArguments : [ '--load-only=' + path . join ( __dirname , 'toolbar_preload.js' ) ] ,
4041 ...Env . WEBVIEW_DEFAULT_PREFERENCES
4142 } } ) ;
43+ Toolbar . toolbar_tab = this . tab ;
44+ this . tab . tab_id = 'toolbar' ;
4245
43- this . tab = Toolbar . toolbar_tab ;
44-
45- Toolbar . toolbar_tab . webContents . loadURL ( url . format ( {
46+ this . tab . webContents . loadURL ( url . format ( {
4647 pathname : path . join ( __dirname , this . __conf . toolbar_html ) ,
4748 protocol : 'file'
4849 } ) ) ;
4950
5051 this . warn ( { height : this . __conf . toolbar_width } ) ;
5152 if ( this . __conf . default_url . protocol == 'file' ) this . __conf . default_url . pathname = path . join ( __dirname , this . __conf . default_url . pathname ) ;
52- TabsManager . newSideTab ( Toolbar . toolbar_tab , 'toolbar' , { height : this . __conf . toolbar_width } ) ;
53+ TabsManager . newSideTab ( this . tab , 'toolbar' , { height : this . __conf . toolbar_width } ) ;
5354 TabsManager . newDefaultBounds ( { y : this . __conf . toolbar_width } ) ;
5455
55-
56- this . newCtrlShortcut ( 'r' , ( ) => { Toolbar . toolbar_tab . webContents . reload ( ) ; } ) ;
56+ this . newCtrlShortcut ( 'r' , ( ) => { this . tab . webContents . reload ( ) ; } ) ;
5757
58- fs . watch ( __dirname , ( event ) => { if ( event == "change" ) { Toolbar . toolbar_tab . webContents . reload ( ) ; this . log ( this . __conf . toolbar_html , "has been modified! Reloading toolbar." ) } } ) ;
58+ fs . watch ( __dirname , ( event ) => { if ( event == "change" ) { this . tab . webContents . reload ( ) ; this . log ( this . __conf . toolbar_html , "has been modified! Reloading toolbar." ) } } ) ;
5959
6060 icpChannel . newMainHandler ( 'created-tab' , ( _ , tab_url ) => this . createNewTab ( url . format ( this . __conf . default_url ) ) ) ;
6161 icpChannel . newMainHandler ( 'switch-tab' , ( _ , index ) => this . setActiveTab ( index ) ) ;
62- icpChannel . newMainHandler ( 'close -tab' , ( _ , index ) => this . closeTab ( index ) ) ;
62+ icpChannel . newMainHandler ( 'closed -tab' , ( _ , index ) => this . closeTab ( TabsManager . idToName ( index ) ) ) ;
6363
64- Toolbar . toolbar_tab . webContents . openDevTools ( { mode : 'detach' } ) ;
64+ this . tab . webContents . openDevTools ( { mode : 'detach' } ) ;
6565 }
6666 }
67-
67+
6868 late_setup ( )
6969 {
70+ this . tab . webContents . on ( 'before-input-event' , ( event , input ) => {
71+ if ( input . key )
72+ {
73+ this . warn ( "forwarding event to active tab" ) ;
74+ TabsManager . getActiveTab ( ) . webContents . send ( 'before-input-event' , input ) ;
75+ event . preventDefault ( ) ;
76+ }
77+ } ) ;
7078 if ( this . __conf . create_on_open == true )
7179 this . requestNewTab ( ) ;
7280 }
7381
7482 // this method can be called from ANYWHERE (after initialization), thanks to the singleton behaviour of the modules.
7583 async requestNewTab ( req_url = this . __conf . default_url )
7684 {
77- icpChannel . sendSignalToRender ( 'create-tab' , Toolbar . toolbar_tab , await this . createNewTab ( typeof ( req_url ) == 'string' ? req_url : url . format ( req_url ) , true ) ) ;
85+ icpChannel . sendSignalToRender ( 'create-tab' , this . tab , await this . createNewTab ( typeof ( req_url ) == 'string' ? req_url : url . format ( req_url ) , true ) ) ;
86+ }
87+
88+ static requestCloseTabId ( tab_id )
89+ {
90+ Toolbar . requestCloseTab ( TabsManager . getNameTab ( tab_id ) ) ;
91+ }
92+
93+ static requestCloseTab ( tab )
94+ {
95+ if ( ! tab || ! tab . tab_id ) return console . log ( '[' , kleur . green ( "toolbar" ) , '] cannot close' , tab && tab . tab_id ) ;
96+ console . log ( '[' , kleur . green ( "toolbar" ) , '] closing' , tab . tab_id ) ;
97+ icpChannel . sendSignalToRender ( 'close-tab' , Toolbar . toolbar_tab , tab . tab_id . substring ( 4 ) ) ;
7898 }
7999
80100 // force_url can be set to true ONLY from this extension's context, NOT from the browser's new tab request
@@ -85,11 +105,10 @@ class Toolbar extends require('../../lib/BaseModule')
85105 preload : path . join ( __dirname , '../preload.js' ) , // Secure bridge
86106 ...Env . WEBVIEW_DEFAULT_PREFERENCES
87107 } } ) ;
88- TabsManager . setNewTab ( newTab , "tab_" + this . tabs_count ++ ) ;
89108 const tab_url = force_url ? requested_url : url . format ( this . __conf . default_url ) ;
90109 newTab . webContents . loadURL ( tab_url ) ;
91110 this . log ( "Created new tab at url:" , tab_url ) ;
92- return getPageTitle ( newTab . webContents ) ;
111+ return { success : await TabsManager . newTab ( newTab , "tab_" + this . tabs_count ++ ) , title : await getPageTitle ( newTab . webContents ) } ;
93112 }
94113
95114 setActiveTab ( index ) {
@@ -98,9 +117,10 @@ class Toolbar extends require('../../lib/BaseModule')
98117 // mainWindow.webContents.send("tab-switched", { index });
99118 }
100119
101- closeTab ( tabId )
120+ closeTab ( tab_name )
102121 {
103- TabsManager . closeTab ( tabId ) ;
122+ this . log ( 'renderer requested tab close' )
123+ TabsManager . closeTabName ( tab_name ) ;
104124 }
105125}
106126
0 commit comments