Skip to content
This repository has been archived by the owner on Feb 26, 2021. It is now read-only.

Fix #1081, switch to next/previous window #1129

Merged
merged 4 commits into from
Mar 3, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
42 changes: 42 additions & 0 deletions extension/intents/window/window.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import * as intentRunner from "../../background/intentRunner.js";

function findTargetWindowId(windowArray, currentWindowId, direction) {
const len = windowArray.length;
// find currentWindowId postion in array
const currentWindowIndex = windowArray.findIndex((window) => (window.id === currentWindowId));
let targetIndex = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't need to be initialized to anything, since it will definitely be reset below.

if ( direction === "next" ) {
targetIndex = Math.floor((currentWindowIndex + 1) % len);
} else {
targetIndex = Math.floor((currentWindowIndex - 1 + len) % len);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Math.floor won't be necessary here, everything is an integer so it should stay an integer.

}
return windowArray[targetIndex].id;
}
// error handle for
function onError(error) {
// console.log(`Error: ${error}`);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There doesn't need to be any error handler, if there's an error in .run() the intentRunner will catch it.


intentRunner.registerIntent({
name: "window.switch",
async run(context) {
// get current activeTab.windowId
const activeTab = await context.activeTab();
const currentWindowId = activeTab.windowId;
// get direction parameter
let direction = "next";
if ( context.parameters ) {
direction = context.parameters.direction;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will definitely be set, so you can just do let direction = context.parameters.direction without the if.

try {
// getAll normal window
const gettingAll = await browser.windows.getAll({windowTypes: ["normal"]});
// find target windowId
const targetWindowId = findTargetWindowId(gettingAll, currentWindowId, direction);
// set target window focuse true
await browser.windows.update(targetWindowId, {focused: true});
} catch (err) {
onError(err);
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can remove the entire try/catch, and the comments all match the code so they aren't really necessary either.

}
});
6 changes: 6 additions & 0 deletions extension/intents/window/window.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
[window.switch]
description = "Switch the current window"
match = """
switch to next window [direction=next]
switch to previous window [direction=back]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd do the phrase something like:

match = """
(switch | activate | focus) (the | to |) next window [direction=next]
(switch | activate | focus) (the | to |) (previous | last) window [direction=back]
"""

"""