1 Star 0 Fork 0

youxiuliang/walk

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
boxlayout.go 13.33 KB
一键复制 编辑 原始数据 按行查看 历史
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559
// Copyright 2010 The Walk Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.
// +build windows
package walk
import (
"math"
"sort"
"sync"
"github.com/lxn/win"
)
type Orientation byte
const (
NoOrientation Orientation = 0
Horizontal = 1 << 0
Vertical = 1 << 1
)
type BoxLayout struct {
LayoutBase
orientation Orientation
hwnd2StretchFactor map[win.HWND]int
}
func newBoxLayout(orientation Orientation) *BoxLayout {
l := &BoxLayout{
LayoutBase: LayoutBase{
margins96dpi: Margins{9, 9, 9, 9},
spacing96dpi: 6,
},
orientation: orientation,
hwnd2StretchFactor: make(map[win.HWND]int),
}
l.layout = l
return l
}
func NewHBoxLayout() *BoxLayout {
return newBoxLayout(Horizontal)
}
func NewVBoxLayout() *BoxLayout {
return newBoxLayout(Vertical)
}
func (l *BoxLayout) Orientation() Orientation {
return l.orientation
}
func (l *BoxLayout) SetOrientation(value Orientation) error {
if value != l.orientation {
switch value {
case Horizontal, Vertical:
default:
return newError("invalid Orientation value")
}
l.orientation = value
l.container.RequestLayout()
}
return nil
}
func (l *BoxLayout) StretchFactor(widget Widget) int {
if factor, ok := l.hwnd2StretchFactor[widget.Handle()]; ok {
return factor
}
return 1
}
func (l *BoxLayout) SetStretchFactor(widget Widget, factor int) error {
if factor != l.StretchFactor(widget) {
if l.container == nil {
return newError("container required")
}
handle := widget.Handle()
if !l.container.Children().containsHandle(handle) {
return newError("unknown widget")
}
if factor < 1 {
return newError("factor must be >= 1")
}
l.hwnd2StretchFactor[handle] = factor
l.container.RequestLayout()
}
return nil
}
func (l *BoxLayout) CreateLayoutItem(ctx *LayoutContext) ContainerLayoutItem {
li := &boxLayoutItem{
size2MinSize: make(map[Size]Size),
orientation: l.orientation,
hwnd2StretchFactor: make(map[win.HWND]int),
}
for hwnd, sf := range l.hwnd2StretchFactor {
li.hwnd2StretchFactor[hwnd] = sf
}
return li
}
type boxLayoutItemInfo struct {
item LayoutItem
index int
prefSize int // in native pixels
minSize int // in native pixels
maxSize int // in native pixels
stretch int
greedy bool
}
type boxLayoutItemInfoList []boxLayoutItemInfo
func (l boxLayoutItemInfoList) Len() int {
return len(l)
}
func (l boxLayoutItemInfoList) Less(i, j int) bool {
_, iIsSpacer := l[i].item.(*spacerLayoutItem)
_, jIsSpacer := l[j].item.(*spacerLayoutItem)
if l[i].greedy == l[j].greedy {
if iIsSpacer == jIsSpacer {
minDiff := l[i].minSize - l[j].minSize
if minDiff == 0 {
return l[i].maxSize/l[i].stretch < l[j].maxSize/l[j].stretch
}
return minDiff > 0
}
return jIsSpacer
}
return l[i].greedy
}
func (l boxLayoutItemInfoList) Swap(i, j int) {
l[i], l[j] = l[j], l[i]
}
type boxLayoutItem struct {
ContainerLayoutItemBase
mutex sync.Mutex
size2MinSize map[Size]Size // in native pixels
orientation Orientation
hwnd2StretchFactor map[win.HWND]int
}
func (li *boxLayoutItem) LayoutFlags() LayoutFlags {
return boxLayoutFlags(li.orientation, li.children)
}
func (li *boxLayoutItem) IdealSize() Size {
return li.MinSize()
}
func (li *boxLayoutItem) MinSize() Size {
return li.MinSizeForSize(li.geometry.ClientSize)
}
func (li *boxLayoutItem) HeightForWidth(width int) int {
return li.MinSizeForSize(Size{width, li.geometry.ClientSize.Height}).Height
}
func (li *boxLayoutItem) MinSizeForSize(size Size) Size {
li.mutex.Lock()
defer li.mutex.Unlock()
if min, ok := li.size2MinSize[size]; ok {
return min
}
bounds := Rectangle{Width: size.Width, Height: size.Height}
items := boxLayoutItems(li, itemsToLayout(li.children), li.orientation, li.alignment, bounds, li.margins96dpi, li.spacing96dpi, li.hwnd2StretchFactor)
margins := MarginsFrom96DPI(li.margins96dpi, li.ctx.dpi)
spacing := IntFrom96DPI(li.spacing96dpi, li.ctx.dpi)
s := Size{margins.HNear + margins.HFar, margins.VNear + margins.VFar}
var maxSecondary int
for _, item := range items {
min := li.MinSizeEffectiveForChild(item.Item)
if hfw, ok := item.Item.(HeightForWidther); ok && hfw.HasHeightForWidth() {
item.Bounds.Height = hfw.HeightForWidth(item.Bounds.Width)
} else {
item.Bounds.Height = min.Height
}
item.Bounds.Width = min.Width
if li.orientation == Horizontal {
maxSecondary = maxi(maxSecondary, item.Bounds.Height)
s.Width += item.Bounds.Width
} else {
maxSecondary = maxi(maxSecondary, item.Bounds.Width)
s.Height += item.Bounds.Height
}
}
if li.orientation == Horizontal {
s.Width += (len(items) - 1) * spacing
s.Height += maxSecondary
} else {
s.Height += (len(items) - 1) * spacing
s.Width += maxSecondary
}
if s.Width > 0 && s.Height > 0 {
li.size2MinSize[size] = s
}
return s
}
func (li *boxLayoutItem) PerformLayout() []LayoutResultItem {
cb := Rectangle{Width: li.geometry.ClientSize.Width, Height: li.geometry.ClientSize.Height}
return boxLayoutItems(li, itemsToLayout(li.children), li.orientation, li.alignment, cb, li.margins96dpi, li.spacing96dpi, li.hwnd2StretchFactor)
}
func boxLayoutFlags(orientation Orientation, children []LayoutItem) LayoutFlags {
if len(children) == 0 {
return ShrinkableHorz | ShrinkableVert | GrowableHorz | GrowableVert
}
var flags LayoutFlags
for i := 0; i < len(children); i++ {
item := children[i]
if _, ok := item.(*splitterHandleLayoutItem); ok || !shouldLayoutItem(item) {
continue
}
if s, ok := item.(*spacerLayoutItem); ok {
if s.greedyLocallyOnly {
continue
}
}
f := item.LayoutFlags()
flags |= f
}
return flags
}
// boxLayoutItems lays out items. bounds parameter is in native pixels.
func boxLayoutItems(container ContainerLayoutItem, items []LayoutItem, orientation Orientation, alignment Alignment2D, bounds Rectangle, margins96dpi Margins, spacing96dpi int, hwnd2StretchFactor map[win.HWND]int) []LayoutResultItem {
if len(items) == 0 {
return nil
}
dpi := container.Context().dpi
margins := MarginsFrom96DPI(margins96dpi, dpi)
spacing := IntFrom96DPI(spacing96dpi, dpi)
var greedyNonSpacerCount int
var greedySpacerCount int
var stretchFactorsTotal [3]int
stretchFactors := make([]int, len(items))
var minSizesRemaining int
minSizes := make([]int, len(items))
maxSizes := make([]int, len(items))
sizes := make([]int, len(items))
prefSizes2 := make([]int, len(items))
var shrinkableAmount1Total int
shrinkableAmount1 := make([]int, len(items))
shrinkable2 := make([]bool, len(items))
growable2 := make([]bool, len(items))
sortedItemInfo := boxLayoutItemInfoList(make([]boxLayoutItemInfo, len(items)))
for i, item := range items {
sf := hwnd2StretchFactor[item.Handle()]
if sf == 0 {
sf = 1
}
stretchFactors[i] = sf
geometry := item.Geometry()
flags := item.LayoutFlags()
max := geometry.MaxSize
var pref Size
if hfw, ok := item.(HeightForWidther); !ok || !hfw.HasHeightForWidth() {
if is, ok := item.(IdealSizer); ok {
pref = is.IdealSize()
}
}
if orientation == Horizontal {
growable2[i] = flags&GrowableVert > 0
minSizes[i] = container.MinSizeEffectiveForChild(item).Width
if max.Width > 0 {
maxSizes[i] = max.Width
} else if pref.Width > 0 && flags&GrowableHorz == 0 {
maxSizes[i] = pref.Width
} else {
maxSizes[i] = 32768
}
prefSizes2[i] = pref.Height
sortedItemInfo[i].prefSize = pref.Width
sortedItemInfo[i].greedy = flags&GreedyHorz > 0
} else {
growable2[i] = flags&GrowableHorz > 0
if hfw, ok := item.(HeightForWidther); ok && hfw.HasHeightForWidth() {
minSizes[i] = hfw.HeightForWidth(bounds.Width - margins.HNear - margins.HFar)
} else {
minSizes[i] = container.MinSizeEffectiveForChild(item).Height
}
if max.Height > 0 {
maxSizes[i] = max.Height
} else if hfw, ok := item.(HeightForWidther); ok && flags&GrowableVert == 0 && hfw.HasHeightForWidth() {
maxSizes[i] = minSizes[i]
} else if pref.Height > 0 && flags&GrowableVert == 0 {
maxSizes[i] = pref.Height
} else {
maxSizes[i] = 32768
}
prefSizes2[i] = pref.Width
sortedItemInfo[i].prefSize = pref.Height
sortedItemInfo[i].greedy = flags&GreedyVert > 0
}
sortedItemInfo[i].index = i
sortedItemInfo[i].minSize = minSizes[i]
sortedItemInfo[i].maxSize = maxSizes[i]
sortedItemInfo[i].stretch = sf
sortedItemInfo[i].item = item
if orientation == Horizontal && flags&(ShrinkableHorz|GrowableHorz|GreedyHorz) == ShrinkableHorz ||
orientation == Vertical && flags&(ShrinkableVert|GrowableVert|GreedyVert) == ShrinkableVert {
if amount := sortedItemInfo[i].prefSize - minSizes[i]; amount > 0 {
shrinkableAmount1[i] = amount
shrinkableAmount1Total += amount
}
}
shrinkable2[i] = orientation == Horizontal && flags&ShrinkableVert != 0 || orientation == Vertical && flags&ShrinkableHorz != 0
if shrinkableAmount1[i] > 0 {
minSizesRemaining += sortedItemInfo[i].prefSize
} else {
minSizesRemaining += minSizes[i]
}
if sortedItemInfo[i].greedy {
if _, isSpacer := item.(*spacerLayoutItem); !isSpacer {
greedyNonSpacerCount++
stretchFactorsTotal[0] += sf
} else {
greedySpacerCount++
stretchFactorsTotal[1] += sf
}
} else {
stretchFactorsTotal[2] += sf
}
}
sort.Stable(sortedItemInfo)
var start1, start2, space1, space2 int
if orientation == Horizontal {
start1 = bounds.X + margins.HNear
start2 = bounds.Y + margins.VNear
space1 = bounds.Width - margins.HNear - margins.HFar
space2 = bounds.Height - margins.VNear - margins.VFar
} else {
start1 = bounds.Y + margins.VNear
start2 = bounds.X + margins.HNear
space1 = bounds.Height - margins.VNear - margins.VFar
space2 = bounds.Width - margins.HNear - margins.HFar
}
spacingRemaining := spacing * (len(items) - 1)
excess := float64(space1 - minSizesRemaining - spacingRemaining)
offsets := [3]int{0, greedyNonSpacerCount, greedyNonSpacerCount + greedySpacerCount}
counts := [3]int{greedyNonSpacerCount, greedySpacerCount, len(items) - greedyNonSpacerCount - greedySpacerCount}
for i := 0; i < 3; i++ {
stretchFactorsRemaining := stretchFactorsTotal[i]
for j := 0; j < counts[i]; j++ {
info := sortedItemInfo[offsets[i]+j]
k := info.index
stretch := stretchFactors[k]
min := info.minSize
max := info.maxSize
var size int
var corrected bool
if shrinkableAmount1[k] > 0 {
size = info.prefSize
if excess < 0.0 {
size -= mini(shrinkableAmount1[k], int(math.Round(-excess/float64(shrinkableAmount1Total)*float64(shrinkableAmount1[k]))))
corrected = true
}
} else {
size = min
}
if !corrected && min < max {
excessSpace := float64(space1 - minSizesRemaining - spacingRemaining)
size += int(math.Round(excessSpace * float64(stretch) / float64(stretchFactorsRemaining)))
if size < min {
size = min
} else if size > max {
size = max
}
}
sizes[k] = size
if shrinkableAmount1[k] > 0 {
minSizesRemaining -= info.prefSize
} else {
minSizesRemaining -= min
}
stretchFactorsRemaining -= stretch
space1 -= (size + spacing)
spacingRemaining -= spacing
}
}
results := make([]LayoutResultItem, 0, len(items))
excessTotal := space1 - minSizesRemaining - spacingRemaining
excessShare := excessTotal / len(items)
halfExcessShare := excessTotal / (len(items) * 2)
p1 := start1
for i, item := range items {
s1 := sizes[i]
var s2 int
if hfw, ok := item.(HeightForWidther); ok && orientation == Horizontal && hfw.HasHeightForWidth() {
s2 = hfw.HeightForWidth(s1)
} else if shrinkable2[i] || growable2[i] {
s2 = space2
} else {
s2 = prefSizes2[i]
}
align := item.Geometry().Alignment
if align == AlignHVDefault {
align = alignment
}
var x, y, w, h, p2 int
if orientation == Horizontal {
switch align {
case AlignHNearVNear, AlignHNearVCenter, AlignHNearVFar:
// nop
case AlignHFarVNear, AlignHFarVCenter, AlignHFarVFar:
p1 += excessShare
default:
p1 += halfExcessShare
}
switch align {
case AlignHNearVNear, AlignHCenterVNear, AlignHFarVNear:
p2 = start2
case AlignHNearVFar, AlignHCenterVFar, AlignHFarVFar:
p2 = start2 + space2 - s2
default:
p2 = start2 + (space2-s2)/2
}
x, y, w, h = p1, p2, s1, s2
} else {
switch align {
case AlignHNearVNear, AlignHCenterVNear, AlignHFarVNear:
// nop
case AlignHNearVFar, AlignHCenterVFar, AlignHFarVFar:
p1 += excessShare
default:
p1 += halfExcessShare
}
switch align {
case AlignHNearVNear, AlignHNearVCenter, AlignHNearVFar:
p2 = start2
case AlignHFarVNear, AlignHFarVCenter, AlignHFarVFar:
p2 = start2 + space2 - s2
default:
p2 = start2 + (space2-s2)/2
}
x, y, w, h = p2, p1, s2, s1
}
if orientation == Horizontal {
switch align {
case AlignHNearVNear, AlignHNearVCenter, AlignHNearVFar:
p1 += excessShare
case AlignHFarVNear, AlignHFarVCenter, AlignHFarVFar:
// nop
default:
p1 += halfExcessShare
}
} else {
switch align {
case AlignHNearVNear, AlignHCenterVNear, AlignHFarVNear:
p1 += excessShare
case AlignHNearVFar, AlignHCenterVFar, AlignHFarVFar:
// nop
default:
p1 += halfExcessShare
}
}
p1 += s1 + spacing
results = append(results, LayoutResultItem{Item: item, Bounds: Rectangle{X: x, Y: y, Width: w, Height: h}})
}
return results
}
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/showyo/walk.git
git@gitee.com:showyo/walk.git
showyo
walk
walk
master

搜索帮助