Skip to content
This repository has been archived by the owner on Jan 22, 2022. It is now read-only.

Commit

Permalink
Merge pull request #382 from dano6/fix-previews
Browse files Browse the repository at this point in the history
Implement swipe to close tab and fix tab previews
  • Loading branch information
UniversalSuperBox authored Nov 25, 2020
2 parents 2049e58 + 6894018 commit 01b1daf
Show file tree
Hide file tree
Showing 4 changed files with 134 additions and 149 deletions.
21 changes: 15 additions & 6 deletions src/app/webbrowser/Browser.qml
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,11 @@ Common.BrowserView {

currentWebview: tabsModel && tabsModel.currentTab ? tabsModel.currentTab.webview : null

TabChrome {
id: invisibleTabChrome
visible: false
}

property bool incognito: false

property var tabsModel: TabsModel {
Expand Down Expand Up @@ -88,7 +93,7 @@ Common.BrowserView {
state.url = tab.url.toString()
state.title = tab.title
state.icon = tab.icon.toString()
state.preview = tab.preview.toString()
state.preview = Qt.resolvedUrl(PreviewManager.previewPathFromUrl(tab.url))
state.savedState = tab.webview ? tab.webview.currentState : tab.restoreState
return state
}
Expand Down Expand Up @@ -552,9 +557,13 @@ Common.BrowserView {
anchors.fill: parent
visible: bottomEdgeHandle.dragging || tabslist.animating || (state == "shown")
onVisibleChanged: {
if (visible)
{
currentWebview.hideContextMenu()
if (visible) {

currentWebview.hideContextMenu();
chrome.state = "hidden";
}
else {
chrome.state = "shown";
}
}

Expand Down Expand Up @@ -682,8 +691,8 @@ Common.BrowserView {
property bool hidden: false

Behavior on y {
enabled: recentView.visible
NumberAnimation {
from: -chrome.height + invisibleTabChrome.height
duration: UbuntuAnimation.FastDuration
}
}
Expand Down Expand Up @@ -840,7 +849,7 @@ Common.BrowserView {
Connections {
target: browser.currentWebview
onLoadingChanged: {
if (browser.currentWebview.loading) {
if (browser.currentWebview.loading && !recentView.visible) {
chrome.state = "shown"
} else if (browser.currentWebview.isFullScreen) {
chrome.state = "hidden"
Expand Down
62 changes: 26 additions & 36 deletions src/app/webbrowser/BrowserTab.qml
Original file line number Diff line number Diff line change
Expand Up @@ -44,25 +44,19 @@ FocusScope {
readonly property real lastCurrent: internal.lastCurrent
property bool incognito
readonly property bool empty: !url.toString() && !initialUrl.toString() && !restoreState && !request
property bool loadingPreview: false
readonly property size previewSize: webview ? Qt.size(webview.width*Screen.devicePixelRatio,
webview.height*Screen.devicePixelRatio) : Qt.size(0,0)
readonly property size previewThumbnailSize: webview ? Qt.size(webview.width/1.5,
webview.height/1.5) : Qt.size(0,0)
//store reference to preview to avoid clearing by garbage collector
property var previewCache
visible: false

// Used as a workaround for https://launchpad.net/bugs/1502675 :
// invoke this on a tab shortly before it is set current.
signal aboutToShow()

Connections {
target: PreviewManager
onPreviewSaved: {
if (pageUrl !== url) return
if (preview == previewUrl) {
// Ensure that the preview URL actually changes,
// for the image to be reloaded
preview = ""
}
preview = previewUrl
}
}

FaviconFetcher {
id: faviconFetcher
shouldCache: !tab.incognito
Expand Down Expand Up @@ -190,40 +184,36 @@ FocusScope {

internal.hiding = true
webview.grabToImage(function(result) {
if (!internal.hiding) {
return
}
internal.hiding = false
visible = false
preview = result.url
previewCache = result
},previewSize);

//save previews to disk for newtabpage and tab during grabbing
webview.grabToImage(function(result) {
internal.hiding = false
PreviewManager.saveToDisk(result, url)
})
},previewThumbnailSize);
}
}

// Take a capture of the current page shortly after it has finished
// loading to give rendering an opportunity to complete. There is
// unfortunately no signal to notify us when rendering has completed.
Timer {
id: delayedCapture
interval: 500
onTriggered: {
if (webview && current && visible && !internal.hiding) {
Connections {
target: recentView
onVisibleChanged: {
if(visible && current && !empty && !webview.incognito) {
preview = ""
loadingPreview = true
webview.grabToImage(function(result) {
preview = result.url
previewCache = result
},previewSize);

webview.grabToImage(function(result) {
PreviewManager.saveToDisk(result, url)
})
},previewThumbnailSize);
}
}
}
Connections {
target: incognito ? null : webview
// onLoadEvent: {
// if ((event.type == Oxide.LoadEvent.TypeSucceeded) ||
// (event.type == Oxide.LoadEvent.TypeFailed)) {
// delayedCapture.restart()
// }
// }
}

onAboutToShow: {
if (!current) {
Expand Down
169 changes: 78 additions & 91 deletions src/app/webbrowser/TabPreview.qml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@

import QtQuick 2.4
import Ubuntu.Components 1.3
import QtQuick.Controls 2.2 as QQC2

Item {
QQC2.SwipeDelegate {
id: tabPreview

property alias title: chrome.title
Expand All @@ -28,121 +29,107 @@ Item {
property var tab
readonly property url url: tab ? tab.url : ""

// The first preview in the tabs list is a special case.
// Since it’s the current tab, instead of displaying a
// capture, the webview below it is displayed.
property bool showContent: true
background: Rectangle {
color: "transparent"
}
padding: 0
swipe.enabled: true
swipe.behind: Rectangle {
width: tabPreview.width
height: tabPreview.height
color: "transparent"
}

swipe.onCompleted: closed()
onClicked: tabPreview.selected()

signal selected()
signal closed()

TabChrome {
id: chrome

anchors {
top: parent.top
left: parent.left
right: parent.right
}
tabWidth: units.gu(26)

onSelected: tabPreview.selected()
onClosed: tabPreview.closed()
}

Item {
anchors {
top: chrome.bottom
topMargin: units.dp(-1)
left: parent.left
right: parent.right
}
height: parent.height
clip: true
contentItem: Item {

Rectangle {
anchors.fill: parent
color: theme.palette.normal.foreground
visible: showContent
}
TabChrome {
id: chrome

Image {
visible: showContent && !previewContainer.visible
source: "assets/tab-artwork.png"
asynchronous: true
fillMode: Image.PreserveAspectFit
width: parent.width / 5
height: width
anchors {
top: parent.top
left: parent.left
right: parent.right
rightMargin: -width / 5
bottom: parent.bottom
bottomMargin: -height / 10
}
tabWidth: units.gu(26)

onSelected: tabPreview.selected()
onClosed: tabPreview.closed()
}

Rectangle {
Item {
anchors {
top: parent.top
top: chrome.bottom
topMargin: units.dp(-1)
left: parent.left
right: parent.right
}
height: units.dp(1)

color: theme.palette.normal.base
}

Label {
visible: showContent && !previewContainer.visible
text: i18n.tr("Tap to view")
anchors {
centerIn: parent
verticalCenterOffset: units.gu(-2)

visible: !tab.loadingPreview
height: parent.height
clip: true

Rectangle {
anchors.fill: parent
color: theme.palette.normal.foreground
}
}

Image {
id: previewContainer
visible: showContent && source.toString() && (status == Image.Ready)
anchors {
left: parent.left
top: parent.top
topMargin: -chrome.height
Image {
visible: !previewContainer.visible
source: "assets/tab-artwork.png"
asynchronous: true
fillMode: Image.PreserveAspectFit
width: parent.width / 5
height: width
anchors {
right: parent.right
rightMargin: -width / 5
bottom: parent.bottom
bottomMargin: -height / 10
}
}
height: sourceSize.height
fillMode: Image.Pad
source: tabPreview.tab ? tabPreview.tab.preview : ""
asynchronous: true
cache: false
onStatusChanged: {
if (status == Image.Error) {
// The cached preview doesn’t exist any longer
tabPreview.tab.preview = ""

Rectangle {
anchors {
top: parent.top
left: parent.left
right: parent.right
}
height: units.dp(1)

color: theme.palette.normal.base
}
}

MouseArea {
objectName: "selectArea"
anchors.fill: parent
acceptedButtons: Qt.AllButtons

// 'clicked' events are emitted even if the cursor has been dragged
// (http://doc.qt.io/qt-5/qml-qtquick-mousearea.html#clicked-signal),
// but we don’t want a drag gesture to select the tab (when e.g. the
// user has reached the top/bottom of the tabs view and starts another
// gesture to drag further beyond the boundaries of the view).
property point pos
onPressed: {
if (mouse.button == Qt.LeftButton) {
pos = Qt.point(mouse.x, mouse.y)
Label {
visible: !previewContainer.visible
text: i18n.tr("Tap to view")
anchors {
top: parent.top
topMargin: units.gu(12)
horizontalCenter: parent.horizontalCenter
}
}
onReleased: {
if (mouse.button == Qt.LeftButton) {
var d = Math.sqrt(Math.pow(mouse.x - pos.x, 2) + Math.pow(mouse.y - pos.y, 2))
if (d < units.gu(1)) {
tabPreview.selected()

Image {
id: previewContainer
visible: source.toString() && (status == Image.Ready)
anchors.fill: parent
anchors.topMargin: units.dp(1)
verticalAlignment: Image.AlignTop
fillMode: Image.PreserveAspectFit
source: tabPreview.tab ? tabPreview.tab.preview : ""
asynchronous: true
cache: false
onStatusChanged: {
if (status != Image.Loading) {
tab.loadingPreview = false
}
}
}
Expand Down
Loading

0 comments on commit 01b1daf

Please sign in to comment.