Skip to content

Commit

Permalink
SwiftWin32: extract the ScaleClient(rect:for:_)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
compnerd committed Sep 24, 2023
1 parent 387cea8 commit 75e52cc
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 20 deletions.
18 changes: 16 additions & 2 deletions Sources/SwiftWin32/CG/Rect.swift
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// Copyright © 2019 Saleem Abdulrasool <compnerd@compnerd.org>
// SPDX-License-Identifier: BSD-3-Clause

@_implementationOnly import func CRT.floor
@_implementationOnly import func CRT.ceil
import CRT
import WinSDK

@inline(__always)
private func __equals(_ lhs: Rect, _ rhs: Rect) -> Bool {
Expand Down Expand Up @@ -275,3 +275,17 @@ extension Rect: CustomDebugStringConvertible {
return "Rect(origin: \(origin), size: \(size))"
}
}

extension Rect {
internal func scaled(for dpi: UINT, style: WindowStyle) -> Rect {
let scale: Double = Double(dpi) / Double(USER_DEFAULT_SCREEN_DPI)

var r: RECT =
RECT(from: self.applying(AffineTransform(scaleX: scale, y: scale)))
if !AdjustWindowRectExForDpi(&r, style.base, false, style.extended, dpi) {
log.warning("AdjustWindowRectExForDpi: \(Error(win32: GetLastError()))")

Check warning on line 286 in Sources/SwiftWin32/CG/Rect.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SwiftWin32/CG/Rect.swift#L286

Added line #L286 was not covered by tests
}

return Rect(from: r)
}
}
25 changes: 7 additions & 18 deletions Sources/SwiftWin32/Views and Controls/View.swift
Original file line number Diff line number Diff line change
Expand Up @@ -60,17 +60,6 @@ private func ClientToWindow(size: inout Size, for style: WindowStyle) {
size = Size(width: Double(r.right - r.left), height: Double(r.bottom - r.top))
}

private func ScaleClient(rect: inout Rect, for dpi: UINT, _ style: WindowStyle) {
let scale: Double = Double(dpi) / Double(USER_DEFAULT_SCREEN_DPI)

var r: RECT =
RECT(from: rect.applying(AffineTransform(scaleX: scale, y: scale)))
if !AdjustWindowRectExForDpi(&r, style.base, false, style.extended, dpi) {
log.warning("AdjustWindowRectExForDpi: \(Error(win32: GetLastError()))")
}
rect = Rect(from: r)
}

private func WindowBasedTransform(for view: View?) -> AffineTransform {
guard var view = view else { return .identity }

Expand Down Expand Up @@ -479,7 +468,7 @@ public class View: Responder {
}

// Scale window for DPI
ScaleClient(rect: &client, for: GetDpiForWindow(self.hWnd), style)
client = client.scaled(for: GetDpiForWindow(self.hWnd), style: style)

// Resize and Position the Window
SetWindowPos(self.hWnd, nil,
Expand Down Expand Up @@ -543,10 +532,10 @@ public class View: Responder {
public var frame: Rect {
didSet {
// Scale window for DPI
var client: Rect = self.frame
ScaleClient(rect: &client, for: GetDpiForWindow(self.hWnd),
WindowStyle(DWORD(bitPattern: self.GWL_STYLE),
DWORD(bitPattern: self.GWL_EXSTYLE)))
let client: Rect =
self.frame.scaled(for: GetDpiForWindow(self.hWnd),
style: WindowStyle(DWORD(bitPattern: self.GWL_STYLE),
DWORD(bitPattern: self.GWL_EXSTYLE)))

Check warning on line 538 in Sources/SwiftWin32/Views and Controls/View.swift

View check run for this annotation

Codecov / codecov/patch

Sources/SwiftWin32/Views and Controls/View.swift#L535-L538

Added lines #L535 - L538 were not covered by tests

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

var client: Rect = view.frame
ScaleClient(rect: &client, for: GetDpiForWindow(view.hWnd), style)
let client: Rect =
view.frame.scaled(for: GetDpiForWindow(view.hWnd), style: style)

// Resize and Position the Window
_ = SetWindowPos(view.hWnd, nil,
Expand Down

0 comments on commit 75e52cc

Please sign in to comment.