-
Notifications
You must be signed in to change notification settings - Fork 7.6k
Close Others extension improvements. #5497
Changes from 5 commits
eaa593b
7ff5efa
913cabc
05e41ef
d0fa2db
83d603c
e1efd6d
fbdd877
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -33,22 +33,24 @@ define(function (require, exports, module) { | |
dm = brackets.getModule("document/DocumentManager"), | ||
docCH = brackets.getModule("document/DocumentCommandHandlers"), | ||
strings = brackets.getModule("i18n!nls/strings"), | ||
settings = JSON.parse(require("text!settings.json")), | ||
working_set_cmenu = Menus.getContextMenu(Menus.ContextMenuIds.WORKING_SET_MENU), | ||
workingSetCmenu = Menus.getContextMenu(Menus.ContextMenuIds.WORKING_SET_MENU), | ||
close_others = "file.close_others", | ||
close_above = "file.close_above", | ||
close_below = "file.close_below"; | ||
|
||
function handleClose(mode) { | ||
|
||
/* | ||
This function collects files based on passing command (close_others/above/below) and execute 'FILE_CLOSE_LIST'. | ||
*/ | ||
function handleClose(cmd) { | ||
|
||
var targetIndex = dm.findInWorkingSet(dm.getCurrentDocument().file.fullPath), | ||
workingSet = dm.getWorkingSet().slice(0), | ||
start = (mode === close_below) ? (targetIndex + 1) : 0, | ||
end = (mode === close_above) ? (targetIndex) : (workingSet.length), | ||
start = (cmd === close_below) ? (targetIndex + 1) : 0, | ||
end = (cmd === close_above) ? (targetIndex) : (workingSet.length), | ||
docList = [], | ||
i; | ||
|
||
if (mode === close_others) { | ||
if (cmd === close_others) { | ||
end--; | ||
workingSet.splice(targetIndex, 1); | ||
} | ||
|
@@ -59,25 +61,67 @@ define(function (require, exports, module) { | |
|
||
CommandManager.execute(Commands.FILE_CLOSE_LIST, {documentList: docList}); | ||
} | ||
|
||
if (settings.close_below) { | ||
CommandManager.register(strings.CMD_FILE_CLOSE_BELOW, close_below, function () { | ||
handleClose(close_below); | ||
}); | ||
working_set_cmenu.addMenuItem(close_below, "", Menus.AFTER, Commands.FILE_CLOSE); | ||
} | ||
|
||
if (settings.close_others) { | ||
CommandManager.register(strings.CMD_FILE_CLOSE_OTHERS, close_others, function () { | ||
handleClose(close_others); | ||
}); | ||
working_set_cmenu.addMenuItem(close_others, "", Menus.AFTER, Commands.FILE_CLOSE); | ||
} | ||
|
||
if (settings.close_above) { | ||
CommandManager.register(strings.CMD_FILE_CLOSE_ABOVE, close_above, function () { | ||
handleClose(close_above); | ||
}); | ||
working_set_cmenu.addMenuItem(close_above, "", Menus.AFTER, Commands.FILE_CLOSE); | ||
|
||
/* | ||
Pass 'command id' and it'll let you know, whether contextmenu for that command is existing or not. | ||
*/ | ||
function isMenuThere(cmd) { | ||
return Menus.getMenuItem(workingSetCmenu.id + "-" + cmd) ? true : false; | ||
} | ||
|
||
/* | ||
This function is responsible for add/remove context menus based on current file selection. | ||
If there is only one file in working set, we won't show any of the three (Close Others, Close Others Above/Below). | ||
If there is more than one file, but selected file is first / last in working set, we will show only "Close Others". | ||
In other cases we will show all three. | ||
*/ | ||
$(workingSetCmenu).on("beforeContextMenuOpen", function () { | ||
var targetIndex = dm.findInWorkingSet(dm.getCurrentDocument().file.fullPath), | ||
closeOthers = (dm.getWorkingSet().length > 1), | ||
closeOtherAbove = (targetIndex > 0), | ||
closeOthersBelow = (targetIndex < dm.getWorkingSet().length - 1); | ||
|
||
if (closeOtherAbove && closeOthersBelow) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It would be better, IMO if we removed the "Close Others" menu item and used "Closed Others Below" or "Close Others Above" when the selection is on the First/Last item. @larz0 what do you think? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think "Close Others" is fine as it's pretty clear already. |
||
if (!isMenuThere(close_above)) { | ||
|
||
CommandManager.register(strings.CMD_FILE_CLOSE_ABOVE, close_above, function () { | ||
handleClose(close_above); | ||
}); | ||
workingSetCmenu.addMenuItem(close_above, "", Menus.AFTER, Commands.FILE_CLOSE); | ||
} | ||
|
||
if (!isMenuThere(close_below)) { | ||
CommandManager.register(strings.CMD_FILE_CLOSE_BELOW, close_below, function () { | ||
handleClose(close_below); | ||
}); | ||
workingSetCmenu.addMenuItem(close_below, "", Menus.BEFORE, Commands.FILE_SAVE); | ||
} | ||
} else { | ||
if (isMenuThere(close_above)) { | ||
workingSetCmenu.removeMenuItem(close_above); | ||
} | ||
|
||
if (isMenuThere(close_below)) { | ||
workingSetCmenu.removeMenuItem(close_below); | ||
} | ||
} | ||
|
||
if (closeOthers) { | ||
if (!isMenuThere(close_others)) { | ||
CommandManager.register(strings.CMD_FILE_CLOSE_OTHERS, close_others, function () { | ||
handleClose(close_others); | ||
}); | ||
|
||
if (isMenuThere(close_above)) { //if "Close Others Above" exists add "Close Others" next to it | ||
workingSetCmenu.addMenuItem(close_others, "", Menus.AFTER, close_above); | ||
} else { //else add "Close Others" next to "Close" | ||
workingSetCmenu.addMenuItem(close_others, "", Menus.AFTER, Commands.FILE_CLOSE); | ||
} | ||
} | ||
} else { | ||
if (isMenuThere(close_others)) { | ||
workingSetCmenu.removeMenuItem(close_others); | ||
} | ||
} | ||
}); | ||
}); |
This file was deleted.
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -126,16 +126,25 @@ define(function (require, exports, module) { | |
|
||
function runCloseOthers() { | ||
var ws = DocumentManager.getWorkingSet(), | ||
e = new $.Event("contextmenu"), | ||
promise; | ||
|
||
if (ws.length > docSelectIndex) { | ||
DocumentManager.getDocumentForPath(ws[docSelectIndex].fullPath).done(function (doc) { | ||
DocumentManager.setCurrentDocument(doc); | ||
|
||
e.pageX = 20; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. do you need to specify mouse coordinates here? If I change them to 0,0 or 500,500 does it still work or is 20,20 the only thing that works? We may want to think about a different way to exercise the handler -- using page relative mouse clicks to invoke a menu is bad. @redmunds -- do you have an opinion on this? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I need to trigger "contextmenu" event on "working set" to open context menu popup. So only context menus (Close Others / Above / Below) will get populate. Without specifying coordinates, There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think this logic depends on the context menu being displayed at 0,0 of page and context menu item being at 20,20. I agree with @JeffryBooher that it's fragile and unnecessary to test this. Instead you can use Related comment: I notice that this extension only displays the "Close Others" command in the context menu when there's more than 1 item in the Working Set. I prefer to always have commands shown and then disable them appropriately. I mention this, because this test depends on context menu item being in a particular position, yet this extension makes this indeterminate for other code. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. _I notice that this extension only displays the "Close Others" command in the context menu when there's more than 1 item in the Working Set. I prefer to always have commands shown and then disable them appropriately_ disable Or remove? Currently it removes unnecessary Or shows necessary "Context Menu Items" based on user file selection as like below screenshot. If we show all 3 all time and if we only disable them (as like in Google Chrome Tabs), context menu would look like lengthy in Brackets. _I think this logic depends on the context menu being displayed at 0,0 of page and context menu item being at 20,20._ Nope. we can specify like below, which means coordinates can be anything.
This extension removes unnecessary menus Or adds necessary menus on workingset's _using page relative mouse clicks to invoke a menu is bad._ :) why? Test file's intention is to open a context menu, not open it at particular position. And if you still think, this hack would cause an error in future, it is just a test file. At maximum, tests would fail. It won't affect Brackets. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If value of |
||
e.pageY = 20; | ||
$("#open-files-container").trigger(e); | ||
}); | ||
|
||
promise = CommandManager.execute(cmdToRun); | ||
waitsForDone(promise, cmdToRun); | ||
} | ||
|
||
runs(function () { | ||
expect(DocumentManager.getCurrentDocument()).not.toBe(null); | ||
}); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this tab is misaligned |
||
} | ||
|
||
it("Close others", function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you fix the tabs in this block. lines 156-159 are misaligned There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is funny issue. It looked good in Brackets and Submlime but not in github. I deleted and recreated those lines to fix this. |
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For consistency -- you should name this
closeOthersAbove
-- you havecloseOtherAbove