How to resume an already running node-webkit window app?

I currently have a node-webkit app for Mac and Windows. When you close the window, the application continues to run in the background and can be reopened through the system tray.

I want my users to open a window from an executable (think of a shortcut to an .exe on the desktop).

On Mac, I did this to allow users to open a window from the Dock:

var gui = require('nw.gui');

gui.App.on('reopen', function() {
    win.show();
});

      

But according to the documentation, the 'reopen' event is a Mac-only function.

Is it possible to have the same behavior on Windows? How?

+3


source to share


2 answers


Yes, there is no reopen function for windows yet, but an alternative would be to use the window.hide()

and tray functions as described in Tray .
In my application, I created custom minimize, maximize and close buttons and provide the following functions for them:

var gui = require('nw.gui');
var window = gui.Window.get();  
var isMaximised = false;

//Minimise:  
minimise: function(){
    window.hide();
    var tray = new gui.Tray({title: 'Tray'});
    var menuTray = new gui.Menu();
    menuTray.append(new gui.MenuItem({ label : 'quit',
      click: function(){
        window.close();
      }
    }));
    tray.menu = menuTray;

    tray.on('click',function(){
      tray.remove();
      window.show();
      tray = null;
    });
}  

//Maximise  
maximise: function(){  
  if (isMaximized) {
    isMaximized = false;
    window.unmaximize();
  } else {
    isMaximized = true;
    window.maximize();
  }
}  

 //Close  
close: function(){  
   window.hide();
   var tray = new gui.Tray({title: 'Tray'});
   var menuTray = new gui.Menu();
   menuTray.append(new gui.MenuItem({ label : 'quit',
       click: function(){
       tray.remove();
       window.close();
       }
   }));
   tray.menu = menuTray;
   tray.on('click',function(){
     tray.remove();
     window.show();
     tray = null;
   });
}

      



So, when you close the app by pressing the close button, it hides the app and creates a menu item in the Dock. When the dock icon is clicked, it shows the application back to the user.
Now, only closing the application can be done by right-clicking on the dock item and selecting the close button, which closes the application.

+2


source


I had a problem and fixed it by using listener to avoid reopening, here is a simple code for your desktop simple Json manifest "package.json":

{
  "main": "index.html",
  "name": "reopen from Desktop",
  "window":
  {
  "width": 300,
  "height": 200
  }
}

      

then a simple index.html file



<html>
 <head>
  <script>
   var gui = require('nw.gui');
   var win = gui.Window.get();
  </script>
 </head>
 <body>
  <script>
   //list to open and make the app show
   gui.App.on('open', function() {
   win.show();
   alert("file re-open");
   });

   // prevent close and make it hide
   win.on('close', function() {
   win.hide(); 
   });
  </script>
  <!-- forse close the app -->
   <center><br><br><input type="button" value="exit app" onclick="win.close(true)"></center>
  </body>
 </html>

      

then copy and run the node-webkit files for the window in the same directory as the normal package path and then rename nw.exe to reopen.exe and then right click and create a desktop shortcut and open the exe file from the folder and click on close which will hide it, then click on the shortcut on the desktop and it will display it again "reopen" with a warning

+1


source







All Articles