From e4205979a6354d7c7985290dae38466343801ecb Mon Sep 17 00:00:00 2001 From: Tisfeng Date: Sat, 2 Mar 2024 21:32:06 +0800 Subject: [PATCH] fix: font size index out of bounds (#438) --- Easydict/Feature/Configuration/Configuration.swift | 3 ++- Easydict/NewApp/Feature/Shortcut/Shortcut+Bind.swift | 8 ++++++-- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/Easydict/Feature/Configuration/Configuration.swift b/Easydict/Feature/Configuration/Configuration.swift index 27c70d935..9e4d0f3bc 100644 --- a/Easydict/Feature/Configuration/Configuration.swift +++ b/Easydict/Feature/Configuration/Configuration.swift @@ -140,7 +140,8 @@ class Configuration: NSObject { } var fontSizeRatio: CGFloat { - fontSizes[Int(fontSizeIndex)] + let safeIndex = max(0, min(Int(fontSizeIndex), fontSizes.count - 1)) + return fontSizes[safeIndex] } static func destroySharedInstance() { diff --git a/Easydict/NewApp/Feature/Shortcut/Shortcut+Bind.swift b/Easydict/NewApp/Feature/Shortcut/Shortcut+Bind.swift index b999f66a6..60ba241c5 100644 --- a/Easydict/NewApp/Feature/Shortcut/Shortcut+Bind.swift +++ b/Easydict/NewApp/Feature/Shortcut/Shortcut+Bind.swift @@ -60,12 +60,16 @@ extension Shortcut { @objc func increaseFontSize() { - Configuration.shared.fontSizeIndex += 1 + if Configuration.shared.fontSizeIndex < Configuration.shared.fontSizes.count - 1 { + Configuration.shared.fontSizeIndex += 1 + } } @objc func decreaseFontSize() { - Configuration.shared.fontSizeIndex -= 1 + if Configuration.shared.fontSizeIndex > 0 { + Configuration.shared.fontSizeIndex -= 1 + } } @objc