window loses focus when inner widgets are used

classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

window loses focus when inner widgets are used

Hans Hillen
Hi

I have a window which I want to close automatically when it loses focus. I
achieved this by adding an onblur handler to the window tag, which works
fine. However, whenever the user interacts with anything inside the window
(like a button), the window will consider this as losing focus and will cloe
as well. How can I make the window realize that it actually still has focus
when the widgets it contains are selected? I suppose I could write a custom
function that checks everytime the the window loses focus, but I suspect
there is a more efficient and simpler way.


_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: window loses focus when inner widgets are used

Boris Zbarsky
Hans Hillen wrote:
> I have a window which I want to close automatically when it loses focus. I
> achieved this by adding an onblur handler to the window tag

And you check the event target in this handler, right?  I believe we have a bug
where we bubble blur events..

-Boris
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: window loses focus when inner widgets are used

Hans Hillen
Yeah, the blur event doesn't seem to bubble.

I thought I had solved the problem using the following solution:

--------------
//catch focus event in window during capturing phase:
window.addEventListener("focus", removeBlurhandler, true);

//catch focus event in window during bubbling phase:
window.addEventListener("focus", setBlurhandler , false);

function removeBlurHandler(event) {
    //if focus occurs within window object (e.g. a checkbox is clicked),the
blur handler is temporarily removed
    dump("\nFocus event detected in capture phase, removing blur handler");
    window.removeEventListener("blur", handleBlur, false);
}

function setBlurHandler(event) {
    //when focus event bubbles back to the window, (re)set the blur handler
    dump("\nFocus event detected in bubbling phase, (re)enabling blur
handler");
    window.addEventListener("blur", handleBlur, false);
}

function handleBlur(event) {
    dump("\nblur event detected");
    //window.close();
}
-------------------

Of course this is not very elegant, but I thought it would work.
Unfortunately, the blur event still occurs before the focus event,
preventing the focus capturing listener to disable the blur event handler:

This can be seen in the following trace:
-------------------
blur event detected
Focus event detected in capture phase, removing blur handler
Focus event detected in bubbling phase, (re)enabling blur handler
-------------------

If anyone else knows a way to work around this, please let me know, thanks!


_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: window loses focus when inner widgets are used

Neil-4
In reply to this post by Boris Zbarsky
Boris Zbarsky wrote:

> Hans Hillen wrote:
>
>> I have a window which I want to close automatically when it loses
>> focus. I achieved this by adding an onblur handler to the window tag
>
> And you check the event target in this handler, right?

Interestingly I tried this with a trunk build and I seem to get three
blur events (with originalTargets of element/document/window), whereas
with 1.6 I only get a blur event on the element.

--
Warning: May contain traces of nuts.
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: window loses focus when inner widgets are used

Hans Hillen
In reply to this post by Boris Zbarsky
Ok that wasn't so difficult after all. like Boris said I just had to check
the event.target. The reason it wasn't working at first is that I was
checking for the target's ID which for some reason was set as undefined, so
now I just do:

if (event.target.nodeType == 9)
  window.close();

Anyway, I feel really stupid now, but thanks for all the help!

Hans


_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul
Reply | Threaded
Open this post in threaded view
|

Re: window loses focus when inner widgets are used

Neil-4
Hans Hillen wrote:

>if (event.target.nodeType == 9)
>
That's Node.DOCUMENT_NODE for anyone who hasn't memorised the number ;-)

event.target == document also works, of course.

--
Warning: May contain traces of nuts.
_______________________________________________
dev-tech-xul mailing list
[hidden email]
https://lists.mozilla.org/listinfo/dev-tech-xul