Skip to content

Commit 205ab85

Browse files
committed
SwiftWin32: extract the ScaleClient(rect:for:_)
This refactors the DPI scaling for the rectangle to a function that we can use more broadly internally. The broader availability prepares us for fixing higher DPI displays.
1 parent 387cea8 commit 205ab85

File tree

2 files changed

+23
-20
lines changed

2 files changed

+23
-20
lines changed

Sources/SwiftWin32/CG/Rect.swift

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
// Copyright © 2019 Saleem Abdulrasool <[email protected]>
22
// SPDX-License-Identifier: BSD-3-Clause
33

4-
@_implementationOnly import func CRT.floor
5-
@_implementationOnly import func CRT.ceil
4+
import CRT
5+
import WinSDK
66

77
@inline(__always)
88
private func __equals(_ lhs: Rect, _ rhs: Rect) -> Bool {
@@ -275,3 +275,17 @@ extension Rect: CustomDebugStringConvertible {
275275
return "Rect(origin: \(origin), size: \(size))"
276276
}
277277
}
278+
279+
extension Rect {
280+
internal func scaled(for dpi: UINT, style: WindowStyle) -> Rect {
281+
let scale: Double = Double(dpi) / Double(USER_DEFAULT_SCREEN_DPI)
282+
283+
var r: RECT =
284+
RECT(from: self.applying(AffineTransform(scaleX: scale, y: scale)))
285+
if !AdjustWindowRectExForDpi(&r, style.base, false, style.extended, dpi) {
286+
log.warning("AdjustWindowRectExForDpi: \(Error(win32: GetLastError()))")
287+
}
288+
289+
return Rect(from: r)
290+
}
291+
}

Sources/SwiftWin32/Views and Controls/View.swift

Lines changed: 7 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -60,17 +60,6 @@ private func ClientToWindow(size: inout Size, for style: WindowStyle) {
6060
size = Size(width: Double(r.right - r.left), height: Double(r.bottom - r.top))
6161
}
6262

63-
private func ScaleClient(rect: inout Rect, for dpi: UINT, _ style: WindowStyle) {
64-
let scale: Double = Double(dpi) / Double(USER_DEFAULT_SCREEN_DPI)
65-
66-
var r: RECT =
67-
RECT(from: rect.applying(AffineTransform(scaleX: scale, y: scale)))
68-
if !AdjustWindowRectExForDpi(&r, style.base, false, style.extended, dpi) {
69-
log.warning("AdjustWindowRectExForDpi: \(Error(win32: GetLastError()))")
70-
}
71-
rect = Rect(from: r)
72-
}
73-
7463
private func WindowBasedTransform(for view: View?) -> AffineTransform {
7564
guard var view = view else { return .identity }
7665

@@ -479,7 +468,7 @@ public class View: Responder {
479468
}
480469

481470
// Scale window for DPI
482-
ScaleClient(rect: &client, for: GetDpiForWindow(self.hWnd), style)
471+
client = client.scaled(for: GetDpiForWindow(self.hWnd), style: style)
483472

484473
// Resize and Position the Window
485474
SetWindowPos(self.hWnd, nil,
@@ -543,10 +532,10 @@ public class View: Responder {
543532
public var frame: Rect {
544533
didSet {
545534
// Scale window for DPI
546-
var client: Rect = self.frame
547-
ScaleClient(rect: &client, for: GetDpiForWindow(self.hWnd),
548-
WindowStyle(DWORD(bitPattern: self.GWL_STYLE),
549-
DWORD(bitPattern: self.GWL_EXSTYLE)))
535+
let client: Rect =
536+
self.frame.scaled(for: GetDpiForWindow(self.hWnd),
537+
style: WindowStyle(DWORD(bitPattern: self.GWL_STYLE),
538+
DWORD(bitPattern: self.GWL_EXSTYLE)))
550539

551540
// Resize and Position the Window
552541
_ = SetWindowPos(self.hWnd, nil,
@@ -703,8 +692,8 @@ public class View: Responder {
703692
WindowStyle(DWORD(bitPattern: view.GWL_STYLE),
704693
DWORD(bitPattern: view.GWL_EXSTYLE))
705694

706-
var client: Rect = view.frame
707-
ScaleClient(rect: &client, for: GetDpiForWindow(view.hWnd), style)
695+
let client: Rect =
696+
view.frame.scaled(for: GetDpiForWindow(view.hWnd), style: style)
708697

709698
// Resize and Position the Window
710699
_ = SetWindowPos(view.hWnd, nil,

0 commit comments

Comments
 (0)