electron 功能请求:Windows任务栏位置

ipakzgxi  于 4个月前  发布在  Electron
关注(0)|答案(1)|浏览(40)

在Windows中,有一个API可以确定任务栏的位置(包括多个显示器),这将是很好的。

fumotvh3

fumotvh31#

对于未来的读者,这里是用Go编写的代码(我将其转换为JS):我相信你能理解其基本思想并进行转换。
它可以在macOS和Windows上运行,并且支持多个显示器:

type TaskBarPos int

const (
	UNKNOWN TaskBarPos = -1
	LEFT    TaskBarPos = 0
	RIGHT   TaskBarPos = 1
	TOP     TaskBarPos = 2
	BOTTOM  TaskBarPos = 3
)

type Rect struct {
	top    int
	bottom int
	width  int
	height int
	left   int
	right  int
}

func (r Rect) centerX() int {
	return r.left + ((r.right - r.left) / 2)
}

func (r Rect) centerY() int {
	return r.top + ((r.bottom - r.top) / 2)
}

func taskbar(tray *js.Object) TaskBarPos {

	// Step 1 - Get relevant display
	display := screen.Call("getDisplayNearestPoint", screen.Call("getCursorScreenPoint"))

	// Step 2 - 
	bounds := display.Get("bounds")
	workArea := display.Get("workArea")
	var tb *Rect

	d := Rect{
		top:    bounds.Get("y").Int(),
		bottom: bounds.Get("y").Int() + bounds.Get("height").Int(),
		width:  bounds.Get("width").Int(),
		height: bounds.Get("height").Int(),
		left:   bounds.Get("x").Int(),
		right:  bounds.Get("x").Int() + bounds.Get("width").Int(),
	}

	wa := Rect{
		top:    workArea.Get("y").Int(),
		bottom: workArea.Get("y").Int() + workArea.Get("height").Int(),
		width:  workArea.Get("width").Int(),
		height: workArea.Get("height").Int(),
		left:   workArea.Get("x").Int(),
		right:  workArea.Get("x").Int() + workArea.Get("width").Int(),
	}

	if tray != nil {
		tBounds := tray.Call("getBounds")
		tb = &Rect{
			top:    tBounds.Get("y").Int(),
			bottom: tBounds.Get("y").Int() + tBounds.Get("height").Int(),
			width:  tBounds.Get("width").Int(),
			height: tBounds.Get("height").Int(),
			left:   tBounds.Get("x").Int(),
			right:  tBounds.Get("x").Int() + tBounds.Get("width").Int(),
		}
	}

	// Step 3 - Determine Position of Taskbar
	if wa.top > d.top {
		return TOP
	} else if wa.bottom < d.bottom {
		return BOTTOM
	} else if wa.left > d.left {
		return LEFT
	} else if wa.right < d.right {
		return RIGHT
	}
	if tb == nil {
		return UNKNOWN
	}

	// Check which corner tray is closest to
	if ((*tb).top - d.top) < (d.bottom - (*tb).bottom) {
		return TOP
	}
	if ((*tb).left - d.left) < (d.right - (*tb).right) {
		return LEFT
	}
	if d.bottom-(*tb).centerY() < d.right-(*tb).centerX() {
		return BOTTOM
	}
	return RIGHT
}

相关问题