Zonble: In-Place User Phrase Guiding Tooltip
This commit is contained in:
parent
3b5f24f328
commit
ea1f6d9a12
|
@ -338,6 +338,8 @@ static double FindHighestScore(const vector<NodeAnchor>& nodes, double epsilon)
|
|||
gCurrentCandidateController.delegate = nil;
|
||||
gCurrentCandidateController.visible = NO;
|
||||
[_candidates removeAllObjects];
|
||||
|
||||
[self _hideTooltip];
|
||||
}
|
||||
|
||||
- (void)setValue:(id)value forTag:(long)tag client:(id)sender
|
||||
|
@ -413,6 +415,8 @@ static double FindHighestScore(const vector<NodeAnchor>& nodes, double epsilon)
|
|||
[_composingBuffer setString:@""];
|
||||
gCurrentCandidateController.visible = NO;
|
||||
[_candidates removeAllObjects];
|
||||
|
||||
[self _hideTooltip];
|
||||
}
|
||||
|
||||
NS_INLINE size_t min(size_t a, size_t b) { return a < b ? a : b; }
|
||||
|
@ -492,6 +496,7 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; }
|
|||
// i.e. the client app needs to take care of where to put ths composing buffer
|
||||
[client setMarkedText:attrString selectionRange:NSMakeRange((NSInteger)_builder->markerCursorIndex(), 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
|
||||
_latestReadingCursor = (NSInteger)_builder->markerCursorIndex();
|
||||
[self _showCurrentMarkedTextTooltipWithClient:client];
|
||||
}
|
||||
else {
|
||||
// we must use NSAttributedString so that the cursor is visible --
|
||||
|
@ -504,6 +509,7 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; }
|
|||
// i.e. the client app needs to take care of where to put ths composing buffer
|
||||
[client setMarkedText:attrString selectionRange:NSMakeRange(cursorIndex, 0) replacementRange:NSMakeRange(NSNotFound, NSNotFound)];
|
||||
_latestReadingCursor = cursorIndex;
|
||||
[self _hideTooltip];
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -609,46 +615,6 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; }
|
|||
return layout;
|
||||
}
|
||||
|
||||
- (NSString *)_currentMarkedText
|
||||
{
|
||||
if (_builder->markerCursorIndex() < 0) {
|
||||
return @"";
|
||||
}
|
||||
if (!_bpmfReadingBuffer->isEmpty()) {
|
||||
return @"";
|
||||
}
|
||||
|
||||
size_t begin = min(_builder->markerCursorIndex(), _builder->cursorIndex());
|
||||
size_t end = max(_builder->markerCursorIndex(), _builder->cursorIndex());
|
||||
// A phrase should contian at least two characters.
|
||||
if (end - begin < 2) {
|
||||
return @"";
|
||||
}
|
||||
|
||||
NSRange range = NSMakeRange((NSInteger)begin, (NSInteger)(end - begin));
|
||||
NSString *phrase = [_composingBuffer substringWithRange:range];
|
||||
NSMutableString *string = [[NSMutableString alloc] init];
|
||||
[string appendString:phrase];
|
||||
[string appendString:@" "];
|
||||
NSMutableArray *readingsArray = [[NSMutableArray alloc] init];
|
||||
vector<std::string> v = _builder->readingsAtRange(begin, end);
|
||||
for(vector<std::string>::iterator it_i=v.begin(); it_i!=v.end(); ++it_i) {
|
||||
[readingsArray addObject:[NSString stringWithUTF8String:it_i->c_str()]];
|
||||
}
|
||||
[string appendString:[readingsArray componentsJoinedByString:@"-"]];
|
||||
return string;
|
||||
}
|
||||
|
||||
- (BOOL)_writeUserPhrase
|
||||
{
|
||||
NSString *currentMarkedPhrase = [self _currentMarkedText];
|
||||
if (![currentMarkedPhrase length]) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return [LanguageModelManager writeUserPhrase:currentMarkedPhrase];
|
||||
}
|
||||
|
||||
- (vChewingEmacsKey)_detectEmacsKeyFromCharCode:(UniChar)charCode modifiers:(NSUInteger)flags
|
||||
{
|
||||
if (flags & NSControlKeyMask) {
|
||||
|
@ -1086,7 +1052,6 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; }
|
|||
return YES;
|
||||
}
|
||||
|
||||
|
||||
// Enter
|
||||
if (charCode == 13) {
|
||||
if (![_composingBuffer length]) {
|
||||
|
@ -1403,24 +1368,30 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; }
|
|||
+ (VTHorizontalCandidateController *)horizontalCandidateController
|
||||
{
|
||||
static VTHorizontalCandidateController *instance = nil;
|
||||
@synchronized(self) {
|
||||
if (!instance) {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
instance = [[VTHorizontalCandidateController alloc] init];
|
||||
}
|
||||
}
|
||||
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
+ (VTVerticalCandidateController *)verticalCandidateController
|
||||
{
|
||||
static VTVerticalCandidateController *instance = nil;
|
||||
@synchronized(self) {
|
||||
if (!instance) {
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
instance = [[VTVerticalCandidateController alloc] init];
|
||||
}
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
+ (TooltipController *)tooltipController
|
||||
{
|
||||
static TooltipController *instance = nil;
|
||||
static dispatch_once_t onceToken;
|
||||
dispatch_once(&onceToken, ^{
|
||||
instance = [[TooltipController alloc] init];
|
||||
});
|
||||
return instance;
|
||||
}
|
||||
|
||||
|
@ -1544,6 +1515,113 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; }
|
|||
gCurrentCandidateController.visible = YES;
|
||||
}
|
||||
|
||||
#pragma mark - User phrases
|
||||
|
||||
- (NSString *)_currentMarkedText
|
||||
{
|
||||
if (_builder->markerCursorIndex() < 0) {
|
||||
return @"";
|
||||
}
|
||||
if (!_bpmfReadingBuffer->isEmpty()) {
|
||||
return @"";
|
||||
}
|
||||
|
||||
size_t begin = min(_builder->markerCursorIndex(), _builder->cursorIndex());
|
||||
size_t end = max(_builder->markerCursorIndex(), _builder->cursorIndex());
|
||||
// A phrase should contian at least two characters.
|
||||
if (end - begin < 1) {
|
||||
return @"";
|
||||
}
|
||||
|
||||
NSRange range = NSMakeRange((NSInteger)begin, (NSInteger)(end - begin));
|
||||
NSString *selectedText = [_composingBuffer substringWithRange:range];
|
||||
return selectedText;
|
||||
}
|
||||
|
||||
- (NSString *)_currentMarkedTextAndReadings
|
||||
{
|
||||
if (_builder->markerCursorIndex() < 0) {
|
||||
return @"";
|
||||
}
|
||||
if (!_bpmfReadingBuffer->isEmpty()) {
|
||||
return @"";
|
||||
}
|
||||
|
||||
size_t begin = min(_builder->markerCursorIndex(), _builder->cursorIndex());
|
||||
size_t end = max(_builder->markerCursorIndex(), _builder->cursorIndex());
|
||||
// A phrase should contian at least two characters.
|
||||
if (end - begin < 2) {
|
||||
return @"";
|
||||
}
|
||||
|
||||
NSRange range = NSMakeRange((NSInteger)begin, (NSInteger)(end - begin));
|
||||
NSString *selectedText = [_composingBuffer substringWithRange:range];
|
||||
NSMutableString *string = [[NSMutableString alloc] init];
|
||||
[string appendString:selectedText];
|
||||
[string appendString:@" "];
|
||||
NSMutableArray *readingsArray = [[NSMutableArray alloc] init];
|
||||
vector<std::string> v = _builder->readingsAtRange(begin, end);
|
||||
for(vector<std::string>::iterator it_i=v.begin(); it_i!=v.end(); ++it_i) {
|
||||
[readingsArray addObject:[NSString stringWithUTF8String:it_i->c_str()]];
|
||||
}
|
||||
[string appendString:[readingsArray componentsJoinedByString:@"-"]];
|
||||
return string;
|
||||
}
|
||||
|
||||
- (BOOL)_writeUserPhrase
|
||||
{
|
||||
NSString *currentMarkedPhrase = [self _currentMarkedTextAndReadings];
|
||||
if (![currentMarkedPhrase length]) {
|
||||
return NO;
|
||||
}
|
||||
|
||||
return [LanguageModelManager writeUserPhrase:currentMarkedPhrase];
|
||||
}
|
||||
|
||||
- (void)_showCurrentMarkedTextTooltipWithClient:(id)client
|
||||
{
|
||||
NSString *text = [self _currentMarkedText];
|
||||
NSInteger length = text.length;
|
||||
if (!length) {
|
||||
[self _hideTooltip];
|
||||
}
|
||||
else if (length == 1) {
|
||||
NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"You are now selecting \"%@\". You can add a phrase with two or more characters.", @""), text];
|
||||
[self _showTooltip:messsage client:client];
|
||||
}
|
||||
else {
|
||||
NSString *messsage = [NSString stringWithFormat:NSLocalizedString(@"You are now selecting \"%@\". Press enter to add a new phrase.", @""), text];
|
||||
[self _showTooltip:messsage client:client];
|
||||
}
|
||||
}
|
||||
|
||||
- (void)_showTooltip:(NSString *)tooltip client:(id)client
|
||||
{
|
||||
NSRect lineHeightRect = NSMakeRect(0.0, 0.0, 16.0, 16.0);
|
||||
|
||||
NSInteger cursor = _latestReadingCursor;
|
||||
if (cursor == [_composingBuffer length] && cursor != 0) {
|
||||
cursor--;
|
||||
}
|
||||
|
||||
// some apps (e.g. Twitter for Mac's search bar) handle this call incorrectly, hence the try-catch
|
||||
@try {
|
||||
[client attributesForCharacterIndex:cursor lineHeightRectangle:&lineHeightRect];
|
||||
}
|
||||
@catch (NSException *exception) {
|
||||
NSLog(@"%@", exception);
|
||||
}
|
||||
|
||||
[[vChewingInputMethodController tooltipController] showTooltip:tooltip atPoint:lineHeightRect.origin];
|
||||
}
|
||||
|
||||
- (void)_hideTooltip
|
||||
{
|
||||
if ([vChewingInputMethodController tooltipController].window.isVisible) {
|
||||
[[vChewingInputMethodController tooltipController] hide];
|
||||
}
|
||||
}
|
||||
|
||||
#pragma mark - Misc menu items
|
||||
|
||||
- (void)showPreferences:(id)sender
|
||||
|
@ -1666,3 +1744,4 @@ NS_INLINE size_t max(size_t a, size_t b) { return a > b ? a : b; }
|
|||
}
|
||||
|
||||
@end
|
||||
|
||||
|
|
|
@ -0,0 +1,100 @@
|
|||
//
|
||||
// TooltipContainer.swift
|
||||
//
|
||||
// Copyright (c) 2021-2022 The vChewing Project.
|
||||
// Copyright (c) 2011-2022 The OpenVanilla Project.
|
||||
//
|
||||
// Contributors:
|
||||
// Weizhong Yang (@zonble) @ OpenVanilla
|
||||
// Shiki Suen (@ShikiSuen) @ vChewing // Color tweaks only
|
||||
//
|
||||
// Based on the Syrup Project and the Formosana Library
|
||||
// by Lukhnos Liu (@lukhnos).
|
||||
//
|
||||
// Permission is hereby granted, free of charge, to any person
|
||||
// obtaining a copy of this software and associated documentation
|
||||
// files (the "Software"), to deal in the Software without
|
||||
// restriction, including without limitation the rights to use,
|
||||
// copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
// copies of the Software, and to permit persons to whom the
|
||||
// Software is furnished to do so, subject to the following
|
||||
// conditions:
|
||||
//
|
||||
// The above copyright notice and this permission notice shall be
|
||||
// included in all copies or substantial portions of the Software.
|
||||
//
|
||||
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
||||
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
||||
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
||||
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
||||
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
||||
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
||||
// OTHER DEALINGS IN THE SOFTWARE.
|
||||
//
|
||||
|
||||
import Cocoa
|
||||
|
||||
public class TooltipController: NSWindowController {
|
||||
private let backgroundColor = NSColor.windowBackgroundColor
|
||||
private var messageTextField: NSTextField
|
||||
private var tooltip: String = "" {
|
||||
didSet {
|
||||
messageTextField.stringValue = tooltip
|
||||
adjustSize()
|
||||
}
|
||||
}
|
||||
|
||||
public init() {
|
||||
let contentRect = NSRect(x: 128.0, y: 128.0, width: 300.0, height: 20.0)
|
||||
let styleMask: NSWindow.StyleMask = [.borderless, .nonactivatingPanel]
|
||||
let panel = NSPanel(contentRect: contentRect, styleMask: styleMask, backing: .buffered, defer: false)
|
||||
panel.level = NSWindow.Level(Int(kCGPopUpMenuWindowLevel) + 1)
|
||||
panel.hasShadow = true
|
||||
|
||||
messageTextField = NSTextField()
|
||||
messageTextField.isEditable = false
|
||||
messageTextField.isSelectable = false
|
||||
messageTextField.isBezeled = false
|
||||
messageTextField.textColor = NSColor.textColor
|
||||
messageTextField.drawsBackground = true
|
||||
messageTextField.backgroundColor = backgroundColor
|
||||
messageTextField.font = .systemFont(ofSize: NSFont.systemFontSize(for: .small))
|
||||
panel.contentView?.addSubview(messageTextField)
|
||||
|
||||
super.init(window: panel)
|
||||
}
|
||||
|
||||
public required init?(coder: NSCoder) {
|
||||
fatalError("init(coder:) has not been implemented")
|
||||
}
|
||||
|
||||
@objc(showTooltip:atPoint:)
|
||||
public func show(tooltip: String, at point: NSPoint) {
|
||||
self.tooltip = tooltip
|
||||
window?.orderFront(nil)
|
||||
set(windowLocation: point)
|
||||
}
|
||||
|
||||
@objc
|
||||
public func hide() {
|
||||
window?.orderOut(nil)
|
||||
}
|
||||
|
||||
private func set(windowLocation location: NSPoint) {
|
||||
var newPoint = location
|
||||
if location.y > 5 {
|
||||
newPoint.y -= 5
|
||||
}
|
||||
window?.setFrameTopLeftPoint(newPoint)
|
||||
}
|
||||
|
||||
private func adjustSize() {
|
||||
let attrString = messageTextField.attributedStringValue;
|
||||
var rect = attrString.boundingRect(with: NSSize(width: 1600.0, height: 1600.0), options: .usesLineFragmentOrigin)
|
||||
rect.size.width += 10
|
||||
messageTextField.frame = rect
|
||||
window?.setFrame(rect, display: true)
|
||||
}
|
||||
|
||||
}
|
|
@ -25,3 +25,5 @@
|
|||
"Please check the permission of at \"%@\"." = "Please check the permission of at \"%@\".";
|
||||
"Edit Excluded Phrases" = "Edit Excluded Phrases";
|
||||
"Use Half-Width Punctuations" = "Use Half-Width Punctuations";
|
||||
"You are now selecting \"%@\". You can add a phrase with two or more characters." = "You are now selecting \"%@\". You can add a phrase with two or more characters.";
|
||||
"You are now selecting \"%@\". Press enter to add a new phrase." = "You are now selecting \"%@\". Press enter to add a new phrase.";
|
||||
|
|
|
@ -25,3 +25,5 @@
|
|||
"Please check the permission of at \"%@\"." = "请检查此处的存取权限:\"%@\".";
|
||||
"Edit Excluded Phrases" = "编辑要滤除的语汇";
|
||||
"Use Half-Width Punctuations" = "啟用半角標點輸出";
|
||||
"You are now selecting \"%@\". You can add a phrase with two or more characters." = "您目前选择了「%@」。请选择至少两个字,才能将其加入自订语汇。";
|
||||
"You are now selecting \"%@\". Press enter to add a new phrase." = "您目前选择了「%@」。按下 Enter 就可以加入到自订语汇中。";
|
||||
|
|
|
@ -25,3 +25,5 @@
|
|||
"Please check the permission of at \"%@\"." = "請檢查此處的存取權限:\"%@\".";
|
||||
"Edit Excluded Phrases" = "編輯要濾除的語彙";
|
||||
"Use Half-Width Punctuations" = "啟用半形標點輸出";
|
||||
"You are now selecting \"%@\". You can add a phrase with two or more characters." = "您目前選擇了「%@」。請選擇至少兩個字,才能將其加入自訂語彙。";
|
||||
"You are now selecting \"%@\". Press enter to add a new phrase." = "您目前選擇了「%@」。按下 Enter 就可以加入到自訂語彙中。";
|
||||
|
|
|
@ -24,6 +24,7 @@
|
|||
5BDF2D012791C03B00838ADB /* PreferencesWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDF2D002791C03B00838ADB /* PreferencesWindowController.swift */; };
|
||||
5BDF2D032791C71200838ADB /* NonModalAlertWindowController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDF2D022791C71200838ADB /* NonModalAlertWindowController.swift */; };
|
||||
5BDF2D062791DFF200838ADB /* AppDelegate.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BDF2D052791DA6700838ADB /* AppDelegate.swift */; };
|
||||
5BE798A42792E58A00337FF9 /* TooltipController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 5BE798A32792E58A00337FF9 /* TooltipController.swift */; };
|
||||
5BF4A6FE27844738007DC6E7 /* frmAboutWindow.m in Sources */ = {isa = PBXBuildFile; fileRef = 5BF4A6FC27844738007DC6E7 /* frmAboutWindow.m */; };
|
||||
5BF4A70027844DC5007DC6E7 /* frmAboutWindow.xib in Resources */ = {isa = PBXBuildFile; fileRef = 5BF4A70227844DC5007DC6E7 /* frmAboutWindow.xib */; };
|
||||
6A0421A815FEF3F50061ED63 /* FastLM.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 6A0421A615FEF3F50061ED63 /* FastLM.cpp */; };
|
||||
|
@ -106,6 +107,7 @@
|
|||
5BDF2D002791C03B00838ADB /* PreferencesWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PreferencesWindowController.swift; sourceTree = "<group>"; };
|
||||
5BDF2D022791C71200838ADB /* NonModalAlertWindowController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = NonModalAlertWindowController.swift; sourceTree = "<group>"; };
|
||||
5BDF2D052791DA6700838ADB /* AppDelegate.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = AppDelegate.swift; sourceTree = "<group>"; };
|
||||
5BE798A32792E58A00337FF9 /* TooltipController.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TooltipController.swift; sourceTree = "<group>"; };
|
||||
5BF4A6FB27844738007DC6E7 /* frmAboutWindow.h */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.h; path = frmAboutWindow.h; sourceTree = "<group>"; };
|
||||
5BF4A6FC27844738007DC6E7 /* frmAboutWindow.m */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.c.objc; path = frmAboutWindow.m; sourceTree = "<group>"; };
|
||||
5BF4A70327844DD0007DC6E7 /* Base */ = {isa = PBXFileReference; lastKnownFileType = file.xib; name = Base; path = Base.lproj/frmAboutWindow.xib; sourceTree = "<group>"; };
|
||||
|
@ -253,6 +255,23 @@
|
|||
path = LanguageModel;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
5BE798A12792E50F00337FF9 /* UI */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5BE798A22792E51F00337FF9 /* TooltipUI */,
|
||||
6A0D4ED815FC0DA600ABF4B3 /* CandidateUI */,
|
||||
);
|
||||
path = UI;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
5BE798A22792E51F00337FF9 /* TooltipUI */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5BE798A32792E58A00337FF9 /* TooltipController.swift */,
|
||||
);
|
||||
path = TooltipUI;
|
||||
sourceTree = "<group>";
|
||||
};
|
||||
6A0D4E9215FC0CFA00ABF4B3 = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
|
@ -285,8 +304,8 @@
|
|||
6A0D4EC215FC0D3C00ABF4B3 /* Source */ = {
|
||||
isa = PBXGroup;
|
||||
children = (
|
||||
5BE798A12792E50F00337FF9 /* UI */,
|
||||
5B58E87D278413E7003EA2AD /* MITLicense.txt */,
|
||||
6A0D4ED815FC0DA600ABF4B3 /* CandidateUI */,
|
||||
6A38BBDD15FC115800A8A51F /* Data */,
|
||||
6A0D4F1215FC0EB100ABF4B3 /* Engine */,
|
||||
6ACA41E715FC1D9000935EF6 /* Installer */,
|
||||
|
@ -647,6 +666,7 @@
|
|||
5BF4A6FE27844738007DC6E7 /* frmAboutWindow.m in Sources */,
|
||||
5B5F4F972792A4EA00922DC2 /* UserPhrasesLM.cpp in Sources */,
|
||||
5B5F4F8E27928F9300922DC2 /* vChewingLM.cpp in Sources */,
|
||||
5BE798A42792E58A00337FF9 /* TooltipController.swift in Sources */,
|
||||
5BDF2D062791DFF200838ADB /* AppDelegate.swift in Sources */,
|
||||
5BC3EE1B278FC48C00F5E44C /* VerticalCandidateController.swift in Sources */,
|
||||
5B42B64027876FDC00BB9B9F /* UserOverrideModel.cpp in Sources */,
|
||||
|
|
Loading…
Reference in New Issue