Skip to content

Commit b8d8954

Browse files
committed
refactor: add setup helper for window initialization
1 parent 305123a commit b8d8954

File tree

1 file changed

+34
-58
lines changed

1 file changed

+34
-58
lines changed

azooKeyMac/InputController/azooKeyMacInputController.swift

Lines changed: 34 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,21 @@ class azooKeyMacInputController: IMKInputController, NSMenuItemValidation { // s
3434
// ダブルタップ検出用
3535
private var lastKey: (time: TimeInterval, code: UInt16) = (0, 0)
3636
private static let doubleTapInterval: TimeInterval = 0.5
37+
private static let candidateWindowInitialSize = CGSize(width: 400, height: 1000)
38+
39+
private static func makeCandidateWindow(contentViewController: NSViewController, inputClient: IMKTextInput?) -> NSWindow {
40+
let window = NSWindow(contentViewController: contentViewController)
41+
window.styleMask = [.borderless]
42+
window.level = .popUpMenu
43+
44+
var rect: NSRect = .zero
45+
inputClient?.attributes(forCharacterIndex: 0, lineHeightRectangle: &rect)
46+
rect.size = candidateWindowInitialSize
47+
window.setFrame(rect, display: true)
48+
window.setIsVisible(false)
49+
window.orderOut(nil)
50+
return window
51+
}
3752

3853
// MARK: - ダブルタップ検出
3954
private func checkAndUpdateDoubleTap(keyCode: UInt16) -> Bool {
@@ -65,48 +80,28 @@ class azooKeyMacInputController: IMKInputController, NSMenuItemValidation { // s
6580
self.liveConversionToggleMenuItem = NSMenuItem()
6681
self.transformSelectedTextMenuItem = NSMenuItem()
6782

68-
// Initialize the candidates window
69-
self.candidatesViewController = CandidatesViewController()
70-
self.candidatesWindow = NSWindow(contentViewController: self.candidatesViewController)
71-
self.candidatesWindow.styleMask = [.borderless]
72-
self.candidatesWindow.level = .popUpMenu
73-
74-
var rect: NSRect = .zero
75-
if let client = inputClient as? IMKTextInput {
76-
client.attributes(forCharacterIndex: 0, lineHeightRectangle: &rect)
77-
}
78-
rect.size = .init(width: 400, height: 1000)
79-
self.candidatesWindow.setFrame(rect, display: true)
80-
self.candidatesWindow.setIsVisible(false)
81-
self.candidatesWindow.orderOut(nil)
82-
83-
// Initialize the prediction window
84-
self.predictionViewController = PredictionCandidatesViewController()
85-
self.predictionWindow = NSWindow(contentViewController: self.predictionViewController)
86-
self.predictionWindow.styleMask = [.borderless]
87-
self.predictionWindow.level = .popUpMenu
83+
let textInputClient = inputClient as? IMKTextInput
8884

89-
if let client = inputClient as? IMKTextInput {
90-
client.attributes(forCharacterIndex: 0, lineHeightRectangle: &rect)
91-
}
92-
rect.size = .init(width: 400, height: 1000)
93-
self.predictionWindow.setFrame(rect, display: true)
94-
self.predictionWindow.setIsVisible(false)
95-
self.predictionWindow.orderOut(nil)
85+
let candidatesViewController = CandidatesViewController()
86+
let predictionViewController = PredictionCandidatesViewController()
87+
let replaceSuggestionsViewController = ReplaceSuggestionsViewController()
9688

97-
// ReplaceSuggestionsViewControllerの初期化
98-
self.replaceSuggestionsViewController = ReplaceSuggestionsViewController()
99-
self.replaceSuggestionWindow = NSWindow(contentViewController: self.replaceSuggestionsViewController)
100-
self.replaceSuggestionWindow.styleMask = [.borderless]
101-
self.replaceSuggestionWindow.level = .popUpMenu
89+
self.candidatesViewController = candidatesViewController
90+
self.predictionViewController = predictionViewController
91+
self.replaceSuggestionsViewController = replaceSuggestionsViewController
10292

103-
if let client = inputClient as? IMKTextInput {
104-
client.attributes(forCharacterIndex: 0, lineHeightRectangle: &rect)
105-
}
106-
rect.size = .init(width: 400, height: 1000)
107-
self.replaceSuggestionWindow.setFrame(rect, display: true)
108-
self.replaceSuggestionWindow.setIsVisible(false)
109-
self.replaceSuggestionWindow.orderOut(nil)
93+
self.candidatesWindow = Self.makeCandidateWindow(
94+
contentViewController: candidatesViewController,
95+
inputClient: textInputClient
96+
)
97+
self.predictionWindow = Self.makeCandidateWindow(
98+
contentViewController: predictionViewController,
99+
inputClient: textInputClient
100+
)
101+
self.replaceSuggestionWindow = Self.makeCandidateWindow(
102+
contentViewController: replaceSuggestionsViewController,
103+
inputClient: textInputClient
104+
)
110105

111106
// PromptInputWindowの初期化
112107
self.promptInputWindow = PromptInputWindow()
@@ -792,25 +787,6 @@ extension azooKeyMacInputController: ReplaceSuggestionsViewControllerDelegate {
792787

793788
// Suggest Candidate
794789
extension azooKeyMacInputController {
795-
// MARK: - Window Setup
796-
func setupReplaceSuggestionWindow() {
797-
self.replaceSuggestionsViewController = ReplaceSuggestionsViewController()
798-
self.replaceSuggestionWindow = NSWindow(contentViewController: self.replaceSuggestionsViewController)
799-
self.replaceSuggestionWindow.styleMask = [.borderless]
800-
self.replaceSuggestionWindow.level = .popUpMenu
801-
802-
var rect: NSRect = .zero
803-
if let client = self.client() {
804-
client.attributes(forCharacterIndex: 0, lineHeightRectangle: &rect)
805-
}
806-
rect.size = .init(width: 400, height: 1000)
807-
self.replaceSuggestionWindow.setFrame(rect, display: true)
808-
self.replaceSuggestionWindow.setIsVisible(false)
809-
self.replaceSuggestionWindow.orderOut(nil)
810-
811-
self.replaceSuggestionsViewController.delegate = self
812-
}
813-
814790
// MARK: - Replace Suggestion Request Handling
815791
@MainActor func requestReplaceSuggestion() {
816792
self.segmentsManager.appendDebugMessage("requestReplaceSuggestion: 開始")

0 commit comments

Comments
 (0)