webview.qml 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  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. import QtQuick.Controls 1.4
  7. KonvergoWindow
  8. {
  9. id: mainWindow
  10. title: "Plex Media Player"
  11. objectName: "mainWindow"
  12. minimumHeight: windowMinSize.height
  13. minimumWidth: windowMinSize.width
  14. function runWebAction(action)
  15. {
  16. if (mainWindow.webDesktopMode)
  17. web.triggerWebAction(action)
  18. }
  19. function actionEnable(enable)
  20. {
  21. action_switchmode.enabled = enable
  22. action_copy.enabled = enable
  23. action_cut.enabled = enable
  24. action_paste.enabled = enable
  25. action_undo.enabled = enable
  26. action_redo.enabled = enable
  27. action_selectall.enabled = enable
  28. action_fullscreen.enabled = enable
  29. }
  30. Action
  31. {
  32. id: action_fullscreen
  33. shortcut: "F11"
  34. onTriggered:
  35. {
  36. mainWindow.toggleFullscreen()
  37. }
  38. }
  39. Action
  40. {
  41. id: action_switchmode
  42. shortcut: "Ctrl+M"
  43. onTriggered:
  44. {
  45. if (mainWindow.webDesktopMode)
  46. components.settings.cycleSetting("main.webMode")
  47. }
  48. }
  49. Action
  50. {
  51. shortcut: StandardKey.Copy
  52. onTriggered: runWebAction(WebEngineView.Copy)
  53. id: action_copy
  54. }
  55. Action
  56. {
  57. shortcut: StandardKey.Cut
  58. onTriggered: runWebAction(WebEngineView.Cut)
  59. id: action_cut
  60. }
  61. Action
  62. {
  63. shortcut: StandardKey.Paste
  64. onTriggered: runWebAction(WebEngineView.Paste)
  65. id: action_paste
  66. }
  67. Action
  68. {
  69. shortcut: StandardKey.SelectAll
  70. onTriggered: runWebAction(WebEngineView.SelectAll)
  71. id: action_selectall
  72. }
  73. Action
  74. {
  75. shortcut: StandardKey.Undo
  76. onTriggered: runWebAction(WebEngineView.Undo)
  77. id: action_undo
  78. }
  79. Action
  80. {
  81. shortcut: StandardKey.Redo
  82. onTriggered: runWebAction(WebEngineView.Redo)
  83. id: action_redo
  84. }
  85. function maxWebScale()
  86. {
  87. return webHeightMax ? ((webHeightMax / Screen.devicePixelRatio) / 720) : 10;
  88. }
  89. MpvVideo
  90. {
  91. id: video
  92. objectName: "video"
  93. // It's not a real item. Its renderer draws onto the view's background.
  94. width: 0
  95. height: 0
  96. visible: false
  97. }
  98. WebEngineView
  99. {
  100. id: web
  101. objectName: "web"
  102. anchors.centerIn: parent
  103. settings.errorPageEnabled: false
  104. settings.localContentCanAccessRemoteUrls: true
  105. profile.httpUserAgent: components.system.getUserAgent()
  106. transformOrigin: Item.TopLeft
  107. url: mainWindow.webUrl
  108. focus: true
  109. property string currentHoveredUrl: ""
  110. onLinkHovered: web.currentHoveredUrl = hoveredUrl
  111. width:
  112. {
  113. if (!mainWindow.webDesktopMode)
  114. {
  115. return Math.round(Math.min((parent.height * 16) / 9, parent.width));
  116. }
  117. else
  118. {
  119. return parent.width;
  120. }
  121. }
  122. height:
  123. {
  124. if (!mainWindow.webDesktopMode)
  125. {
  126. return Math.round(Math.min((parent.width * 9) / 16, parent.height));
  127. }
  128. else
  129. {
  130. return parent.height;
  131. }
  132. }
  133. scale:
  134. {
  135. if (mainWindow.webDesktopMode)
  136. return 1;
  137. if (mainWindow.windowScale < mainWindow.maxWebScale())
  138. {
  139. // Web renders at windows scale, no scaling
  140. return 1;
  141. }
  142. else
  143. {
  144. // Web should max out at maximum scaling
  145. return mainWindow.windowScale / mainWindow.maxWebScale();
  146. }
  147. }
  148. Component.onCompleted:
  149. {
  150. // set the transparency
  151. // (setting this here as a UserAgent workaround at least for qt5.5)
  152. backgroundColor : "#111111"
  153. forceActiveFocus()
  154. mainWindow.reloadWebClient.connect(reload)
  155. actionEnable(mainWindow.webDesktopMode)
  156. }
  157. onLoadingChanged:
  158. {
  159. // we use a timer here to switch to the webview since
  160. // it take a few moments for the webview to render
  161. // after it has loaded.
  162. //
  163. if (loadRequest.status == WebEngineView.LoadSucceededStatus)
  164. {
  165. console.log("Loaded web-client successfully from: " + web.url);
  166. }
  167. else if (loadRequest.status == WebEngineView.LoadFailedStatus)
  168. {
  169. console.log("FAILED TO LOAD web-client successfully from: " + web.url);
  170. errorLabel.visible = true
  171. errorLabel.text = "Error loading client, this is bad and should not happen<br>" +
  172. "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>" +
  173. loadRequest.errorString + " [" + loadRequest.errorCode + "]</pre><br><br>" +
  174. "Provide the <a target='_blank' href='file://"+ components.system.logFilePath + "'>logfile</a> as well."
  175. }
  176. actionEnable(mainWindow.webDesktopMode)
  177. }
  178. onNewViewRequested:
  179. {
  180. if (request.userInitiated)
  181. {
  182. console.log("Opening external URL: " + web.currentHoveredUrl)
  183. components.system.openExternalUrl(web.currentHoveredUrl)
  184. }
  185. }
  186. onFullScreenRequested:
  187. {
  188. console.log("Request fullscreen: " + request.toggleOn)
  189. mainWindow.setFullScreen(request.toggleOn)
  190. request.accept()
  191. }
  192. onJavaScriptConsoleMessage:
  193. {
  194. components.system.info(message)
  195. }
  196. onCertificateError:
  197. {
  198. console.log(error.url + " :" + error.description + error.error)
  199. }
  200. }
  201. Text
  202. {
  203. id: errorLabel
  204. z: 5
  205. anchors.centerIn: parent
  206. color: "#999999"
  207. linkColor: "#cc7b19"
  208. text: "Generic error"
  209. font.pixelSize: 32
  210. font.bold: true
  211. visible: false
  212. verticalAlignment: Text.AlignVCenter
  213. textFormat: Text.StyledText
  214. onLinkActivated:
  215. {
  216. if (link == "reload")
  217. {
  218. errorLabel.visible = false
  219. web.reload()
  220. }
  221. else
  222. {
  223. Qt.openUrlExternally(link)
  224. }
  225. }
  226. }
  227. Rectangle
  228. {
  229. id: debug
  230. color: "black"
  231. z: 10
  232. anchors.centerIn: parent
  233. width: parent.width
  234. height: parent.height
  235. opacity: 0.7
  236. visible: mainWindow.showDebugLayer
  237. Text
  238. {
  239. id: debugLabel
  240. width: (parent.width - 50) / 2
  241. height: parent.height - 25
  242. anchors.left: parent.left
  243. anchors.leftMargin: 64
  244. anchors.top: parent.top
  245. anchors.topMargin: 54
  246. anchors.bottomMargin: 54
  247. color: "white"
  248. font.pixelSize: width / 45
  249. wrapMode: Text.WrapAnywhere
  250. function windowDebug()
  251. {
  252. var dbg = mainWindow.debugInfo + "Window and web\n";
  253. dbg += " Window size: " + parent.width + "x" + parent.height + " - " + web.width + "x" + web.height + "\n";
  254. dbg += " DevicePixel ratio: " + Screen.devicePixelRatio + "\n";
  255. dbg += " Web Max Height: " + (webHeightMax / Screen.devicePixelRatio) + " / Max scale: " + mainWindow.maxWebScale() + "\n";
  256. dbg += " Web scale: " + webScale + " / Window scale: " + windowScale + "\n";
  257. dbg += " Scale applied: " + web.scale + "\n";
  258. return dbg;
  259. }
  260. text: windowDebug()
  261. }
  262. Text
  263. {
  264. id: videoLabel
  265. width: (parent.width - 50) / 2
  266. height: parent.height - 25
  267. anchors.right: parent.right
  268. anchors.left: debugLabel.right
  269. anchors.rightMargin: 64
  270. anchors.top: parent.top
  271. anchors.topMargin: 54
  272. anchors.bottomMargin: 54
  273. color: "white"
  274. font.pixelSize: width / 45
  275. wrapMode: Text.WrapAnywhere
  276. text: mainWindow.videoInfo
  277. }
  278. }
  279. property QtObject webChannel: web.webChannel
  280. }