Reputation: 290
I made a custom AppDelegate which should remove the window toolbar and add a corner radius of 10px which all works very well hoeever now, you can't drag the window around.
I am also very new to AppKit and struggled to put this thing together with great help of Ai however I can't get it to work. Thanks in advance,
Here is my code:
import SwiftUI
@main
struct WindowTestApp: App {
@NSApplicationDelegateAdaptor(AppDelegate.self) var appDelegate
var body: some Scene {
WindowGroup {
ContentView()
}
.windowResizability(.contentSize)
}
}
class AppDelegate: NSObject, NSApplicationDelegate {
var window: NSWindow?
func applicationDidFinishLaunching(_ notification: Notification) {
window = NSApplication.shared.windows.first
if let window = window {
// Remove title bar and make window borderless
window.styleMask = [.borderless, .resizable, .closable, .miniaturizable, .fullSizeContentView]
window.isOpaque = false
window.backgroundColor = NSColor.clear
window.titleVisibility = .hidden
window.titlebarAppearsTransparent = true
// Set window corner radius
window.contentView?.wantsLayer = true
window.contentView?.layer?.cornerRadius = 10
window.contentView?.layer?.masksToBounds = true
// Add custom draggable view
let draggableView = DraggableView(frame: window.contentView?.bounds ?? .zero)
draggableView.autoresizingMask = [.width, .height]
window.contentView?.addSubview(draggableView)
}
}
}
class DraggableView: NSView {
private var initialLocation: NSPoint = .zero
override func mouseDown(with event: NSEvent) {
// Get the initial mouse location
initialLocation = event.locationInWindow
}
override func mouseDragged(with event: NSEvent) {
guard let window = self.window else { return }
// Get the current mouse location
let currentLocation = event.locationInWindow
// Calculate the new origin for the window
let deltaX = currentLocation.x - initialLocation.x
let deltaY = currentLocation.y - initialLocation.y
var newOrigin = window.frame.origin
newOrigin.x += deltaX
newOrigin.y += deltaY
// Update the window's origin
window.setFrameOrigin(newOrigin)
}
}
Upvotes: 0
Views: 68