Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add extra padding for zoomed in web pages #887

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions Model/Utilities/Javascript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// This file is part of Kiwix for iOS & macOS.
//
// Kiwix is free software; you can redistribute it and/or modify it
// under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 3 of the License, or
// any later version.
//
// Kiwix is distributed in the hope that it will be useful, but
// WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
// General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Kiwix; If not, see https://www.gnu.org/licenses/.

import Foundation

enum CSSUnit: String {
// swiftlint:disable:next identifier_name
case em
}

enum Javascript {
static func webkitPadding(size: UInt8, unit: CSSUnit = .em) -> String {
let paddingTemplate = "document.getElementsByTagName('body')[0].style.webkit%@='%d%@'"
return ["PaddingStart", "PaddingEnd", "PaddingBefore", "PaddingAfter"].map { property in
String(format: paddingTemplate, property, size, unit.rawValue)
}.joined(separator: "; ")
}
}
5 changes: 4 additions & 1 deletion Views/BuildingBlocks/WebView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,11 +74,14 @@ final class WebViewController: UIViewController {
private var layoutCancellable: AnyCancellable?
private var currentScrollViewOffset: CGFloat = 0.0
private var compactViewNavigationController: UINavigationController?

private static let webkitPaddingJS: String = Javascript.webkitPadding(size: 1, unit: .em)

init(webView: WKWebView) {
self.webView = webView
pageZoomObserver = Defaults.observe(.webViewPageZoom) { change in
webView.adjustTextSize(pageZoom: change.newValue)
// adjust padding, as with large zoom in the content is too close to the sides
webView.evaluateJavaScript(Self.webkitPaddingJS, completionHandler: nil)
}
super.init(nibName: nil, bundle: nil)
}
Expand Down