@@ -16,70 +16,57 @@ func restartProcess() {
16
16
}
17
17
18
18
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
+ }
42
35
43
36
@objc func plugin( _: Any ? = nil ) {
44
- FcitxInputController . pluginManager. refreshPlugins ( )
45
- FcitxInputController . pluginManager. showWindow ( nil )
37
+ openWindow ( " plugin " , PluginManager . self)
46
38
}
47
39
48
40
@objc func restart( _: Any ? = nil ) {
49
41
restartProcess ( )
50
42
}
51
43
52
44
@objc func about( _: Any ? = nil ) {
53
- FcitxInputController . fcitxAbout. refresh ( )
54
- FcitxInputController . fcitxAbout. showWindow ( nil )
45
+ openWindow ( " about " , FcitxAboutController . self)
55
46
}
56
47
57
48
@objc func globalConfig( _: Any ? = nil ) {
58
- FcitxInputController . globalConfigController. refresh ( )
59
- FcitxInputController . globalConfigController. showWindow ( nil )
49
+ openWindow ( " global " , GlobalConfigController . self)
60
50
}
61
51
62
52
@objc func inputMethod( _: Any ? = nil ) {
63
- FcitxInputController . inputMethodConfigController. refresh ( )
64
- FcitxInputController . inputMethodConfigController. showWindow ( nil )
53
+ openWindow ( " im " , InputMethodConfigController . self)
65
54
}
66
55
67
56
@objc func themeEditor( _: Any ? = nil ) {
68
- FcitxInputController . themeEditorController. refresh ( )
69
- FcitxInputController . themeEditorController. showWindow ( nil )
57
+ openWindow ( " theme " , ThemeEditorController . self)
70
58
}
71
59
72
60
@objc func advanced( _: Any ? = nil ) {
73
- FcitxInputController . advancedController. refresh ( )
74
- FcitxInputController . advancedController. showWindow ( nil )
61
+ openWindow ( " advanced " , AdvancedController . self)
75
62
}
76
63
}
77
64
78
65
/// All config window controllers should subclass this. It sets up
79
66
/// application states so that the config windows can receive user
80
67
/// input.
81
68
class ConfigWindowController : NSWindowController , NSWindowDelegate , NSToolbarDelegate {
82
- static var numberOfConfigWindows : Int = 0
69
+ var key : String = " "
83
70
84
71
override init ( window: NSWindow ? ) {
85
72
super. init ( window: window)
@@ -94,10 +81,6 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSToolbarDel
94
81
95
82
override func showWindow( _ sender: Any ? = nil ) {
96
83
if let window = window {
97
- if !window. isVisible {
98
- ConfigWindowController . numberOfConfigWindows += 1
99
- }
100
-
101
84
// Switch to normal activation policy so that the config windows
102
85
// can receive key events.
103
86
if NSApp . activationPolicy ( ) != . regular {
@@ -111,12 +94,11 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSToolbarDel
111
94
112
95
func windowShouldClose( _ sender: NSWindow ) -> Bool {
113
96
sender. orderOut ( nil )
114
- ConfigWindowController . numberOfConfigWindows -= 1
115
-
97
+ // Free memory and reset state.
98
+ FcitxInputController . controllers . removeValue ( forKey : key )
116
99
// Switch back.
117
- if ConfigWindowController . numberOfConfigWindows < = 0 {
100
+ if FcitxInputController . controllers . count = = 0 {
118
101
NSApp . setActivationPolicy ( . prohibited)
119
- ConfigWindowController . numberOfConfigWindows = 0
120
102
}
121
103
return false
122
104
}
@@ -163,4 +145,10 @@ class ConfigWindowController: NSWindowController, NSWindowDelegate, NSToolbarDel
163
145
@objc func toggleSidebar( _ sender: Any ? ) {
164
146
// Wow, we don't have to do anything here.
165
147
}
148
+
149
+ func setKey( _ key: String ) {
150
+ self . key = key
151
+ }
152
+
153
+ func refresh( ) { }
166
154
}
0 commit comments