cerebroapp / cerebro

🔵 Cerebro is an open-source launcher to improve your productivity and efficiency
https://www.cerebroapp.com/
MIT License
8.33k stars 455 forks source link

[feature] Choose screen behavior #647

Closed emilioidk closed 10 months ago

emilioidk commented 1 year ago

Feature

Cerebro always opens in the middle of the first screen but for people using several screens (which I assume is quite common in the kind of people that would use Cerebro) it would be much more ergonomic to be able to set the screen behavior. By default Cerebro should open centered in the screen in which the mouse pointer is but the option to set a fixed screen to open Cerebro in could be very useful for many users.

emilioidk commented 1 year ago

I was digging into how to make it possible and I was able to accomplish it with almost no effort. The video I attach shows my local dev version opening in the screen in which the mouse pointer is at.

https://user-images.githubusercontent.com/2219357/212408992-c365695c-346d-46be-91e6-7f1f3f14cf62.mp4

There are some caveats with my solution based entirely in my lack of competence in Electron and my intention of doing a quick prototype to see how it would work. Without understanding much why, I would have expected the code for this functionality to live in the "renderer" side of the application however to make it work I needed to use Electron's screen (https://www.electronjs.org/docs/latest/api/screen) which is only available in the main process. The code in my prototype looks like this

app.whenReady().then(() => {
  // We cannot require the screen module until the app is ready.
  const { screen } = require('electron')

  ...

  mainWindow.on('show', (event) => {
    const cursorScreenPoint = screen.getCursorScreenPoint()
    const nearestDisplay = screen.getDisplayNearestPoint(cursorScreenPoint)

    const goalWidth = 650 // hardcoded now, should get from config or calculate accordingly
    const goalX = Math.floor(nearestDisplay.bounds.x + (nearestDisplay.size.width - goalWidth) / 2)
    const goalY = 200 // hardcoded now, should get from config or calculate accordingly

    config.set('winPosition', [goalX, goalY])
  })

I have only tested this example in Ubuntu 22.04 but the methods used from screen seem to be available for all platforms.

ogustavo-pereira commented 1 year ago

Hi thanks @emilioidk for the feature suggestion, always wanted to improve rendering on multiple screens, feel free to upload a pr so we can review

emilioidk commented 11 months ago

@oguhpereira I'd forgotten about it, sorry about that. I have now created a PR, looking forward to getting feedback.

ogustavo-pereira commented 10 months ago

Thanks @emilioidk