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

chore: add GraphQL mutation for sending system notifications via Electron #26773

Merged
merged 14 commits into from
May 24, 2023
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 @@ -1775,6 +1775,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 @@ -761,6 +761,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 @@ -773,7 +786,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 @@ -186,6 +186,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