Files
OrionToolbar/patches/oriontoolbar_settings_modal_controller.py
2026-10-23 09:56:46 +01:00

112 lines
3.1 KiB
Python

from pathlib import Path
from datetime import datetime
import shutil
root = Path.cwd()
source_file = root / "OrionToolbar/OrionMenuView.swift"
controller_file = root / "OrionToolbar/OrionSettingsModal.swift"
if not source_file.exists():
raise SystemExit("ERROR: OrionMenuView.swift not found")
source = source_file.read_text()
old = """ private func showSettings() {
openSettings()
NSApplication.shared.activate(ignoringOtherApps: true)
}"""
new = """ private func showSettings() {
OrionSettingsModal.shared.present(vm: vm)
}"""
if old not in source:
raise SystemExit(
"ERROR: Settings implementation differs from expected source. "
"No files changed. Inspect showSettings() before patching."
)
timestamp = datetime.now().strftime("%Y%m%d-%H%M%S-%f")
backup_dir = root / "backups" / timestamp
for path in [source_file, controller_file]:
if path.exists():
destination = backup_dir / path.relative_to(root)
destination.parent.mkdir(parents=True, exist_ok=True)
shutil.copy2(path, destination)
controller = r'''import AppKit
import SwiftUI
@MainActor
final class OrionSettingsModal: NSObject, NSWindowDelegate {
static let shared = OrionSettingsModal()
private var window: NSWindow?
private var modalRunning = false
func present(vm: OrionVMController) {
if let window {
NSApplication.shared.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(nil)
window.orderFrontRegardless()
return
}
let content = SettingsView()
.environmentObject(vm)
let window = NSWindow(
contentRect: NSRect(x: 0, y: 0, width: 650, height: 380),
styleMask: [.titled, .closable, .miniaturizable],
backing: .buffered,
defer: false
)
window.title = "OrionToolbar Settings"
window.contentView = NSHostingView(rootView: content)
window.center()
window.level = .floating
window.isReleasedWhenClosed = false
window.delegate = self
self.window = window
NSApplication.shared.activate(ignoringOtherApps: true)
window.makeKeyAndOrderFront(nil)
window.orderFrontRegardless()
DispatchQueue.main.async { [weak self, weak window] in
guard let self,
let window,
self.window === window,
!self.modalRunning else {
return
}
self.modalRunning = true
NSApplication.shared.runModal(for: window)
self.modalRunning = false
}
}
func windowWillClose(_ notification: Notification) {
if modalRunning {
NSApplication.shared.stopModal()
}
window = nil
}
}
'''
source_file.write_text(source.replace(old, new, 1))
controller_file.write_text(controller)
print("UPDATED:", source_file.relative_to(root))
print("CREATED:", controller_file.relative_to(root))
print("BACKUP:", backup_dir)
print()
print("IMPORTANT: Ensure OrionSettingsModal.swift is included")
print("in the OrionToolbar Xcode application target.")