realm / realm/SwiftLint

Rule Request: Disallow `NSApp.activate(ignoringOtherApps: true)` in `applicationDidFinishLaunching`

Open
#2,643 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

rule-request
Dominant language
Swift
Stars
19.7k
Forks
2.3k
Avg merge
1d 1h
Merged PRs (30d)
11

Description

New Issue Checklist
New rule request

Most macOS apps abuse this method. The problem is the ignoringOtherApps: true part. It means if you launch the app and then switch to a different app while it's launching, it will steal focus when it has finished launching. This is something that is hard to catch in development, but users gets annoyed by this behavior.

I propose reporting when NSApp.activate(ignoringOtherApps: true) is used inside applicationDidFinishLaunching. It's ok to use ignoringOtherApps: true in other situations. It's also ok to use NSApp.activate(ignoringOtherApps: false) inside applicationDidFinishLaunching.

Having this rule will hopefully make devs aware they're using the API incorrectly and fix their apps.

  1. Why should this rule be added? Share links to existing discussion about what
    the community thinks about this.

Because it's a widespread problem that affects real users: https://twitter.com/sindresorhus/status/1085002325511426051

  1. Provide several examples of what would and wouldn't trigger violations.

Would trigger:

@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
	func applicationDidFinishLaunching(_ notification: Notification) {
		NSApp.activate(ignoringOtherApps: true)
	}
}

Would not trigger:

@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
	func applicationDidFinishLaunching(_ notification: Notification) {
		NSApp.activate(ignoringOtherApps: false)
	}
}
@NSApplicationMain
final class AppDelegate: NSObject, NSApplicationDelegate {
	func foo() {
		NSApp.activate(ignoringOtherApps: true)
	}
}
  1. Should the rule be configurable, if so what parameters should be configurable?

No.

  1. Should the rule be opt-in or enabled by default? Why?
    See README.md for guidelines on when to mark a rule as opt-in.

Enabled by default.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start with README.md's opt-in-rules guidance and review SwiftLint's existing rule and test conventions, since this issue does not name implementation files or tests. Done means reporting only true activation inside applicationDidFinishLaunching, while allowing false activation there and true activation elsewhere, with coverage for the supplied examples.

Written by the indexing model from the issue text.

Assessment

Tech stack
swift
Domain
tooling
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.