Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updates to command-line arguments #2013

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
53 changes: 53 additions & 0 deletions js/menuRenderer.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ var PDFViewer = require('pdfViewer.js')
var tabEditor = require('navbar/tabEditor.js')
var readerView = require('readerView.js')
var taskOverlay = require('taskOverlay/taskOverlay.js')
var { searchAndSortTasks, moveToTaskCommand } = require('searchbar/customBangs.js')

module.exports = {
initialize: function () {
Expand Down Expand Up @@ -94,6 +95,58 @@ module.exports = {
browserUI.addTab(newTab, {
enterEditMode: !data.url // only enter edit mode if the new tab is empty
})

if (data.taskQuery) {
// use the first search result
// if there is no result, need to create a new task
let task

if (/^\d+$/.test(data.taskQuery)) {
task = tasks.get(data.taskQuery)
} else {
task = searchAndSortTasks(data.taskQuery, excludeSelected=false)[0]?.task
}

if (!task) {
task = tasks.get(tasks.add(undefined, tasks.getIndex(tasks.getSelected().id) + 1))
task.name = data.taskQuery
}

moveToTaskCommand(task.id)
Copy link
Collaborator

Choose a reason for hiding this comment

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

IIUC, this is creating a tab in the current task, then using moveToTaskCommand to move it to the new task. This should just be a switchToTask call before the addTab call instead.

(However, if we send a switchToTask IPC from the main process, as I suggested in my other comment, this whole block of code should go away anyway).

}

})

ipc.on('switchToTask', function (e, data) {
/* new tabs can't be created in modal mode */
if (modalMode.enabled()) {
return
}

/* new tabs can't be created in focus mode */
if (focusMode.enabled()) {
focusMode.warn()
return
}

if (data.taskQuery) {
// use the first search result
// if there is no result, need to create a new task
let task

if (/^\d+$/.test(data.taskQuery)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Maybe this would be simpler / better as:

if (tasks.get(data.taskQuery)) {
    task = tasks.get...
} else {
    task = searchAndSortTasks...
}

if (!task) {
    task = tasks.get(tasks.add(...
}

So task IDs would work, but any number that's not a task ID would be used for search or to create a new task.

task = tasks.get(data.taskQuery)
} else {
task = searchAndSortTasks(data.taskQuery, excludeSelected=false)[0]?.task
}

if (!task) {
task = tasks.get(tasks.add(undefined, tasks.getIndex(tasks.getSelected().id) + 1))
task.name = data.taskQuery
}

browserUI.switchToTask(task.id)
}
})

ipc.on('saveCurrentPage', async function () {
Expand Down
20 changes: 12 additions & 8 deletions js/searchbar/customBangs.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,19 @@ function getTaskByNameOrNumber (text) {

// return an array of tasks sorted by last activity
// if a search string is present, filter the results with a basic fuzzy search
function searchAndSortTasks (text) {
function searchAndSortTasks (text, excludeSelected=true) {

let taskResults = tasks
.filter(t => t.id !== tasks.getSelected().id)
.map(t => Object.assign({}, { task: t }, { lastActivity: tasks.getLastActivity(t.id) }))

if (excludeSelected === true){
taskResults = tasks.filter(t => t.id !== tasks.getSelected().id)
}

taskResults = taskResults.sort(function (a, b) {
return b.lastActivity - a.lastActivity
})
taskResults = taskResults
.map(t => Object.assign({}, { task: t }, { lastActivity: tasks.getLastActivity(t.id) }))
.sort(function (a, b) {
return b.lastActivity - a.lastActivity
})

if (text !== '') {
// fuzzy search
Expand All @@ -89,7 +94,6 @@ function searchAndSortTasks (text) {
return (exactMatch || fuzzyTitleScore > 0.4)
})
}

return taskResults
}

Expand Down Expand Up @@ -364,4 +368,4 @@ function initialize () {
})
}

module.exports = { initialize }
module.exports = { initialize, searchAndSortTasks, moveToTaskCommand }
29 changes: 22 additions & 7 deletions main/main.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ var secondaryMenu = null
var isFocusMode = false
var appIsReady = false

const isFirstInstance = app.requestSingleInstanceLock()
const isFirstInstance = app.requestSingleInstanceLock(process.argv)

if (!isFirstInstance) {
app.quit()
Expand Down Expand Up @@ -116,23 +116,38 @@ function openTabInWindow (url) {

function handleCommandLineArguments (argv) {
// the "ready" event must occur before this function can be used
var initTaskQuery = undefined
if (argv) {

// check for -t task query
if (argv.includes('-t') && argv.indexOf('-t') > 0){
// query for specific task to add search to
initTaskQuery = argv[argv.indexOf('-t') + 1]
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think we can assume that IPC messages will be received in the order they're sent, in which case we can just send a switchToTask IPC here, and then leave the code below as switchToTab without any task information. That should simplify this a fair amount.

}

argv.forEach(function (arg, idx) {
if (arg && arg.toLowerCase() !== __dirname.toLowerCase()) {
// URL
if (arg.indexOf('://') !== -1) {
// URL
sendIPCToWindow(mainWindow, 'addTab', {
url: arg
url: arg,
taskQuery: initTaskQuery
})
} else if (idx > 0 && argv[idx - 1] === '-s') {
// search
sendIPCToWindow(mainWindow, 'addTab', {
url: arg
url: arg,
taskQuery: initTaskQuery
})
} else if (/\.(m?ht(ml)?|pdf)$/.test(arg) && fs.existsSync(arg)) {
// local files (.html, .mht, mhtml, .pdf)
sendIPCToWindow(mainWindow, 'addTab', {
url: 'file://' + path.resolve(arg)
url: 'file://' + path.resolve(arg),
taskQuery: initTaskQuery
})
} else if (initTaskQuery) {
sendIPCToWindow(mainWindow, 'switchToTask', {
taskQuery: initTaskQuery
})
}
}
Expand Down Expand Up @@ -367,14 +382,14 @@ app.on('continue-activity', function(e, type, userInfo, details) {
}
})

app.on('second-instance', function (e, argv, workingDir) {
app.on('second-instance', function (e, argv, workingDir, additionalData) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

argv here should be the arguments of the second instance already - why do they need to be passed in with additionalData?

if (mainWindow) {
if (mainWindow.isMinimized()) {
mainWindow.restore()
}
mainWindow.focus()
// add a tab with the new URL
handleCommandLineArguments(argv)
handleCommandLineArguments(additionalData)
}
})

Expand Down