-
Notifications
You must be signed in to change notification settings - Fork 121
Fix #1081, switch to next/previous window #1129
Changes from 3 commits
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 |
---|---|---|
@@ -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; | ||
if ( direction === "next" ) { | ||
targetIndex = Math.floor((currentWindowIndex + 1) % len); | ||
} else { | ||
targetIndex = Math.floor((currentWindowIndex - 1 + len) % len); | ||
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.
|
||
} | ||
return windowArray[targetIndex].id; | ||
} | ||
// error handle for | ||
function onError(error) { | ||
// console.log(`Error: ${error}`); | ||
} | ||
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. There doesn't need to be any error handler, if there's an error in |
||
|
||
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; | ||
} | ||
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 will definitely be set, so you can just do |
||
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); | ||
} | ||
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. You can remove the entire try/catch, and the comments all match the code so they aren't really necessary either. |
||
} | ||
}); |
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] | ||
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'd do the phrase something like:
|
||
""" |
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.
This doesn't need to be initialized to anything, since it will definitely be reset below.