TeachMate/TeachMate/Utilities/WindowSizeManager.swift

69 lines
1.9 KiB
Swift
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// WindowSizeManager.swift
// TeachMate
//
// Created by Hongli on 2025/3/13.
//
import SwiftUI
#if os(macOS)
//
struct WindowSizeManager {
enum WindowSizePreset {
case calendar // -
case schedule // -
var size: CGSize {
switch self {
case .calendar:
return CGSize(width: 500, height: 800) //
case .schedule:
return CGSize(width: 1200, height: 680) //
}
}
}
//
static func resizeWindow(to preset: WindowSizePreset) {
guard let window = NSApplication.shared.windows.first else { return }
let newSize = preset.size
let currentFrame = window.frame
//
let newOriginX = currentFrame.origin.x + (currentFrame.width - newSize.width) / 2
let newOriginY = currentFrame.origin.y + (currentFrame.height - newSize.height) / 2
let newFrame = NSRect(
x: newOriginX,
y: newOriginY,
width: newSize.width,
height: newSize.height
)
//
window.animator().setFrame(newFrame, display: true, animate: true)
}
}
//
struct WindowSizeModifier: ViewModifier {
let preset: WindowSizeManager.WindowSizePreset
func body(content: Content) -> some View {
content
.onAppear {
WindowSizeManager.resizeWindow(to: preset)
}
}
}
// View便使
extension View {
func adjustWindowSize(to preset: WindowSizeManager.WindowSizePreset) -> some View {
modifier(WindowSizeModifier(preset: preset))
}
}
#endif