Skip to content

Commit

Permalink
Fix performance issue of menu delegates. No more populating menus to …
Browse files Browse the repository at this point in the history
…determine keyboard shortcuts.
  • Loading branch information
dkocher committed Nov 17, 2010
1 parent 2c1355a commit 9610923
Show file tree
Hide file tree
Showing 12 changed files with 182 additions and 57 deletions.
7 changes: 6 additions & 1 deletion source/ch/cyberduck/ui/cocoa/BrowserController.java
Expand Up @@ -554,6 +554,11 @@ public void setEditMenu(NSMenu editMenu) {
protected Local getSelectedFile() {
return BrowserController.this.getSelectedFile();
}

@Override
protected ID getTarget() {
return BrowserController.this.id();
}
};
this.editMenu.setDelegate(editMenuDelegate.id());
}
Expand Down Expand Up @@ -4217,7 +4222,7 @@ else if(itemIdentifier.equals(TOOLBAR_EDIT)) {
item.setAction(Foundation.selector("editButtonClicked:"));
// Add a menu representation for text mode of toolbar
NSMenuItem toolbarMenu = NSMenuItem.itemWithTitle(Locale.localizedString(TOOLBAR_EDIT),
Foundation.selector("editMenuClicked:"), "");
Foundation.selector("editButtonClicked:"), "");
NSMenu editMenu = NSMenu.menu();
editMenu.setAutoenablesItems(true);
editMenu.setDelegate(editMenuDelegate.id());
Expand Down
47 changes: 43 additions & 4 deletions source/ch/cyberduck/ui/cocoa/MainController.java
Expand Up @@ -209,6 +209,11 @@ protected Local getSelectedFile() {
}
return null;
}

@Override
protected ID getTarget() {
return MainController.getBrowser().id();
}
};
this.editMenu.setDelegate(editMenuDelegate.id());
}
Expand Down Expand Up @@ -273,7 +278,12 @@ protected List<Path> getSelected() {

public void setArchiveMenu(NSMenu archiveMenu) {
this.archiveMenu = archiveMenu;
this.archiveMenuDelegate = new ArchiveMenuDelegate();
this.archiveMenuDelegate = new ArchiveMenuDelegate() {
@Override
protected ID getTarget() {
return MainController.getBrowser().id();
}
};
this.archiveMenu.setDelegate(archiveMenuDelegate.id());
}

Expand All @@ -283,7 +293,12 @@ public void setArchiveMenu(NSMenu archiveMenu) {

public void setBookmarkMenu(NSMenu bookmarkMenu) {
this.bookmarkMenu = bookmarkMenu;
this.bookmarkMenuDelegate = new BookmarkMenuDelegate();
this.bookmarkMenuDelegate = new BookmarkMenuDelegate() {
@Override
protected ID getTarget() {
return MainController.getBrowser().id();
}
};
this.bookmarkMenu.setDelegate(bookmarkMenuDelegate.id());
}

Expand All @@ -293,7 +308,12 @@ public void setBookmarkMenu(NSMenu bookmarkMenu) {

public void setHistoryMenu(NSMenu historyMenu) {
this.historyMenu = historyMenu;
this.historyMenuDelegate = new HistoryMenuDelegate();
this.historyMenuDelegate = new HistoryMenuDelegate() {
@Override
protected ID getTarget() {
return MainController.getBrowser().id();
}
};
this.historyMenu.setDelegate(historyMenuDelegate.id());
}

Expand All @@ -303,7 +323,12 @@ public void setHistoryMenu(NSMenu historyMenu) {

public void setRendezvousMenu(NSMenu rendezvousMenu) {
this.rendezvousMenu = rendezvousMenu;
this.rendezvousMenuDelegate = new RendezvousMenuDelegate();
this.rendezvousMenuDelegate = new RendezvousMenuDelegate() {
@Override
protected ID getTarget() {
return MainController.getBrowser().id();
}
};
this.rendezvousMenu.setDelegate(rendezvousMenuDelegate.id());
}

Expand Down Expand Up @@ -991,6 +1016,20 @@ public static List<BrowserController> getBrowsers() {
return browsers;
}

/**
* Browser with key focus
*
* @return Null if no browser window is open
*/
public static BrowserController getBrowser() {
for(BrowserController browser : MainController.getBrowsers()) {
if(browser.window().isKeyWindow()) {
return browser;
}
}
return null;
}


/**
* Makes a unmounted browser window the key window and brings it to the front
Expand Down
5 changes: 5 additions & 0 deletions source/ch/cyberduck/ui/cocoa/TransferController.java
Expand Up @@ -280,6 +280,11 @@ public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger i,
}
return super.menuUpdateItemAtIndex(menu, item, i, cancel);
}

@Override
protected Selector getDefaultAction() {
return Foundation.selector("bandwidthPopupChanged:");
}
}

@Action
Expand Down
75 changes: 58 additions & 17 deletions source/ch/cyberduck/ui/cocoa/delegate/AbstractMenuDelegate.java
Expand Up @@ -22,9 +22,9 @@
import ch.cyberduck.ui.cocoa.application.NSEvent;
import ch.cyberduck.ui.cocoa.application.NSMenu;
import ch.cyberduck.ui.cocoa.application.NSMenuItem;
import ch.cyberduck.ui.cocoa.foundation.NSObject;

import org.rococoa.ID;
import org.rococoa.Selector;
import org.rococoa.cocoa.foundation.NSInteger;

import org.apache.commons.lang.StringUtils;
Expand All @@ -36,21 +36,14 @@
public abstract class AbstractMenuDelegate extends ProxyController implements NSMenu.Delegate {
private static Logger log = Logger.getLogger(AbstractMenuDelegate.class);

@Override
public NSObject proxy() {
return this.proxy(CDKeyboardMenuDelegate.class);
}

@Override
public ID id() {
return this.id(CDKeyboardMenuDelegate.class);
}

/**
* Menu needs revalidation
*/
private boolean update;

/**
*
*/
public AbstractMenuDelegate() {
this.setNeedsUpdate(true);
}
Expand Down Expand Up @@ -87,19 +80,59 @@ public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger ind
return !cancel;
}

public boolean hasKeyEquivalent(NSEvent event) {
return false;
/**
* @return
*/
protected abstract Selector getDefaultAction();

/**
* Keyboard shortcut target
*
* @return Target for item actions with a keyboard shortcut.
*/
protected ID getTarget() {
return this.id();
}

public String getActionForKeyEquivalent(NSEvent event) {
/**
* Lowercase shortcut key
*
* @return Null if no key equivalent for any menu item
*/
protected String getKeyEquivalent() {
return null;
}

/**
* Modifier mask for item with shortcut
*
* @return Command Mask
*/
protected int getModifierMask() {
return NSEvent.NSCommandKeyMask;
}

public boolean menuHasKeyEquivalent_forEvent(NSMenu menu, NSEvent event) {
log.debug("menuHasKeyEquivalent_forEvent:" + event);
log.debug("menuHasKeyEquivalent_forEvent:" + menu);
if(StringUtils.isBlank(this.getKeyEquivalent())) {
return false;
}
if((event.modifierFlags() & this.getModifierMask()) == this.getModifierMask()) {
return event.charactersIgnoringModifiers().equals(this.getKeyEquivalent());
}
return false;
}

public ID menuKeyEquivalentTarget_forEvent(NSMenu menu, NSEvent event) {
log.debug("menuKeyEquivalentTarget_forEvent:" + menu);
return this.getTarget();
}

public Selector menuKeyEquivalentAction_forEvent(NSMenu menu, NSEvent event) {
log.debug("menuKeyEquivalentAction_forEvent:" + menu);
return this.getDefaultAction();
}

/**
* Menu needs revalidation before being displayed the next time
*/
Expand All @@ -109,21 +142,29 @@ protected void setNeedsUpdate(boolean u) {
}

/**
* @return
* @return True if the menu is populated and needs no update.
*/
protected boolean isPopulated() {
return !update;
}

/**
* @return Separator menu item
*/
protected NSMenuItem seperator() {
return NSMenuItem.separatorItem();
}

/**
* Validate menu item
*
* @param item
* @return False if menu item should be disabled.
*/
public boolean validateMenuItem(NSMenuItem item) {
return true;
}


protected void clearShortcut(NSMenuItem item) {
this.setShortcut(item, StringUtils.EMPTY, 0);
}
Expand Down
Expand Up @@ -23,6 +23,7 @@
import ch.cyberduck.ui.cocoa.application.NSMenuItem;

import org.rococoa.Foundation;
import org.rococoa.Selector;
import org.rococoa.cocoa.foundation.NSInteger;

/**
Expand All @@ -44,7 +45,12 @@ public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger ind
final Archive archive = Archive.getKnownArchives()[index.intValue()];
item.setRepresentedObject(archive.getIdentifier());
item.setTitle(archive.getIdentifier());
item.setAction(Foundation.selector("archiveMenuClicked:"));
item.setAction(this.getDefaultAction());
return super.menuUpdateItemAtIndex(menu, item, index, cancel);
}

@Override
protected Selector getDefaultAction() {
return Foundation.selector("archiveMenuClicked:");
}
}
Expand Up @@ -27,6 +27,7 @@
import ch.cyberduck.ui.cocoa.application.NSMenuItem;

import org.rococoa.Foundation;
import org.rococoa.Selector;
import org.rococoa.cocoa.foundation.NSInteger;

import org.apache.log4j.Logger;
Expand Down Expand Up @@ -80,7 +81,7 @@ public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger ind
item.setTitle(h.getNickname());
item.setTarget(this.id());
item.setImage(IconCache.iconNamed(h.getProtocol().icon(), 16));
item.setAction(Foundation.selector("bookmarkMenuItemClicked:"));
item.setAction(this.getDefaultAction());
item.setRepresentedObject(h.getUuid());
}
return super.menuUpdateItemAtIndex(menu, item, index, cancel);
Expand All @@ -91,4 +92,9 @@ public void bookmarkMenuItemClicked(final NSMenuItem sender) {
BrowserController controller = MainController.newDocument();
controller.mount(BookmarkCollection.defaultCollection().lookup(sender.representedObject()));
}

@Override
protected Selector getDefaultAction() {
return Foundation.selector("bookmarkMenuItemClicked:");
}
}
20 changes: 16 additions & 4 deletions source/ch/cyberduck/ui/cocoa/delegate/EditMenuDelegate.java
Expand Up @@ -27,6 +27,7 @@
import ch.cyberduck.ui.cocoa.odb.EditorFactory;

import org.rococoa.Foundation;
import org.rococoa.Selector;
import org.rococoa.cocoa.foundation.NSInteger;

import org.apache.commons.lang.ObjectUtils;
Expand Down Expand Up @@ -94,7 +95,7 @@ public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger ind
String editor = editors.keySet().toArray(new String[editors.size()])[index.intValue()];
item.setTitle(editor);
if(identifier.equalsIgnoreCase(defaultEditor)) {
setShortcut(item, "k", NSEvent.NSCommandKeyMask);
setShortcut(item, this.getKeyEquivalent(), this.getModifierMask());
}
else {
clearShortcut(item);
Expand All @@ -104,7 +105,18 @@ public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger ind
return super.menuUpdateItemAtIndex(menu, item, index, cancel);
}

public boolean menuHasKeyEquivalent_forEvent(NSMenu menu, NSEvent event) {
return false;
@Override
protected String getKeyEquivalent() {
return "k";
}

@Override
protected int getModifierMask() {
return NSEvent.NSCommandKeyMask;
}

@Override
protected Selector getDefaultAction() {
return Foundation.selector("editButtonClicked:");
}
}
}
10 changes: 8 additions & 2 deletions source/ch/cyberduck/ui/cocoa/delegate/HistoryMenuDelegate.java
Expand Up @@ -28,6 +28,7 @@
import ch.cyberduck.ui.cocoa.application.NSMenuItem;

import org.rococoa.Foundation;
import org.rococoa.Selector;
import org.rococoa.cocoa.foundation.NSInteger;

import org.apache.commons.lang.StringUtils;
Expand All @@ -36,7 +37,7 @@
/**
* @version $Id$
*/
public class HistoryMenuDelegate extends CollectionMenuDelegate<Host> {
public abstract class HistoryMenuDelegate extends CollectionMenuDelegate<Host> {
private static Logger log = Logger.getLogger(HistoryMenuDelegate.class);

public HistoryMenuDelegate() {
Expand Down Expand Up @@ -77,7 +78,7 @@ public boolean menuUpdateItemAtIndex(NSMenu menu, NSMenuItem item, NSInteger ind
else if(index.intValue() < size) {
Host h = HistoryCollection.defaultCollection().get(index.intValue());
item.setTitle(h.getNickname());
item.setAction(Foundation.selector("historyMenuItemClicked:"));
item.setAction(this.getDefaultAction());
item.setRepresentedObject(h.getUuid());
item.setTarget(this.id());
item.setEnabled(true);
Expand Down Expand Up @@ -110,4 +111,9 @@ public void clearMenuItemClicked(NSMenuItem sender) {
// Delete all bookmark files
HistoryCollection.defaultCollection().clear();
}

@Override
protected Selector getDefaultAction() {
return Foundation.selector("historyMenuItemClicked:");
}
}
Expand Up @@ -30,6 +30,7 @@
import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

Expand Down Expand Up @@ -68,10 +69,7 @@ protected List<Path> getSelected() {
*/
@Override
protected List<AbstractPath.DescriptiveUrl> getURLs(Path selected) {
if(!cache.containsKey(selected)) {
cache.put(selected, selected.getHttpURLs());
}
return cache.get(selected);
return new ArrayList<AbstractPath.DescriptiveUrl>(selected.getHttpURLs());
}

@Action
Expand Down

0 comments on commit 9610923

Please sign in to comment.