Skip to content

Commit

Permalink
chore: add GraphQL mutation for sending system notifications via Elec…
Browse files Browse the repository at this point in the history
…tron (#26773)
  • Loading branch information
astone123 authored May 24, 2023
1 parent 7d06057 commit b1f699a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 2 deletions.
17 changes: 16 additions & 1 deletion packages/data-context/src/actions/ElectronActions.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import type { App, BrowserWindow, OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, SaveDialogReturnValue } from 'electron'
import type { App, BrowserWindow, OpenDialogOptions, OpenDialogReturnValue, SaveDialogOptions, SaveDialogReturnValue, Notification } from 'electron'
import os from 'os'
import type { DataContext } from '..'
import _ from 'lodash'
Expand All @@ -13,6 +13,7 @@ export interface ElectronApiShape {
copyTextToClipboard(text: string): void
isMainWindowFocused(): boolean
focusMainWindow(): void
createNotification(title: string, body: string): Notification
}

export class ElectronActions {
Expand Down Expand Up @@ -104,4 +105,18 @@ export class ElectronActions {
return obj.filePath || null
})
}

showSystemNotification (title: string, body: string, onClick?: () => void) {
const notification = this.ctx.electronApi.createNotification(title, body)

const defaultOnClick = async () => {
await this.ctx.actions.browser.focusActiveBrowserWindow()
}

const clickHandler = onClick || defaultOnClick

notification.on('click', clickHandler)

notification.show()
}
}
3 changes: 3 additions & 0 deletions packages/graphql/schemas/schema.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -1786,6 +1786,9 @@ type Mutation {
"""Set failed tests for the current run to be used by the runner"""
setTestsForRun(testsBySpec: [TestsBySpecInput!]!): Boolean

"""Show system notification via Electron"""
showSystemNotification(body: String!, title: String!): Boolean

"""Switch Testing type and relaunch browser"""
switchTestingTypeAndRelaunch(testingType: TestingTypeEnum!): Boolean

Expand Down
15 changes: 14 additions & 1 deletion packages/graphql/src/schemaTypes/objectTypes/gql-Mutation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -775,6 +775,19 @@ export const mutation = mutationType({
},
})

t.boolean('showSystemNotification', {
description: 'Show system notification via Electron',
args: {
title: nonNull(stringArg()),
body: nonNull(stringArg()),
},
resolve: async (source, args, ctx) => {
ctx.actions.electron.showSystemNotification(args.title, args.body)

return true
},
})

t.boolean('moveToRelevantRun', {
description: 'Allow the relevant run for debugging marked as next to be considered the current relevant run',
args: {
Expand All @@ -787,7 +800,7 @@ export const mutation = mutationType({
},
})

//Using a mutation to just return data in order to be able to await the results in the component
// Using a mutation to just return data in order to be able to await the results in the component
t.list.nonNull.string('testsForRun', {
description: 'Return the set of test titles for the given spec path',
args: {
Expand Down
3 changes: 3 additions & 0 deletions packages/server/lib/makeDataContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,9 @@ export function makeDataContext (options: MakeDataContextOptions): DataContext {
focusMainWindow () {
return focusMainWindow()
},
createNotification (title, body) {
return new electron.Notification({ title, body })
},
},
localSettingsApi: {
async setPreferences (object: AllowedState) {
Expand Down
5 changes: 5 additions & 0 deletions packages/server/lib/modes/interactive.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,11 @@ export = {
},

async run (options: LaunchArgs, _loading: Promise<void>) {
// Need to set this for system notifications to appear as "Cypress" on Windows
if (app.setAppUserModelId) {
app.setAppUserModelId('Cypress')
}

// Note: We do not await the `_loading` promise here since initializing
// the data context can significantly delay initial render of the UI
// https://github.com/cypress-io/cypress/issues/26388#issuecomment-1492616609
Expand Down

4 comments on commit b1f699a

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1f699a May 24, 2023

Choose a reason for hiding this comment

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

Circle has built the linux arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.13.1/linux-arm64/develop-b1f699a0a7be9ed77950ac3565d70389be3193f0/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1f699a May 24, 2023

Choose a reason for hiding this comment

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

Circle has built the linux x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.13.1/linux-x64/develop-b1f699a0a7be9ed77950ac3565d70389be3193f0/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1f699a May 24, 2023

Choose a reason for hiding this comment

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

Circle has built the darwin arm64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.13.1/darwin-arm64/develop-b1f699a0a7be9ed77950ac3565d70389be3193f0/cypress.tgz

@cypress-bot
Copy link
Contributor

@cypress-bot cypress-bot bot commented on b1f699a May 24, 2023

Choose a reason for hiding this comment

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

Circle has built the win32 x64 version of the Test Runner.

Learn more about this pre-release build at https://on.cypress.io/advanced-installation#Install-pre-release-version

Run this command to install the pre-release locally:

npm install https://cdn.cypress.io/beta/npm/12.13.1/win32-x64/develop-b1f699a0a7be9ed77950ac3565d70389be3193f0/cypress.tgz

Please sign in to comment.