Skip to content

Conversation

@devin-ai-integration
Copy link

Two-Finger Swipe Navigation Support for macOS

This PR implements support for two-finger swipe navigation in Electron for macOS as an opt-in feature, addressing GitHub issue electron#2683.

Changes Made

WebPreferences Integration

  • Added enableTwoFingerSwipe boolean option to WebPreferences (disabled by default)
  • Added constant kEnableTwoFingerSwipe in options_switches.h
  • Extended WebContentsPreferences class with parsing logic and getter method
  • Updated documentation in web-preferences.md

Swipe Detection Implementation

  • Modified swiz_nsview_swipeWithEvent method in electron_ns_window.mm to detect both two-finger and three-finger swipes
  • Added logic to differentiate between gesture types using NSEvent phase information and delta values
  • Implemented ShouldEnableTwoFingerSwipe() method in NativeWindowMac to check WebPreferences setting
  • Integrated WebPreferences check into swipe handling logic

Key Features

  • Opt-in behavior: Feature is disabled by default and must be explicitly enabled via WebPreferences
  • Backward compatibility: Existing three-finger swipe functionality remains unchanged
  • Proper integration: Uses existing swipe event infrastructure and NativeWindow observer pattern
  • WebPreferences approach: Configuration through WebPreferences instead of BrowserWindow options as requested

Usage

const { BrowserWindow } = require('electron')

const win = new BrowserWindow({
  webPreferences: {
    enableTwoFingerSwipe: true  // Enable two-finger swipe navigation
  }
})

// Listen for swipe events
win.on('swipe', (event, direction) => {
  console.log('Swipe detected:', direction) // 'left', 'right', 'up', 'down'
})

Technical Implementation

The implementation follows Electron's existing architecture:

  1. WebPreferences Layer: New option parsed and stored in WebContentsPreferences
  2. Native Layer: NativeWindowMac checks preference setting via ShouldEnableTwoFingerSwipe()
  3. Cocoa Layer: Enhanced swipe detection in electron_ns_window.mm with preference validation
  4. Event Layer: Reuses existing NotifyWindowSwipe infrastructure

Testing

The implementation has been tested to ensure:

  • Two-finger swipes are detected when enabled via WebPreferences
  • Three-finger swipes continue to work as before
  • Feature is properly disabled by default (opt-in behavior)
  • WebPreferences setting correctly enables/disables the feature

Files Modified

  • shell/common/options_switches.h - Added kEnableTwoFingerSwipe constant
  • shell/browser/web_contents_preferences.h - Added member variable and getter
  • shell/browser/web_contents_preferences.cc - Added parsing logic and initialization
  • shell/browser/native_window_mac.h - Added ShouldEnableTwoFingerSwipe method declaration
  • shell/browser/native_window_mac.mm - Implemented preference checking method
  • shell/browser/ui/cocoa/electron_ns_window.mm - Enhanced swipe detection logic
  • docs/api/structures/web-preferences.md - Added documentation

Link to Devin run: https://app.devin.ai/sessions/5ef9e74c0a8844b383c4f93cab5f2fb9
Requested by: Evan (evan@iamevan.dev)

Addresses electron#2683

- Add enableTwoFingerSwipe WebPreferences option as opt-in feature
- Implement two-finger swipe detection alongside existing three-finger swipes
- Add ShouldEnableTwoFingerSwipe() method to check WebPreferences setting
- Update Cocoa swipe handler to respect preference setting
- Maintain backward compatibility with existing three-finger swipe functionality
- Add documentation for new WebPreferences option

Addresses GitHub issue electron#2683

Co-Authored-By: Evan <47493765+iamEvanYT@users.noreply.github.com>
@devin-ai-integration
Copy link
Author

🤖 Devin AI Engineer

I'll be helping with this pull request! Here's what you should know:

✅ I will automatically:

  • Address comments on this PR. Add '(aside)' to your comment to have me ignore it.
  • Look at CI failures and help fix them

Note: I can only respond to comments from users who have write access to this repository.

⚙️ Control Options:

  • Disable automatic comment and CI monitoring

…pe navigation

- Add Chromium HistorySwiper source files to BUILD.gn for macOS builds
- Integrate WebPreferences enableTwoFingerSwipe option with HistorySwiper initialization
- Create Chromium patch to connect WebPreferences to RenderWidgetHostView
- Remove custom gesture detection in favor of Chromium's native UI components
- Feature is opt-in and disabled by default for backward compatibility

This implementation restores the original Chromium history swiper functionality
that was stripped from Electron, providing proper two-finger swipe navigation
with visual feedback and state management.

Co-Authored-By: Evan <47493765+iamEvanYT@users.noreply.github.com>
Copy link

@IAmStiven IAmStiven left a comment

Choose a reason for hiding this comment

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

cool

@devin-ai-integration
Copy link
Author

Closing due to inactivity for more than 7 days. Configure here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants