Skip to content

Commit f50a404

Browse files
authored
refactor window management: free memory and reset state (#267)
1 parent 35f1471 commit f50a404

File tree

7 files changed

+41
-53
lines changed

7 files changed

+41
-53
lines changed

src/config/about.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class FcitxAboutController: ConfigWindowController {
429429
window.contentView = NSHostingView(rootView: view)
430430
}
431431

432-
func refresh() {
432+
override func refresh() {
433433
view.refresh()
434434
}
435435
}

src/config/advanced.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class AdvancedController: ConfigWindowController {
1818
attachToolbar(window)
1919
}
2020

21-
func refresh() {
21+
override func refresh() {
2222
view.refresh()
2323
}
2424
}
@@ -79,7 +79,7 @@ struct AdvancedView: View {
7979
Fcitx.setConfig("fcitx://config/addon/\(selected)", config.encodeValue())
8080
},
8181
close: {
82-
FcitxInputController.advancedController.window?.performClose(nil)
82+
FcitxInputController.closeWindow("advanced")
8383
})
8484
}
8585
}

src/config/globalconfig.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class GlobalConfigController: ConfigWindowController {
1616
attachToolbar(window)
1717
}
1818

19-
func refresh() {
19+
override func refresh() {
2020
view.refresh()
2121
}
2222
}

src/config/inputmethod.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class InputMethodConfigController: ConfigWindowController {
1818
attachToolbar(window)
1919
}
2020

21-
func refresh() {
21+
override func refresh() {
2222
view.refresh()
2323
}
2424
}
@@ -148,7 +148,7 @@ struct InputMethodConfigView: View {
148148
save(configModel)
149149
},
150150
close: {
151-
FcitxInputController.inputMethodConfigController.window?.performClose(_: nil)
151+
FcitxInputController.closeWindow("im")
152152
})
153153
}.padding([.top], 1) // Cannot be 0 otherwise content overlaps with title bar.
154154
} else if let errorMsg = viewModel.errorMsg {

src/config/menu.swift

Lines changed: 32 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -16,70 +16,57 @@ func restartProcess() {
1616
}
1717

1818
extension FcitxInputController {
19-
static var fcitxAbout: FcitxAboutController = {
20-
return FcitxAboutController()
21-
}()
22-
static var pluginManager: PluginManager = {
23-
return PluginManager()
24-
}()
25-
static var globalConfigController: GlobalConfigController = {
26-
return GlobalConfigController()
27-
}()
28-
static var inputMethodConfigController: InputMethodConfigController = {
29-
return InputMethodConfigController()
30-
}()
31-
static var themeEditorController: ThemeEditorController = {
32-
return ThemeEditorController()
33-
}()
34-
static var advancedController: AdvancedController = {
35-
return AdvancedController()
36-
}()
37-
38-
static var controllers = [
39-
"global": globalConfigController,
40-
"theme": themeEditorController,
41-
]
19+
static var controllers = [String: ConfigWindowController]()
20+
21+
func openWindow(_ key: String, _ type: ConfigWindowController.Type) {
22+
var controller = FcitxInputController.controllers[key]
23+
if controller == nil {
24+
controller = type.init()
25+
controller?.setKey(key)
26+
FcitxInputController.controllers[key] = controller
27+
}
28+
controller?.refresh()
29+
controller?.showWindow(nil)
30+
}
31+
32+
static func closeWindow(_ key: String) {
33+
FcitxInputController.controllers[key]?.window?.performClose(nil)
34+
}
4235

4336
@objc func plugin(_: Any? = nil) {
44-
FcitxInputController.pluginManager.refreshPlugins()
45-
FcitxInputController.pluginManager.showWindow(nil)
37+
openWindow("plugin", PluginManager.self)
4638
}
4739

4840
@objc func restart(_: Any? = nil) {
4941
restartProcess()
5042
}
5143

5244
@objc func about(_: Any? = nil) {
53-
FcitxInputController.fcitxAbout.refresh()
54-
FcitxInputController.fcitxAbout.showWindow(nil)
45+
openWindow("about", FcitxAboutController.self)
5546
}
5647

5748
@objc func globalConfig(_: Any? = nil) {
58-
FcitxInputController.globalConfigController.refresh()
59-
FcitxInputController.globalConfigController.showWindow(nil)
49+
openWindow("global", GlobalConfigController.self)
6050
}
6151

6252
@objc func inputMethod(_: Any? = nil) {
63-
FcitxInputController.inputMethodConfigController.refresh()
64-
FcitxInputController.inputMethodConfigController.showWindow(nil)
53+
openWindow("im", InputMethodConfigController.self)
6554
}
6655

6756
@objc func themeEditor(_: Any? = nil) {
68-
FcitxInputController.themeEditorController.refresh()
69-
FcitxInputController.themeEditorController.showWindow(nil)
57+
openWindow("theme", ThemeEditorController.self)
7058
}
7159

7260
@objc func advanced(_: Any? = nil) {
73-
FcitxInputController.advancedController.refresh()
74-
FcitxInputController.advancedController.showWindow(nil)
61+
openWindow("advanced", AdvancedController.self)
7562
}
7663
}
7764

7865
/// All config window controllers should subclass this. It sets up
7966
/// application states so that the config windows can receive user
8067
/// input.
8168
class ConfigWindowController: NSWindowController, NSWindowDelegate, NSToolbarDelegate {
82-
static var numberOfConfigWindows: Int = 0
69+
var key: String = ""
8370

8471
override init(window: NSWindow?) {
8572
super.init(window: window)
@@ -94,10 +81,6 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSToolbarDel
9481

9582
override func showWindow(_ sender: Any? = nil) {
9683
if let window = window {
97-
if !window.isVisible {
98-
ConfigWindowController.numberOfConfigWindows += 1
99-
}
100-
10184
// Switch to normal activation policy so that the config windows
10285
// can receive key events.
10386
if NSApp.activationPolicy() != .regular {
@@ -111,12 +94,11 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSToolbarDel
11194

11295
func windowShouldClose(_ sender: NSWindow) -> Bool {
11396
sender.orderOut(nil)
114-
ConfigWindowController.numberOfConfigWindows -= 1
115-
97+
// Free memory and reset state.
98+
FcitxInputController.controllers.removeValue(forKey: key)
11699
// Switch back.
117-
if ConfigWindowController.numberOfConfigWindows <= 0 {
100+
if FcitxInputController.controllers.count == 0 {
118101
NSApp.setActivationPolicy(.prohibited)
119-
ConfigWindowController.numberOfConfigWindows = 0
120102
}
121103
return false
122104
}
@@ -163,4 +145,10 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSToolbarDel
163145
@objc func toggleSidebar(_ sender: Any?) {
164146
// Wow, we don't have to do anything here.
165147
}
148+
149+
func setKey(_ key: String) {
150+
self.key = key
151+
}
152+
153+
func refresh() {}
166154
}

src/config/plugin.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ struct PluginView: View {
305305
}
306306

307307
private func restart() {
308-
FcitxInputController.pluginManager.window?.performClose(_: nil)
308+
FcitxInputController.closeWindow("plugin")
309309
DispatchQueue.main.async {
310310
restartProcess()
311311
}
@@ -482,7 +482,7 @@ class PluginManager: ConfigWindowController {
482482
window.contentView = NSHostingView(rootView: view)
483483
}
484484

485-
func refreshPlugins() {
485+
override func refresh() {
486486
view.refreshPlugins()
487487
}
488488
}

src/config/themeeditor.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ThemeEditorController: ConfigWindowController {
1616
attachToolbar(window)
1717
}
1818

19-
func refresh() {
19+
override func refresh() {
2020
view.refresh()
2121
}
2222
}

0 commit comments

Comments
 (0)