webview.qml 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. import QtQuick 2.4
  2. import Konvergo 1.0
  3. import QtWebEngine 1.1
  4. import QtWebChannel 1.0
  5. import QtQuick.Window 2.2
  6. KonvergoWindow
  7. {
  8. id: mainWindow
  9. title: "Plex Media Player"
  10. objectName: "mainWindow"
  11. visible: true
  12. minimumHeight: 240
  13. minimumWidth: 426
  14. height: 720
  15. width: 1280
  16. function getMaxHeightArg()
  17. {
  18. if (webMaxHeight > 0)
  19. return "?maxHeight=" + (webMaxHeight / Screen.devicePixelRatio);
  20. return ""
  21. }
  22. MpvVideo
  23. {
  24. id: video
  25. objectName: "video"
  26. // It's not a real item. Its renderer draws onto the view's background.
  27. width: 0
  28. height: 0
  29. visible: false
  30. }
  31. WebEngineView
  32. {
  33. id: web
  34. objectName: "web"
  35. anchors.centerIn: parent
  36. settings.errorPageEnabled: false
  37. settings.localContentCanAccessRemoteUrls: true
  38. profile.httpUserAgent: components.system.getUserAgent()
  39. url: components.settings.value("path", "startupurl") + getMaxHeightArg()
  40. transformOrigin: Item.TopLeft
  41. width: Math.min((parent.height * 16) / 9, parent.width)
  42. height: Math.min((parent.width * 9) / 16, parent.height)
  43. function getDesiredScale()
  44. {
  45. var verticalScale = height / 720;
  46. var horizontalScale = width / 1280;
  47. return Math.min(verticalScale, horizontalScale);
  48. }
  49. scale:
  50. {
  51. var desiredScale = getDesiredScale();
  52. var maximumScale = webMaxHeight ? ((webMaxHeight / Screen.devicePixelRatio) / 720) : 10;
  53. if (desiredScale < maximumScale) {
  54. // Web renders at windows scale, no scaling
  55. return 1;
  56. } else {
  57. // Web should max out at maximum scaling
  58. return desiredScale / maximumScale;
  59. }
  60. }
  61. zoomFactor:
  62. {
  63. var desiredScale = getDesiredScale();
  64. if (desiredScale < 1)
  65. return desiredScale;
  66. else
  67. return 1;
  68. }
  69. Component.onCompleted:
  70. {
  71. // set the transparency
  72. // (setting this here as a UserAgent workaround at least for qt5.5)
  73. backgroundColor : "#111111"
  74. forceActiveFocus()
  75. mainWindow.reloadWebClient.connect(reload)
  76. }
  77. onLoadingChanged:
  78. {
  79. // we use a timer here to switch to the webview since
  80. // it take a few moments for the webview to render
  81. // after it has loaded.
  82. //
  83. if (loadRequest.status == WebEngineView.LoadSucceededStatus)
  84. {
  85. console.log("Loaded web-client successfully from: " + web.url);
  86. }
  87. else if (loadRequest.status == WebEngineView.LoadFailedStatus)
  88. {
  89. errorLabel.visible = true
  90. errorLabel.text = "Error loading client, this is bad and should not happen<br>" +
  91. "You can try to <a href='reload'>reload</a> or head to our <a href='http://plex.tv/support'>support page</a><br><br>Actual Error: <pre>" +
  92. loadRequest.url + "\n" + loadRequest.errorString + " [" + loadRequest.errorCode + "]</pre><br><br>" +
  93. "Provide the <a href='file://"+ components.system.logFilePath() + "'>logfile</a> as well."
  94. }
  95. }
  96. onJavaScriptConsoleMessage:
  97. {
  98. components.system.info(message)
  99. }
  100. onCertificateError:
  101. {
  102. console.log(error.url + " :" + error.description + error.error)
  103. }
  104. }
  105. Text
  106. {
  107. id: errorLabel
  108. z: 5
  109. anchors.centerIn: parent
  110. color: "#999999"
  111. linkColor: "#cc7b19"
  112. text: "Generic error"
  113. font.pixelSize: 32
  114. font.bold: true
  115. visible: false
  116. verticalAlignment: Text.AlignVCenter
  117. textFormat: Text.StyledText
  118. onLinkActivated:
  119. {
  120. if (link == "reload")
  121. {
  122. web.reload()
  123. errorLabel.visible = false
  124. }
  125. else
  126. {
  127. Qt.openUrlExternally(link)
  128. }
  129. }
  130. }
  131. Rectangle
  132. {
  133. id: debug
  134. color: "black"
  135. z: 10
  136. anchors.centerIn: parent
  137. width: parent.width
  138. height: parent.height
  139. opacity: 0.7
  140. visible: mainWindow.showDebugLayer
  141. Text
  142. {
  143. id: debugLabel
  144. width: (parent.width - 50) / 2
  145. height: parent.height - 25
  146. anchors.left: parent.left
  147. anchors.leftMargin: 64
  148. anchors.top: parent.top
  149. anchors.topMargin: 54
  150. anchors.bottomMargin: 54
  151. color: "white"
  152. font.pixelSize: width / 45
  153. wrapMode: Text.WrapAnywhere
  154. function windowDebug()
  155. {
  156. var dbg = mainWindow.debugInfo + "Window and web\n";
  157. dbg += " Window size: " + parent.width + "x" + parent.height + "\n";
  158. dbg += " DevicePixel ratio: " + Screen.devicePixelRatio + "\n";
  159. dbg += " Web Max Height: " + (webMaxHeight / Screen.devicePixelRatio) + "\n";
  160. dbg += " Web scale: " + Math.round(web.scale * 100) / 100 + "\n";
  161. dbg += " Desired Scale: " + Math.round(web.getDesiredScale() * 100) / 100 + "\n";
  162. dbg += " Zoom Factor: " + Math.round(web.zoomFactor * 100) / 100 + "\n";
  163. return dbg;
  164. }
  165. text: windowDebug()
  166. }
  167. Text
  168. {
  169. id: videoLabel
  170. width: (parent.width - 50) / 2
  171. height: parent.height - 25
  172. anchors.right: parent.right
  173. anchors.left: debugLabel.right
  174. anchors.rightMargin: 64
  175. anchors.top: parent.top
  176. anchors.topMargin: 54
  177. anchors.bottomMargin: 54
  178. color: "white"
  179. font.pixelSize: width / 45
  180. wrapMode: Text.NoWrap
  181. text: mainWindow.videoInfo
  182. }
  183. }
  184. property QtObject webChannel: web.webChannel
  185. }