Skip to content

Commit

Permalink
close main menu (server) and toolbar menus (server and desktop) when …
Browse files Browse the repository at this point in the history
…keyboard shortcut used

- Fixes #6080
  • Loading branch information
gtritchie committed Feb 9, 2020
1 parent 7a3fe53 commit f7e0c60
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 13 deletions.
20 changes: 17 additions & 3 deletions src/gwt/src/org/rstudio/core/client/command/BaseMenuBar.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* BaseMenuBar.java
*
* Copyright (C) 2009-19 by RStudio, PBC
* Copyright (C) 2009-20 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
Expand All @@ -22,6 +22,7 @@
import com.google.gwt.user.client.ui.MenuItem;
import com.google.gwt.user.client.ui.MenuItemSeparator;
import com.google.gwt.user.client.ui.UIObject;
import org.rstudio.core.client.HandlerRegistrations;
import org.rstudio.core.client.SeparatorManager;
import org.rstudio.core.client.dom.DomUtils;
import org.rstudio.core.client.widget.events.GlassVisibilityEvent;
Expand All @@ -34,6 +35,7 @@
import java.util.List;

public class BaseMenuBar extends MenuBar
implements CommandHandler
{
private class PositionComparator implements Comparator<UIObject>
{
Expand Down Expand Up @@ -63,6 +65,7 @@ public BaseMenuBar(boolean vertical)
// subclasses are instantiated using generated code--don't feel
// like messing with all that now
eventBus_ = RStudioGinjector.INSTANCE.getEventBus();
commandHandler_ = new HandlerRegistrations();
}

private MenuItem getTargetedMenuItem(Event event)
Expand Down Expand Up @@ -149,6 +152,7 @@ protected void onLoad()
if (vertical_ && glass++ == 0)
eventBus_.fireEvent(new GlassVisibilityEvent(true));
super.onLoad();
commandHandler_.add(eventBus_.addHandler(CommandEvent.TYPE, this));
for (MenuItem child : getItems())
{
if (child instanceof AppMenuItem)
Expand Down Expand Up @@ -179,6 +183,7 @@ protected void onLoad()
protected void onUnload()
{
super.onUnload();
commandHandler_.removeHandler();
if (vertical_ && --glass == 0)
eventBus_.fireEvent(new GlassVisibilityEvent(false));
}
Expand Down Expand Up @@ -300,6 +305,15 @@ public void keyboardActivateItem(int index)
doItemAction(item, true, true);
}

@Override
public void onCommand(AppCommand command)
{
if (command.getExecutedFromShortcut())
{
closeAllChildren(false);
}
}

/**
* Reference count for glass visibility. NOTE: Perhaps this should be
* hoisted into a more general class so that everyone who raises
Expand All @@ -308,8 +322,8 @@ public void keyboardActivateItem(int index)
private static int glass = 0;

private boolean autoHideRedundantSeparators_ = true;
private final ArrayList<MenuItemSeparator> separators_ =
new ArrayList<MenuItemSeparator>();
private final ArrayList<MenuItemSeparator> separators_ = new ArrayList<>();
private final EventBus eventBus_;
private final boolean vertical_;
private final HandlerRegistrations commandHandler_;
}
48 changes: 38 additions & 10 deletions src/gwt/src/org/rstudio/core/client/widget/ToolbarPopupMenu.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*
* ToolbarPopupMenu.java
*
* Copyright (C) 2009-19 by RStudio, PBC
* Copyright (C) 2009-20 by RStudio, PBC
*
* Unless you have received this program directly from RStudio pursuant
* to the terms of a commercial license agreement with RStudio, then
Expand Down Expand Up @@ -34,13 +34,19 @@
import com.google.gwt.user.client.ui.MenuItemSeparator;
import com.google.gwt.user.client.ui.Widget;

import org.rstudio.core.client.HandlerRegistrations;
import org.rstudio.core.client.command.AppCommand;
import org.rstudio.core.client.command.AppMenuItem;
import org.rstudio.core.client.command.BaseMenuBar;
import org.rstudio.core.client.command.CommandEvent;
import org.rstudio.core.client.command.CommandHandler;
import org.rstudio.core.client.dom.DomUtils;
import org.rstudio.core.client.dom.DomUtils.NodePredicate;
import org.rstudio.studio.client.RStudioGinjector;
import org.rstudio.studio.client.application.events.EventBus;

public class ToolbarPopupMenu extends ThemedPopupPanel
implements CommandHandler
{
// Extensibility point for dynamically constructed popup menus. The default
// implementation returns itself, but extensions can do some work to build
Expand All @@ -58,6 +64,8 @@ public ToolbarPopupMenu()
menuBar_ = createMenuBar();
Widget mainWidget = createMainWidget();
setWidget(mainWidget);
events_ = RStudioGinjector.INSTANCE.getEventBus();
commandHandler_ = new HandlerRegistrations();
}

public ToolbarPopupMenu(ToolbarPopupMenu parent)
Expand All @@ -76,11 +84,19 @@ protected Widget createMainWidget()
return menuBar_;
}

@Override
protected void onLoad()
{
super.onLoad();
commandHandler_.add(events_.addHandler(CommandEvent.TYPE, this));
}

@Override
protected void onUnload()
{
super.onUnload();
menuBar_.selectItem(null);
commandHandler_.removeHandler();
}

public void selectFirst()
Expand Down Expand Up @@ -131,25 +147,25 @@ public void setAutoOpen(boolean autoOpen)

public void insertItem(MenuItem menuItem, int beforeIndex)
{
ScheduledCommand command = menuItem.getScheduledCommand() ;
ScheduledCommand command = menuItem.getScheduledCommand();
if (command != null)
menuItem.setScheduledCommand(new ToolbarPopupMenuCommand(command));
menuBar_.insertItem(menuItem, beforeIndex) ;
menuBar_.insertItem(menuItem, beforeIndex);
}

public void removeItem(MenuItem menuItem)
{
menuBar_.removeItem(menuItem) ;
menuBar_.removeItem(menuItem);
}

public boolean containsItem(MenuItem menuItem)
{
return menuBar_.getItemIndex(menuItem) >= 0 ;
return menuBar_.getItemIndex(menuItem) >= 0;
}

public void clearItems()
{
menuBar_.clearItems() ;
menuBar_.clearItems();
}

public void addSeparator()
Expand All @@ -174,7 +190,7 @@ public void addSeparator(int minPx)

public int getItemCount()
{
return menuBar_.getItemCount() ;
return menuBar_.getItemCount();
}

public List<MenuItem> getMenuItems() { return menuBar_.getMenuItems(); }
Expand Down Expand Up @@ -214,7 +230,7 @@ protected class ToolbarMenuBar extends BaseMenuBar
{
public ToolbarMenuBar(boolean vertical)
{
super(vertical) ;
super(vertical);
}

@Override
Expand Down Expand Up @@ -292,7 +308,7 @@ public void onPreviewNativeEvent(NativePreviewEvent e)

public int getItemCount()
{
return getItems().size() ;
return getItems().size();
}

public List<MenuItem> getMenuItems() { return getItems(); }
Expand Down Expand Up @@ -351,10 +367,22 @@ public boolean test(Node node)
if (tableNode == null)
return null;

return tableNode.<Element>cast();
return tableNode.cast();

}

@Override
public void onCommand(AppCommand command)
{
if (command.getExecutedFromShortcut())
{
if (menuBar_.isVisible())
menuBar_.setVisible(false);
}
}

protected ToolbarMenuBar menuBar_;
private ToolbarPopupMenu parent_;
private EventBus events_;
private HandlerRegistrations commandHandler_;
}

0 comments on commit f7e0c60

Please sign in to comment.