Created
February 1, 2025 20:36
-
-
Save hbouhadji/2005c2f711f0e3077ba9cb1db72a208c to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Cocoa | |
class AppDelegate: NSObject, NSApplicationDelegate { | |
var window: NSWindow! | |
func applicationDidFinishLaunching(_ notification: Notification) { | |
// Définir l'icône de l'application | |
if let image = createApplicationIcon() { | |
NSApplication.shared.applicationIconImage = image | |
} | |
// Créer la fenêtre | |
let windowRect = NSRect(x: 100, y: 100, width: 400, height: 200) | |
window = NSWindow( | |
contentRect: windowRect, | |
styleMask: [.titled, .closable, .miniaturizable], | |
backing: .buffered, | |
defer: false | |
) | |
// Configurer la fenêtre | |
window.title = "Hello World" | |
window.center() | |
// Créer le label | |
let label = NSTextField(frame: NSRect(x: 0, y: 0, width: 200, height: 50)) | |
label.stringValue = "Hello World!" | |
label.isEditable = false | |
label.isBordered = false | |
label.backgroundColor = .clear | |
label.alignment = .center | |
label.translatesAutoresizingMaskIntoConstraints = false | |
// Ajouter le label à la fenêtre | |
window.contentView?.addSubview(label) | |
// Centrer le label dans la fenêtre | |
if let contentView = window.contentView { | |
NSLayoutConstraint.activate([ | |
label.centerXAnchor.constraint(equalTo: contentView.centerXAnchor), | |
label.centerYAnchor.constraint(equalTo: contentView.centerYAnchor) | |
]) | |
} | |
// Afficher la fenêtre et activer l'application | |
window.makeKeyAndOrderFront(nil) | |
NSApp.activate(ignoringOtherApps: true) | |
} | |
private func createApplicationIcon() -> NSImage? { | |
let size = NSSize(width: 128, height: 128) | |
let image = NSImage(size: size) | |
image.lockFocus() | |
// Dessiner un cercle rouge comme exemple d'icône | |
let rect = NSRect(x: 0, y: 0, width: size.width, height: size.height) | |
NSColor.red.setFill() | |
let circlePath = NSBezierPath(ovalIn: rect.insetBy(dx: 10, dy: 10)) | |
circlePath.fill() | |
// Dessiner un "H" blanc au centre | |
let text = "H" as NSString | |
let font = NSFont.boldSystemFont(ofSize: 80) | |
let attributes: [NSAttributedString.Key: Any] = [ | |
.font: font, | |
.foregroundColor: NSColor.white | |
] | |
let textSize = text.size(withAttributes: attributes) | |
let textPoint = NSPoint( | |
x: (size.width - textSize.width) / 2, | |
y: (size.height - textSize.height) / 2 | |
) | |
text.draw(at: textPoint, withAttributes: attributes) | |
image.unlockFocus() | |
return image | |
} | |
func applicationShouldTerminateAfterLastWindowClosed(_ sender: NSApplication) -> Bool { | |
return true | |
} | |
} | |
// Point d'entrée de l'application | |
let app = NSApplication.shared | |
let delegate = AppDelegate() | |
app.delegate = delegate | |
app.setActivationPolicy(.regular) | |
app.run() | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment