From 9634809dad4c3dba3a1491341dee030b3d84491d Mon Sep 17 00:00:00 2001 From: Joseph Atkins-Turkish Date: Mon, 30 Nov 2015 17:33:35 -0800 Subject: [PATCH] Panes remember their focus. --- ide/static/ide/js/fuzzyprompt.js | 10 +++++++++- ide/static/ide/js/sidebar.js | 16 +++++++++++++--- 2 files changed, 22 insertions(+), 4 deletions(-) diff --git a/ide/static/ide/js/fuzzyprompt.js b/ide/static/ide/js/fuzzyprompt.js index 8abcfcd1..4a391e2d 100644 --- a/ide/static/ide/js/fuzzyprompt.js +++ b/ide/static/ide/js/fuzzyprompt.js @@ -8,10 +8,11 @@ CloudPebble.FuzzyPrompt = (function() { var selected_id = null; var default_item; var selection_was_made; + var focus_pane; var opened = false; var prompt_kind = null; var COMMANDS_ENABLED = true; - + var focus_pane_selector = '#main-pane' // While manual is false, always highlight the first item var manual = false; @@ -171,6 +172,8 @@ CloudPebble.FuzzyPrompt = (function() { selection_was_made = false; opened = true; prompt_kind = kind; + focus_pane = $(focus_pane_selector).get()[0]; + // Build up the list of files to search through var id = 0; _.each(sources, function(source) { @@ -237,6 +240,11 @@ CloudPebble.FuzzyPrompt = (function() { if (opened) { opened = false; prompt.modal('hide'); + // If we switched page, never refocus + if (focus_pane != $(focus_pane_selector).get()[0]) { + refocus = false; + } + // Otherwise, if we want to refocus, do so if (refocus) { setTimeout(function () { $(previously_active).focus(); diff --git a/ide/static/ide/js/sidebar.js b/ide/static/ide/js/sidebar.js index 79bc2c7b..1cd8e91a 100644 --- a/ide/static/ide/js/sidebar.js +++ b/ide/static/ide/js/sidebar.js @@ -13,12 +13,14 @@ CloudPebble.Sidebar = (function() { var pane = $('#main-pane'); var suspend_function = pane.data('pane-suspend-function'); + if(suspend_function) suspend_function(); var list_entry = $('#sidebar-pane-' + pane_id); if(list_entry) { list_entry.removeClass('active'); } + suspended_panes[pane_id] = pane; pane.detach(); // Create a new empty one. @@ -40,7 +42,10 @@ CloudPebble.Sidebar = (function() { }; var refocus_pane = function(pane) { - pane.find('*[autofocus]').first().focus(); + setTimeout(function() { + var previous_focus = pane.data('previous-focus'); + (previous_focus || pane.find('*[autofocus]').first().focus()).focus(); + }, 50); }; var restore_suspended_pane = function(id) { @@ -59,7 +64,7 @@ CloudPebble.Sidebar = (function() { pane.data('pane-restore-function')(); } - refocus_pane(pane); + refocus_pane($('#main-pane')); return true; } @@ -68,7 +73,7 @@ CloudPebble.Sidebar = (function() { var set_main_pane = function(pane, id, restore_function, destroy_function) { $('#main-pane').append(pane).data('pane-id', id); - refocus_pane(pane); + refocus_pane($('#main-pane')); if(restore_function) { $('#main-pane').data('pane-restore-function', restore_function); } @@ -162,6 +167,11 @@ CloudPebble.Sidebar = (function() { $('#sidebar-pane-github > a').click(CloudPebble.GitHub.Show); $('#sidebar-pane-timeline > a').click(CloudPebble.Timeline.show); $('#new-source-file').click(CloudPebble.Editor.Create); + + $('#pane-parent').on('focusin', '#main-pane *', _.debounce(function(e) { + $('#main-pane').data('previous-focus', $(e.target)); + }, 1)); + init(); }, SetPopover: function(pane_id, title, content) {