Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improvements to GlobalShortcuts #307

Open
wants to merge 5 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion ide/static/ide/css/ide.css
Original file line number Diff line number Diff line change
Expand Up @@ -1136,8 +1136,25 @@ span.cm-autofilled-end {
text-align: left;
}

#fuzzy-results > div {
.fuzzy-subheader {
font-weight: bold;
color: #666;
font-variant: small-caps;
margin-left: 1em;

}

.fuzzy-hint {
opacity: 0.5;
}
.fuzzy-hint::before {
content: ' <';
}
.fuzzy-hint::after {
content: '>';
}

#fuzzy-results > div {
line-height: 28px;
cursor: pointer;
cursor: hand;
Expand Down
53 changes: 8 additions & 45 deletions ide/static/ide/js/cloudpebble.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@ CloudPebble.ProgressBar = (function() {
},
Hide: function() {
hide();
},
Error: function(msg) {
$('#progress-pane').find('.progress').addClass('progress-danger').removeClass('progress-striped')
.after($('<div>').text(msg).css({margin: 'auto', width: '300px'}));
}
};
})();
Expand Down Expand Up @@ -67,7 +71,6 @@ CloudPebble.Init = function() {
CloudPebble.Dependencies.Init();
CloudPebble.Documentation.Init();
CloudPebble.FuzzyPrompt.Init();
CloudPebble.ProgressBar.Hide();

// Add source files.
$.each(data.source_files, function(index, value) {
Expand All @@ -86,8 +89,11 @@ CloudPebble.Init = function() {
$('.sdk3-only').hide();
}
return null;
}).then(function() {
CloudPebble.ProgressBar.Hide();
}).catch(function(err) {
alert("Something went wrong:\n" + err.message);
CloudPebble.ProgressBar.Error(err);
throw err;
});

window.addEventListener('beforeunload', function(e) {
Expand Down Expand Up @@ -233,46 +239,3 @@ CloudPebble.Utils = {
return interpolate(ngettext("%s second", "%s seconds", n), [n]);
}
};

CloudPebble.GlobalShortcuts = (function() {
var make_shortcut_checker = function (command) {
if (!(command.indexOf('-') > -1)) {
command = _.findKey(CodeMirror.keyMap.default, _.partial(_.isEqual, command));
}
var split = command.split('-');
var modifier = ({
'ctrl': 'ctrlKey',
'cmd': 'metaKey'
})[split[0].toLowerCase()];
return function (e) {
return (e[modifier] && String.fromCharCode(e.keyCode) == split[1]);
}
};


var global_shortcuts = {};

$(document).keydown(function (e) {
if (!e.isDefaultPrevented()) {
_.each(global_shortcuts, function (shortcut) {
if (shortcut.checker(e)) {
shortcut.func(e);
e.preventDefault();
}
});
}
});

return {
SetShortcutHandlers: function (shortcuts) {
var new_shortcuts = _.mapObject(shortcuts, function (func, key) {
return {
checker: make_shortcut_checker(key),
func: func
};

});
_.extend(global_shortcuts, new_shortcuts);
}
}
})();
3 changes: 1 addition & 2 deletions ide/static/ide/js/compile.js
Original file line number Diff line number Diff line change
Expand Up @@ -179,10 +179,9 @@ CloudPebble.Compile = (function() {
commands[gettext("Show Phone Logs")] = function() { show_app_logs(ConnectionType.Phone); };
commands[gettext("Show Emulator Logs")] = function() { show_app_logs(ConnectionType.Qemu); };
commands[gettext("Show Last Build Log")] = function() {show_build_log(mLastBuild.id)};
commands[gettext("Compilation")] = function() { show_compile_pane();};
commands[gettext("Clear App Logs")] = function() { show_clear_logs_prompt(); };
commands[gettext("Take Screenshot")] = function() { take_screenshot(); };
CloudPebble.FuzzyPrompt.AddCommands(commands);
CloudPebble.FuzzyPrompt.AddCommands(gettext('Actions'), commands);

SharedPebble.on('app_log', handle_app_log);
SharedPebble.on('phone_log', handle_phone_log);
Expand Down
3 changes: 0 additions & 3 deletions ide/static/ide/js/dependencies.js
Original file line number Diff line number Diff line change
Expand Up @@ -609,9 +609,6 @@ CloudPebble.Dependencies = (function() {
show_dependencies_pane();
},
Init: function() {
var commands = {};
commands[gettext("Dependencies")] = CloudPebble.Dependencies.Show;
CloudPebble.FuzzyPrompt.AddCommands(commands);
dependencies_template = $('#dependencies-pane-template').remove().removeClass('hide');
alerts.init(dependencies_template);

Expand Down
89 changes: 61 additions & 28 deletions ide/static/ide/js/editor.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ CloudPebble.Editor = (function() {
var unsaved_files = 0;
var is_fullscreen = false;
var resume_fullscreen = false;
var current_editor = null;

var add_source_file = function(file) {
CloudPebble.Sidebar.AddSourceFile(file, function() {
Expand Down Expand Up @@ -129,8 +130,9 @@ CloudPebble.Editor = (function() {
if(language_has_autocomplete && USER_SETTINGS.autocomplete === 2) {
settings.extraKeys = {'Ctrl-Space': 'autocomplete'};
}

if(language_has_autocomplete && USER_SETTINGS.autocomplete !== 0) {
settings.extraKeys['Tab'] = function() {
settings.extraKeys['Tab'] = function selectNextArgument() {
var marks = code_mirror.getAllMarks();
var cursor = code_mirror.getCursor();
var closest = null;
Expand Down Expand Up @@ -163,7 +165,7 @@ CloudPebble.Editor = (function() {
if(USER_SETTINGS.use_spaces) {
var spaces = Array(settings.indentUnit + 1).join(' ');
var oldTab = settings.extraKeys['Tab'];
settings.extraKeys['Tab'] = function(cm) {
settings.extraKeys['Tab'] = function indentMoreOrSelectNextArgument(cm) {
// If we already overrode tab, check that one.
if(oldTab) {
if(oldTab(cm) !== CodeMirror.Pass) {
Expand Down Expand Up @@ -213,12 +215,8 @@ CloudPebble.Editor = (function() {
return CodeMirror.Pass;
};
}
settings.extraKeys['Cmd-/'] = function(cm) {
CodeMirror.commands.toggleComment(cm);
};
settings.extraKeys['Ctrl-/'] = function(cm) {
CodeMirror.commands.toggleComment(cm);
};
settings.extraKeys['Ctrl-/'] = 'toggleComment';
settings.extraKeys['Cmd-/'] = 'toggleComment';

settings.gutters = ['CodeMirror-linenumbers', 'CodeMirror-foldgutter'];
if(file_kind_in(['js', 'json'])) {
Expand Down Expand Up @@ -262,10 +260,9 @@ CloudPebble.Editor = (function() {

var help_shortcut = /Mac/.test(navigator.platform) ? 'Shift-Cmd-Ctrl-/' : 'Shift-Ctrl-Alt-/';

settings.extraKeys[help_shortcut] = function(cm) {
settings.extraKeys[help_shortcut] = function lookupFunction(cm) {
var pos = cm.cursorCoords();
var token = code_mirror.getTokenAt(cm.getCursor());

create_popover(cm, token.string, pos.left, pos.top);
};

Expand Down Expand Up @@ -484,20 +481,30 @@ CloudPebble.Editor = (function() {
alert(gettext(interpolate("Failed to reload file. %s", [error])));
});
};

function set_save_shortcut() {
CloudPebble.GlobalShortcuts.SetShortcutHandlers({
"PlatformCmd-S": save
});
}
set_save_shortcut();

CloudPebble.Sidebar.SetActivePane(pane, {
id: 'source-' + file.id,
onRestore: function() {
current_editor = code_mirror;
code_mirror.refresh();
_.defer(function() { code_mirror.focus(); });
check_safe();
set_save_shortcut();
refresh_ib();
},
onSuspend: function() {
if (is_fullscreen) {
fullscreen(code_mirror, false);
resume_fullscreen = true;
}
current_editor = null;
},
onDestroy: function() {
if(!was_clean) {
Expand All @@ -522,7 +529,7 @@ CloudPebble.Editor = (function() {
CloudPebble.Sidebar.ClearIcon('source-' + file.id);
};

var save = function() {
function save() {
// Make sure we're up to date with whatever changed in IB.
if(ib_showing) {
var content = code_mirror.getValue();
Expand Down Expand Up @@ -580,16 +587,12 @@ CloudPebble.Editor = (function() {
},
pattern)
};
code_mirror.show_rename_prompt = show_rename_prompt;

var ib_pane = $('#ui-editor-pane-template').clone().removeClass('hide').appendTo(pane).hide();
var ib_editor = new IB(ib_pane.find('.ui-canvas'), ib_pane.find('#ui-properties'), ib_pane.find('#ui-toolkit'), ib_pane.find('#ui-layer-list > div'));
var ib_showing = false;

CloudPebble.GlobalShortcuts.SetShortcutHandlers({
save: function() {
save().catch(alert);
}
});

function toggle_ib() {
if(!ib_showing) {
Expand Down Expand Up @@ -765,20 +768,14 @@ CloudPebble.Editor = (function() {
fullscreen(code_mirror, true);
}

var commands = {};
commands[gettext('Rename File')] = function() {
// We need to use a timeout because the rename prompt will conflict with the old prompt
setTimeout(show_rename_prompt, 0);
};
CloudPebble.FuzzyPrompt.ReplaceCommands(commands);

// Tell Google
ga('send', 'event', 'file', 'open');
return code_mirror;
}).catch(function(error) {
var error_box = $('<div class="alert alert-error"></div>');
error_box.text(interpolate(gettext("Something went wrong: %s"), [error.message]));
CloudPebble.Sidebar.SetActivePane(error_box, {id: ''});
throw error;
}).finally(function() {
CloudPebble.ProgressBar.Hide();
});
Expand All @@ -796,7 +793,7 @@ CloudPebble.Editor = (function() {
cm.showHint({hint: CloudPebble.Editor.Autocomplete.complete, completeSingle: false});
};
CodeMirror.commands.save = function(cm) {
cm.cloudpebble_save().catch(alert);;
cm.cloudpebble_save().catch(alert);
};
CodeMirror.commands.saveAll = function(cm) {
save_all().catch(alert);
Expand All @@ -823,14 +820,50 @@ CloudPebble.Editor = (function() {
edit_source_file(item.file);
}
});
var commands = {};
var global_commands = {};
if (CloudPebble.ProjectProperties.is_runnable) {
commands[gettext('Run')] = run;
global_commands[gettext('Run')] = run;
} else {
commands[gettext('Build')] = run;
global_commands[gettext('Build')] = run;
}
var local_commands = {};
local_commands[gettext('Rename Source File')] = function() {
// We need to defer because the rename prompt will conflict with the old prompt
_.defer(function() {
current_editor.show_rename_prompt();
});
};

function neaten_command_name(str) {
var result = str.replace(/(\S)([A-Z])/g, '$1 $2').replace(/^./, function(str){ return str.toUpperCase(); });
result = result.replace('Doc ', 'Document ');
result = result.replace('Go ', 'Go To ');
result = result.replace('Del ', 'Delete ');
return result;
}

var codemirror_commands = [
{command: 'indentAuto', refocus: true},
{command: 'toggleComment', hint: 'Cmd-/ or Ctrl-/', refocus: true},
'find', 'replace', 'indentMore', 'indentLess', 'save', 'saveAll'
];

_.each(codemirror_commands, function(entry) {
var command = !!entry.command ? entry.command : entry;
var name = (!!entry.name ? entry.name : neaten_command_name(command));
local_commands[name] = function() {
current_editor.execCommand(command);
if (!!entry.refocus) current_editor.focus();
};
local_commands[name].hint = !!entry.hint ? entry.hint : CloudPebble.GlobalShortcuts.GetShortcutForCommand(command);
});

CloudPebble.FuzzyPrompt.AddCommands(gettext('File'), local_commands, function() {
return (!!current_editor);
});


CloudPebble.FuzzyPrompt.AddCommands(commands);
CloudPebble.FuzzyPrompt.AddCommands(gettext('Actions'), global_commands);

}

Expand Down
Loading