webview.qml 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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
  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.fill: parent
  36. anchors.horizontalCenter: parent.horizontalCenter
  37. settings.errorPageEnabled: false
  38. settings.localContentCanAccessRemoteUrls: true
  39. profile.httpUserAgent: components.system.getUserAgent()
  40. url: components.settings.value("path", "startupurl") + getMaxHeightArg()
  41. transformOrigin: Item.TopLeft
  42. scale:
  43. {
  44. var verticalScale = height / 720;
  45. var horizontalScale = width / 1280;
  46. var desiredScale = Math.min(verticalScale, horizontalScale);
  47. var maximumScale = webMaxHeight ? (webMaxHeight / 720) : 10;
  48. if (desiredScale < 1) {
  49. // Web renders at 1:1, so scale down
  50. return desiredScale;
  51. } else if (desiredScale < maximumScale) {
  52. // Web renders at windows scale, no scaling
  53. return 1;
  54. } else {
  55. // Web should max out at maximum scaling
  56. return desiredScale / maximumScale;
  57. }
  58. }
  59. Component.onCompleted:
  60. {
  61. // set the transparency
  62. // (setting this here as a UserAgent workaround at least for qt5.5)
  63. backgroundColor : "#111111"
  64. forceActiveFocus()
  65. mainWindow.reloadWebClient.connect(reload)
  66. }
  67. onLoadingChanged:
  68. {
  69. // we use a timer here to switch to the webview since
  70. // it take a few moments for the webview to render
  71. // after it has loaded.
  72. //
  73. if (loadRequest.status == WebEngineView.LoadSucceededStatus)
  74. {
  75. console.log("Loaded web-client successfully from: " + web.url);
  76. }
  77. else if (loadRequest.status == WebEngineView.LoadFailedStatus)
  78. {
  79. errorLabel.visible = true
  80. errorLabel.text = "Error loading client, this is bad and should not happen<br>" +
  81. "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>" +
  82. loadRequest.url + "\n" + loadRequest.errorString + " [" + loadRequest.errorCode + "]</pre><br><br>" +
  83. "Provide the <a href='file://"+ components.system.logFilePath() + "'>logfile</a> as well."
  84. }
  85. }
  86. onJavaScriptConsoleMessage:
  87. {
  88. components.system.info(message)
  89. }
  90. onCertificateError:
  91. {
  92. console.log(error.url + " :" + error.description + error.error)
  93. }
  94. }
  95. Text
  96. {
  97. id: errorLabel
  98. z: 5
  99. anchors.centerIn: parent
  100. color: "#999999"
  101. linkColor: "#cc7b19"
  102. text: "Generic error"
  103. font.pixelSize: 32
  104. font.bold: true
  105. visible: false
  106. verticalAlignment: Text.AlignVCenter
  107. textFormat: Text.StyledText
  108. onLinkActivated:
  109. {
  110. if (link == "reload")
  111. {
  112. web.reload()
  113. errorLabel.visible = false
  114. }
  115. else
  116. {
  117. Qt.openUrlExternally(link)
  118. }
  119. }
  120. }
  121. Rectangle
  122. {
  123. id: debug
  124. color: "black"
  125. z: 10
  126. anchors.centerIn: parent
  127. width: parent.width
  128. height: parent.height
  129. opacity: 0.7
  130. visible: mainWindow.showDebugLayer
  131. Text
  132. {
  133. id: debugLabel
  134. width: (parent.width - 50) / 2
  135. height: parent.height - 25
  136. anchors.left: parent.left
  137. anchors.leftMargin: 64
  138. anchors.top: parent.top
  139. anchors.topMargin: 54
  140. anchors.bottomMargin: 54
  141. color: "white"
  142. font.pixelSize: width / 45
  143. wrapMode: Text.WrapAnywhere
  144. function windowDebug()
  145. {
  146. var dbg = mainWindow.debugInfo + "Window and web\n";
  147. dbg += " Window size: " + parent.width + "x" + parent.height + "\n";
  148. dbg += " DevicePixel ratio: " + Screen.devicePixelRatio + "\n";
  149. dbg += " Web Max Height: " + webMaxHeight + "\n";
  150. dbg += " Web scale: " + Math.round(web.scale * 100) / 100;
  151. return dbg;
  152. }
  153. text: windowDebug()
  154. }
  155. Text
  156. {
  157. id: videoLabel
  158. width: (parent.width - 50) / 2
  159. height: parent.height - 25
  160. anchors.right: parent.right
  161. anchors.left: debugLabel.right
  162. anchors.rightMargin: 64
  163. anchors.top: parent.top
  164. anchors.topMargin: 54
  165. anchors.bottomMargin: 54
  166. color: "white"
  167. font.pixelSize: width / 45
  168. wrapMode: Text.NoWrap
  169. text: mainWindow.videoInfo
  170. }
  171. }
  172. property QtObject webChannel: web.webChannel
  173. }