Skip to content

Commit 0ec9379

Browse files
committed
DPI: Merge SizePixels to Size
Signed-off-by: Simon Rozman <simon@rozman.si>
1 parent c2762a2 commit 0ec9379

43 files changed

Lines changed: 310 additions & 318 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

bitmap.go

Lines changed: 21 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ const inchesPerMeter = 39.37007874
2222
type Bitmap struct {
2323
hBmp win.HBITMAP
2424
hPackedDIB win.HGLOBAL
25-
size SizePixels
25+
size Size // in native pixels
2626
dpi int
2727
}
2828

@@ -39,33 +39,34 @@ func BitmapFrom(src interface{}, dpi int) (*Bitmap, error) {
3939
return iconCache.Bitmap(img, dpi)
4040
}
4141

42-
// NewBitmap creates an opaque bitmap with given size at screen DPI.
42+
// NewBitmap creates an opaque bitmap with given size in 1/96" units at screen DPI.
4343
//
4444
// Deprecated: Newer applications should use NewBitmapForDPI.
4545
func NewBitmap(size Size) (*Bitmap, error) {
4646
dpi := screenDPI()
4747
return newBitmap(SizeFrom96DPI(size, dpi), false, dpi)
4848
}
4949

50-
// NewBitmapForDPI creates an opaque bitmap with given size and DPI.
51-
func NewBitmapForDPI(size SizePixels, dpi int) (*Bitmap, error) {
50+
// NewBitmapForDPI creates an opaque bitmap with given size in native pixels and DPI.
51+
func NewBitmapForDPI(size Size, dpi int) (*Bitmap, error) {
5252
return newBitmap(size, false, dpi)
5353
}
5454

55-
// NewBitmapWithTransparentPixels creates a transparent bitmap with given size at screen DPI.
55+
// NewBitmapWithTransparentPixels creates a transparent bitmap with given size in 1/96" units at screen DPI.
5656
//
5757
// Deprecated: Newer applications should use NewBitmapWithTransparentPixelsForDPI.
5858
func NewBitmapWithTransparentPixels(size Size) (*Bitmap, error) {
5959
dpi := screenDPI()
6060
return newBitmap(SizeFrom96DPI(size, dpi), true, dpi)
6161
}
6262

63-
// NewBitmapWithTransparentPixelsForDPI creates a transparent bitmap with given size and DPI.
64-
func NewBitmapWithTransparentPixelsForDPI(size SizePixels, dpi int) (*Bitmap, error) {
63+
// NewBitmapWithTransparentPixelsForDPI creates a transparent bitmap with given size in native pixels and DPI.
64+
func NewBitmapWithTransparentPixelsForDPI(size Size, dpi int) (*Bitmap, error) {
6565
return newBitmap(size, true, dpi)
6666
}
6767

68-
func newBitmap(size SizePixels, transparent bool, dpi int) (bmp *Bitmap, err error) {
68+
// newBitmap creates a bitmap with given size in native pixels and DPI.
69+
func newBitmap(size Size, transparent bool, dpi int) (bmp *Bitmap, err error) {
6970
err = withCompatibleDC(func(hdc win.HDC) error {
7071
bufSize := int(size.Width * size.Height * 4)
7172

@@ -194,7 +195,8 @@ func newBitmapFromResource(res *uint16, dpi int) (bm *Bitmap, err error) {
194195
return
195196
}
196197

197-
func NewBitmapFromImageWithSize(image Image, size SizePixels) (*Bitmap, error) {
198+
// NewBitmapFromImageWithSize creates a bitmap with given size in native units and paints the image on it streched.
199+
func NewBitmapFromImageWithSize(image Image, size Size) (*Bitmap, error) {
198200
var disposables Disposables
199201
defer disposables.Treat()
200202

@@ -231,15 +233,17 @@ func NewBitmapFromWindow(window Window) (*Bitmap, error) {
231233
return newBitmapFromHBITMAP(hBmp, window.DPI())
232234
}
233235

234-
// NewBitmapFromIcon creates a new bitmap with given size and 96dpi and paints the icon on it.
236+
// NewBitmapFromIcon creates a new bitmap with given size in native pixels and 96dpi and paints the
237+
// icon on it.
235238
//
236239
// Deprecated: Newer applications should use NewBitmapFromIconForDPI.
237-
func NewBitmapFromIcon(icon *Icon, size SizePixels) (*Bitmap, error) {
240+
func NewBitmapFromIcon(icon *Icon, size Size) (*Bitmap, error) {
238241
return NewBitmapFromIconForDPI(icon, size, 96)
239242
}
240243

241-
// NewBitmapFromIconForDPI creates a new bitmap with given size and DPI and paints the icon on it.
242-
func NewBitmapFromIconForDPI(icon *Icon, size SizePixels, dpi int) (*Bitmap, error) {
244+
// NewBitmapFromIconForDPI creates a new bitmap with given size in native pixels and DPI and paints
245+
// the icon on it.
246+
func NewBitmapFromIconForDPI(icon *Icon, size Size, dpi int) (*Bitmap, error) {
243247
hBmp, err := hBitmapFromIcon(icon, size, dpi)
244248
if err != nil {
245249
return nil, err
@@ -420,7 +424,7 @@ func newBitmapFromHBITMAP(hBmp win.HBITMAP, dpi int) (bmp *Bitmap, err error) {
420424
return &Bitmap{
421425
hBmp: hBmp,
422426
hPackedDIB: hPackedDIB,
423-
size: SizePixels{
427+
size: Size{
424428
int(bmih.BiWidth),
425429
int(bmih.BiHeight),
426430
},
@@ -501,7 +505,9 @@ func hBitmapFromWindow(window Window) (win.HBITMAP, error) {
501505
return hBmp, nil
502506
}
503507

504-
func hBitmapFromIcon(icon *Icon, size SizePixels, dpi int) (win.HBITMAP, error) {
508+
// hBitmapFromIcon creates a new win.HBITMAP with given size in native pixels and DPI, and paints
509+
// the icon on it stretched.
510+
func hBitmapFromIcon(icon *Icon, size Size, dpi int) (win.HBITMAP, error) {
505511
hdc := win.GetDC(0)
506512
defer win.ReleaseDC(0, hdc)
507513

boxlayout.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ func (l *BoxLayout) SetStretchFactor(widget Widget, factor int) error {
102102

103103
func (l *BoxLayout) CreateLayoutItem(ctx *LayoutContext) ContainerLayoutItem {
104104
li := &boxLayoutItem{
105-
size2MinSize: make(map[SizePixels]SizePixels),
105+
size2MinSize: make(map[Size]Size),
106106
orientation: l.orientation,
107107
hwnd2StretchFactor: make(map[win.HWND]int),
108108
}
@@ -157,7 +157,7 @@ func (l boxLayoutItemInfoList) Swap(i, j int) {
157157
type boxLayoutItem struct {
158158
ContainerLayoutItemBase
159159
mutex sync.Mutex
160-
size2MinSize map[SizePixels]SizePixels
160+
size2MinSize map[Size]Size // in native pixels
161161
orientation Orientation
162162
hwnd2StretchFactor map[win.HWND]int
163163
}
@@ -166,19 +166,19 @@ func (li *boxLayoutItem) LayoutFlags() LayoutFlags {
166166
return boxLayoutFlags(li.orientation, li.children)
167167
}
168168

169-
func (li *boxLayoutItem) IdealSize() SizePixels {
169+
func (li *boxLayoutItem) IdealSize() Size {
170170
return li.MinSize()
171171
}
172172

173-
func (li *boxLayoutItem) MinSize() SizePixels {
173+
func (li *boxLayoutItem) MinSize() Size {
174174
return li.MinSizeForSize(li.geometry.ClientSize)
175175
}
176176

177177
func (li *boxLayoutItem) HeightForWidth(width int) int {
178-
return li.MinSizeForSize(SizePixels{width, li.geometry.ClientSize.Height}).Height
178+
return li.MinSizeForSize(Size{width, li.geometry.ClientSize.Height}).Height
179179
}
180180

181-
func (li *boxLayoutItem) MinSizeForSize(size SizePixels) SizePixels {
181+
func (li *boxLayoutItem) MinSizeForSize(size Size) Size {
182182
li.mutex.Lock()
183183
defer li.mutex.Unlock()
184184

@@ -192,7 +192,7 @@ func (li *boxLayoutItem) MinSizeForSize(size SizePixels) SizePixels {
192192

193193
margins := MarginsFrom96DPI(li.margins96dpi, li.ctx.dpi)
194194
spacing := IntFrom96DPI(li.spacing96dpi, li.ctx.dpi)
195-
s := SizePixels{margins.HNear + margins.HFar, margins.VNear + margins.VFar}
195+
s := Size{margins.HNear + margins.HFar, margins.VNear + margins.VFar}
196196

197197
var maxSecondary int
198198
for _, item := range items {
@@ -318,7 +318,7 @@ func boxLayoutItems(container ContainerLayoutItem, items []LayoutItem, orientati
318318
flags := item.LayoutFlags()
319319

320320
max := geometry.MaxSize
321-
var pref SizePixels
321+
var pref Size
322322
if hfw, ok := item.(HeightForWidther); !ok || !hfw.HasHeightForWidth() {
323323
if is, ok := item.(IdealSizer); ok {
324324
pref = is.IdealSize()

brush.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -382,9 +382,9 @@ func NewGradientBrush(vertexes []GradientVertex, triangles []GradientTriangle) (
382382
}
383383

384384
func newGradientBrush(vertexes []GradientVertex, triangles []GradientTriangle, orientation gradientOrientation) (*GradientBrush, error) {
385-
var size SizePixels
385+
var size Size
386386
for _, v := range vertexes {
387-
size = maxSizePixels(size, SizePixels{int(v.X), int(v.Y)})
387+
size = maxSize(size, Size{int(v.X), int(v.Y)})
388388
}
389389

390390
gb := &GradientBrush{vertexes: vertexes, triangles: triangles, orientation: orientation, absolute: size.Width > 1 || size.Height > 1}
@@ -414,7 +414,8 @@ func (*GradientBrush) simple() bool {
414414
return false
415415
}
416416

417-
func (b *GradientBrush) create(size SizePixels) (*BitmapBrush, error) {
417+
// create creates a gradient brush at given size in native pixels.
418+
func (b *GradientBrush) create(size Size) (*BitmapBrush, error) {
418419
var disposables Disposables
419420
defer disposables.Treat()
420421

button.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -218,12 +218,13 @@ func (b *Button) WndProc(hwnd win.HWND, msg uint32, wParam, lParam uintptr) uint
218218
return b.WidgetBase.WndProc(hwnd, msg, wParam, lParam)
219219
}
220220

221-
func (b *Button) idealSize() SizePixels {
221+
// idealSize returns ideal button size in native pixels.
222+
func (b *Button) idealSize() Size {
222223
var s win.SIZE
223224

224225
b.SendMessage(win.BCM_GETIDEALSIZE, 0, uintptr(unsafe.Pointer(&s)))
225226

226-
return maxSizePixels(sizeFromSIZE(s), b.dialogBaseUnitsToPixels(SizeDBU{50, 14}))
227+
return maxSize(sizeFromSIZE(s), b.dialogBaseUnitsToPixels(SizeDBU{50, 14}))
227228
}
228229

229230
func (b *Button) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
@@ -234,17 +235,17 @@ func (b *Button) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
234235

235236
type buttonLayoutItem struct {
236237
LayoutItemBase
237-
idealSize SizePixels
238+
idealSize Size // in native pixels
238239
}
239240

240241
func (li *buttonLayoutItem) LayoutFlags() LayoutFlags {
241242
return 0
242243
}
243244

244-
func (li *buttonLayoutItem) IdealSize() SizePixels {
245+
func (li *buttonLayoutItem) IdealSize() Size {
245246
return li.MinSize()
246247
}
247248

248-
func (li *buttonLayoutItem) MinSize() SizePixels {
249+
func (li *buttonLayoutItem) MinSize() Size {
249250
return li.idealSize
250251
}

canvas.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ func (c *Canvas) roundedRectangle(brush Brush, pen Pen, bounds Rectangle, ellips
446446
}
447447

448448
// roundedRectanglePixels draws a rounded rectangle in native pixels.
449-
func (c *Canvas) roundedRectanglePixels(brush Brush, pen Pen, bounds RectanglePixels, ellipseSize SizePixels, sizeCorrection int) error {
449+
func (c *Canvas) roundedRectanglePixels(brush Brush, pen Pen, bounds RectanglePixels, ellipseSize Size, sizeCorrection int) error {
450450
return c.withBrushAndPen(brush, pen, func() error {
451451
if !win.RoundRect(
452452
c.hdc,
@@ -464,19 +464,30 @@ func (c *Canvas) roundedRectanglePixels(brush Brush, pen Pen, bounds RectanglePi
464464
})
465465
}
466466

467+
// DrawRoundedRectangle draws a rounded rectangle in 1/96" units. sizeCorrection parameter is in native
468+
// pixels.
469+
//
470+
// Deprecated: Newer applications should use DrawRoundedRectanglePixels.
467471
func (c *Canvas) DrawRoundedRectangle(pen Pen, bounds Rectangle, ellipseSize Size) error {
468472
return c.roundedRectangle(nullBrushSingleton, pen, bounds, ellipseSize, 0)
469473
}
470474

471-
func (c *Canvas) DrawRoundedRectanglePixels(pen Pen, bounds RectanglePixels, ellipseSize SizePixels) error {
475+
// DrawRoundedRectanglePixels draws a rounded rectangle in native pixels.
476+
func (c *Canvas) DrawRoundedRectanglePixels(pen Pen, bounds RectanglePixels, ellipseSize Size) error {
472477
return c.roundedRectanglePixels(nullBrushSingleton, pen, bounds, ellipseSize, 0)
473478
}
474479

480+
// FillRoundedRectangle draws a filled rounded rectangle in 1/96" units. sizeCorrection parameter
481+
// is in native
482+
// pixels.
483+
//
484+
// Deprecated: Newer applications should use FillRoundedRectanglePixels.
475485
func (c *Canvas) FillRoundedRectangle(brush Brush, bounds Rectangle, ellipseSize Size) error {
476486
return c.roundedRectangle(brush, nullPenSingleton, bounds, ellipseSize, 1)
477487
}
478488

479-
func (c *Canvas) FillRoundedRectanglePixels(brush Brush, bounds RectanglePixels, ellipseSize SizePixels) error {
489+
// FillRoundedRectanglePixels draws a filled rounded rectangle in native pixels.
490+
func (c *Canvas) FillRoundedRectanglePixels(brush Brush, bounds RectanglePixels, ellipseSize Size) error {
480491
return c.roundedRectanglePixels(brush, nullPenSingleton, bounds, ellipseSize, 1)
481492
}
482493

checkbox.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ const (
2020
CheckIndeterminate CheckState = win.BST_INDETERMINATE
2121
)
2222

23-
var checkBoxCheckSize SizePixels
23+
var checkBoxCheckSize Size // in native pixels
2424

2525
type CheckBox struct {
2626
Button

combobox.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -713,24 +713,24 @@ func (cb *ComboBox) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
713713

714714
return &comboBoxLayoutItem{
715715
layoutFlags: layoutFlags,
716-
idealSize: SizePixels{w, h},
716+
idealSize: Size{w, h},
717717
}
718718
}
719719

720720
type comboBoxLayoutItem struct {
721721
LayoutItemBase
722722
layoutFlags LayoutFlags
723-
idealSize SizePixels
723+
idealSize Size // in native pixels
724724
}
725725

726726
func (li *comboBoxLayoutItem) LayoutFlags() LayoutFlags {
727727
return li.layoutFlags
728728
}
729729

730-
func (li *comboBoxLayoutItem) IdealSize() SizePixels {
730+
func (li *comboBoxLayoutItem) IdealSize() Size {
731731
return li.idealSize
732732
}
733733

734-
func (li *comboBoxLayoutItem) MinSize() SizePixels {
734+
func (li *comboBoxLayoutItem) MinSize() Size {
735735
return li.idealSize
736736
}

dateedit.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -256,17 +256,17 @@ func (de *DateEdit) CreateLayoutItem(ctx *LayoutContext) LayoutItem {
256256

257257
type dateEditLayoutItem struct {
258258
LayoutItemBase
259-
idealSize SizePixels
259+
idealSize Size // in native pixels
260260
}
261261

262262
func (*dateEditLayoutItem) LayoutFlags() LayoutFlags {
263263
return GrowableHorz
264264
}
265265

266-
func (li *dateEditLayoutItem) IdealSize() SizePixels {
266+
func (li *dateEditLayoutItem) IdealSize() Size {
267267
return li.idealSize
268268
}
269269

270-
func (li *dateEditLayoutItem) MinSize() SizePixels {
270+
func (li *dateEditLayoutItem) MinSize() Size {
271271
return li.idealSize
272272
}

dialog.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -163,9 +163,9 @@ func (dlg *Dialog) Show() {
163163
}
164164

165165
if !willRestore {
166-
var size SizePixels
166+
var size Size
167167
if layout := dlg.Layout(); layout != nil {
168-
size = maxSizePixels(dlg.clientComposite.MinSizeHint(), dlg.MinSizePixels())
168+
size = maxSize(dlg.clientComposite.MinSizeHint(), dlg.MinSizePixels())
169169
} else {
170170
size = dlg.SizePixels()
171171
}

examples/externalwidgets/externalwidgets.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ func (li *myWidgetLayoutItem) LayoutFlags() walk.LayoutFlags {
8989
return 0
9090
}
9191

92-
func (li *myWidgetLayoutItem) IdealSize() walk.SizePixels {
92+
func (li *myWidgetLayoutItem) IdealSize() walk.Size {
9393
return walk.SizeFrom96DPI(li.idealSize, li.Context().DPI())
9494
}
9595

0 commit comments

Comments
 (0)