Set window position in native selector based on data from extension

This commit is contained in:
hensm
2019-07-12 19:15:38 +01:00
parent d8d8f01347
commit f2907cdc15
4 changed files with 40 additions and 19 deletions

View File

@@ -2,21 +2,47 @@ import Cocoa
class AppDelegate : NSObject, NSApplicationDelegate {
var initData: InitData!
var mainWindow: NSWindow?
var mainWindowController: NSWindowController?
var mainWindowViewController: ViewController?
func applicationDidFinishLaunching (_ aNotification: Notification) {
if (CommandLine.argc < 2) {
fputs("Error: Not enough args\n", stderr)
exit(1)
}
guard let data = CommandLine.arguments[1].data(using: .utf8) else {
fputs("Error: Failed to convert input to data\n", stderr)
exit(1)
}
do {
// Decode and store initialization JSON data
self.initData = try JSONDecoder().decode(InitData.self, from: data)
} catch {
fputs("Error: Failed to parse input data\n", stderr)
exit(1)
}
let window = NSWindow(
contentRect: NSZeroRect
, styleMask: [ .titled, .closable ]
, backing: .buffered
, defer: false)
let screenHeight = NSScreen.main!.frame.height
window.titleVisibility = .hidden
window.isMovableByWindowBackground = true
window.orderFrontRegardless()
window.center()
window.setFrameTopLeftPoint(NSPoint(
x: self.initData.windowPositionX
, y: Int(screenHeight - CGFloat(self.initData.windowPositionY))))
let windowController = NSWindowController(window: window)
windowController.showWindow(window)

View File

@@ -21,24 +21,7 @@ class ViewController : NSViewController {
override func viewDidLoad () {
super.viewDidLoad()
if (CommandLine.argc < 2) {
fputs("Error: Not enough args\n", stderr)
exit(1)
}
guard let data = CommandLine.arguments[1].data(using: .utf8) else {
fputs("Error: Failed to convert input to data\n", stderr)
exit(1)
}
do {
// Decode and store initialization JSON data
self.initData = try JSONDecoder().decode(InitData.self, from: data)
} catch {
fputs("Error: Failed to parse input data\n", stderr)
exit(1)
}
self.initData = (NSApplication.shared.delegate as! AppDelegate).initData
/**
* View Hierarchy

View File

@@ -3,6 +3,9 @@ struct InitData : Codable {
let defaultMediaType: MediaType
let availableMediaTypes: Int
let windowPositionX: Int
let windowPositionY: Int
let i18n_extensionName: String
let i18n_castButtonTitle: String
let i18n_mediaTypeApp: String