Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feat/ocr error tips #603

Merged
merged 12 commits into from
Jul 11, 2024
34 changes: 34 additions & 0 deletions Easydict/App/Localizable.xcstrings
Original file line number Diff line number Diff line change
Expand Up @@ -842,6 +842,40 @@
}
}
},
"enable_youdao_ocr" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Enable Youdao OCR "
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "开启有道OCR识别"
}
}
}
},
"enable_youdao_ocr_desc" : {
"extractionState" : "manual",
"localizations" : {
"en" : {
"stringUnit" : {
"state" : "translated",
"value" : "Use the Youdao OCR for identification after a default OCR failure"
}
},
"zh-Hans" : {
"stringUnit" : {
"state" : "translated",
"value" : "在默认OCR识别失败后使用有道OCR进行识别"
}
}
}
},
"error_api" : {
"localizations" : {
"en" : {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ extension Defaults.Keys {
)

static var disableTipsView = Key<Bool>("disableTipsViewKey", default: false)
static var enableYoudaoOCR = Key<Bool>("enableYoudaoOCR", default: true)
tisfeng marked this conversation as resolved.
Show resolved Hide resolved
}

extension Defaults.Keys {
Expand Down
2 changes: 2 additions & 0 deletions Easydict/Swift/Feature/Configuration/Configuration.swift
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ class Configuration: NSObject {
@DefaultsWrapper(.enableBetaFeature) private(set) var beta: Bool

@DefaultsWrapper(.showQuickActionButton) var showQuickActionButton: Bool

@DefaultsWrapper(.enableYoudaoOCR) var enableYoudaoOCR: Bool

var cancellables: Set<AnyCancellable> = []

Expand Down
2 changes: 1 addition & 1 deletion Easydict/Swift/View/SettingView/SettingView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ struct SettingView: View {
case .disabled:
500
case .advanced:
310
350
case .privacy:
320
case .about:
Expand Down
10 changes: 10 additions & 0 deletions Easydict/Swift/View/SettingView/Tabs/TabView/AdvancedTab.swift
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,15 @@ struct AdvancedTab: View {
labelText: "disable_tips_view"
)
}

Toggle(isOn: $enableYoudaoOCR) {
AdvancedTabItemView(
color: .orange,
systemImage: "circle.rectangle.filled.pattern.diagonalline",
labelText: "enable_youdao_ocr",
subtitleText: "enable_youdao_ocr_desc"
)
}
}
Section {
Toggle(isOn: $enableBetaFeature) {
Expand All @@ -55,6 +64,7 @@ struct AdvancedTab: View {
@Default(.defaultTTSServiceType) private var defaultTTSServiceType
@Default(.enableBetaFeature) private var enableBetaFeature
@Default(.disableTipsView) private var disableTipsView
@Default(.enableYoudaoOCR) private var enableYoudaoOCR
}

#Preview {
Expand Down
2 changes: 1 addition & 1 deletion Easydict/objc/Service/Model/EZDetectManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,7 @@ - (void)handleOCRResult:(EZOCRResult *_Nullable)ocrResult error:(NSError *_Nulla
Sometimes Apple OCR may fail, like Japanese text, but we have set Japanese as preferred language and OCR again when OCR result is empty, currently it seems work, but we do not guarantee it is always work in other languages.
*/

if (Configuration.shared.beta) {
if (Configuration.shared.enableYoudaoOCR) {
[self.youdaoService ocr:self.queryModel completion:^(EZOCRResult *_Nullable youdaoOCRResult, NSError *_Nullable youdaoOCRError) {
if (!youdaoOCRError) {
completion(youdaoOCRResult, nil);
Expand Down
6 changes: 6 additions & 0 deletions Easydict/objc/ViewController/Cell/EZTableTipsCell.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@ typedef NS_ENUM(NSInteger, EZTipsCellType) {
EZTipsCellTypeWordSelectionOCR,
EZTipsCellTypeSelectWords,
EZTipsCellTypeStillPopup,
EZTipsCellTypeErrorTips,
EZTipsCellTypeWarnTips,
EZTipsCellTypeInfoTips,
};

@interface EZTableTipsCell : NSTableRowView
Expand All @@ -28,6 +31,9 @@ typedef NS_ENUM(NSInteger, EZTipsCellType) {


- (instancetype)initWithFrame:(CGRect)frame type:(EZTipsCellType)type;
- (instancetype)initWithFrame:(NSRect)frame
type:(EZTipsCellType)type
content:(NSString *)content;

/// update tips cell content with type
/// - Parameters:
Expand Down
57 changes: 40 additions & 17 deletions Easydict/objc/ViewController/Cell/EZTableTipsCell.m
Original file line number Diff line number Diff line change
Expand Up @@ -23,14 +23,23 @@ @interface EZTableTipsCell ()
@property (nonatomic, strong) NSString *questionSolveURL;
@property (nonatomic, strong) NSString *seeMoreURL;
@property (nonatomic, assign) EZTipsCellType tipsType;
@property (nonatomic, assign) BOOL showResloveBtnFlag;
@property (nonatomic, copy) NSString *contentStr;
@end

@implementation EZTableTipsCell

- (instancetype)initWithFrame:(CGRect)frame type:(EZTipsCellType)type {
return [self initWithFrame:frame type:type content:@""];
}

- (instancetype)initWithFrame:(NSRect)frame
type:(EZTipsCellType)type
content:(NSString *)content {
self = [super initWithFrame:frame];
if (self) {
self.tipsType = type;
self.contentStr = content;
[self setupUI];
[self updateQuestionContent];
}
Expand All @@ -43,6 +52,7 @@ - (void)updateTipsCellType:(EZTipsCellType)type {

- (void)updateTipsContent:(NSString *)content type:(EZTipsCellType)type {
if (!EZ_isEmptyString(content)) {
self.contentStr = content;
self.tipsContentLabel.stringValue = content;
}
self.tipsType = type;
Expand Down Expand Up @@ -99,28 +109,41 @@ - (void)updateConstraints {
}

- (void)updateQuestionContent {
NSArray *questions = self.dataDict[@"questions"];
NSInteger index = 0;
if (self.tipsType == EZTipsCellTypeNone) {
// random question and slove
index = arc4random() % questions.count;
if (self.tipsType == EZTipsCellTypeErrorTips ||
self.tipsType == EZTipsCellTypeWarnTips ||
self.tipsType == EZTipsCellTypeInfoTips) {
self.showResloveBtnFlag = NO;
self.solveBtn.hidden = YES;
self.moreBtn.hidden = YES;
self.tipsContentLabel.stringValue = self.contentStr;
} else {
// show special question
index = self.tipsType;
}
self.tipsContentLabel.stringValue = questions[index];
NSArray *solves;
if ([EZLanguageManager.shared isSystemChineseFirstLanguage]) {
solves = self.dataDict[@"solveZh"];
} else {
solves = self.dataDict[@"solveEn"];
self.showResloveBtnFlag = YES;
NSArray *questions = self.dataDict[@"questions"];
NSInteger index = 0;
if (self.tipsType == EZTipsCellTypeNone) {
// random question and slove
index = arc4random() % questions.count;
} else {
// show special question
index = self.tipsType;
}
self.contentStr = questions[index];
self.tipsContentLabel.stringValue = self.contentStr;
NSArray *solves;
if ([EZLanguageManager.shared isSystemChineseFirstLanguage]) {
solves = self.dataDict[@"solveZh"];
} else {
solves = self.dataDict[@"solveEn"];
}
self.questionSolveURL = solves[index];
self.solveBtn.link = self.questionSolveURL;
self.solveBtn.hidden = NO;
self.moreBtn.hidden = NO;
}
self.questionSolveURL = solves[index];
self.solveBtn.link = self.questionSolveURL;
}

- (CGFloat)cellHeight {
CGFloat cellHeight = 9 + 20 + 12 + self.tipsContentLabel.height + 9 + 32 + 6;
CGFloat cellHeight = 9 + 20 + 12 + self.tipsContentLabel.height + 9 + (self.showResloveBtnFlag? 32: 0) + 6;
return cellHeight;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,10 @@ @interface EZBaseQueryViewController () <NSTableViewDelegate, NSTableViewDataSou

@property (nonatomic, assign) BOOL isTipsViewVisible;

@property (nonatomic, assign) EZTipsCellType tipsCellType;

@property (nonatomic, copy) NSString *tipsCellContent;


@end

Expand Down Expand Up @@ -495,7 +499,7 @@ - (void)startOCRImage:(NSImage *)image actionType:(EZActionType)actionType {

if (error) {
NSString *errorMsg = [error localizedDescription];
self.queryView.alertText = errorMsg;
[self showTipsView:YES content:errorMsg type:EZTipsCellTypeErrorTips];
return;
}

Expand Down Expand Up @@ -656,6 +660,15 @@ - (void)updateActionType:(EZActionType)actionType {
}

- (void)showTipsView:(BOOL)isVisible {
[self showTipsView:isVisible content:@"" type:EZTipsCellTypeTextEmpty];
}

- (void)showTipsView:(BOOL)isVisible
content:(NSString *)content
type:(EZTipsCellType)type {
self.tipsCellType = type;
self.tipsCellContent = content;
[self.tipsCell updateTipsContent:content type:type];
[self showTipsView:isVisible completion:nil];
}

Expand Down Expand Up @@ -865,7 +878,8 @@ - (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(null
if (row == 2 && self.isTipsViewVisible) {
EZTableTipsCell *tipsCell = [self.tableView makeViewWithIdentifier:EZTableTipsCellId owner:self];
if (!tipsCell) {
tipsCell = [[EZTableTipsCell alloc] initWithFrame:[self tableViewContentBounds] type:EZTipsCellTypeTextEmpty];
tipsCell = [[EZTableTipsCell alloc] initWithFrame:[self tableViewContentBounds]
type:self.tipsCellType content:self.tipsCellContent];
tipsCell.identifier = EZTableTipsCellId;
}
self.tipsCell = tipsCell;
Expand All @@ -892,7 +906,11 @@ - (CGFloat)tableView:(NSTableView *)tableView heightOfRow:(NSInteger)row {
} else if (row == 2 && self.isTipsViewVisible) {
if (!self.tipsCell) {
// mini cell height
height = 104;
if ([self isCustomTipsType]) {
height = 80;
} else {
height = 104;
}
} else {
height = [self.tipsCell cellHeight];
}
Expand Down Expand Up @@ -1676,4 +1694,10 @@ - (void)autoCopyTranslatedTextOfService:(EZQueryService *)service {
}];
}

- (BOOL)isCustomTipsType {
return self.tipsCellType == EZTipsCellTypeErrorTips ||
self.tipsCellType == EZTipsCellTypeInfoTips ||
self.tipsCellType == EZTipsCellTypeWarnTips;
}

@end