画面情報
ディスプレイとモニターに関する情報を取得します
画面情報
Wails は、すべてのプラットフォームで動作する統一画面 APIを提供します。一貫したコードで、画面情報の取得、複数モニターの検出、画面プロパティ(サイズ、位置、DPI)の照会、プライマリディスプレイの特定、DPI スケーリングへの対応が可能です。
クイックスタート
go
// Get all screens
screens := app.Screen.GetAll()
for _, screen := range screens {
fmt.Printf("Screen: %s (%dx%d)\n",
screen.Name, screen.Size.Width, screen.Size.Height)
}
// Get primary screen
primary := app.Screen.GetPrimary()
fmt.Printf("Primary: %s\n", primary.Name)これだけです!クロスプラットフォームで画面情報を取得できます。
画面情報の取得
すべての画面
go
screens := app.Screen.GetAll()
for _, screen := range screens {
fmt.Printf("ID: %s\n", screen.ID)
fmt.Printf("Name: %s\n", screen.Name)
fmt.Printf("Size: %dx%d\n", screen.Size.Width, screen.Size.Height)
fmt.Printf("Position: %d,%d\n", screen.X, screen.Y)
fmt.Printf("Scale: %.2f\n", screen.ScaleFactor)
fmt.Printf("Primary: %v\n", screen.IsPrimary)
fmt.Println("---")
}プライマリ画面
go
primary := app.Screen.GetPrimary()
fmt.Printf("Primary screen: %s\n", primary.Name)
fmt.Printf("Resolution: %dx%d\n", primary.Size.Width, primary.Size.Height)
fmt.Printf("Scale factor: %.2f\n", primary.ScaleFactor)現在の画面
ウィンドウが表示されている画面を取得します:
go
screen, err := window.GetScreen()
if err != nil {
// handle error
}
fmt.Printf("Window is on: %s\n", screen.Name)ID による画面の取得
go
screen := app.Screen.GetByID("screen-id")
if screen != nil {
fmt.Printf("Found screen: %s\n", screen.Name)
}画面のプロパティ
画面の構造
go
type Screen struct {
ID string // Unique identifier
Name string // Display name
ScaleFactor float32 // DPI scale (1.0, 1.5, 2.0, etc.)
X, Y int // Position (top-left, logical pixels)
Size Size // Logical size (Width, Height)
Bounds Rect // Logical bounds (X, Y, Width, Height)
PhysicalBounds Rect // Physical bounds (scaled)
WorkArea Rect // Logical work area excluding taskbars/menubars
PhysicalWorkArea Rect // Physical work area
IsPrimary bool // Is this the primary screen?
Rotation float32 // Screen rotation in degrees (e.g. 0, 90, 180, 270)
}
// Size and Rect are simple value types:
type Size struct{ Width, Height int }
type Rect struct{ X, Y, Width, Height int }ピクセル単位のサイズは screen.Size.Width / screen.Size.Height から読み取ります。トップレベルに Width/Height フィールドはありません。
物理ピクセルと論理ピクセル
go
screen := app.Screen.GetPrimary()
// Logical pixels (what you use)
logicalWidth := screen.Size.Width
logicalHeight := screen.Size.Height
// Physical pixels (actual display)
physicalWidth := int(float32(screen.Size.Width) * screen.ScaleFactor)
physicalHeight := int(float32(screen.Size.Height) * screen.ScaleFactor)
fmt.Printf("Logical: %dx%d\n", logicalWidth, logicalHeight)
fmt.Printf("Physical: %dx%d\n", physicalWidth, physicalHeight)
fmt.Printf("Scale: %.2f\n", screen.ScaleFactor)一般的なスケール係数:
1.0- 標準 DPI(96 DPI)1.25- 125% スケーリング(120 DPI)1.5- 150% スケーリング(144 DPI)2.0- 200% スケーリング(192 DPI)- Retina3.0- 300% スケーリング(288 DPI)- 4K/5K
ウィンドウの配置
画面中央への配置
go
func centreOnScreen(window *application.WebviewWindow, screen *Screen) {
windowWidth, windowHeight := window.Size()
x := screen.X + (screen.Size.Width-windowWidth)/2
y := screen.Y + (screen.Size.Height-windowHeight)/2
window.SetPosition(x, y)
}特定の画面への配置
go
func moveToScreen(window *application.WebviewWindow, screenIndex int) {
screens := app.Screen.GetAll()
if screenIndex < 0 || screenIndex >= len(screens) {
return
}
screen := screens[screenIndex]
// Centre on target screen
centreOnScreen(window, screen)
}画面を基準とした配置
go
// Top-left corner
func positionTopLeft(window *application.WebviewWindow, screen *Screen) {
window.SetPosition(screen.X+10, screen.Y+10)
}
// Top-right corner
func positionTopRight(window *application.WebviewWindow, screen *Screen) {
windowWidth, _ := window.Size()
window.SetPosition(screen.X+screen.Size.Width-windowWidth-10, screen.Y+10)
}
// Bottom-right corner
func positionBottomRight(window *application.WebviewWindow, screen *Screen) {
windowWidth, windowHeight := window.Size()
window.SetPosition(
screen.X+screen.Size.Width-windowWidth-10,
screen.Y+screen.Size.Height-windowHeight-10,
)
}マルチモニター対応
複数モニターの検出
go
func hasMultipleMonitors() bool {
return len(app.Screen.GetAll()) > 1
}
func getMonitorCount() int {
return len(app.Screen.GetAll())
}すべてのモニターの一覧表示
go
func listMonitors() {
screens := app.Screen.GetAll()
fmt.Printf("Found %d monitor(s):\n", len(screens))
for i, screen := range screens {
primary := ""
if screen.IsPrimary {
primary = " (Primary)"
}
fmt.Printf("%d. %s%s\n", i+1, screen.Name, primary)
fmt.Printf(" Resolution: %dx%d\n", screen.Size.Width, screen.Size.Height)
fmt.Printf(" Position: %d,%d\n", screen.X, screen.Y)
fmt.Printf(" Scale: %.2fx\n", screen.ScaleFactor)
}
}モニターの選択
go
func chooseMonitor() (*Screen, error) {
screens := app.Screen.GetAll()
if len(screens) == 1 {
return screens[0], nil
}
// Show dialog to choose
var options []string
for i, screen := range screens {
primary := ""
if screen.IsPrimary {
primary = " (Primary)"
}
options = append(options,
fmt.Sprintf("%d. %s%s - %dx%d",
i+1, screen.Name, primary, screen.Size.Width, screen.Size.Height))
}
// Use dialog to select
// (Implementation depends on your dialog system)
return screens[0], nil
}完全な例
マルチモニター対応ウィンドウマネージャー
go
type MultiMonitorManager struct {
app *application.App
windows map[int]*application.WebviewWindow
}
func NewMultiMonitorManager(app *application.App) *MultiMonitorManager {
return &MultiMonitorManager{
app: app,
windows: make(map[int]*application.WebviewWindow),
}
}
func (m *MultiMonitorManager) CreateWindowOnScreen(screenIndex int) error {
screens := m.app.Screen.GetAll()
if screenIndex < 0 || screenIndex >= len(screens) {
return errors.New("invalid screen index")
}
screen := screens[screenIndex]
// Create window
window := m.app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: fmt.Sprintf("Window on %s", screen.Name),
Width: 800,
Height: 600,
})
// Centre on screen
x := screen.X + (screen.Size.Width-800)/2
y := screen.Y + (screen.Size.Height-600)/2
window.SetPosition(x, y)
window.Show()
m.windows[screenIndex] = window
return nil
}
func (m *MultiMonitorManager) CreateWindowOnEachScreen() {
screens := m.app.Screen.GetAll()
for i := range screens {
m.CreateWindowOnScreen(i)
}
}画面変更の検出
go
type ScreenMonitor struct {
app *application.App
lastScreens []*Screen
changeHandler func([]*Screen)
}
func NewScreenMonitor(app *application.App) *ScreenMonitor {
return &ScreenMonitor{
app: app,
lastScreens: app.Screen.GetAll(),
}
}
func (sm *ScreenMonitor) OnScreenChange(handler func([]*Screen)) {
sm.changeHandler = handler
}
func (sm *ScreenMonitor) Start() {
ticker := time.NewTicker(2 * time.Second)
go func() {
for range ticker.C {
sm.checkScreens()
}
}()
}
func (sm *ScreenMonitor) checkScreens() {
current := sm.app.Screen.GetAll()
if len(current) != len(sm.lastScreens) {
sm.lastScreens = current
if sm.changeHandler != nil {
sm.changeHandler(current)
}
}
}DPI 対応のウィンドウサイズ設定
go
func createDPIAwareWindow(screen *Screen) *application.WebviewWindow {
// Base size at 1.0 scale
baseWidth := 800
baseHeight := 600
// Adjust for DPI
width := int(float32(baseWidth) * screen.ScaleFactor)
height := int(float32(baseHeight) * screen.ScaleFactor)
window := app.Window.NewWithOptions(application.WebviewWindowOptions{
Title: "DPI-Aware Window",
Width: width,
Height: height,
})
// Centre on screen
x := screen.X + (screen.Size.Width-width)/2
y := screen.Y + (screen.Size.Height-height)/2
window.SetPosition(x, y)
return window
}画面レイアウトビジュアライザー
go
func visualiseScreenLayout() string {
screens := app.Screen.GetAll()
var layout strings.Builder
layout.WriteString("Screen Layout:\n\n")
for i, screen := range screens {
primary := ""
if screen.IsPrimary {
primary = " [PRIMARY]"
}
layout.WriteString(fmt.Sprintf("Screen %d: %s%s\n", i+1, screen.Name, primary))
layout.WriteString(fmt.Sprintf(" Position: (%d, %d)\n", screen.X, screen.Y))
layout.WriteString(fmt.Sprintf(" Size: %dx%d\n", screen.Size.Width, screen.Size.Height))
layout.WriteString(fmt.Sprintf(" Scale: %.2fx\n", screen.ScaleFactor))
layout.WriteString(fmt.Sprintf(" Physical: %dx%d\n",
int(float32(screen.Size.Width)*screen.ScaleFactor),
int(float32(screen.Size.Height)*screen.ScaleFactor)))
layout.WriteString("\n")
}
return layout.String()
}ベストプラクティス
✅ 推奨事項
- 画面数を確認する - 単一モニターと複数モニターの両方に対応します
- 論理ピクセルを使用する - Wails が DPI を自動的に処理します
- ウィンドウを中央に配置する - 固定位置よりも優れた UX を実現できます
- 位置を検証する - ウィンドウが画面内に表示されるようにします
- 画面の変更に対応する - モニターは追加または取り外される場合があります
- 異なる DPI 設定でテストする - 100%、125%、150%、200%
❌ 非推奨事項
- 位置をハードコードしない - 画面の寸法を使用します
- プライマリ画面だと決めつけない - ユーザーが複数のモニターを使用している可能性があります
- スケール係数を無視しない - DPI 対応には重要です
- 画面外に配置しない - 座標を検証します
- 画面の変更への対応を忘れない - ノート PC はドッキングまたはドッキング解除されることがあります
- 物理ピクセルを使用しない - 論理ピクセルを使用します
プラットフォームによる違い
macOS
- Retina ディスプレイ(スケール係数 2x)
- 複数ディスプレイの使用が一般的
- 座標系:左下が(0,0)
- Spaces(仮想デスクトップ)が配置に影響します
Windows
- さまざまな DPI スケーリング(100%、125%、150%、200%)
- 複数ディスプレイの使用が一般的
- 座標系:左上が(0,0)
- モニターごとの DPI 対応
Linux
- デスクトップ環境によって異なります
- X11 と Wayland で違いがあります
- DPI スケーリングの対応状況は異なります
- 複数ディスプレイに対応しています
次のステップ
ウィンドウ
ウィンドウ管理について学びます。
ウィンドウオプション
ウィンドウの外観を設定します。
複数のウィンドウ
マルチウィンドウのパターンについて説明します。
バインディング
JavaScript から Go 関数を呼び出します。