From 671aed70de8f8f11c688aad6259decc1d211bf3b Mon Sep 17 00:00:00 2001 From: ShikiSuen Date: Tue, 18 Jan 2022 23:49:14 +0800 Subject: [PATCH] Zonble: Tooltip UI // multiple bug fixes. - Stop Tooltip UI from running out of the screen. - Discourage user-phrase when replacement is ON. - Typo Fix. --- Source/InputMethodController.mm | 12 ++++-- Source/UI/TooltipUI/TooltipController.swift | 45 ++++++++++++++++++--- Source/en.lproj/Localizable.strings | 1 + Source/ja.lproj/Localizable.strings | 1 + Source/zh-Hans.lproj/Localizable.strings | 1 + Source/zh-Hant.lproj/Localizable.strings | 1 + 6 files changed, 52 insertions(+), 9 deletions(-) diff --git a/Source/InputMethodController.mm b/Source/InputMethodController.mm index 0905c5c1..7c04ade7 100644 --- a/Source/InputMethodController.mm +++ b/Source/InputMethodController.mm @@ -1424,13 +1424,17 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; } if (!length) { [self _hideTooltip]; } + else if (Preferences.phraseReplacementEnabled) { + NSString *message = [NSString stringWithFormat:NSLocalizedString(@"⚠︎ Phrase replacement mode enabled, interfering user phrase entry.", @""), text]; + [self _showTooltip:message client:client]; + } else if (length == 1) { - NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"\"%@\" length must ≥ 2 for a user phrase.", @""), text]; - [self _showTooltip:messsage client:client]; + NSString *message = [NSString stringWithFormat:NSLocalizedString(@"\"%@\" length must ≥ 2 for a user phrase.", @""), text]; + [self _showTooltip:message client:client]; } else { - NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"\"%@\" selected. ENTER to add user phrase.", @""), text]; - [self _showTooltip:messsage client:client]; + NSString *message = [NSString stringWithFormat:NSLocalizedString(@"\"%@\" selected. ENTER to add user phrase.", @""), text]; + [self _showTooltip:message client:client]; } } diff --git a/Source/UI/TooltipUI/TooltipController.swift b/Source/UI/TooltipUI/TooltipController.swift index 8d9013c4..7301f7ba 100644 --- a/Source/UI/TooltipUI/TooltipController.swift +++ b/Source/UI/TooltipUI/TooltipController.swift @@ -54,12 +54,47 @@ public class TooltipController: NSWindowController { window?.orderOut(nil) } - private func set(windowLocation location: NSPoint) { - var newPoint = location - if location.y > 5 { - newPoint.y -= 5 + private func set(windowLocation windowTopLeftPoint: NSPoint) { + + var adjustedPoint = windowTopLeftPoint + adjustedPoint.y -= 5 + + var screenFrame = NSScreen.main?.visibleFrame ?? NSRect.zero + for screen in NSScreen.screens { + let frame = screen.visibleFrame + if windowTopLeftPoint.x >= frame.minX && + windowTopLeftPoint.x <= frame.maxX && + windowTopLeftPoint.y >= frame.minY && + windowTopLeftPoint.y <= frame.maxY { + screenFrame = frame + break + } } - window?.setFrameTopLeftPoint(newPoint) + + let windowSize = window?.frame.size ?? NSSize.zero + + // bottom beneath the screen? + if adjustedPoint.y - windowSize.height < screenFrame.minY { + adjustedPoint.y = screenFrame.minY + windowSize.height + } + + // top over the screen? + if adjustedPoint.y >= screenFrame.maxY { + adjustedPoint.y = screenFrame.maxY - 1.0 + } + + // right + if adjustedPoint.x + windowSize.width >= screenFrame.maxX { + adjustedPoint.x = screenFrame.maxX - windowSize.width + } + + // left + if adjustedPoint.x < screenFrame.minX { + adjustedPoint.x = screenFrame.minX + } + + window?.setFrameTopLeftPoint(adjustedPoint) + } private func adjustSize() { diff --git a/Source/en.lproj/Localizable.strings b/Source/en.lproj/Localizable.strings index 6f67322e..15c2a383 100644 --- a/Source/en.lproj/Localizable.strings +++ b/Source/en.lproj/Localizable.strings @@ -32,3 +32,4 @@ "There should not be duplicated keys." = "There should not be duplicated keys."; "Please specify at least 4 candidate keys." = "Please specify at least 4 candidate keys."; "Maximum 15 candidate keys allowed." = "Maximum 15 candidate keys allowed."; +"⚠︎ Phrase replacement mode enabled, interfering user phrase entry." = "⚠︎ Phrase replacement mode enabled, interfering user phrase entry."; diff --git a/Source/ja.lproj/Localizable.strings b/Source/ja.lproj/Localizable.strings index 5afd2ab9..f25d1d80 100644 --- a/Source/ja.lproj/Localizable.strings +++ b/Source/ja.lproj/Localizable.strings @@ -32,3 +32,4 @@ "There should not be duplicated keys." = "言選り用キー陣列に同じキーの重複登録はできません。"; "Please specify at least 4 candidate keys." = "言選り用キー陣列に少なくとも4つのキーをご登録ください。"; "Maximum 15 candidate keys allowed." = "言選り用キー陣列には最多15つキー登録できます。"; +"⚠︎ Phrase replacement mode enabled, interfering user phrase entry." = "⚠︎ 言葉置換機能稼働中、新添付言葉にも影響。"; diff --git a/Source/zh-Hans.lproj/Localizable.strings b/Source/zh-Hans.lproj/Localizable.strings index f88e9867..b96478e5 100644 --- a/Source/zh-Hans.lproj/Localizable.strings +++ b/Source/zh-Hans.lproj/Localizable.strings @@ -32,3 +32,4 @@ "There should not be duplicated keys." = "选字键不得重复。"; "Please specify at least 4 candidate keys." = "请至少指定四个选字键。"; "Maximum 15 candidate keys allowed." = "选字键最多只能指定十五个。"; +"⚠︎ Phrase replacement mode enabled, interfering user phrase entry." = "⚠︎ 语汇置换功能已启用,会波及语汇自订。"; diff --git a/Source/zh-Hant.lproj/Localizable.strings b/Source/zh-Hant.lproj/Localizable.strings index 3ef845c7..a37dfe0c 100644 --- a/Source/zh-Hant.lproj/Localizable.strings +++ b/Source/zh-Hant.lproj/Localizable.strings @@ -32,3 +32,4 @@ "There should not be duplicated keys." = "選字鍵不得重複。"; "Please specify at least 4 candidate keys." = "請至少指定四個選字鍵。"; "Maximum 15 candidate keys allowed." = "選字鍵最多只能指定十五個。"; +"⚠︎ Phrase replacement mode enabled, interfering user phrase entry." = "⚠︎ 語彙置換功能已啟用,會波及語彙自訂。";