Skip to content

Commit

Permalink
匀速平滑-兼容虚拟机
Browse files Browse the repository at this point in the history
  • Loading branch information
yanquer committed Dec 18, 2024
1 parent 0ae8efb commit cbce6a3
Show file tree
Hide file tree
Showing 15 changed files with 418 additions and 222 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@

# [5.1.0]

- 新增匀速平滑以适配 `parallels` 虚拟机精细滚动. (原来的滚动太长了)

# [5.0.0]

- 支持 MacOS 14, 解决平滑滚动时崩溃问题
Expand Down
490 changes: 273 additions & 217 deletions Mos/Base.lproj/Main.storyboard

Large diffs are not rendered by default.

6 changes: 6 additions & 0 deletions Mos/Options/ExceptionalApplication.swift
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,12 @@ extension ExceptionalApplication {
func getDuration() -> Double {
return inherit ? Options.shared.scrollAdvanced.durationTransition : scrollAdvanced.durationTransition
}

// 匀速平滑
func getUniform() -> Double {
return (inherit ? Options.shared.scrollAdvanced.uniform : scrollAdvanced.uniform) ?? OptionsVal.uniformValD
}

// 功能
func isSmooth(_ block: Bool) -> Bool {
if block { return false }
Expand Down
2 changes: 2 additions & 0 deletions Mos/Options/Options.swift
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ extension Options {
scrollBasic.smooth = UserDefaults.standard.bool(forKey: "smooth")
scrollBasic.reverse = UserDefaults.standard.bool(forKey: "reverse")
// 滚动:高级
scrollAdvanced.uniform = UserDefaults.standard.double(forKey: "uniform")
scrollAdvanced.dash = UserDefaults.standard.integer(forKey: "dash")
scrollAdvanced.toggle = UserDefaults.standard.integer(forKey: "toggle")
scrollAdvanced.block = UserDefaults.standard.integer(forKey: "block")
Expand All @@ -81,6 +82,7 @@ extension Options {
UserDefaults.standard.set(scrollBasic.smooth, forKey:"smooth")
UserDefaults.standard.set(scrollBasic.reverse, forKey:"reverse")
// 滚动:高级
UserDefaults.standard.set(scrollAdvanced.uniform, forKey:"uniform")
UserDefaults.standard.set(scrollAdvanced.dash, forKey:"dash")
UserDefaults.standard.set(scrollAdvanced.toggle, forKey:"toggle")
UserDefaults.standard.set(scrollAdvanced.block, forKey:"block")
Expand Down
5 changes: 4 additions & 1 deletion Mos/ScrollCore/ScrollCore.swift
Original file line number Diff line number Diff line change
Expand Up @@ -63,13 +63,15 @@ class ScrollCore {
enableReverse = false
var step = Options.shared.scrollAdvanced.step,
speed = Options.shared.scrollAdvanced.speed,
duration = Options.shared.scrollAdvanced.durationTransition
duration = Options.shared.scrollAdvanced.durationTransition,
uniform_ = Options.shared.scrollAdvanced.uniform ?? OptionsVal.uniformValD
if let exceptionalApplication = ScrollCore.shared.exceptionalApplication {
enableSmooth = exceptionalApplication.isSmooth(ScrollCore.shared.blockSmooth)
enableReverse = exceptionalApplication.isReverse()
step = exceptionalApplication.getStep()
speed = exceptionalApplication.getSpeed()
duration = exceptionalApplication.getDuration()
uniform_ = exceptionalApplication.getUniform()
} else if !Options.shared.general.allowlist {
enableSmooth = Options.shared.scrollBasic.smooth && !ScrollCore.shared.blockSmooth
enableReverse = Options.shared.scrollBasic.reverse
Expand Down Expand Up @@ -121,6 +123,7 @@ class ScrollCore {
y: scrollEvent.Y.usableValue,
x: scrollEvent.X.usableValue,
speed: speed,
uniform_: uniform_,
amplification: ScrollCore.shared.dashAmplification
).tryStart()
}
Expand Down
56 changes: 52 additions & 4 deletions Mos/ScrollCore/ScrollPoster.swift
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class ScrollPoster {
private var current = (y: 0.0, x: 0.0) // 当前滚动距离
private var delta = (y: 0.0, x: 0.0) // 滚动方向记录
private var buffer = (y: 0.0, x: 0.0) // 滚动缓冲距离
// 匀速平滑
private var uniformVal = OptionsVal.uniformValD
// 滚动配置
private var shifting = false
private var duration = Options.shared.scrollAdvanced.durationTransition
Expand All @@ -35,7 +37,8 @@ class ScrollPoster {
// MARK: - 滚动数据更新控制
@available(macOS 14.0, *)
extension ScrollPoster {
func update(event: CGEvent, proxy: CGEventTapProxy, duration: Double, y: Double, x: Double, speed: Double, amplification: Double = 1) -> Self {
func update(event: CGEvent, proxy: CGEventTapProxy, duration: Double, y: Double, x: Double, speed: Double, uniform_: Double, amplification: Double = 1) -> Self {
uniformVal = uniform_
// 更新依赖数据
ref.event = event
ref.proxy = proxy
Expand Down Expand Up @@ -94,7 +97,7 @@ extension ScrollPoster {
extension ScrollPoster {

func createDisplayLink() {
NSLog("poster?.isPaused: \(String(describing: poster?.isPaused))")
// NSLog("poster?.isPaused: \(String(describing: poster?.isPaused))")
// 还在就不用创建, isPaused 判断存在问题, 先不用, 改为每次都用新的
// if !(poster?.isPaused ?? true) {
// return
Expand All @@ -118,7 +121,7 @@ extension ScrollPoster {

@objc func step(displaylink: CADisplayLink) {
if canRun{
print(displaylink.targetTimestamp)
// NSLog("displaylink.targetTimestamp: \(displaylink.targetTimestamp)")
poster = displaylink
ScrollPoster.shared.processing()
}
Expand Down Expand Up @@ -158,8 +161,53 @@ extension ScrollPoster {
// MARK: - 数据处理及发送
@available(macOS 14.0, *)
private extension ScrollPoster {

// 处理滚动事件
func processing(){
if (uniformVal != OptionsVal.uniformValD){
uniformProssing()
} else {
learpProcessing()
}
}

// 匀速滚动, 使用界面给的 Uniform Key 值作为步长
func uniformProssing() {

let uniformStep = uniformVal

let xDerection = buffer.x > 0 ? 1.0 : (buffer.x == 0 ? 0:-1.0),
yDreaction = buffer.y > 0 ? 1.0 : (buffer.y == 0 ? 0:-1.0)

// 更新滚动位置
current = (
y: current.y + uniformStep * yDreaction,
x: current.x + uniformStep * xDerection
)
// 需要滚动的位置
let filledValue = (
y: uniformStep * yDreaction,
x: uniformStep * xDerection
)
// 变换滚动结果
let shiftedValue = shift(with: filledValue)

// NSLog("uniformProssing run shiftedValue \(shiftedValue) \n buffer \(buffer) \n current \(current) \n precision: \(Options.shared.scrollAdvanced.precision)")
// 发送滚动结果
post(ref, shiftedValue)

// 临近目标距离 暂停滚动
if (
abs(current.y) >= abs(buffer.y) &&
abs(current.x) >= abs(buffer.x)
) {
NSLog("uniformProssing stop with uniformStep \(uniformStep)")
stop(Phase.PauseManual)
}
}

// 处理滚动事件
func processing() {
func learpProcessing() {
// 计算插值
let frame = (
y: Interpolator.lerp(src: current.y, dest: buffer.y, trans: duration),
Expand Down
10 changes: 10 additions & 0 deletions Mos/Utils/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -100,8 +100,18 @@ class OPTIONS_SCROLL_BASIC_DEFAULT: Codable {
didSet {Options.shared.saveOptions()}
}
}

struct OptionsVal{
static let uniformValD = 0.0
}

// 滚动参数
class OPTIONS_SCROLL_ADVANCED_DEFAULT: Codable {
// 匀速平滑
var uniform: Double? = OptionsVal.uniformValD {
didSet {Options.shared.saveOptions()}
}

// 高级
var dash:Int? = 0 {
didSet {Options.shared.saveOptions()}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,11 @@ class PreferencesAdvanceViewController: NSViewController {
@IBOutlet weak var dashKeyPopUpButton: NSPopUpButton!
@IBOutlet weak var toggleKeyPopUpButton: NSPopUpButton!
@IBOutlet weak var disableKeyPopUpButton: NSPopUpButton!

// 匀速平滑
@IBOutlet weak var scrollUniformInput: NSTextField!
@IBOutlet weak var scrollUniformStepper: NSStepper!

@IBOutlet weak var scrollStepSlider: NSSlider!
@IBOutlet weak var scrollStepInput: NSTextField!
@IBOutlet weak var scrollStepStepper: NSStepper!
Expand All @@ -32,13 +37,30 @@ class PreferencesAdvanceViewController: NSViewController {

override func viewDidLoad() {
// 禁止自动 Focus
scrollUniformInput.refusesFirstResponder = true // 设置以保证 input 行为正常, 不然不知为何 input 某些时候总有个 10 的最小值无法更改
scrollStepInput.refusesFirstResponder = true
scrollSpeedInput.refusesFirstResponder = true
scrollDurationInput.refusesFirstResponder = true
// 读取设置
syncViewWithOptions()
}


// 匀速平滑
@IBAction func scrollUniformInputChange(_ sender: NSTextField) {
NSLog("scrollUniformInputChange val: \(sender.doubleValue)")
setScrollUniform(value: sender.doubleValue)
}

@IBAction func scrollUniformStepperChange(_ sender: NSStepper) {
NSLog("scrollUniformStepperChange val: \(sender.doubleValue)")
setScrollUniform(value: sender.doubleValue)
}
func setScrollUniform(value: Double) {
getTargetApplicationScrollOptions().uniform = value
syncViewWithOptions()
}

// 加速
@IBAction func dashKeyPopUpButtonChange(_ sender: NSPopUpButton) {
let index = sender.indexOfSelectedItem
Expand Down Expand Up @@ -130,6 +152,20 @@ extension PreferencesAdvanceViewController {
dashKeyPopUpButton.selectItem(at: 0)
}
dashKeyPopUpButton.isEnabled = enabled

// 匀速平滑
let uniform = scroll.uniform ?? 0.0
let strVal = String(format: "%.2f", uniform)
// NSLog("syncViewWithOptions uniform: \(strVal)")

scrollUniformStepper.doubleValue = uniform
scrollUniformStepper.isEnabled = enabled
// NSLog("syncViewWithOptions uniform last scrollUniformInput.stringValue: \(scrollUniformInput.stringValue)")
scrollUniformInput.stringValue = strVal
scrollUniformInput.isEnabled = enabled
// NSLog("syncViewWithOptions uniform current scrollUniformInput.stringValue: \(scrollUniformInput.stringValue)")
// NSLog("==== syncViewWithOptions ====")

// 转换
if let index = MODIFIER_KEY_SET.all.codes.firstIndex(of: CGKeyCode(scroll.toggle ?? 0)) {
toggleKeyPopUpButton.selectItem(at: index+PopUpButtonPadding)
Expand Down
2 changes: 2 additions & 0 deletions Mos/en.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"cIk-j1-vYt.title" = "You need to re-run Mos from Finder-Application instead of the launch pad. (The launch pad will automatically ignore the launched app)";
// Advanced
"iW5-CD-5OC.title" = "Advanced";
"R1W-2Q-Veo.title" = "Uniform Key:";
"r1k-PC-wcP.title" = "Scrolling uniform step which enabled on smooth.
0 is disabled";
"hOO-M8-I1z.title" = "Dash Key:";
"Vhz-Rn-lcU.title" = "Disabled";
"Z59-tN-QjE.title" = "Increase scrolling speed on long pages";
Expand Down
2 changes: 2 additions & 0 deletions Mos/zh-Hans.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"cIk-j1-vYt.title" = "请从 访达-应用程序 中再次启动 Mos。(启动台会自动忽略已启动的应用程序)";
// Advanced
"iW5-CD-5OC.title" = "高级";
"R1W-2Q-Veo.title" = "匀速平滑:";
"r1k-PC-wcP.title" = "当平滑可用时匀速滚动步长.
默认 0 表示不可用匀速";
"hOO-M8-I1z.title" = "加速键:";
"Vhz-Rn-lcU.title" = "不使用";
"Z59-tN-QjE.title" = "加快在长页面时的滚动速度";
Expand Down
2 changes: 2 additions & 0 deletions Mos/zh-Hant-HK.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@
"cIk-j1-vYt.title" = "您需要從 Finder 中的「應用程式」而不是從「啟動台」重新執行 Mos。(「啟動台」會自動忽略已啟動的應用程式)";
// Advanced
"iW5-CD-5OC.title" = "進階";
"n6Q-AG-UsN.title" = "均速順暢:";
"XPg-0N-vsL.title" = "當滑動功能可用時,就會以均速進行滾動步长.
預設 0 表示無法使用均速";
"hOO-M8-I1z.title" = "加速鍵:";
"Vhz-Rn-lcU.title" = "停用";
"Z59-tN-QjE.title" = "加快在長頁面的捲動速度";
Expand Down
2 changes: 2 additions & 0 deletions Mos/zh-Hant-TW.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"cIk-j1-vYt.title" = "您需要從 Finder 中的「應用程式」而不是從「啟動台」重新執行 Mos。(「啟動台」會自動忽略已啟動的應用程式)";
// Advanced
"iW5-CD-5OC.title" = "進階";
"R1W-2Q-Veo.title" = "均速順暢:";
"r1k-PC-wcP.title" = "當滑動功能可用時,就會以均速進行滾動步长.
預設 0 表示無法使用均速";
"hOO-M8-I1z.title" = "加速鍵:";
"Vhz-Rn-lcU.title" = "停用";
"Z59-tN-QjE.title" = "加快在長頁面的捲動速度";
Expand Down
2 changes: 2 additions & 0 deletions Mos/zh-Hant.lproj/Main.strings
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@
"cIk-j1-vYt.title" = "您需要從 Finder 中的「應用程式」而不是從「啟動台」重新執行 Mos。(「啟動台」會自動忽略已啟動的應用程式)";
// Advanced
"iW5-CD-5OC.title" = "進階";
"R1W-2Q-Veo.title" = "均速順暢:";
"r1k-PC-wcP.title" = "當滑動功能可用時,就會以均速進行滾動步长.
預設 0 表示無法使用均速";
"hOO-M8-I1z.title" = "加速鍵:";
"Vhz-Rn-lcU.title" = "停用";
"Z59-tN-QjE.title" = "加快在長頁面的捲動速度";
Expand Down
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Mos New


原作者没维护了, 拉下来看了下.

- 解决了崩溃问题,
- 新增 `匀速平滑` 兼容虚拟机 `parallels` 滚动太长

[匀速平滑](docs/resources/image/uniform-scroll.jpg)

匀速滚动的值即为匀速单步长度. 0 表示默认不使用匀速.

目前只测试了在虚拟机中的行为.




下面是原来的


<p align="center">
<a href="http://mos.caldis.me/">
<img width="320" src="https://github.com/Caldis/Mos/blob/master/docs/resources/image/intro.png?raw=true">
Expand Down
Binary file added docs/resources/image/uniform-scroll.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit cbce6a3

Please sign in to comment.