Hide clippy.swf using DialogVisibleHandler

Instead of searching the entire document DOM for elements that can
embed clippy.swf have each instance register a single handler to hide
(and reshow) its element.  This should speed up displaying dialogs on
screens that don't use CopyableLabel, as there will be no handlers
registered in these locations.

It also removes a browser permutation from the code, making the
application code a bit less complicated.

Change-Id: Ia0c16a187fbd0ffb2cabb5737d16e8806f2bce23
This commit is contained in:
Shawn Pearce
2013-07-20 16:17:14 -07:00
parent 32125bcbc8
commit 99399a00cb
6 changed files with 26 additions and 129 deletions

View File

@@ -15,12 +15,14 @@
package com.google.gwtexpui.clippy.client;
import com.google.gwt.core.client.Scheduler;
import com.google.gwt.dom.client.Style;
import com.google.gwt.event.dom.client.BlurEvent;
import com.google.gwt.event.dom.client.BlurHandler;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.event.shared.HandlerRegistration;
import com.google.gwt.http.client.URL;
import com.google.gwt.user.client.Command;
import com.google.gwt.user.client.DOM;
@@ -33,6 +35,8 @@ import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwtexpui.safehtml.client.SafeHtml;
import com.google.gwtexpui.safehtml.client.SafeHtmlBuilder;
import com.google.gwtexpui.user.client.DialogVisibleEvent;
import com.google.gwtexpui.user.client.DialogVisibleHandler;
import com.google.gwtexpui.user.client.UserAgent;
/**
@@ -70,6 +74,7 @@ public class CopyableLabel extends Composite implements HasText {
private Label textLabel;
private TextBox textBox;
private Element swf;
private HandlerRegistration hideHandler;
public CopyableLabel() {
this("");
@@ -155,6 +160,19 @@ public class CopyableLabel extends Composite implements HasText {
DOM.removeChild(getElement(), swf);
}
DOM.appendChild(getElement(), swf = SafeHtml.parse(h));
if (hideHandler == null) {
hideHandler =
UserAgent.addDialogVisibleHandler(new DialogVisibleHandler() {
@Override
public void onDialogVisible(DialogVisibleEvent event) {
swf.getStyle().setVisibility(
event.isVisible()
? Style.Visibility.HIDDEN
: Style.Visibility.VISIBLE);
}
});
}
}
}
@@ -176,6 +194,14 @@ public class CopyableLabel extends Composite implements HasText {
embedMovie();
}
@Override
public void onUnload() {
if (hideHandler != null) {
hideHandler.removeHandler();
hideHandler = null;
}
}
private void showTextBox() {
if (textBox == null) {
textBox = new TextBox();

View File

@@ -15,13 +15,4 @@
-->
<module>
<inherits name="com.google.gwt.user.User"/>
<replace-with class="com.google.gwtexpui.user.client.PluginSafeDialogBoxImplAutoHide">
<when-type-is class="com.google.gwtexpui.user.client.PluginSafeDialogBoxImpl" />
<any>
<when-property-is name="user.agent" value="safari"/>
<when-property-is name="user.agent" value="gecko"/>
<when-property-is name="user.agent" value="gecko1_8"/>
</any>
</replace-with>
</module>

View File

@@ -14,7 +14,6 @@
package com.google.gwtexpui.user.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.DialogBox;
/**
@@ -30,9 +29,6 @@ import com.google.gwt.user.client.ui.DialogBox;
* prior setting when the dialog is hidden.
* */
public class PluginSafeDialogBox extends DialogBox {
private final PluginSafeDialogBoxImpl impl =
GWT.create(PluginSafeDialogBoxImpl.class);
public PluginSafeDialogBox() {
this(false);
}
@@ -48,21 +44,18 @@ public class PluginSafeDialogBox extends DialogBox {
@Override
public void setVisible(final boolean show) {
UserAgent.fireDialogVisible(show);
impl.visible(show);
super.setVisible(show);
}
@Override
public void show() {
UserAgent.fireDialogVisible(true);
impl.visible(true);
super.show();
}
@Override
public void hide(final boolean autoClosed) {
UserAgent.fireDialogVisible(false);
impl.visible(false);
super.hide(autoClosed);
}
}

View File

@@ -1,20 +0,0 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gwtexpui.user.client;
class PluginSafeDialogBoxImpl {
void visible(final boolean dialogVisible) {
}
}

View File

@@ -1,86 +0,0 @@
// Copyright (C) 2009 The Android Open Source Project
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.google.gwtexpui.user.client;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.dom.client.NodeList;
import com.google.gwt.user.client.ui.UIObject;
import java.util.ArrayList;
class PluginSafeDialogBoxImplAutoHide extends PluginSafeDialogBoxImpl {
private boolean hidden;
private ArrayList<HiddenElement> hiddenElements =
new ArrayList<HiddenElement>();
@Override
void visible(final boolean dialogVisible) {
if (dialogVisible) {
hideAll();
} else {
showAll();
}
}
private void hideAll() {
if (!hidden) {
hideSet(Document.get().getElementsByTagName("object"));
hideSet(Document.get().getElementsByTagName("embed"));
hideSet(Document.get().getElementsByTagName("applet"));
hidden = true;
}
}
private void hideSet(final NodeList<Element> all) {
for (int i = 0; i < all.getLength(); i++) {
final Element e = all.getItem(i);
if (UIObject.isVisible(e)) {
hiddenElements.add(new HiddenElement(e));
}
}
}
private void showAll() {
if (hidden) {
for (final HiddenElement e : hiddenElements) {
e.restore();
}
hiddenElements.clear();
hidden = false;
}
}
private static class HiddenElement {
private final Element element;
private final String visibility;
HiddenElement(final Element element) {
this.element = element;
this.visibility = getVisibility(element);
setVisibility(element, "hidden");
}
void restore() {
setVisibility(element, visibility);
}
private static native String getVisibility(Element elem)
/*-{ return elem.style.visibility; }-*/;
private static native void setVisibility(Element elem, String disp)
/*-{ elem.style.visibility = disp; }-*/;
}
}

View File

@@ -14,7 +14,6 @@
package com.google.gwtexpui.user.client;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.ui.PopupPanel;
/**
@@ -30,9 +29,6 @@ import com.google.gwt.user.client.ui.PopupPanel;
* prior setting when the dialog is hidden.
* */
public class PluginSafePopupPanel extends PopupPanel {
private final PluginSafeDialogBoxImpl impl =
GWT.create(PluginSafeDialogBoxImpl.class);
public PluginSafePopupPanel() {
this(false);
}
@@ -48,21 +44,18 @@ public class PluginSafePopupPanel extends PopupPanel {
@Override
public void setVisible(final boolean show) {
UserAgent.fireDialogVisible(show);
impl.visible(show);
super.setVisible(show);
}
@Override
public void show() {
UserAgent.fireDialogVisible(true);
impl.visible(true);
super.show();
}
@Override
public void hide(final boolean autoClosed) {
UserAgent.fireDialogVisible(false);
impl.visible(false);
super.hide(autoClosed);
}
}