BaseWindow
ウインドウを作成したり、制御したりします。
Process: Main
BaseWindow provides a flexible way to compose multiple web views in a
single window. For windows with only a single, full-size web view, the
BrowserWindow class may be a simpler option.
app モジュールの ready イベントが発生するまでは、このモジュールは使用できません。
// メインプロセス内。
const { BaseWindow, WebContentsView } = require('electron')
const win = new BaseWindow({ width: 800, height: 600 })
const leftView = new WebContentsView()
leftView.webContents.loadURL('https://electronjs.org')
win.contentView.addChildView(leftView)
const rightView = new WebContentsView()
rightView.webContents.loadURL('https://github.com/electron/electron')
win.contentView.addChildView(rightView)
leftView.setBounds({ x: 0, y: 0, width: 400, height: 600 })
rightView.setBounds({ x: 400, y: 0, width: 400, height: 600 })
親ウィンドウと子ウィンドウ
parent オプションを使用することで、子ウインドウを作成できます。
const { BaseWindow } = require('electron')
const parent = new BaseWindow()
const child = new BaseWindow({ parent })
child ウインドウは、常に parent ウインドウの前面に表示されます。
モーダルウィンドウ
モーダルウインドウは、親ウインドウを無効にする子ウインドウです。 モーダルウインドウを作成するには、parent と modal の両方のオプションを設定しなければなりません。
const { BaseWindow } = require('electron')
const parent = new BaseWindow()
const child = new BaseWindow({ parent, modal: true })
プラットフォームに関する注意事項
- macOSでは、モーダルウインドウは親ウインドウに付随したシートとして表示されます。
- 親ウインドウが移動したとき、macOSでは、子ウインドウは親ウインドウに対する相対的な位置を維持しますが、Windows と Linux では、子ウインドウは移動しません。
- Linux では、モーダルウインドウのタイプは
dialogに変更されます。 - Linuxでは、多くのデスクトップ環境は、モーダルウインドウを非表示にすることをサポートしていません。
Resource management
When you add a WebContentsView to a BaseWindow and the BaseWindow
is closed, the webContents of the WebContentsView are not destroyed
automatically.
It is your responsibility to close the webContents when you no longer need them, e.g. when
the BaseWindow is closed:
const { BaseWindow, WebContentsView } = require('electron')
const win = new BaseWindow({ width: 800, height: 600 })
const view = new WebContentsView()
win.contentView.addChildView(view)
win.on('closed', () => {
view.webContents.close()
})
Unlike with a BrowserWindow, if you don't explicitly close the
webContents, you'll encounter memory leaks.
クラス: BaseWindow
ウインドウを作成したり、制御したりします。
Process: Main
BaseWindow は EventEmitter を継承しています。
options によって設定されたネイティブのプロパティで新しい BrowserWindow を生成します。
Electron's built-in classes cannot be subclassed in user code. For more information, see the FAQ.
new BaseWindow([options])
インスタンスイベント
new BaseWindow で作成されたオブジェクトでは以下のイベントが発生します。
Some events are only available on specific operating systems and are labeled as such.
イベント: 'close'
戻り値:
eventEvent
ウインドウがクローズされようとするときに発生します。 これは、DOM の beforeunload と unload イベントの前に発生します。 event.preventDefault() を呼び出すことで、ウインドウを閉じる処理がキャンセルされます。
大抵はウインドウをクローズさせる必要があるかどうかを判断するために beforeunload ハンドラーを使用したいと思うでしょうが、これはウインドウがリロードされるときにも呼び出されます。 Electron では、undefined 以外の値を return すれば閉じる処理をキャンセルします。 以下がその例です。
window.onbeforeunload = (e) => {
console.log('I do not want to be closed')
// ユーザーにメッセージボックスが表示される通常のブラウザーとは異なり、
// 非 void の値を返すと閉じる操作は暗黙的にキャンセルされます。
// ユーザーにアプリケーションの終了を確認させるには、ダイアログ API を
// 使用することを推奨します。
e.returnValue = false
}
There is a subtle difference between the behaviors of window.onbeforeunload = handler and
window.addEventListener('beforeunload', handler). It is recommended to always set the
event.returnValue explicitly, instead of only returning a value, as the former works more
consistently within Electron.
イベント: 'closed'
ウインドウが閉じられたときに発生します。 このイベントを受け取った後は、ウインドウへの参照を削除し、以降そのウインドウを使用しないようにしてください。
Event: 'query-session-end' Windows
戻り値:
eventWindowSessionEndEvent
Emitted when a session is about to end due to a shutdown, machine restart, or user log-off.
Calling event.preventDefault() can delay the system shutdown, though it’s generally best
to respect the user’s choice to end the session. However, you may choose to use it if
ending the session puts the user at risk of losing data.
イベント: 'session-end' Windows
戻り値:
eventWindowSessionEndEvent
Emitted when a session is about to end due to a shutdown, machine restart, or user log-off. Once this event fires, there is no way to prevent the session from ending.
イベント: 'blur'
戻り値:
eventEvent
ウインドウがフォーカスを失うときに発生します。
イベント: 'focus'
戻り値:
eventEvent
ウインドウがフォーカスを得るときに発生します。
イベント: 'show'
ウインドウが表示されるときに発生します。
イベント: 'hide'
ウインドウが非表示になるときに発生します。
イベント: 'maximize'
ウィンドウが最大化されるときに発生します。
イベント: 'unmaximize'
ウインドウが最大化状態から抜けるときに発生します。
イベント: 'minimize'
ウィンドウが最小化されるときに発生します。
イベント: 'restore'
ウインドウが最小化状態から復元されたときに発生します。
イベント: 'will-resize' macOS Windows
戻り値:
eventEventnewBoundsRectangle - Size the window is being resized to.detailsObjectedge(string) - サイズ変更のためにドラッグされているウインドウの縁。bottom,left,right,top-left,top-right,bottom-left,bottom-rightのいずれかになります。
ウィンドウがリサイズされる前に発生します。 event.preventDefault() を呼び出すことで、ウインドウのリサイズを阻害できます。
このイベントは、ウィンドウが手動でリサイズされようとしているときにしか発生しません。 ウインドウを setBounds や setSize でリサイズする時には、このイベントは発生しません。
edge オプションが取りうる値と動作は、プラットフォーム依存です。 以下は取りうる値です。
- Windows では、取りうる値は
bottom,top,left,right,top-left,top-right,bottom-left,bottom-rightです。 - macOS では、取りうる値は
bottom、rightです。- 値
bottomは垂直方向のサイズ変更の表現に使用されます。 - 値
rightは水平方向のサイズ変更の表現に使用されます。
- 値
イベント: 'resize'
ウインドウがリサイズされた後に発生します。
イベント: 'resized' macOS Windows
ウインドウがリサイズされるときに一度発生します。
これは、通常、ウィンドウが手動でリサイズされようとしているときにしか発生しません。 macOS の場合、setBounds/setSize でウィンドウのサイズを変更して animate パラメーターを true に設定すると、サイズ変更が完了したときにもこのイベントが発生します。
イベント: 'will-move' macOS Windows
戻り値:
eventEventnewBoundsRectangle - Location the window is being moved to.
ウィンドウが移動される前に発生します。 Windows では、event.preventDefault() を呼び出すことでウインドウの移動を阻害できます。
このイベントは、ウィンドウが手動で移動されようとしているときにしか発生しません。 ウインドウを setPosition/setBounds/center で移動させる時には、このイベントは発生しません。
イベント: 'move'
ウインドウが新しい位置に移動されているときに発生します。
イベント: 'moved' macOS Windows
ウインドウが新しい位置に移動されるときに一回だけ、発生します。
On macOS, this event is an alias of move.
イベント: 'enter-full-screen'
ウインドウがフルスクリーン状態に入るときに発生します。
イベント: 'leave-full-screen'
ウインドウがフルスクリーン状態を抜けるときに発生します。
イベント: 'always-on-top-changed'
戻り値:
eventEventisAlwaysOnTopboolean
ウインドウが常に他のウインドウの手前に表示されるように設定またはそれが解除されたときに発生します。
イベント: 'app-command' Windows Linux
戻り値:
eventEventcommandstring
アプリのコマンドが 呼び出されたときに発生します。 これらは、Windowsで幾つかのマウスに組み込まれている "Back" ボタンだけでなく、一般的にキーボードのメディアキーやブラウザコマンドとも関連付けられています。
コマンドは小文字にされ、アンダースコアはハイフンに置き換えられ、APPCOMMAND_ の接頭辞は除かれます。
例: APPCOMMAND_BROWSER_BACKWARD は browser-backward として発生します。
const { BaseWindow } = require('electron')
const win = new BaseWindow()
win.on('app-command', (e, cmd) => {
// ユーザーがマウスの戻るボタンを押したときにウインドウのナビゲーションを戻します
if (cmd === 'browser-backward') {
// ナビゲーションする適切な WebContents を見つけます。
}
})
Linux 上では以下のアプリコマンドが明示的にサポートされます。
browser-backwardbrowser-forward
イベント: 'swipe' macOS
戻り値:
eventEvent- Event: 'swipe' macOS
3 本指でのスワイプ時に発生します。 取りうる方向は up, right, down, left です。
このイベントは、スワイプで画面の内容が移動しない、古い macOS スタイルのトラックパッドスワイプを元にしています。 ほとんどの macOS トラックパッドは、このようなスワイプを許可するように設定されていないため、適切にスワイプさせるには システム環境設定 > トラックパッド > その他のジェスチャ の「ページ間をスワイプ」の設定を「2 本指または 3 本指でスワイプ」に設定する必要があります。
イベント: 'rotate-gesture' macOS
戻り値:
eventEventrotationFloat
トラックパッドの回転ジェスチャで発生します。 回転ジェスチャーが終了するまで継続的に発生します。 各イベント発生の rotation の値は、最後の発生以降に回転した度数法の角度です。 回転ジェスチャーにおいて発生した最後のイベントは、常に 0 の値になります。 反時計回りの回転値は正であり、時計回りの回転値は負です。
イベント: 'sheet-begin' macOS
ウインドウがシートを開くときに発生します。
イベント: 'sheet-end' macOS
ウインドウがシートを閉じたときに発生します。
イベント: 'new-window-for-tab' macOS
ネイティブの新規タブボタンがクリックされるときに発生します。
Event: 'system-context-menu' Windows Linux
戻り値:
eventEventpointPoint - The screen coordinates where the context menu was triggered.
システムコンテキストメニューがウィンドウ上でトリガーされたときに発生します。 通常ユーザーがウィンドウのクライアントエリア以外を右クリックしたときにトリガーされます。 これはウインドウタイトルバーか、フレームレスウィンドウで -webkit-app-region: drag と宣言した任意の領域です。
event.preventDefault() を呼ぶと、そのメニューは表示されなくなります。
To convert point to DIP, use screen.screenToDipPoint(point).
静的メソッド
BaseWindow クラスには以下の静的メソッドがあります。
BaseWindow.getAllWindows()
戻り値 BrowserWindow[] - すべての開かれたブラウザウインドウの配列。
BaseWindow.getFocusedWindow()
戻り値 BrowserWindow | null - このアプリケーションでフォーカスされているウインドウ。フォーカスが無い場合は null を返します。
BaseWindow.fromId(id)
idInteger
戻り値 BrowserWindow | null - 指定された id のウインドウ。
インスタンスプロパティ
new BaseWindow で作成されたオブジェクトは、以下のプロパティを持っています。
const { BaseWindow } = require('electron')
// この例では `win` がインスタンスです
const win = new BaseWindow({ width: 800, height: 600 })
win.id 読み取り専用
Integer 型のプロパティです。そのウインドウの一意な ID を表します。 各 ID は、この Electron アプリケーション全体のすべての BrowserWindow インスタンス間で一意です。
win.contentView
View 型のプロパティで、ウインドウの内容のビューです。
win.tabbingIdentifier macOS 読み取り専用
string 型 (任意) のプロパティで、BrowserWindow コンストラクタに渡された tabbingIdentifier に等しくなります。何も設定されていない場合は undefined です。
win.autoHideMenuBar Linux Windows
boolean 型のプロパティです。ウインドウがユーザーによって手動で最小化できるかどうかを決定します。 セットすると、メニューバーはユーザが単独で Alt キーを押したときのみに表示されます。
メニューバーが既に表示されている場合、このプロパティを true にセットしてもすぐに非表示にはなりません。
win.simpleFullScreen
boolean 型のプロパティです。ウインドウがシンプルな (Lion 以前の) フルスクリーンモードかどうかを決定します。
win.fullScreen
boolean 型のプロパティです。ウインドウがフルスクリーンモードかどうかを決定します。
win.focusable Windows macOS
boolean 型のプロパティです。ウインドウにフォーカスできるどうかを決定します。
win.visibleOnAllWorkspaces macOS Linux
boolean 型のプロパティで、ウインドウがすべてのワークスペースで表示されるどうかを決定します。
Always returns false on Windows.
win.shadow
boolean 型のプロパティです。ウインドウに影があるかどうかを決定します。
win.menuBarVisible Windows Linux
boolean 型のプロパティで、メニューバーが表示されるかどうかを決定します。
If the menu bar is auto-hide, users can still bring up the menu bar by pressing the single Alt key.
win.kiosk
boolean 型のプロパティで、ウインドウがキオスクモードかどうかを決定します。
win.documentEdited macOS
boolean 型のプロパティで、ウインドウのドキュメントが編集されたかどうかを決定します。
true にすると、タイトルバーのアイコンが灰色になります。
win.representedFilename macOS
string 型のプロパティです。ウインドウが表すファイルのパス名を決定し、そのファイルのアイコンをウインドウのタイトルバーに表示します。
win.title
string 型のプロパティで、ネイティブウインドウのタイトルを決定します。
The title of the web page can be different from the title of the native window.
win.minimizable macOS Windows
boolean 型のプロパティで、ウインドウがユーザーによって手動で最小化できるかどうかを決定します。
Linux ではセッターは何もしませんが、ゲッターは true を返します。
win.maximizable macOS Windows
boolean 型のプロパティで、ウインドウがユーザーによって手動で最大化できるかどうかを決定します。
Linux ではセッターは何もしませんが、ゲッターは true を返します。
win.fullScreenable
boolean 型のプロパティで、ウインドウを最大化/ズームするウインドウボタンでフルスクリーンモードや最大化をトグル切り替えできるかどうかを決定します。
win.resizable
boolean 型のプロパティで、ウインドウがユーザーによって手動でリサイズできるかどうかを決定します。
win.closable macOS Windows
boolean 型のプロパティで、ウインドウがユーザーによって手動でリサイズできるかどうかを決定します。
Linux ではセッターは何もしませんが、ゲッターは true を返します。
win.movable macOS Windows
boolean 型のプロパティで、ウインドウがユーザーによって動かせるかどうかを決定します。
Linux ではセッターは何もしませんが、ゲッターは true を返します。
win.excludedFromShownWindowsMenu macOS
boolean 型のプロパティで、このウインドウをアプリケーションのウインドウのメニューから除外するかどうかを決定します。 省略値は false です。
const { Menu, BaseWindow } = require('electron')
const win = new BaseWindow({ height: 600, width: 600 })
const template = [
{
role: 'windowmenu'
}
]
win.excludedFromShownWindowsMenu = true
const menu = Menu.buildFromTemplate(template)
Menu.setApplicationMenu(menu)
win.accessibleTitle
string 型のプロパティで、スクリーンリーダーなどのアクセシビリティツールにのみ提供される代替タイトルを定義します。 この文字列はユーザに直接表示されません。
win.snapped Windows Readonly
A boolean property that indicates whether the window is arranged via Snap.
インスタンスメソッド
new BaseWindow で作成されたオブジェクトは、次のインスタンスメソッドを持っています。
Some methods are only available on specific operating systems and are labeled as such.
win.setContentView(view)
viewView
ウインドウの内容のビューを設定します。
win.getContentView()
Returns View - The content view of the window.
win.destroy()
強制的にウインドウを閉じます。ウェブページで unload と beforeunload のイベントは発生せず、close イベントもこのウインドウで発生しません。しかし、closed イベントが発生することは保証されます。
win.close()
ウインドウを閉じようとします。 これは、ユーザーが手動でウィンドウの閉じるボタンをクリックした場合と同じ効果があります。 ただし、 Web ページはウィンドウが閉じようとするのををキャンセルすることができます。 close イベント をご参照ください。
win.focus()
ウインドウにフォーカスを当てます。
win.blur()
ウインドウからフォーカスを外します。
win.isFocused()
戻り値 boolean - ウインドウがフォーカスされているかどうか。
win.isDestroyed()
戻り値 boolean - ウインドウが破棄されているかどうか。
win.show()
表示し、ウインドウにフォーカスを当てます。
win.showInactive()
ウインドウを表示しますが、フォーカスを当てません。
win.hide()
ウインドウを非表示にします。
win.isVisible()
戻り値 boolean - アプリのフォアグラウンドでウインドウがユーザーに見えるかどうか。
win.isModal()
戻り値 boolean - 現在のウインドウがモーダルウインドウかどうか。
win.maximize()
ウィンドウを最大化します。 ウインドウがまだ表示されていない場合、併せてウインドウを表示 (ただし、フォーカスは当たりません) します。
win.unmaximize()
ウインドウの最大化を解除します。
win.isMaximized()
戻り値 boolean - ウインドウが最大化されているかどうか。
win.minimize()
ウィンドウを最小化します。 一部のプラットフォームでは、最小化されたウィンドウが Dock に表示されます。
win.restore()
ウインドウを最小化された状態からその前の状態に戻します。
win.isMinimized()
戻り値 boolean - ウインドウが最小化されているかどうか。
win.setFullScreen(flag)
flagboolean
ウインドウをフルスクリーンモードにするかどうかを設定します。
On macOS, fullscreen transitions take place asynchronously. If further actions depend on the fullscreen state, use the 'enter-full-screen' or > 'leave-full-screen' events.
win.isFullScreen()
戻り値 boolean - ウインドウがフルスクリーンモードであるかどうか。
win.setSimpleFullScreen(flag) macOS
flagboolean
簡易フルスクリーンモードに設定したり、解除したりします。
macOS Lion (10.7) より前のバージョンで見られる簡易フルスクリーンモードはネイティブのフルスクリーン動作をエミュレートします。
win.isSimpleFullScreen() macOS
戻り値 boolean - ウインドウが簡易 (Lion 以前の) フルスクリーンモードであるかどうか。
win.isNormal()
戻り値 boolean - ウインドウが通常の状態 (最大化されていない、最小化されていない、フルスクリーンモードでない) かどうか。
win.setAspectRatio(aspectRatio[, extraSize])
aspectRatioFloat - 内容のビューの一部に維持させるアスペクト比。extraSizeSize (optional) macOS - The extra size not to be included while maintaining the aspect ratio.
これはウインドウのアスペクト比を維持します。 ピクセルで指定した追加のサイズによって、開発者は、アスペクト比の計算に含まれないスペースを確保することができます。 このAPIはウインドウのサイズとそのコンテンツのサイズの差異も考慮しています。
HDビデオプレーヤーと関連したコントロールを持つ通常のウインドウを考えてみましょう。 ひょっとすると、左端に15ピクセルのコントロール、右端に25ピクセルのコントロール、プレーヤーの下部に50ピクセルのコントロールがあるかもしれません。 プレーヤー内で 16:9 アスペクト比 (HD @1920x1280 の標準的なアスペクト比) を維持するためには、この関数を 16/9 と { width: 40, height: 50 } の引数で呼び出します。 2番目の引数は、追加の幅と高さがコンテンツビューの中に収まるかを気にしません。それらはただ存在しているだけです。 全体のコンテンツビュー内にある余分な幅と高さの領域を単純に足し合わせます。
win.setSize などの API でプログラム上からウインドウをリサイズした場合、アスペクト比は維持されません。
アスペクト比をリセットするには、aspectRatio の値として 0 を渡します。つまり、win.setAspectRatio(0) です。
win.setBackgroundColor(backgroundColor)
backgroundColorstring - 16進数、RGB、RGBA、HSL、HSLA、または名前付き CSS カラー形式の色。 16 進数タイプの場合のアルファチャンネルは任意です。
有効な backgroundColor の値の例を以下に示します。
- Hex
- #fff (略記 RGB)
- #ffff (略記 ARGB)
- #ffffff (RGB)
- #ffffffff (ARGB)
- RGB
rgb\(([\d]+),\s*([\d]+),\s*([\d]+)\)- 例: rgb(255, 255, 255)
- RGBA
rgba\(([\d]+),\s*([\d]+),\s*([\d]+),\s*([\d.]+)\)- 例: rgba(255, 255, 255, 1.0)
- HSL
hsl\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%\)- 例: hsl(200, 20%, 50%)
- HSLA
hsla\((-?[\d.]+),\s*([\d.]+)%,\s*([\d.]+)%,\s*([\d.]+)\)- 例: hsla(200, 20%, 50%, 0.5)
- 色の名前
- オプションは SkParseColor.cpp にリストされています。
- CSS カラーモジュールレベル 3 のキーワードと似ていますが、大文字と小文字を区別します。
- 例:
bluevioletやred
- 例:
ウィンドウの背景色を設定します。 See Setting backgroundColor.
win.previewFile(path[, displayName]) macOS
pathstring - Quick Lookでプレビューするファイルへの絶対パス。 これは、Quick Look がパスのファイル名とファイル拡張子を使用して、開くファイルのコンテンツタイプを決定するため重要です。displayNamestring (任意) - Quick Look のモーダルビューに表示するファイルの名前。 これは純粋に見た目だけのもので、ファイルのコンテンツタイプには影響しません。 省略値は、pathです。
Quick Look を使用して、指定パスのファイルをプレビューします。
win.closeFilePreview() macOS
現在開いている Quick Look のパネルを閉じます。
win.setBounds(bounds[, animate])
boundsPartial<Rectangle>animateboolean (任意) macOS
指定した境界までウインドウのサイズを変更して移動します。 指定されていないプロパティは、既定で現在の値になります。
const { BaseWindow } = require('electron')
const win = new BaseWindow()
// すべての境界のプロパティを設定します
win.setBounds({ x: 440, y: 225, width: 800, height: 600 })
// 1 つの境界のプロパティを設定します
win.setBounds({ width: 100 })
// { x: 440, y: 225, width: 100, height: 600 }
console.log(win.getBounds())
On macOS, the y-coordinate value cannot be smaller than the Tray height. Tray の高さは時期やオペレーティングシステムによって異なりますが、20 から 40px の間です。 Tray の高さより低い値を渡すと、Tray と同じ高さのウインドウになります。
win.getBounds()
Returns Rectangle - The bounds of the window as Object.
On macOS, the y-coordinate value returned will be at minimum the Tray height. 例えば、トレイの高さを 38 にして win.setBounds({ x: 25, y: 20, width: 800, height: 600 }) を呼び出すと、win.getBounds() は { x: 25, y: 38, width: 800, height: 600 } を返します。
win.getBackgroundColor()
戻り値 string - ウインドウの背景色を 16 進数 (#RRGGBB) 形式で取得します。
The alpha value is not returned alongside the red, green, and blue values.
win.setContentBounds(bounds[, animate])
boundsRectangleanimateboolean (任意) macOS
指定した境界までウインドウのクライアント領域 (例えば、Webページ) のサイズを変更して移動します。
win.getContentBounds()
Returns Rectangle - The bounds of the window's client area as Object.
win.getNormalBounds()
Returns Rectangle - Contains the window bounds of the normal state
Whatever the current state of the window : maximized, minimized or in fullscreen, this function always returns the position and size of the window in normal state. 通常状態においては、getBounds と getNormalBounds は同じ Rectangle を返します。
win.setEnabled(enable)
enableboolean
ウインドウを無効にするか有効にします。
win.isEnabled()
戻り値 boolean - ウインドウが有効化されているかどうか。
win.setSize(width, height[, animate])
widthIntegerheightIntegeranimateboolean (任意) macOS
ウインドウのサイズを width と height に変更します。 width または height が最小サイズ制約の設定値より低い場合、ウィンドウはその最小サイズにスナップします。
win.getSize()
戻り値 Integer[] - ウインドウの幅と高さを格納しています。
win.setContentSize(width, height[, animate])
widthIntegerheightIntegeranimateboolean (任意) macOS
ウインドウのクライアント領域 (例えばウェブページ) のサイズを width と height に変更します。
win.getContentSize()
戻り値 Integer[] - ウインドウのクライアント領域の幅と高さを格納しています。
win.setMinimumSize(width, height)
widthIntegerheightInteger
ウインドウの最小サイズを width と height に設定します。
win.getMinimumSize()
戻り値 Integer[] - ウインドウの最小の幅と高さを格納しています。
win.setMaximumSize(width, height)
widthIntegerheightInteger
ウインドウの最大サイズを width と height に設定します。
win.getMaximumSize()
戻り値 Integer[] - ウインドウの最大の幅と高さを格納しています。
win.setResizable(resizable)
resizableboolean
ウインドウがユーザによって手動でサイズ変更できるかどうかを設定します。
win.isResizable()
戻り値 boolean - ウインドウがユーザによって手動でサイズ変更できるかどうか。
win.setMovable(movable) macOS Windows
movableboolean
ウインドウがユーザーによって手動で移動できるかどうかを設定します。 Linux では何もしません。
win.isMovable() macOS Windows
戻り値 boolean - ウインドウがユーザーによって移動できるかどうか。
Linux では常に true を返します。
win.setMinimizable(minimizable) macOS Windows
minimizableboolean
ウインドウがユーザーによって手動で最小化できるかどうかを設定します。 Linux では何もしません。
win.isMinimizable() macOS Windows
戻り値 boolean - ウインドウがユーザによって手動で最小化できるかどうか。
Linux では常に true を返します。
win.setMaximizable(maximizable) macOS Windows
maximizableboolean
ウインドウがユーザーによって手動で最大化できるかどうかを設定します。 Linux では何もしません。
win.isMaximizable() macOS Windows
戻り値 boolean - ウインドウがユーザによって手動で最大化できるかどうか。
Linux では常に true を返します。
win.setFullScreenable(fullscreenable)
fullscreenableboolean
ウインドウの最大化/ズームボタンでフルスクリーンモードに切り替えるか、ウインドウを最大化するかを設定します。
win.isFullScreenable()
戻り値 boolean - ウインドウの最大化/ズームボタンでフルスクリーンモードに切り替えるのか、それともウインドウを最大化するのか。
win.setClosable(closable) macOS Windows
closableboolean
ウインドウがユーザーによって手動で閉じられるかどうかを設定します。 Linux では何もしません。
win.isClosable() macOS Windows
戻り値 boolean - ウインドウがユーザによって手動で閉じられるかどうか。
Linux では常に true を返します。
win.setHiddenInMissionControl(hidden) macOS
hiddenboolean
ユーザーが Mission Control に切り替えたときに、このウインドウを隠すかどうかを設定します。
win.isHiddenInMissionControl() macOS
戻り値 boolean - ユーザーが Mission Control に切り替えたときに、このウインドウを非表示にするかどうか。
win.setAlwaysOnTop(flag[, level][, relativeLevel])
flagbooleanlevelstring (任意) macOS Windows - 値はnormal,floating,torn-off-menu,modal-panel,main-menu,status,pop-up-menu,screen-saver, と(非推奨) のいずれかです.dockflagが true の場合、省略値はfloatingです。 flag が false の場合、levelはnormalにリセットされます。 注意として、floatingからstatusまでのものにおいては、ウインドウは macOS では Dock の後ろ側に、Windows ではタスクバーの後ろ側に配置されます。pop-up-menu以降は、macOS では Dock の手前に、Windows ではタスクバーの手前に表示されます。 詳細については、macOS のドキュメント を参照してください。relativeLevelInteger (任意) macOS - 指定されたlevelかた相対的にこのウインドウを配置する上位レイヤーの数。 省略値は0です。 注意として、Apple はscreen-saverより上のレベルで 1 より高く設定することを推奨していません。
ウィンドウを常に他のウィンドウの上に表示するかどうかを設定します。 この設定を行った後でも、ウィンドウはまだ通常のものであり、フォーカスが当てられないツールボックスウィンドウではありません。
win.isAlwaysOnTop()
戻り値 boolean - ウインドウが常に他のウインドウの上に表示されるかどうか。
win.moveAbove(mediaSourceId)
mediaSourceIdstring - DesktopCapturerSource の ID の形式のウインドウ ID。 例えば "window:1869:0" 。
Z オーダーの意味で、ウィンドウをソースウィンドウの上に移動します。 mediaSourceId がウインドウの ID でないか、そのウインドウが存在しない場合、このメソッドはエラーを送出します。
win.moveTop()
フォーカスに関係なく上 (Z順序) にウィンドウを移動します。
win.center()
ウインドウを画面の中央に移動します。
win.setPosition(x, y[, animate])
xIntegeryIntegeranimateboolean (任意) macOS
ウインドウを x と y に動かします。
win.getPosition()
戻り値 Integer[] - ウインドウの現在の位置を格納しています。
win.setTitle(title)
titlestring
ネイティブのウインドウのタイトルを title に変更します。
win.getTitle()
戻り値 string - ネイティブウインドウのタイトル。
The title of the web page can be different from the title of the native window.
win.setSheetOffset(offsetY[, offsetX]) macOS
offsetYFloatoffsetXFloat (任意)
macOS においてシートを設置する位置を変更します。 既定では、シートはウィンドウフレームのすぐ下に設置されますが、 HTML で表示されたツールバーの下に表示することもできます。 以下がその例です。
const { BaseWindow } = require('electron')
const win = new BaseWindow()
const toolbarRect = document.getElementById('toolbar').getBoundingClientRect()
win.setSheetOffset(toolbarRect.height)
win.flashFrame(flag)
History
| Version(s) | Changes |
|---|---|
None | macOS で |
None | API ADDED |
flagboolean
ユーザの注意を引きつけるためにウインドウの点滅を開始または停止します。
win.setSkipTaskbar(skip) macOS Windows
skipboolean
ウインドウがタスクバーに表示されなくなります。
win.setKiosk(flag)
flagboolean
キオスクモードに入ったり出たりします。
win.isKiosk()
戻り値 boolean - ウインドウがキオスクモードであるかどうか。
win.isTabletMode() Windows
戻り値 boolean - ウインドウが Windows 10 タブレットモードであるかどうか。
Windows 10 ユーザーは PC をタブレットとして使用 できるため、このモードではアプリはタイトルバーを拡大したり、タイトル バーのボタンを非表示にするなど、タブレット向けに UI を最適化する選択ができます。
この API は、ウインドウがタブレットモードかどうかを返します。resize イベントでタブレットモードへの変更をリッスンすることもできます。
win.getMediaSourceId()
戻り値 string - DesktopCapturerSource の ID の形式のウィンドウ ID。 例えば "window:1324:0" 。
より正確には、フォーマットは window:id:other_id です。ここでの id は、Windows では HWND、macOS では CGWindowID (uint64_t)、Linux では Window (unsigned long) です。 other_id は、同じトップレベルウィンドウ内のウェブコンテンツ (タブ) を識別するために使用されます。
win.getNativeWindowHandle()
戻り値 Buffer - プラットフォーム固有のウインドウのハンドル。
ハンドルのネイティブ型は、Windows では HWND、macOS では NSView*、Linux では Window (unsigned long) です。
win.hookWindowMessage(message, callback) Windows
messageIntegercallbackFunctionwParamBuffer - WndProc に指定されたwParamlParamBuffer - WndProc に指定されたlParam
ウィンドウメッセージをフックします。 メッセージが WndProc で受信されると、callback が呼び出されます。
win.isWindowMessageHooked(message) Windows
messageInteger
戻り値 boolean - メッセージがフックされているかどうかによって、true または false になります。
win.unhookWindowMessage(message) Windows
messageInteger
ウインドウメッセージのフックを解除します。
win.unhookAllWindowMessages() Windows
すべてのウインドウメッセージのフックを解除します。
win.setRepresentedFilename(filename) macOS
filenamestring
ウインドウが表すファイルのパス名を設定します。ファイルのアイコンがウインドウのタイトルバーに表示されます。
win.getRepresentedFilename() macOS
戻り値 string - ウインドウが表示中のファイルのパス名。
win.setDocumentEdited(edited) macOS
editedboolean
ウインドウのドキュメントが編集されたかどうかを指定します。true に設定すると、タイトルバーのアイコンがグレーになります。
win.isDocumentEdited() macOS
戻り値 boolean - ウインドウのドキュメントが編集されたかどうか。
win.setMenu(menu) Linux Windows
menuMenu | null
menu をウインドウのメニューバーとして設定します。
win.removeMenu() Linux Windows
ウインドウのメニューバーを消去します。
win.setProgressBar(progress[, options])
progressDouble
プログレスバーの進捗を設定します。 有効な範囲は [0, 1.0] です。
進捗 < 0 の場合、プログレスバーは削除されます。進捗 > 1 の場合、不確定モードに変更します。
Linux プラットフォームでは Unity デスクトップ環境のみがサポートされており、package.json の desktopName フィールドに *.desktop ファイル名を指定する必要があります。 既定では、{app.name}.desktop であるとみなされます。
Windowsでは、モードを渡すことができます。 受け付ける値は none, normal, indeterminate, error, paused です。 モードを設定せずに (ただし、有効範囲内の値で) setProgressBar を呼び出した場合、normal とみなされます。
win.setOverlayIcon(overlay, description) Windows
overlayNativeImage | null - the icon to display on the bottom right corner of the taskbar icon. この引数がnullの場合、オーバーレイは消去されます。descriptionstring - アクセシビリティのスクリーンリーダーに提供される説明
現在のタスクバーアイコンの上に、通常、何らかのアプリケーションステータスを伝えたり、ユーザーに控えめに通知したりするのに使われる16 x 16ピクセルのオーバレイを設定します。
win.invalidateShadow() macOS
ウインドウの影を現在のウインドウの形状に基づいて再計算するように更新します。
透過した BrowserWindow は、macOS で視覚的な残留物を残すことがあります。
このメソッドは、例えばアニメーションを実行するときにこれらの残留物を消去するときに利用できます。
win.setHasShadow(hasShadow)
hasShadowboolean
ウインドウに影を付けるべきかどうかを設定します。
win.hasShadow()
戻り値 boolean - ウインドウに影が付いているかどうか。
win.setOpacity(opacity) Windows macOS
opacitynumber - 0.0 (完全に透明) と 1.0 (完全に不透明) の間
ウィンドウの不透明度を設定します。 Linux では何もしません。 範囲外の値は [0, 1] に収められます。
win.getOpacity()
戻り値 number - 0.0 (完全に透明) と 1.0 (完全に不透明) の間です。 Linuxでは常に 1 を返します。
win.setShape(rects) Windows Linux 実験的
rectsRectangle[] - Sets a shape on the window. 空のリストを渡すと、ウィンドウが四角形に戻ります。
ウィンドウの形を設定すると、システム内で描画とユーザ操作が許可されているウィンドウ内の領域が決まります。 与えられた領域の外側のピクセルでは描画されず、マウスイベントも登録されません。 領域外のマウスイベントはそのウィンドウでは受信されませんが、ウィンドウの後ろにあるものにそのイベントがフォールスルーします。
win.setThumbarButtons(buttons) Windows
buttonsThumbarButton[]
戻り値 boolean - ボタンの追加に成功したかどうか
タスクバーボタンレイアウトのウインドウのサムネイルイメージに指定されたボタンのセットと一緒にサムネイルツールバーを追加します。 戻り値の boolean オブジェクトは、サムネイルの追加に成功したかどうかを示します。
限られた空間のため、サムネイルツールバーのボタン数は、7以下にしてください。 一度、サムネイルツールバーをセットアップすると、プラットフォームの制約のため、ツールバーを削除することはできません。 しかしながら、ボタンを取り除くためにAPIを空の配列で呼び出すことはできます。
buttons は、Button オブジェクトの配列です。
ButtonObjecticonNativeImage - The icon showing in thumbnail toolbar.clickFunctiontooltipstring (任意) - ボタンのツールチップのテキスト。flagsstring[] (任意) - ボタンの特定の状態や動作を制御します。 省略値は、['enabled']です。
flags は、以下の string を含めることができる配列です。
enabled- そのボタンはアクティブかつユーザが使用可能です。disabled- そのボタンは無効化されます。 存在しますが、ユーザの操作に応答しないことを示す視覚的状態です。dismissonclick- そのボタンをクリックすると、サムネイルウインドウがすぐに閉じます。nobackground- そのボタンの縁を描画せず、画像のみを使用します。hidden- そのボタンはユーザに表示されません。noninteractive- そのボタンは有効ですが、反応せず、押されたボタンの状態も描画されません。 この値は、例えば通知内で使用するボタンを想定しています。
win.setThumbnailClip(region) Windows
regionRectangle - Region of the window
タスクバーのウインドウの上でホバリングするときに表示されるサムネイルイメージとして表示するウインドウの領域を設定します。 空の領域: { x: 0, y: 0, width: 0, height: 0 } を指定することで、サムネイルをウインドウ全体にリセットすることができます。
win.setThumbnailToolTip(toolTip) Windows
toolTipstring
タスクバーのウインドウサムネイルでホバリングするときに表示されるツールチップを設定します。
win.setAppDetails(options) Windows
ウインドウのタスクバーボタンのプロパティを設定します。
relaunchCommand and relaunchDisplayName must always be set
together. いずれかが設定されていない場合、どちらも使用されません。
win.setAccentColor(accentColor) Windows
accentColorboolean | string - The accent color for the window. By default, follows user preference in System Settings.
Sets the system accent color and highlighting of active window border.
The accentColor parameter accepts the following values:
- Color string - Sets a custom accent color using standard CSS color formats (Hex, RGB, RGBA, HSL, HSLA, or named colors). Alpha values in RGBA/HSLA formats are ignored and the color is treated as fully opaque.
true- Uses the system's default accent color from user preferences in System Settings.false- Explicitly disables accent color highlighting for the window.
例:
const win = new BrowserWindow({ frame: false })
// Set red accent color.
win.setAccentColor('#ff0000')
// RGB format (alpha ignored if present).
win.setAccentColor('rgba(255,0,0,0.5)')
// Use system accent color.
win.setAccentColor(true)
// Disable accent color.
win.setAccentColor(false)
win.getAccentColor() Windows
Returns string | boolean - the system accent color and highlighting of active window border in Hex RGB format.
If a color has been set for the window that differs from the system accent color, the window accent color will
be returned. Otherwise, a boolean will be returned, with true indicating that the window uses the global system accent color, and false indicating that accent color highlighting is disabled for this window.
win.setIcon(icon) Windows Linux
iconNativeImage | string
ウインドウのアイコンを変更します。
win.setWindowButtonVisibility(visible) macOS
visibleboolean
ウインドウの信号ボタンを表示するかどうかを設定します。
win.setAutoHideMenuBar(hide) Windows Linux
hideboolean
ウィンドウのメニューバーを自動的に非表示にするかどうかを設定します。 セットすると、メニューバーはユーザが単独で Alt キーを押したときのみに表示されます。
メニューバーが既に表示されている場合、setAutoHideMenuBar(true) を呼び出してもすぐに非表示にはなりません。
win.isMenuBarAutoHide() Windows Linux
戻り値 boolean - メニューバーを自動的に非表示にするかどうか。
win.setMenuBarVisibility(visible) Windows Linux
visibleboolean
メニューバーを表示するかどうかを設定します。 メニューバーが自動的に非表示にされている場合でも、ユーザが単独で Alt キーを押下すれば、依然としてメニューバーを表示させることができます。
win.isMenuBarVisible() Windows Linux
戻り値 boolean - メニューバーを表示しているかどうか。
win.isSnapped() Windows
Returns boolean - whether the window is arranged via Snap.
The window is snapped via buttons shown when the mouse is hovered over window maximize button, or by dragging it to the edges of the screen.
win.setVisibleOnAllWorkspaces(visible[, options]) macOS Linux
visibleboolean
ウインドウをすべてのワークスペースで表示させるかどうかを設定します。
This API does nothing on Windows.
win.isVisibleOnAllWorkspaces() macOS Linux
戻り値 boolean - ウインドウがすべてのワークスペースで表示されているかどうか。
This API always returns false on Windows.
win.setIgnoreMouseEvents(ignore[, options])
ignoreboolean
ウインドウがすべてのマウスイベントを無視するようにします。
このウインドウで発生するすべてのマウスイベントは、このウインドウの下にあるウインドウに渡されますが、このウインドウにフォーカスがある場合、依然としてキーボードイベントは受信されます。
win.setContentProtection(enable) macOS Windows
enableboolean
他のアプリによってウインドウのコンテンツがキャプチャされるのを防止します。
macOS では、NSWindow の共有タイプを NSWindowSharingNone に設定します。
Windows では、 SetWindowDisplayAffinity を WDA_EXCLUDEFROMCAPTURE で呼び出します。
Windows 10 バージョン 2004 以降からウインドウのキャプチャが完全に削除されましたが、古い Windows バージョンで WDA_MONITOR が適用された場合は黒いウィンドウをキャプチャするように動作します。
win.isContentProtected() macOS Windows
Returns boolean - whether or not content protection is currently enabled.
win.setFocusable(focusable) macOS Windows
focusableboolean
ウインドウにフォーカスできるかどうかを変更します。
macOS ではウィンドウからフォーカスは除去されません。
win.isFocusable() macOS Windows
戻り値 boolean - ウインドウがフォーカスできるかどうか。
win.setParentWindow(parent)
parentBaseWindow | null
現在のウインドウの親ウインドウとして parent を設定します。null を渡すと、現在のウインドウをトップレベルウインドウにします。
win.getParentWindow()
戻り値 BrowserWindow | null - 親ウインドウ、もしくは親が無ければ null です。
win.getChildWindows()
戻り値 BrowserWindow[] - すべての子ウインドウ。
win.setAutoHideCursor(autoHide) macOS
autoHideboolean
タイプしているときにカーソルを非表示にするかどうかを制御します。
win.selectPreviousTab() macOS
ネイティブのタブが有効で、ウインドウに他のタブがあるとき、一つ前のタブを選択します。
win.selectNextTab() macOS
ネイティブのタブが有効で、ウインドウに他のタブがあるとき、次のタブを選択します。
win.showAllTabs() macOS
ネイティブのタブが有効な場合に、タブの概要を表示または非表示にします。
win.mergeAllWindows() macOS
ネイティブのタブが有効で複数の開いているウインドウがあるとき、すべてのウインドウを複数のタブで1つのウインドウにマージします。
win.moveTabToNewWindow() macOS
ネイティブのタブが有効で現在のウインドウに複数のタブがあるとき、現在のタブを新しいウインドウに移動します。
win.toggleTabBar() macOS
ネイティブのタブが有効で現在のウインドウにタブが1つだけしかないとき、タブバーを表示するかどうかを切り替えます。
win.addTabbedWindow(baseWindow) macOS
baseWindowBaseWindow
ウインドウインスタンスのタブの後ろに、このウインドウのタブとしてウインドウを追加します。
win.setVibrancy(type) macOS
typestring | null -titlebar,selection,menu,popover,sidebar,header,sheet,window,hud,fullscreen-ui,tooltip,content,under-window,under-pageのいずれかにできます。 詳細は macOS のドキュメント を参照してください。
ウインドウにすりガラス効果を追加します。 null または空の文字列を渡すと、ウインドウのすりガラス効果を取り除きます。
win.setBackgroundMaterial(material) Windows
materialstringauto- デスクトップウインドウマネージャ (DWM) に、このウインドウのシステム描画の背景マテリアルを自動決定させます。 これが既定値です。none- システムの背景を描画しません。mica- 長い間表示されるウインドウに対応する背景マテリアルの効果を描画します。acrylic- 一時的なウインドウに対応する背景マテリアルの効果を描画します。tabbed- タブ化したタイトルバー付きのウインドウに対応する背景マテリアルの効果を描画します。
このメソッドは、非クライアント領域の背後を含む、ブラウザウインドウのシステム描画の背景マテリアルを設定します。
詳細については Windows のドキュメント をご参照ください。
This method is only supported on Windows 11 22H2 and up.
win.setWindowButtonPosition(position) macOS
positionPoint | null
フレームレスウインドウにおける信号機ボタンのカスタム位置を設定します。
null を渡すと既定の位置へリセットします。
win.getWindowButtonPosition() macOS
戻り値 Point | null - フレームレスウインドウの信号機ボタンのカスタム位置。カスタム位置がない場合は null が返されます。
win.setTouchBar(touchBar) macOS
touchBarTouchBar | null
現在のウインドウのTouchBarレイアウトを設定します。 null または undefined を指定すると、TouchBar が消去されます。 このメソッドは TouchBar 搭載マシンでのみ作用します。
The TouchBar API is currently experimental and may change or be removed in future Electron releases.
win.setTitleBarOverlay(options) Windows Linux
ウインドウコントロールオーバーレイがすでに有効になっている Window に対して、このメソッドはそのタイトルバーのオーバーレイのスタイルを更新します。
Linux では、color を明示的に設定していない場合、symbolColor は color に対して最小限のアクセシビリティがあるコントラストになるように自動計算されます。