Skip to content

Commit

Permalink
Feature: add registerExtenalJSBridgeFunction bridge
Browse files Browse the repository at this point in the history
  • Loading branch information
yichengchen committed Oct 27, 2018
1 parent 981ff51 commit a7a0533
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 15 deletions.
4 changes: 2 additions & 2 deletions ClashX/Models/ClashRule.swift
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ import Foundation
class ClashRule:Codable {

let type:String
let payload:String
let proxy:String
let payload:String?
let proxy:String?
}

class ClashRuleResponse:Codable {
Expand Down
44 changes: 31 additions & 13 deletions ClashX/ViewControllers/ClashWebViewContoller.swift
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ import RxSwift
import RxCocoa

class ClashWebViewContoller: NSViewController {
let webview: WKWebView = CustomWKWebView()
let webview: CustomWKWebView = CustomWKWebView()
var bridge:WebViewJavascriptBridge?
var disposeBag = DisposeBag()

Expand All @@ -27,15 +27,15 @@ class ClashWebViewContoller: NSViewController {
super.viewDidLoad()
webview.uiDelegate = self
webview.navigationDelegate = self

if #available(OSX 10.11, *) {
webview.customUserAgent = "ClashX Runtime"
} else {
// Fallback on earlier versions
}
if NSAppKitVersion.current.rawValue > 1500 {
webview.setValue(false, forKey: "drawsBackground")
}
else {
} else {
webview.setValue(true, forKey: "drawsTransparentBackground")
}
view.addSubview(webview)
Expand All @@ -56,6 +56,7 @@ class ClashWebViewContoller: NSViewController {
}

bridge = JsBridgeHelper.initJSbridge(webview: webview, delegate: self)
registerExtenalJSBridgeFunction()

webview.configuration.preferences.setValue(true, forKey: "developerExtrasEnabled")

Expand All @@ -71,21 +72,32 @@ class ClashWebViewContoller: NSViewController {

override func viewWillAppear() {
super.viewWillAppear()
self.view.window?.titleVisibility = .hidden
self.view.window?.titlebarAppearsTransparent = true
self.view.window?.styleMask.insert(.fullSizeContentView)
view.window?.titleVisibility = .hidden
view.window?.titlebarAppearsTransparent = true
view.window?.styleMask.insert(.fullSizeContentView)

NSApp.activate(ignoringOtherApps: true)
self.view.window?.makeKeyAndOrderFront(self)

self.view.window?.isOpaque = false
self.view.window?.backgroundColor = NSColor.clear
view.window?.makeKeyAndOrderFront(self)


view.window?.isOpaque = false
view.window?.backgroundColor = NSColor.clear
view.window?.styleMask.remove(.resizable)
view.window?.styleMask.remove(.miniaturizable)
}

}

extension ClashWebViewContoller {
func registerExtenalJSBridgeFunction(){
self.bridge?.registerHandler("setDragAreaHeight") {(anydata, responseCallback) in
if let height = anydata as? CGFloat {
self.webview.dragableAreaHeight = height;
}
responseCallback?(nil)
}
}
}

extension ClashWebViewContoller:WKUIDelegate,WKNavigationDelegate {
func webViewWebContentProcessDidTerminate(_ webView: WKWebView) {
}
Expand All @@ -111,18 +123,24 @@ extension ClashWebViewContoller:WKUIDelegate,WKNavigationDelegate {
}
return nil;
}

}


class CustomWKWebView: WKWebView {

var dragableAreaHeight:CGFloat = 20;
let alwaysDragableLeftAreaWidth:CGFloat = 150;

override func mouseDown(with event: NSEvent) {
super.mouseDown(with: event)
let x = event.locationInWindow.x
let y = (self.window?.frame.size.height ?? 0) - event.locationInWindow.y
if y<20 {

if x < alwaysDragableLeftAreaWidth || y < dragableAreaHeight {
if #available(OSX 10.11, *) {
self.window?.performDrag(with: event)
}
}

}
}

0 comments on commit a7a0533

Please sign in to comment.