forked from BasedHardware/omi
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathResizeHandleView.swift
More file actions
82 lines (68 loc) · 2.58 KB
/
Copy pathResizeHandleView.swift
File metadata and controls
82 lines (68 loc) · 2.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import AppKit
import SwiftUI
// MARK: - Resize Handle NSViewRepresentable
/// NSViewRepresentable that adds a bottom-right corner resize handle.
/// Always active — not gated by the draggableBarEnabled toggle.
struct ResizeHandleView: NSViewRepresentable {
weak var targetWindow: NSWindow?
func makeNSView(context: Context) -> ResizeHandleNSView {
let view = ResizeHandleNSView()
view.targetWindow = targetWindow
return view
}
func updateNSView(_ nsView: ResizeHandleNSView, context: Context) {
nsView.targetWindow = targetWindow
}
}
class ResizeHandleNSView: NSView {
weak var targetWindow: NSWindow?
private var initialMouseLocation: NSPoint = .zero
private var initialWindowFrame: NSRect = .zero
override func resetCursorRects() {
addCursorRect(bounds, cursor: .crosshair)
}
override func mouseDown(with event: NSEvent) {
initialMouseLocation = NSEvent.mouseLocation
initialWindowFrame = targetWindow?.frame ?? .zero
(targetWindow as? FloatingControlBarWindow)?.isUserResizing = true
}
override func mouseUp(with event: NSEvent) {
(targetWindow as? FloatingControlBarWindow)?.finishUserResponseResize()
}
override func mouseDragged(with event: NSEvent) {
guard let window = targetWindow else { return }
let current = NSEvent.mouseLocation
let deltaX = current.x - initialMouseLocation.x
let deltaY = current.y - initialMouseLocation.y
let minW: CGFloat = 430
let minH: CGFloat = 250
let newWidth = max(minW, initialWindowFrame.width + deltaX * 2)
let newHeight = max(minH, initialWindowFrame.height - deltaY) // screen-y up = drag down = height grows
let newOriginX = initialWindowFrame.midX - newWidth / 2
let newOriginY = initialWindowFrame.maxY - newHeight
window.setFrame(
NSRect(x: newOriginX, y: newOriginY, width: newWidth, height: newHeight),
display: true
)
}
override var acceptsFirstResponder: Bool { false }
override func acceptsFirstMouse(for event: NSEvent?) -> Bool { true }
}
// MARK: - Resize Grip Visual
/// Three staggered diagonal dots — standard macOS resize-grip indicator.
struct ResizeGripShape: View {
var body: some View {
Canvas { context, size in
let r: CGFloat = 1.5
let positions: [(CGFloat, CGFloat)] = [
(size.width - r * 2, r * 2),
(size.width - r * 2 - 4, r * 2 + 4),
(size.width - r * 2 - 8, r * 2 + 8),
]
for (cx, cy) in positions {
let rect = CGRect(x: cx - r, y: cy - r, width: r * 2, height: r * 2)
context.fill(Path(ellipseIn: rect), with: .foreground)
}
}
}
}