mirror of
https://github.com/coteditor/CotEditor.git
synced 2024-09-11 11:25:57 +03:00
Avoid storeing encoding NSMenuItems
This commit is contained in:
parent
906bc08e0e
commit
6b59ef512c
@ -18,6 +18,7 @@ develop
|
||||
### Fixes
|
||||
|
||||
- Fix an issue find string is not shared to other applications after quitting CotEditor.
|
||||
- [beta] Fix some memory leaks.
|
||||
|
||||
|
||||
|
||||
|
@ -250,7 +250,7 @@ final class DocumentController: NSDocumentController {
|
||||
menu.addItem(autoDetectItem)
|
||||
menu.addItem(NSMenuItem.separator())
|
||||
|
||||
let items = EncodingManager.shared.encodingMenuItems
|
||||
let items = EncodingManager.shared.createEncodingMenuItems()
|
||||
for item in items {
|
||||
menu.addItem(item)
|
||||
}
|
||||
|
@ -44,29 +44,24 @@ extension Notification.Name {
|
||||
|
||||
// MARK:
|
||||
|
||||
private let UTF8Tag = Int(String.Encoding.utf8.rawValue)
|
||||
|
||||
|
||||
final class EncodingManager: NSObject {
|
||||
|
||||
// MARK: Public Properties
|
||||
|
||||
static let shared = EncodingManager()
|
||||
|
||||
|
||||
// MARK: Private Properties
|
||||
|
||||
private let UTF8Tag = Int(String.Encoding.utf8.rawValue)
|
||||
private var _menuItems = [NSMenuItem]()
|
||||
|
||||
|
||||
|
||||
// MARK:
|
||||
// MARK: -
|
||||
// MARK: Lifecycle
|
||||
|
||||
override private init() {
|
||||
|
||||
super.init()
|
||||
|
||||
self.buildEncodingMenuItems()
|
||||
|
||||
UserDefaults.standard.addObserver(self, forKeyPath: DefaultKeys.encodingList.rawValue, context: nil)
|
||||
}
|
||||
|
||||
@ -82,7 +77,10 @@ final class EncodingManager: NSObject {
|
||||
override func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
|
||||
|
||||
if keyPath == DefaultKeys.encodingList.rawValue {
|
||||
self.buildEncodingMenuItems()
|
||||
moof()
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
NotificationCenter.default.post(name: .EncodingListDidUpdate, object: self)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -124,9 +122,19 @@ final class EncodingManager: NSObject {
|
||||
|
||||
|
||||
/// return copied encoding menu items
|
||||
var encodingMenuItems: [NSMenuItem] {
|
||||
func createEncodingMenuItems() -> [NSMenuItem] {
|
||||
|
||||
return self._menuItems.map { $0.copy() as! NSMenuItem }
|
||||
return self.defaultEncodings.map { encoding in
|
||||
guard let encoding = encoding else {
|
||||
return NSMenuItem.separator()
|
||||
}
|
||||
|
||||
let item = NSMenuItem()
|
||||
item.title = String.localizedName(of: encoding)
|
||||
item.tag = Int(encoding.rawValue)
|
||||
|
||||
return item
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@ -135,7 +143,7 @@ final class EncodingManager: NSObject {
|
||||
|
||||
menu.removeAllItems()
|
||||
|
||||
for item in self.encodingMenuItems {
|
||||
for item in self.createEncodingMenuItems() {
|
||||
item.action = #selector(EncodingHolder.changeEncoding)
|
||||
item.target = nil
|
||||
menu.addItem(item)
|
||||
@ -151,29 +159,4 @@ final class EncodingManager: NSObject {
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
// MARK: Private Methods
|
||||
|
||||
/// build encoding menu items
|
||||
private func buildEncodingMenuItems() {
|
||||
|
||||
self._menuItems = self.defaultEncodings.map { encoding in
|
||||
guard let encoding = encoding else {
|
||||
return NSMenuItem.separator()
|
||||
}
|
||||
|
||||
let item = NSMenuItem()
|
||||
item.title = String.localizedName(of: encoding)
|
||||
item.tag = Int(encoding.rawValue)
|
||||
|
||||
return item
|
||||
}
|
||||
|
||||
// notify that new encoding menu items was created
|
||||
DispatchQueue.main.async { [weak self] in
|
||||
NotificationCenter.default.post(name: .EncodingListDidUpdate, object: self)
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -346,7 +346,7 @@ final class FormatPaneController: NSViewController, NSTableViewDelegate {
|
||||
guard let inOpenMenu = self.inOpenEncodingMenu?.menu,
|
||||
let inNewMenu = self.inNewEncodingMenu?.menu else { return }
|
||||
|
||||
let menuItems = EncodingManager.shared.encodingMenuItems
|
||||
let menuItems = EncodingManager.shared.createEncodingMenuItems()
|
||||
|
||||
inOpenMenu.removeAllItems()
|
||||
inNewMenu.removeAllItems()
|
||||
@ -358,7 +358,7 @@ final class FormatPaneController: NSViewController, NSTableViewDelegate {
|
||||
|
||||
let UTF8Int = Int(String.Encoding.utf8.rawValue)
|
||||
for item in menuItems {
|
||||
inOpenMenu.addItem(item.copy() as! NSMenuItem)
|
||||
inOpenMenu.addItem(item)
|
||||
inNewMenu.addItem(item.copy() as! NSMenuItem)
|
||||
|
||||
// add "UTF-8 with BOM" item only to "In New" menu
|
||||
|
Loading…
Reference in New Issue
Block a user