Search in sources :

Example 1 with EditorKeyBindings

use of org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings in project rstudio by rstudio.

the class ModifyKeyboardShortcutsWidget method applyChanges.

private void applyChanges() {
    // Build up command diffs for save after application
    final EditorKeyBindings editorBindings = EditorKeyBindings.create();
    final EditorKeyBindings appBindings = EditorKeyBindings.create();
    final EditorKeyBindings addinBindings = EditorKeyBindings.create();
    // Loop through all changes and apply based on type
    for (Map.Entry<KeyboardShortcutEntry, KeyboardShortcutEntry> entry : changes_.entrySet()) {
        KeyboardShortcutEntry newBinding = entry.getValue();
        String id = newBinding.getId();
        // Get all commands with this ID.
        List<KeyboardShortcutEntry> bindingsWithId = new ArrayList<KeyboardShortcutEntry>();
        for (KeyboardShortcutEntry binding : originalBindings_) if (binding.getId().equals(id))
            bindingsWithId.add(binding);
        // Collect all shortcuts.
        List<KeySequence> keys = new ArrayList<KeySequence>();
        for (KeyboardShortcutEntry binding : bindingsWithId) keys.add(binding.getKeySequence());
        int commandType = newBinding.getCommandType();
        if (commandType == KeyboardShortcutEntry.TYPE_RSTUDIO_COMMAND)
            appBindings.setBindings(id, keys);
        else if (commandType == KeyboardShortcutEntry.TYPE_EDITOR_COMMAND)
            editorBindings.setBindings(id, keys);
        else if (commandType == KeyboardShortcutEntry.TYPE_ADDIN)
            addinBindings.setBindings(id, keys);
    }
    // Tell satellites that they need to update bindings.
    appCommands_.addBindingsAndSave(appBindings, new CommandWithArg<EditorKeyBindings>() {

        @Override
        public void execute(EditorKeyBindings bindings) {
            events_.fireEventToAllSatellites(new RStudioKeybindingsChangedEvent(bindings));
        }
    });
    editorCommands_.addBindingsAndSave(editorBindings, new CommandWithArg<EditorKeyBindings>() {

        @Override
        public void execute(EditorKeyBindings bindings) {
            events_.fireEventToAllSatellites(new EditorKeybindingsChangedEvent(bindings));
        }
    });
    addins_.addBindingsAndSave(addinBindings, new CommandWithArg<EditorKeyBindings>() {

        @Override
        public void execute(EditorKeyBindings bindings) {
            events_.fireEvent(new AddinsKeyBindingsChangedEvent(bindings));
        }
    });
    closeDialog();
}
Also used : EditorKeyBindings(org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings) ArrayList(java.util.ArrayList) EditorKeybindingsChangedEvent(org.rstudio.core.client.events.EditorKeybindingsChangedEvent) JsArrayString(com.google.gwt.core.client.JsArrayString) RStudioKeybindingsChangedEvent(org.rstudio.core.client.events.RStudioKeybindingsChangedEvent) AddinsKeyBindingsChangedEvent(org.rstudio.studio.client.workbench.addins.AddinsKeyBindingsChangedEvent) Map(java.util.Map) HashMap(java.util.HashMap) KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence)

Example 2 with EditorKeyBindings

use of org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings in project rstudio by rstudio.

the class ModifyKeyboardShortcutsWidget method collectShortcuts.

private void collectShortcuts() {
    final List<KeyboardShortcutEntry> bindings = new ArrayList<KeyboardShortcutEntry>();
    SerializedCommandQueue queue = new SerializedCommandQueue();
    // Load addins discovered as part of package exports. This registers
    // the addin, with the actual keybinding to be registered later,
    // if discovered.
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            RAddins rAddins = addins_.getRAddins();
            for (String key : JsUtil.asIterable(rAddins.keys())) {
                RAddin addin = rAddins.get(key);
                bindings.add(new KeyboardShortcutEntry(addin.getPackage() + "::" + addin.getBinding(), addin.getName(), new KeySequence(), KeyboardShortcutEntry.TYPE_ADDIN, false, AppCommand.Context.Addin));
            }
            continuation.execute();
        }
    });
    // Load saved addin bindings
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            addins_.loadBindings(new CommandWithArg<EditorKeyBindings>() {

                @Override
                public void execute(EditorKeyBindings addinBindings) {
                    for (String commandId : addinBindings.iterableKeys()) {
                        EditorKeyBinding addinBinding = addinBindings.get(commandId);
                        for (KeyboardShortcutEntry binding : bindings) {
                            if (binding.getId() == commandId) {
                                List<KeySequence> keys = addinBinding.getKeyBindings();
                                if (keys.size() >= 1)
                                    binding.setDefaultKeySequence(keys.get(0));
                                if (keys.size() >= 2) {
                                    for (int i = 1; i < keys.size(); i++) {
                                        bindings.add(new KeyboardShortcutEntry(binding.getId(), binding.getName(), keys.get(i), KeyboardShortcutEntry.TYPE_ADDIN, false, AppCommand.Context.Addin));
                                    }
                                }
                            }
                        }
                    }
                    continuation.execute();
                }
            });
        }
    });
    // Ace loading command
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            // Ace Commands
            JsArray<AceCommand> aceCommands = editorCommands_.getCommands();
            for (int i = 0; i < aceCommands.length(); i++) {
                AceCommand command = aceCommands.get(i);
                JsArrayString shortcuts = command.getBindingsForCurrentPlatform();
                if (shortcuts != null) {
                    String id = command.getInternalName();
                    String name = command.getDisplayName();
                    boolean custom = command.isCustomBinding();
                    for (int j = 0; j < shortcuts.length(); j++) {
                        String shortcut = shortcuts.get(j);
                        KeySequence keys = KeySequence.fromShortcutString(shortcut);
                        int type = KeyboardShortcutEntry.TYPE_EDITOR_COMMAND;
                        bindings.add(new KeyboardShortcutEntry(id, name, keys, type, custom, AppCommand.Context.Editor));
                    }
                }
            }
            continuation.execute();
        }
    });
    // RStudio commands
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            // RStudio Commands
            appCommands_.loadBindings(new CommandWithArg<EditorKeyBindings>() {

                @Override
                public void execute(final EditorKeyBindings customBindings) {
                    Map<String, AppCommand> commands = commands_.getCommands();
                    for (Map.Entry<String, AppCommand> entry : commands.entrySet()) {
                        AppCommand command = entry.getValue();
                        if (isExcludedCommand(command))
                            continue;
                        String id = command.getId();
                        String name = getAppCommandName(command);
                        int type = KeyboardShortcutEntry.TYPE_RSTUDIO_COMMAND;
                        boolean isCustom = customBindings.hasKey(id);
                        List<KeySequence> keySequences = new ArrayList<KeySequence>();
                        if (isCustom)
                            keySequences = customBindings.get(id).getKeyBindings();
                        else
                            keySequences.add(command.getKeySequence());
                        for (KeySequence keys : keySequences) {
                            KeyboardShortcutEntry binding = new KeyboardShortcutEntry(id, name, keys, type, isCustom, command.getContext());
                            bindings.add(binding);
                        }
                    }
                    continuation.execute();
                }
            });
        }
    });
    // Sort and finish up
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(final Command continuation) {
            Collections.sort(bindings, new Comparator<KeyboardShortcutEntry>() {

                @Override
                public int compare(KeyboardShortcutEntry o1, KeyboardShortcutEntry o2) {
                    if (o1.getContext() != o2.getContext())
                        return o1.getContext().compareTo(o2.getContext());
                    return o1.getName().compareTo(o2.getName());
                }
            });
            originalBindings_ = bindings;
            updateData(bindings);
            continuation.execute();
        }
    });
    queue.addCommand(new SerializedCommand() {

        @Override
        public void onExecute(Command continuation) {
            if (initialFilterText_ != null) {
                filterWidget_.setText(initialFilterText_);
                filter();
            }
            continuation.execute();
        }
    });
    // Exhaust the queue
    queue.run();
}
Also used : SerializedCommand(org.rstudio.core.client.SerializedCommand) RAddins(org.rstudio.studio.client.workbench.addins.Addins.RAddins) SerializedCommandQueue(org.rstudio.core.client.SerializedCommandQueue) ArrayList(java.util.ArrayList) JsArrayString(com.google.gwt.core.client.JsArrayString) CommandWithArg(org.rstudio.core.client.CommandWithArg) Comparator(java.util.Comparator) EditorKeyBinding(org.rstudio.core.client.command.EditorCommandManager.EditorKeyBinding) EditorKeyBindings(org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings) JsArray(com.google.gwt.core.client.JsArray) RAddin(org.rstudio.studio.client.workbench.addins.Addins.RAddin) JsArrayString(com.google.gwt.core.client.JsArrayString) Command(com.google.gwt.user.client.Command) SerializedCommand(org.rstudio.core.client.SerializedCommand) AceCommand(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceCommand) AceCommand(org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceCommand) KeySequence(org.rstudio.core.client.command.KeyboardShortcut.KeySequence) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

JsArrayString (com.google.gwt.core.client.JsArrayString)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 EditorKeyBindings (org.rstudio.core.client.command.EditorCommandManager.EditorKeyBindings)2 KeySequence (org.rstudio.core.client.command.KeyboardShortcut.KeySequence)2 JsArray (com.google.gwt.core.client.JsArray)1 Command (com.google.gwt.user.client.Command)1 Comparator (java.util.Comparator)1 CommandWithArg (org.rstudio.core.client.CommandWithArg)1 SerializedCommand (org.rstudio.core.client.SerializedCommand)1 SerializedCommandQueue (org.rstudio.core.client.SerializedCommandQueue)1 EditorKeyBinding (org.rstudio.core.client.command.EditorCommandManager.EditorKeyBinding)1 EditorKeybindingsChangedEvent (org.rstudio.core.client.events.EditorKeybindingsChangedEvent)1 RStudioKeybindingsChangedEvent (org.rstudio.core.client.events.RStudioKeybindingsChangedEvent)1 RAddin (org.rstudio.studio.client.workbench.addins.Addins.RAddin)1 RAddins (org.rstudio.studio.client.workbench.addins.Addins.RAddins)1 AddinsKeyBindingsChangedEvent (org.rstudio.studio.client.workbench.addins.AddinsKeyBindingsChangedEvent)1 AceCommand (org.rstudio.studio.client.workbench.views.source.editors.text.ace.AceCommand)1