PlayingExternallyView.swift 3.01 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
/*****************************************************************************
 * VLCPlayingExternallyView.swift
 * VLC for iOS
 *****************************************************************************
 * Copyright (c) 2018 VideoLAN. All rights reserved.
 * $Id$
 *
 * Authors: Carola Nitz <caro # videolan.org>
 *
 * Refer to the COPYING file of the official project for license.
 *****************************************************************************/

import Foundation

15
 @IBDesignable class PlayingExternallyView: UIView {
16

17
    @IBInspectable var nibName: String?
18 19 20 21 22 23 24
    @IBOutlet weak var playingExternallyTitle: UILabel!
    @IBOutlet weak var playingExternallyDescription: UILabel!
    @objc var displayView: UIView? {
        return externalWindow?.rootViewController?.view
    }
    var externalWindow: UIWindow?

25
    @objc func shouldDisplay(_ show: Bool, movieView: UIView) {
26 27 28 29 30 31 32 33 34 35 36
        self.isHidden = !show
        if show {
            guard let screen = UIScreen.screens.count > 1 ? UIScreen.screens[1] : nil else {
                return
            }
            screen.overscanCompensation = .none
            externalWindow = UIWindow(frame: screen.bounds)
            guard let externalWindow = externalWindow else {
                return
            }
            externalWindow.rootViewController = VLCExternalDisplayController()
37
            externalWindow.rootViewController?.view.addSubview(movieView)
38 39
            externalWindow.screen = screen
            externalWindow.rootViewController?.view.frame = externalWindow.bounds
40
            movieView.frame = externalWindow.bounds
41 42 43
        } else {
            externalWindow = nil
        }
44
        externalWindow?.isHidden = !show
45 46 47
    }

    override func awakeFromNib() {
48 49
        super.awakeFromNib()
        xibSetup()
50 51 52 53
        playingExternallyTitle.text = NSLocalizedString("PLAYING_EXTERNALLY_TITLE", comment: "")
        playingExternallyDescription.text = NSLocalizedString("PLAYING_EXTERNALLY_DESC", comment:"")
    }

54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
    func xibSetup() {
        guard let view = loadViewFromNib() else { return }
        view.frame = bounds
        view.autoresizingMask =
            [.flexibleWidth, .flexibleHeight]
        addSubview(view)
    }

    func loadViewFromNib() -> PlayingExternallyView? {
        guard let nibName = nibName else { return nil }
        let bundle = Bundle(for: type(of: self))
        let nib = UINib(nibName: nibName, bundle: bundle)
        return nib.instantiate(
            withOwner: self,
            options: nil).first as? PlayingExternallyView
    }

71
    @objc func updateUI(rendererItem: VLCRendererItem?, title: String) {
72
        if let rendererItem = rendererItem {
73
            playingExternallyTitle.text = title + NSLocalizedString("PLAYING_EXTERNALLY_ADDITION", comment:"\n should stay in every translation")
74
            playingExternallyDescription.text = rendererItem.name
75
        } else {
76
            playingExternallyTitle.text = title
77 78 79 80
            playingExternallyDescription.text = NSLocalizedString("PLAYING_EXTERNALLY_DESC", comment:"")
        }
    }
}