Skip to content

Commit

Permalink
Add ability to proper handle focus events when parts changes own focus (
Browse files Browse the repository at this point in the history
eclipse-che#3310)

* Add ability to proper handle focus events when parts changes own focus

* Use lambdas
  • Loading branch information
Vladyslav Zhukovskii committed May 30, 2017
1 parent 7592d48 commit b37b576
Show file tree
Hide file tree
Showing 5 changed files with 58 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,8 @@
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.DoubleClickEvent;
import com.google.gwt.event.dom.client.DoubleClickHandler;
import com.google.gwt.event.dom.client.MouseUpEvent;
import com.google.gwt.event.dom.client.MouseUpHandler;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.DockLayoutPanel;
import com.google.gwt.user.client.ui.FlowPanel;
Expand Down Expand Up @@ -85,13 +81,10 @@ public BaseView(PartStackUIResources resources) {
toolBar = new DockLayoutPanel(Style.Unit.PX);
toolBar.addStyleName(resources.partStackCss().ideBasePartToolbar());
toolBar.getElement().setAttribute("role", "toolbar");
toolBar.addDomHandler(new MouseUpHandler() {
@Override
public void onMouseUp(MouseUpEvent event) {
//activate last focused element if user clicked on part header
if (lastFocused != null) {
lastFocused.setFocus(true);
}
toolBar.addDomHandler(event -> {
//activate last focused element if user clicked on part header
if (lastFocused != null) {
lastFocused.setFocus(true);
}
}, MouseUpEvent.getType());
container.addNorth(toolBar, 23);
Expand All @@ -103,7 +96,7 @@ public void onMouseUp(MouseUpEvent event) {
toolbarHeader.getElement().setAttribute("role", "toolbar-header");
toolBar.addNorth(toolbarHeader, 22);

// padding 2 pixels from the right
//padding 2 pixels from the right
toolbarHeader.addEast(new FlowPanel(), 2);

titleLabel = new Label();
Expand All @@ -114,15 +107,8 @@ public void onMouseUp(MouseUpEvent event) {
addMinimizeButton();
addMenuButton();

/**
* Handle double clicking on the toolbar header
*/
toolbarHeader.addDomHandler(new DoubleClickHandler() {
@Override
public void onDoubleClick(DoubleClickEvent event) {
onToggleMaximize();
}
}, DoubleClickEvent.getType());
//handle double clicking on the toolbar header
toolbarHeader.addDomHandler(event -> onToggleMaximize(), DoubleClickEvent.getType());
}

/**
Expand All @@ -133,12 +119,7 @@ private void addMinimizeButton() {
minimize.getElement().setAttribute("name", "workBenchIconMinimize");
minimizeButton = new ToolButton(minimize);

minimizeButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
onMinimize();
}
});
minimizeButton.addClickHandler(event -> onMinimize());

addToolButton(minimizeButton);

Expand All @@ -155,12 +136,7 @@ private void addMaximizeButton() {
SVGImage maximize = new SVGImage(resources.maximizePart());
maximize.getElement().setAttribute("name", "workBenchIconMaximize");
ToolButton maximizeButton = new ToolButton(maximize);
maximizeButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
onToggleMaximize();
}
});
maximizeButton.addClickHandler(event -> onToggleMaximize());

addToolButton(maximizeButton);

Expand All @@ -176,13 +152,10 @@ public void onClick(ClickEvent event) {
private void addMenuButton() {
final ToolButton menuButton = new ToolButton(FontAwesome.COG + " " + FontAwesome.CARET_DOWN);
menuButton.getElement().setAttribute("name", "workBenchIconMenu");
menuButton.addClickHandler(new ClickHandler() {
@Override
public void onClick(ClickEvent event) {
int left = getAbsoluteLeft(menuButton.getElement());
int top = getAbsoluteTop(menuButton.getElement());
delegate.onPartMenu(left, top + 21);
}
menuButton.addClickHandler(event -> {
int left = getAbsoluteLeft(menuButton.getElement());
int top = getAbsoluteTop(menuButton.getElement());
delegate.onPartMenu(left, top + 21);
});

toolbarHeader.addEast(menuButton, 25);
Expand Down Expand Up @@ -327,12 +300,9 @@ public final void setToolbarHeight(int height) {
public final void setFocus(boolean focused) {
this.focused = focused;
if (focused) {
Scheduler.get().scheduleDeferred(new Scheduler.ScheduledCommand() {
@Override
public void execute() {
focusView();
}
});
Scheduler.get().scheduleDeferred(this::focusView);
} else {
Scheduler.get().scheduleDeferred(this::blurView);
}
}

Expand All @@ -350,4 +320,8 @@ protected void focusView() {
getElement().focus();
}

protected void blurView() {
getElement().blur();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -193,6 +193,11 @@ protected void focusView() {
tree.setFocus(true);
}

@Override
protected void blurView() {
tree.setFocus(false);
}

@Override
public Tree getTree() {
return tree;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -425,19 +425,19 @@ protected void onMouseClick(ClickEvent ce) {
Node sel = node.getNode();
if (e.getCtrlOrMetaKey() && isSelected(sel)) {
doDeselect(Collections.singletonList(sel), false);
tree.focus();
tree.setFocus(true);

// reset the starting location of the click when meta is used during a multiselect
lastSelectedNode = sel;
} else if (e.getCtrlOrMetaKey()) {
doSelect(Collections.singletonList(sel), true, false);
tree.focus();
tree.setFocus(true);

// reset the starting location of the click when meta is used during a multiselect
lastSelectedNode = sel;
} else if (isSelected(sel) && !e.getShiftKey() && !e.getCtrlOrMetaKey() && selectionStorage.size() > 0) {
doSelect(Collections.singletonList(sel), false, false);
tree.focus();
tree.setFocus(true);
}
}
}
Expand Down Expand Up @@ -466,7 +466,7 @@ protected void onMouseDown(MouseDownEvent mde) {
} else {
switch (selectionMode) {
case SIMPLE:
tree.focus();
tree.setFocus(true);
if (isSelected(sel)) {
deselect(sel);
} else {
Expand All @@ -475,7 +475,7 @@ protected void onMouseDown(MouseDownEvent mde) {
break;

case SINGLE:
tree.focus();
tree.setFocus(true);
if (isMeta && isSelected) {
deselect(sel);
} else if (!isSelected) {
Expand All @@ -497,7 +497,7 @@ protected void onMouseDown(MouseDownEvent mde) {

// holding shift down, selecting the same item again, selecting itself
if (sel == lastSelectedNode) {
tree.focus();
tree.setFocus(true);
doSelect(Collections.singletonList(sel), false, false);

} else if (lastSelTreeEl != null && selTreeNodeEl != null) {
Expand Down Expand Up @@ -529,22 +529,22 @@ protected void onMouseDown(MouseDownEvent mde) {
}
}

tree.focus();
tree.setFocus(true);
doSelect(selectedItems, false, false);

// change back to last selected, the walking causes this need
lastSelectedNode = previouslyLastSelected;
}

} else if (!isSelected(sel)) {
tree.focus();
tree.setFocus(true);
doSelect(Collections.singletonList(sel), e.getCtrlOrMetaKey(), false);

// reset the starting location of multi select
lastSelectedNode = sel;
} else if (isSelected(sel) && !e.getShiftKey() && !e.getCtrlOrMetaKey() && !selectionStorage.isEmpty()) {
doSelect(Collections.singletonList(sel), false, false);
tree.focus();
tree.setFocus(true);
} else if (isSelected(sel) && !selectionStorage.isEmpty()) {
doDeselect(Collections.singletonList(sel), false);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ public class Tree extends FocusWidget implements HasBeforeExpandNodeHandlers,

private boolean focusConstrainScheduled = false;

private boolean focused = false;
protected boolean focused = false;

public Tree(NodeStorage nodeStorage, NodeLoader nodeLoader) {
this(nodeStorage, nodeLoader, GWT.<TreeStyles>create(TreeStyles.class));
Expand Down Expand Up @@ -673,13 +673,16 @@ public void scrollIntoView(Node node) {
container.scrollIntoView();
focusEl.getStyle().setLeft((nodeStorage.getDepth(node) - 1) * 16, Style.Unit.PX);
focusEl.getStyle().setTop(container.getOffsetTop(), Style.Unit.PX);
setFocus(true);
}

/**
* Sets window focus to current tree.
*/
public void focus() {
@Override
public void setFocus(boolean focused) {
super.setFocus(focused);
focusImpl.focus(focusEl);
this.focused = focused;

view.onFocusUpdated();
}

/**
Expand Down Expand Up @@ -1577,8 +1580,6 @@ private void onClick(Event event) {
toggle(node.getNode());
}
}

focus();
}

private void onAfterFirstAttach() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,9 @@ public void onLoadChange(NodeDescriptor node, boolean loading) {
}

public void onOverChange(NodeDescriptor node, boolean over) {
setClassName(getNodeContainer(node), tree.getTreeStyles().styles().hover(), over);
if (tree.focused) {
setClassName(getNodeContainer(node), tree.getTreeStyles().styles().hover(), over);
}
}

public void onSelectChange(Node node, boolean select) {
Expand All @@ -236,11 +238,23 @@ public void onSelectChange(Node node, boolean select) {
if (nodeDescriptor != null) {
Element e = getNodeContainer(nodeDescriptor);
if (e != null) {
setClassName(e, tree.getTreeStyles().styles().selected(), select);
setClassName(e, tree.getTreeStyles().styles().hover(), false);
setClassName(e, tree.getTreeStyles().styles().selected(), false);

if (select) {
setClassName(e, tree.focused ? tree.getTreeStyles().styles().selected() : tree.getTreeStyles().styles().hover(), true);
}

}
}
}

public void onFocusUpdated() {
for (Node node : tree.getSelectionModel().getSelectedNodes()) {
onSelectChange(node, tree.getSelectionModel().isSelected(node));
}
}

public void onTextChange(NodeDescriptor node, SafeHtml text) {
Element textEl = getPresentableTextContainer(node);
if (textEl != null) {
Expand Down

0 comments on commit b37b576

Please sign in to comment.