Fix a memory leak in OptionalMenu

This commit is contained in:
1024jp 2024-06-02 15:03:04 +09:00
parent dc072dee95
commit a679eb0b50
2 changed files with 12 additions and 6 deletions

View File

@ -3,6 +3,10 @@
4.8.6 (unreleased)
--------------------------
### Fixes
- Fix a trivial memory leak in the line ending menu (thanks to Yoshimasa Niwa).
4.8.5 (653)

View File

@ -8,7 +8,7 @@
//
// ---------------------------------------------------------------------------
//
// © 2022-2023 1024jp
// © 2022-2024 1024jp
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
@ -59,9 +59,11 @@ final class OptionalMenu: NSMenu, NSMenuDelegate {
func menuWillOpen(_ menu: NSMenu) {
self.update() // UI validation is performed here
self.validateKeyEvent(force: true)
self.validateKeyEvent(forcibly: true)
let timer = Timer.scheduledTimer(timeInterval: 0.1, target: self, selector: #selector(validateKeyEvent), userInfo: nil, repeats: true)
let timer = Timer.scheduledTimer(withTimeInterval: 0.1, repeats: true) { [weak self] _ in
self?.validateKeyEvent()
}
RunLoop.current.add(timer, forMode: .eventTracking)
self.trackingTimer = timer
}
@ -78,12 +80,12 @@ final class OptionalMenu: NSMenu, NSMenuDelegate {
/// Checks the state of the modifier key press and update the item visibility.
///
/// - Parameter force: Whether forcing to update the item visibility.
@objc private func validateKeyEvent(force: Bool = false) {
/// - Parameter forcibly: Whether forcing to update the item visibility.
@objc private func validateKeyEvent(forcibly: Bool = false) {
let shows = NSEvent.modifierFlags.contains(.option)
guard force || shows != self.isShowingOptionalItems else { return }
guard forcibly || shows != self.isShowingOptionalItems else { return }
self.updateOptionalItems(shows: shows)
}