Skip to content

Commit

Permalink
Add extra padding for zoomed in
Browse files Browse the repository at this point in the history
  • Loading branch information
BPerlakiH committed Aug 4, 2024
1 parent 7d4b5b7 commit 47c5f4d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
29 changes: 29 additions & 0 deletions Model/Utilities/Javascript.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// 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 {
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

0 comments on commit 47c5f4d

Please sign in to comment.