From d0df19e54885828db89939fa47b26a9c9aa1b43a Mon Sep 17 00:00:00 2001 From: IdoKendo Date: Wed, 9 Apr 2025 19:50:53 +0300 Subject: [PATCH] feat: move tab to an existing window by orientation --- background_scripts/all_commands.js | 32 +++++++++++++++++++++++ background_scripts/commands.js | 4 +++ background_scripts/main.js | 42 ++++++++++++++++++++++++++++++ 3 files changed, 78 insertions(+) diff --git a/background_scripts/all_commands.js b/background_scripts/all_commands.js index fb17a25c3..1d02166d4 100644 --- a/background_scripts/all_commands.js +++ b/background_scripts/all_commands.js @@ -471,6 +471,38 @@ const allCommands = [ background: true, }, + { + name: "mergeTabToExistingWindowOnLeft", + desc: "Move tab to an existing window on left, if exists", + group: "tabs", + advanced: true, + background: true, + }, + + { + name: "mergeTabToExistingWindowOnRight", + desc: "Move tab to an existing window on right, if exists", + group: "tabs", + advanced: true, + background: true, + }, + + { + name: "mergeTabToExistingWindowAbove", + desc: "Move tab to an existing window above, if exists", + group: "tabs", + advanced: true, + background: true, + }, + + { + name: "mergeTabToExistingWindowBelow", + desc: "Move tab to an existing window below, if exists", + group: "tabs", + advanced: true, + background: true, + }, + { name: "closeTabsOnLeft", desc: "Close tabs on the left", diff --git a/background_scripts/commands.js b/background_scripts/commands.js index 8a881d04e..04756c467 100644 --- a/background_scripts/commands.js +++ b/background_scripts/commands.js @@ -345,6 +345,10 @@ const defaultKeyMappings = { "g0": "firstTab", "g$": "lastTab", "W": "moveTabToNewWindow", + "wh": "mergeTabToExistingWindowOnLeft", + "wl": "mergeTabToExistingWindowOnRight", + "wk": "mergeTabToExistingWindowAbove", + "wj": "mergeTabToExistingWindowBelow", "t": "createTab", "yt": "duplicateTab", "x": "removeTab", diff --git a/background_scripts/main.js b/background_scripts/main.js index 6add5aec6..e971ce59a 100644 --- a/background_scripts/main.js +++ b/background_scripts/main.js @@ -232,6 +232,35 @@ function nextZoomLevel(currentZoom, steps) { } } +const moveTabToExistingWindow = function(orientation, currentTab) { + chrome.windows.getCurrent({}, currentWindow => { + chrome.windows.getAll({populate: true}, windows => { + const filteredWindows = windows.filter(window => { + if (window.id !== currentWindow.id) { + if (orientation === 'left') { + return window.left < currentWindow.left; + } else if (orientation === 'right') { + return window.left > currentWindow.left; + } else if (orientation === 'top') { + return window.top < currentWindow.top; + } else if (orientation === 'bottom') { + return window.top > currentWindow.top; + } + } + }); + if (filteredWindows.length > 0) { + const destinationWindow = filteredWindows[0]; + chrome.tabs.move(currentTab.id, { windowId: destinationWindow.id, index: -1 }).then(() => { + chrome.windows.get(destinationWindow.id, {populate: true}, newWindow => { + const newTab = newWindow.tabs.slice(-1)[0]; + selectSpecificTab({ id: newTab.id }); + }); + }); + } + }); + }); +}; + // These are commands which are bound to keystrokes which must be handled by the background page. // They are mapped in commands.js. const BackgroundCommands = { @@ -313,6 +342,19 @@ const BackgroundCommands = { }); }, + mergeTabToExistingWindowOnLeft(request) { + moveTabToExistingWindow("left", request.tab); + }, + mergeTabToExistingWindowOnRight(request) { + moveTabToExistingWindow("right", request.tab); + }, + mergeTabToExistingWindowAbove(request) { + moveTabToExistingWindow("top", request.tab); + }, + mergeTabToExistingWindowBelow(request) { + moveTabToExistingWindow("bottom", request.tab); + }, + nextTab(request) { return selectTab("next", request); },