Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

nw.js window.on() not registered if app started as hidden #5013

Closed
anjanapaulose opened this issue Jun 22, 2016 · 14 comments
Closed

nw.js window.on() not registered if app started as hidden #5013

anjanapaulose opened this issue Jun 22, 2016 · 14 comments
Assignees

Comments

@anjanapaulose
Copy link

anjanapaulose commented Jun 22, 2016

I am using nw 0.14.5 version.
I have to start the nw app in tray mode. I have defined the "on" function for "focus" as

var win= nw.Window.get();
win.on("focus", function(){
win.show();
});

and for tray click,

tray.on('click', function() {
win.show();
win.focus();
});

Now when I click on tray, the app comes into view as normal. I also have a shortcut to this app in start menu.

The issue is as follows:

The app is started as tray. I clicked atleast once on tray and app has came into view atleast once. Minimize will reduce the app to tray.
After this, if i try the startmenu shortcut, app comes into view as expected.

But if the app is started as tray, and I have never clicked tray, which means the app has never come into view.
In this case, if I try the start menu shortcut, the win.on("focus") code is never getting triggered, failing the app to show up.

If the app is already displayed atleast once, this issue is not seen. The window object itself is not defined unless the app is shown once.

Is there any condition like the win.on("focus") or such events are registered only when app is shown.
Please suggest a solution to this.

@anjanapaulose
Copy link
Author

Hi, any updates on this bug?

@ghostoy
Copy link
Member

ghostoy commented Jun 30, 2016

What do you mean by 'try the start menu shortcut'?

@anjanapaulose
Copy link
Author

Just create a exe which brings the running nw to focus.
Sample:

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.ComponentModel;
using System.Runtime.InteropServices;

namespace Sample
{
class Program
{

    [DllImport("user32.dll")]
    private static extern
    bool SetForegroundWindow(IntPtr hWnd);
    [DllImport("user32.dll")]
    private static extern
    bool ShowWindowAsync(IntPtr hWnd, int nCmdShow);
    [DllImport("user32.dll")]
    private static extern
    bool IsIconic(IntPtr hWnd);

    static void Main()
    {            
        var currentSessionID = Process.GetCurrentProcess().SessionId;
        Process[] processes = Process.GetProcessesByName("nw");           
        int currentProcID = Process.GetCurrentProcess().Id;
        Process[] nwInThisSession = (from process in processes where process.SessionId == currentSessionID && process.Id != currentProcID select process).ToArray();
        if (nwInThisSession != null && nwInThisSession.Length > 0)
        {
            int SW_RESTORE = 9;                              
            foreach (Process p in nwInThisSession)
            {
                if (!string.IsNullOrEmpty(p.MainWindowTitle))
                {
                    IntPtr hWnd = p.MainWindowHandle;
                    if (IsIconic(hWnd))
                    {
                        ShowWindowAsync(hWnd, SW_RESTORE);
                    }
                    // bring it to the foreground
                    SetForegroundWindow(hWnd);
                    break;
                }
            }
        }          

    }
}

}

@ghostoy
Copy link
Member

ghostoy commented Jul 8, 2016

@anjanapaulose I haven't tested your C# app. But I think IsIconic is used to test if windows is minimized not invisible. So could you try test with IsWindowVisible and call ShowWindowAsync with SW_SHOW or SW_NORMAL?

@ghostoy
Copy link
Member

ghostoy commented Jul 14, 2016

@anjanapaulose Does my suggestions work for you?

@anjanapaulose
Copy link
Author

Sorry for the delay, tried as per your suggestion. Didn't work.

@ghostoy
Copy link
Member

ghostoy commented Jul 17, 2016

Could you upload the entire project of your C# testing app so that I can test with?

@anjanapaulose
Copy link
Author

I was unable to upload the whole project as zip, it was failing. Please see the attached txt file, rename it to Program.cs. I used a Visual Studio project with output as exe. Please go though the comments added to know which case is working for what scenarios.

Program.txt

@ghostoy
Copy link
Member

ghostoy commented Jul 20, 2016

This should be the limitation of Windows API. See http://stackoverflow.com/questions/16185217/c-sharp-process-mainwindowhandle-always-returns-intptr-zero.

A process has a main window associated with it only if the process has a graphical interface. If the associated process does not have a main window, the MainWindowHandle value is zero. The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. This can be the case for processes that appear as icons in the notification area, at the far right of the taskbar.

@ghostoy ghostoy closed this as completed Jul 20, 2016
@anjanapaulose
Copy link
Author

Thanks, I am not so familiar with windows processes, just a doubt

"The value is also zero for processes that have been hidden, that is, processes that are not visible in the taskbar. "

So if the process is in task bar, it should come up, right?. So in this case, it will always come once its clicked. Whats the difference if tray icon is once clicked and not.

@ghostoy
Copy link
Member

ghostoy commented Jul 28, 2016

@anjanapaulose I'm confused. Here is following you addressed previously.

But if the app is started as tray, and I have never clicked tray, which means the app has never come into view.

I assume it means the window is hidden on start since "the app has never come into view" and the title of this issue is "... if app started as hidden". In this case, there will not be a taskbar icon, and you will not get the window's handle or title.

@anjanapaulose
Copy link
Author

Ohh, yes, that is one case. If the app itself is started as hidden, it will never come into view.

I have also seen the below scenario.
If I start the app as tray, the program doesn't work unless I click on tray once. This means app is in tray, still I cant use the shortcut. Once I click tray, and app comes into view, then onwards the shortcut works even if the app is hidden as tray(not minimized). You could try that with the same sample app

@ghostoy
Copy link
Member

ghostoy commented Jul 29, 2016

@anjanapaulose If it's hidden and only shown in tray, you will not get the window handle and get it shown. That's the limit of Windows API. If it's minimized as icon in the task bar, then your shortcut will work.

@anjanapaulose
Copy link
Author

Ok, thanks for the clarification.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants