mirror of
https://github.com/hensm/fx_cast.git
synced 2026-06-11 01:59:58 +00:00
Implement initial NativeMacReceiverSelector
This commit is contained in:
47
app/NativeMacReceiverSelector/AppDelegate.swift
Normal file
47
app/NativeMacReceiverSelector/AppDelegate.swift
Normal file
@@ -0,0 +1,47 @@
|
||||
import Cocoa
|
||||
|
||||
|
||||
class AppDelegate: NSObject {
|
||||
var mainWindow: NSWindow?
|
||||
var mainWindowController: NSWindowController?
|
||||
var mainWindowViewController: ViewController?
|
||||
}
|
||||
|
||||
extension AppDelegate: NSApplicationDelegate {
|
||||
func applicationDidFinishLaunching(_ aNotification: Notification) {
|
||||
let window = NSPanel(
|
||||
contentRect: NSZeroRect
|
||||
, styleMask: [
|
||||
.titled
|
||||
, .closable
|
||||
, .hudWindow
|
||||
, .utilityWindow
|
||||
, .nonactivatingPanel
|
||||
]
|
||||
, backing: .buffered
|
||||
, defer: false)
|
||||
|
||||
window.title = "fx_cast"
|
||||
window.orderFrontRegardless()
|
||||
window.center()
|
||||
|
||||
let windowController = NSWindowController(window: window)
|
||||
windowController.showWindow(window)
|
||||
|
||||
let viewController = ViewController()
|
||||
window.contentViewController = viewController
|
||||
window.makeKeyAndOrderFront(self)
|
||||
|
||||
self.mainWindow = window
|
||||
self.mainWindowController = windowController
|
||||
self.mainWindowViewController = viewController
|
||||
|
||||
NSApp.activate(ignoringOtherApps: true)
|
||||
}
|
||||
|
||||
func applicationWillTerminate(_ aNotification: Notification) {}
|
||||
|
||||
func applicationShouldTerminateAfterLastWindowClosed(_ app: NSApplication) -> Bool {
|
||||
return true
|
||||
}
|
||||
}
|
||||
79
app/NativeMacReceiverSelector/ReceiverView.swift
Normal file
79
app/NativeMacReceiverSelector/ReceiverView.swift
Normal file
@@ -0,0 +1,79 @@
|
||||
import Cocoa
|
||||
|
||||
protocol ReceiverViewDelegate: AnyObject {
|
||||
func didCast (_ receiver: Receiver)
|
||||
}
|
||||
|
||||
class ReceiverView: NSStackView {
|
||||
weak var receiverViewDelegate: ReceiverViewDelegate?
|
||||
|
||||
var receiver: Receiver!
|
||||
var constraintsSet = false
|
||||
|
||||
|
||||
override init (frame: CGRect) {
|
||||
super.init(frame: frame)
|
||||
}
|
||||
required init? (coder: NSCoder) {
|
||||
super.init(coder: coder)
|
||||
}
|
||||
|
||||
init (receiver: Receiver) {
|
||||
super.init(frame: CGRect(x: 0, y: 0, width: 0, height: 0))
|
||||
|
||||
self.receiver = receiver
|
||||
|
||||
let metaStackView = NSStackView(views: [
|
||||
makeLabel(receiver.friendlyName, size: 14)
|
||||
, makeLabel("\(receiver.host):\(receiver.port)"
|
||||
, size: NSFont.smallSystemFontSize
|
||||
, color: .secondaryLabelColor)
|
||||
])
|
||||
|
||||
metaStackView.alignment = .leading
|
||||
metaStackView.orientation = .vertical
|
||||
metaStackView.spacing = 4
|
||||
|
||||
|
||||
let castButton = WideButton(
|
||||
title: "Cast"
|
||||
, target: self
|
||||
, action: #selector(ReceiverView.onCast))
|
||||
|
||||
castButton.bezelStyle = .rounded
|
||||
|
||||
|
||||
self.addArrangedSubview(metaStackView)
|
||||
self.addArrangedSubview(castButton)
|
||||
self.distribution = .fill
|
||||
}
|
||||
|
||||
override func updateConstraints () {
|
||||
super.updateConstraints()
|
||||
|
||||
if !constraintsSet {
|
||||
self.translatesAutoresizingMaskIntoConstraints = false
|
||||
self.leadingAnchor.constraint(equalTo: superview!.leadingAnchor, constant: 8).isActive = true
|
||||
self.trailingAnchor.constraint(equalTo: superview!.trailingAnchor, constant: -8).isActive = true
|
||||
|
||||
constraintsSet = true
|
||||
}
|
||||
}
|
||||
|
||||
@objc
|
||||
func onCast () {
|
||||
self.receiverViewDelegate?.didCast(self.receiver)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
class WideButton: NSButton {
|
||||
override var intrinsicContentSize: NSSize {
|
||||
var size = super.intrinsicContentSize
|
||||
if size.width < 100 {
|
||||
size.width = 100
|
||||
}
|
||||
|
||||
return size
|
||||
}
|
||||
}
|
||||
152
app/NativeMacReceiverSelector/ViewController.swift
Normal file
152
app/NativeMacReceiverSelector/ViewController.swift
Normal file
@@ -0,0 +1,152 @@
|
||||
import Cocoa
|
||||
import Foundation
|
||||
|
||||
|
||||
func makeLabel(_ text: String,
|
||||
size: CGFloat = 0,
|
||||
color: NSColor = NSColor.textColor) -> NSTextField {
|
||||
|
||||
let textField = NSTextField()
|
||||
textField.stringValue = text
|
||||
textField.backgroundColor = .clear
|
||||
textField.isEditable = false
|
||||
textField.isBezeled = false
|
||||
textField.sizeToFit()
|
||||
|
||||
// Text
|
||||
textField.font = NSFont.systemFont(ofSize: size)
|
||||
textField.textColor = color
|
||||
|
||||
return textField
|
||||
}
|
||||
|
||||
|
||||
struct InitData: Codable {
|
||||
let receivers: [Receiver]
|
||||
let defaultMediaType: MediaType
|
||||
|
||||
let i18n_mediaTypeApp: String
|
||||
let i18n_mediaTypeTab: String
|
||||
let i18n_mediaTypeScreen: String
|
||||
}
|
||||
|
||||
struct ReceiverSelection: Codable {
|
||||
let receiver: Receiver
|
||||
let mediaType: MediaType
|
||||
}
|
||||
|
||||
enum MediaType: Int, Codable {
|
||||
case app
|
||||
case tab
|
||||
case screen
|
||||
}
|
||||
|
||||
struct Receiver: Codable {
|
||||
let friendlyName: String
|
||||
let host: String
|
||||
let id: String
|
||||
let port: Int
|
||||
}
|
||||
|
||||
|
||||
class ViewController: NSViewController {
|
||||
var mediaTypePopUpButton: NSPopUpButton!
|
||||
|
||||
override func loadView () {
|
||||
let visualEffectView = NSVisualEffectView()
|
||||
visualEffectView.blendingMode = .behindWindow
|
||||
visualEffectView.state = .active
|
||||
|
||||
self.view = visualEffectView
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
|
||||
|
||||
let initData: InitData!
|
||||
|
||||
do {
|
||||
initData = try JSONDecoder().decode(InitData.self, from: data)
|
||||
} catch {
|
||||
fputs("Error: Failed to parse input data\n", stderr)
|
||||
exit(1)
|
||||
}
|
||||
|
||||
|
||||
let stackView = NSStackView()
|
||||
stackView.orientation = .vertical
|
||||
stackView.alignment = .leading
|
||||
stackView.autoresizingMask = [ .width, .height ]
|
||||
stackView.edgeInsets = NSEdgeInsetsMake(8, 8, 8, 8)
|
||||
|
||||
|
||||
self.mediaTypePopUpButton = NSPopUpButton()
|
||||
self.mediaTypePopUpButton.addItems(withTitles: [
|
||||
initData.i18n_mediaTypeApp
|
||||
, initData.i18n_mediaTypeTab
|
||||
, initData.i18n_mediaTypeScreen
|
||||
])
|
||||
|
||||
self.mediaTypePopUpButton.selectItem(at: initData.defaultMediaType.rawValue)
|
||||
|
||||
let mediaTypeStackView = NSStackView(views: [
|
||||
makeLabel("Cast"),
|
||||
self.mediaTypePopUpButton,
|
||||
makeLabel("to:")
|
||||
])
|
||||
|
||||
|
||||
stackView.addArrangedSubview(mediaTypeStackView)
|
||||
|
||||
|
||||
for receiver in initData.receivers {
|
||||
let receiverSeparator = NSBox()
|
||||
receiverSeparator.boxType = .separator
|
||||
|
||||
let receiverView = ReceiverView(receiver: receiver)
|
||||
receiverView.receiverViewDelegate = self
|
||||
|
||||
stackView.addArrangedSubview(receiverSeparator)
|
||||
stackView.addArrangedSubview(receiverView)
|
||||
}
|
||||
|
||||
|
||||
self.view.addSubview(stackView)
|
||||
self.view.autoresizesSubviews = true
|
||||
self.view.frame.size.width = 350
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
extension ViewController: ReceiverViewDelegate {
|
||||
func didCast (_ receiver: Receiver) {
|
||||
do {
|
||||
let mediaType = MediaType(
|
||||
rawValue: self.mediaTypePopUpButton.indexOfSelectedItem)!
|
||||
|
||||
let selection = ReceiverSelection(
|
||||
receiver: receiver
|
||||
, mediaType: mediaType)
|
||||
|
||||
let jsonData = try JSONEncoder().encode(selection)
|
||||
let jsonString = String(data: jsonData, encoding: .utf8)
|
||||
|
||||
print(jsonString!)
|
||||
fflush(stdout)
|
||||
} catch {
|
||||
fputs("Error: Failed to encode output data", stderr)
|
||||
exit(1)
|
||||
}
|
||||
}
|
||||
}
|
||||
10
app/NativeMacReceiverSelector/main.swift
Normal file
10
app/NativeMacReceiverSelector/main.swift
Normal file
@@ -0,0 +1,10 @@
|
||||
import Cocoa
|
||||
|
||||
|
||||
let app = NSApplication.shared
|
||||
|
||||
let delegate = AppDelegate()
|
||||
app.delegate = delegate
|
||||
|
||||
app.setActivationPolicy(.regular)
|
||||
app.run()
|
||||
Reference in New Issue
Block a user