Fix navigation on macOS.

This commit is contained in:
John Preston 2024-02-15 13:48:49 +04:00
parent 8b62c37c34
commit 5f3c380d56
2 changed files with 34 additions and 10 deletions

View file

@ -93,13 +93,16 @@ var IV = {
}, },
lastScrollTop: 0, lastScrollTop: 0,
frameScrolled: function (e) { frameScrolled: function (e) {
const now = document.documentElement.scrollTop; const was = IV.lastScrollTop;
if (now < 100) { IV.lastScrollTop = IV.findPageScroll().scrollTop;
IV.updateJumpToTop(was < IV.lastScrollTop);
},
updateJumpToTop: function (scrolledDown) {
if (IV.lastScrollTop < 100) {
document.getElementById('bottom_up').classList.add('hidden'); document.getElementById('bottom_up').classList.add('hidden');
} else if (now > IV.lastScrollTop && now > 200) { } else if (scrolledDown && IV.lastScrollTop > 200) {
document.getElementById('bottom_up').classList.remove('hidden'); document.getElementById('bottom_up').classList.remove('hidden');
} }
IV.lastScrollTop = now;
}, },
updateStyles: function (styles) { updateStyles: function (styles) {
if (IV.styles !== styles) { if (IV.styles !== styles) {
@ -221,7 +224,13 @@ var IV = {
} }
}, },
init: function () { init: function () {
IV.platform = window.navigator.platform.toLowerCase();
IV.mac = IV.platform.startsWith('mac');
IV.win = IV.platform.startsWith('win');
window.history.replaceState(IV.computeCurrentState(), ''); window.history.replaceState(IV.computeCurrentState(), '');
IV.lastScrollTop = window.history.state.scroll;
IV.findPageScroll().onscroll = IV.frameScrolled;
const buttons = document.getElementsByClassName('fixed_button'); const buttons = document.getElementsByClassName('fixed_button');
for (let i = 0; i < buttons.length; ++i) { for (let i = 0; i < buttons.length; ++i) {
@ -287,7 +296,9 @@ var IV = {
}, 3000); }, 3000);
}, },
scrollTo: function (y, instant) { scrollTo: function (y, instant) {
document.getElementById('bottom_up').classList.add('hidden'); if (y < 200) {
document.getElementById('bottom_up').classList.add('hidden');
}
IV.findPageScroll().scrollTo({ IV.findPageScroll().scrollTo({
top: y || 0, top: y || 0,
behavior: instant ? 'instant' : 'smooth' behavior: instant ? 'instant' : 'smooth'
@ -348,6 +359,7 @@ var IV = {
el.innerHTML = '<div class="page-slide"><article>' el.innerHTML = '<div class="page-slide"><article>'
+ data.html + data.html
+ '</article></div>'; + '</article></div>';
el.onscroll = IV.frameScrolled;
IV.cache[index].dom = el; IV.cache[index].dom = el;
IV.navigateToDOM(index, hash); IV.navigateToDOM(index, hash);
@ -357,7 +369,7 @@ var IV = {
navigateToDOM: function (index, hash) { navigateToDOM: function (index, hash) {
IV.pending = null; IV.pending = null;
if (IV.index == index) { if (IV.index == index) {
IV.jumpToHash(hash); IV.jumpToHash(hash, IV.mac);
return; return;
} }
window.history.replaceState(IV.computeCurrentState(), ''); window.history.replaceState(IV.computeCurrentState(), '');
@ -429,11 +441,16 @@ var IV = {
IV.initMedia(); IV.initMedia();
if (scroll === undefined) { if (scroll === undefined) {
IV.jumpToHash(hash, true); IV.jumpToHash(hash, true);
} else {
IV.lastScrollTop = scroll;
IV.updateJumpToTop(true);
} }
} else if (scroll !== undefined) { } else if (scroll !== undefined) {
IV.scrollTo(scroll); IV.scrollTo(scroll, IV.mac);
IV.lastScrollTop = scroll;
IV.updateJumpToTop(true);
} else { } else {
IV.jumpToHash(hash); IV.jumpToHash(hash, IV.mac);
} }
}, },
back: function () { back: function () {
@ -449,7 +466,6 @@ document.onclick = IV.frameClickHandler;
document.onkeydown = IV.frameKeyDown; document.onkeydown = IV.frameKeyDown;
document.onmouseenter = IV.frameMouseEnter; document.onmouseenter = IV.frameMouseEnter;
document.onmouseup = IV.frameMouseUp; document.onmouseup = IV.frameMouseUp;
document.onscroll = IV.frameScrolled;
window.onmessage = IV.postMessageHandler; window.onmessage = IV.postMessageHandler;
window.addEventListener('popstate', function (e) { window.addEventListener('popstate', function (e) {

View file

@ -204,7 +204,9 @@ Controller::Controller()
} }
Controller::~Controller() { Controller::~Controller() {
_window->hide(); if (_window) {
_window->hide();
}
_ready = false; _ready = false;
_webview = nullptr; _webview = nullptr;
_title = nullptr; _title = nullptr;
@ -424,6 +426,10 @@ void Controller::createWebview(const QString &dataPath) {
}); });
}); });
raw->setDataRequestHandler([=](Webview::DataRequest request) { raw->setDataRequestHandler([=](Webview::DataRequest request) {
const auto pos = request.id.find('#');
if (pos != request.id.npos) {
request.id = request.id.substr(0, pos);
}
if (!request.id.starts_with("iv/")) { if (!request.id.starts_with("iv/")) {
_dataRequests.fire(std::move(request)); _dataRequests.fire(std::move(request));
return Webview::DataResult::Pending; return Webview::DataResult::Pending;
@ -520,11 +526,13 @@ void Controller::showInWindow(const QString &dataPath, Prepared page) {
} }
} else if (_ready) { } else if (_ready) {
_webview->eval(navigateScript(index, hash)); _webview->eval(navigateScript(index, hash));
_window->raise();
_window->activateWindow(); _window->activateWindow();
_window->setFocus(); _window->setFocus();
} else { } else {
_navigateToIndexWhenReady = index; _navigateToIndexWhenReady = index;
_navigateToHashWhenReady = hash; _navigateToHashWhenReady = hash;
_window->raise();
_window->activateWindow(); _window->activateWindow();
_window->setFocus(); _window->setFocus();
} }