メインコンテンツへ飛ぶ

Custom Window Interactions

Custom draggable regions​

By default, windows are dragged using the title bar provided by the OS chrome. Apps that remove the default title bar need to use the app-region CSS property to define specific areas that can be used to drag the window. Setting app-region: drag marks a rectangular area as draggable.

It is important to note that draggable areas ignore all pointer events. For example, a button element that overlaps a draggable region will not emit mouse clicks or mouse enter/exit events within that overlapping area. Setting app-region: no-drag reenables pointer events by excluding a rectangular area from a draggable region.

To make the whole window draggable, you can add app-region: drag as body's style:

styles.css
body {
app-region: drag;
}

そして、ウインドウ全体をドラッグ可能にした場合、ボタンをドラッグ不可として同時にマークしなければなりません。そうでなければ、ユーザーがボタンをクリックすることができなくなります。

styles.css
button {
app-region: no-drag;
}

If you're only setting a custom title bar as draggable, you also need to make all buttons in title bar non-draggable.

ヒント: テキスト選択の無効化​

ドラッグ可能な領域を作成したときに、ドラッグの動作がテキストの選択と競合することがあるでしょう。 For example, when you drag the title bar, you may accidentally select its text contents. これを防止するには、以下のようにドラッグ可能な領域内のテキスト選択を無効にする必要があります。

.titlebar {
user-select: none;
app-region: drag;
}

ヒント: コンテキストメニューの無効化​

いくつかのプラットフォームでは、ドラッグ可能な領域は非クライアントのフレームとして扱われます。そのため、ドラッグ可能な領域を右クリックするとシステムメニューが現れます。 すべてのプラットフォームでコンテキストメニューが正しく動作するようにするには、絶対にカスタムのコンテキストメニューをドラッグ可能な領域で使用しないでください。

Click-through windows​

To create a click-through window, i.e. making the window ignore all mouse events, you can call the win.setIgnoreMouseEvents(ignore) API:

main.js
const { BrowserWindow } = require('electron')

const win = new BrowserWindow()
win.setIgnoreMouseEvents(true)

Forward mouse events macOS Windows​

マウスメッセージを無視すると、ウェブコンテンツはマウスの動きを感知しないようになり、マウスを動かしたときのイベントが発生しなくなります。 On Windows and macOS, an optional parameter can be used to forward mouse move messages to the web page, allowing events such as mouseleave to be emitted:

main.js
const { BrowserWindow, ipcMain } = require('electron')

const path = require('node:path')

const win = new BrowserWindow({
webPreferences: {
preload: path.join(__dirname, 'preload.js')
}
})

ipcMain.on('set-ignore-mouse-events', (event, ignore, options) => {
const win = BrowserWindow.fromWebContents(event.sender)
win.setIgnoreMouseEvents(ignore, options)
})
preload.js
window.addEventListener('DOMContentLoaded', () => {
const el = document.getElementById('clickThroughElement')
el.addEventListener('mouseenter', () => {
ipcRenderer.send('set-ignore-mouse-events', true, { forward: true })
})
el.addEventListener('mouseleave', () => {
ipcRenderer.send('set-ignore-mouse-events', false)
})
})

This makes the web page click-through when over the #clickThroughElement element, and returns to normal outside it.