Files
mue/safari/Mue/ViewController.swift
alexsparkes cfb9915a8b Refactor and update various components and styles
- Added shebang to commit-msg.sh for better script execution.
- Updated Dockerfile to use a specific version of the bun image.
- Improved logging format in SafariWebExtensionHandler.swift for better readability.
- Simplified CSS variable for text color in Style.css.
- Refactored ViewController.swift to enhance readability and maintainability.
- Improved conditional checks in ModalTopBar.jsx and Tooltip.jsx for better clarity.
- Updated SCSS files to use comments consistently and removed redundant lines.
- Enhanced error handling and background loading logic in backgroundLoader.js.
- Refactored useBackgroundEvents.js and useBackgroundLoader.js for better readability.
- Cleaned up quicklinks components and utilities for improved code quality.
- Added empty error.scss file for future styling.
- Updated toast.scss and index.scss with consistent comment styles.
- Improved number formatting logic in formatNumber.js for better clarity.
2026-02-03 21:51:53 +00:00

62 lines
1.9 KiB
Swift

//
// ViewController.swift
// Mue
//
// Created by David Ralph on 02/01/2026.
//
import Cocoa
import SafariServices
import WebKit
let extensionBundleIdentifier = "mueauthors.mue.Extension"
class ViewController: NSViewController, WKNavigationDelegate, WKScriptMessageHandler {
@IBOutlet var webView: WKWebView!
override func viewDidLoad() {
super.viewDidLoad()
self.webView.navigationDelegate = self
self.webView.configuration.userContentController.add(self, name: "controller")
let mainUrl = Bundle.main.url(forResource: "Main", withExtension: "html")!
let resourceUrl = Bundle.main.resourceURL!
self.webView.loadFileURL(mainUrl, allowingReadAccessTo: resourceUrl)
}
func webView(_ webView: WKWebView, didFinish navigation: WKNavigation!) {
SFSafariExtensionManager.getStateOfSafariExtension(
withIdentifier: extensionBundleIdentifier
) { (state, error) in
guard let state = state, error == nil else {
// Insert code to inform the user that something went wrong.
return
}
DispatchQueue.main.async {
if #available(macOS 13, *) {
webView.evaluateJavaScript("show(\(state.isEnabled), true)")
} else {
webView.evaluateJavaScript("show(\(state.isEnabled), false)")
}
}
}
}
func userContentController(_ userContentController: WKUserContentController, didReceive message: WKScriptMessage) {
guard let messageBody = message.body as? String, messageBody == "open-preferences" else {
return
}
SFSafariApplication.showPreferencesForExtension(withIdentifier: extensionBundleIdentifier) { _ in
DispatchQueue.main.async {
NSApplication.shared.terminate(nil)
}
}
}
}