Skip to content

Commit

Permalink
perf(UI): show model button width based on window width ratio
Browse files Browse the repository at this point in the history
  • Loading branch information
tisfeng committed Apr 19, 2024
1 parent c64c5c9 commit 164d9be
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 24 deletions.
65 changes: 41 additions & 24 deletions Easydict/objc/ViewController/View/ResultView/EZResultView.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#import "EZHoverButton.h"
#import "EZLoadingAnimationView.h"
#import "NSImage+EZSymbolmage.h"
#import "NSObject+EZWindowType.h"

@interface EZResultView ()

Expand Down Expand Up @@ -215,8 +216,6 @@ - (void)setup {
[self.serviceNameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
make.left.equalTo(self.serviceIcon.mas_right).offset(2);
make.centerY.equalTo(self.topBarView).offset(0);

make.width.mas_lessThanOrEqualTo(127); // the lenght of "Built-In AI Translate"
}];

[self.serviceModelButton mas_makeConstraints:^(MASConstraintMaker *make) {
Expand Down Expand Up @@ -268,7 +267,6 @@ - (void)setResult:(EZQueryResult *)result {

mm_weakify(self);

CGFloat modelButtonWidth = 0;
if ([self isBaseOpenAIService:result.service]) {
EZBaseOpenAIService *service = (EZBaseOpenAIService *)result.service;
self.serviceModelButton.title = service.model;
Expand All @@ -279,14 +277,7 @@ - (void)setResult:(EZQueryResult *)result {
mm_strongify(self);
[self showModelSelectionMenu:button];
}];

[self.serviceModelButton sizeToFit];
modelButtonWidth = MIN(self.serviceModelButton.width, 105); // the length of "gpt-4-turbo-preview"
}
[self.serviceModelButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(modelButtonWidth);
}];


[self.wordResultView refreshWithResult:result];

Expand All @@ -311,6 +302,46 @@ - (void)setQueryTextBlock:(void (^)(NSString *_Nonnull))clickTextBlock {

#pragma mark -

- (void)updateConstraints {
[self.serviceNameLabel mas_updateConstraints:^(MASConstraintMaker *make) {
// 127 is the lenght of "Built-In AI Translate"
make.width.mas_lessThanOrEqualTo(127 * [self windowWidthRatio]);
}];

CGFloat modelButtonWidth = 0;
if ([self isBaseOpenAIService:self.result.service]) {
[self.serviceModelButton sizeToFit];
// 105 is the length of "gpt-4-turbo-preview"
modelButtonWidth = MIN(self.serviceModelButton.width, 105 * [self windowWidthRatio]);
}

[self.serviceModelButton mas_updateConstraints:^(MASConstraintMaker *make) {
make.width.mas_equalTo(modelButtonWidth);
}];

[super updateConstraints];
}

- (CGFloat)windowWidthRatio {
CGFloat minimumWindowWidth = [EZLayoutManager.shared minimumWindowSize:self.windowType].width;
return self.width / minimumWindowWidth;
}

#pragma mark - Public Methods

- (void)updateLoadingAnimation {
[self startOrStopLoadingAnimation:self.result.isLoading];
}

- (void)startOrStopLoadingAnimation:(BOOL)isLoading {
if (isLoading) {
self.errorImageView.hidden = YES;
}
[self.loadingView startLoading:isLoading];
}

#pragma mark - Update UI

- (void)updateWordResultViewHeight:(CGFloat)wordResultViewHeight {
if (self.result.HTMLString.length) {
self.result.webViewManager.wordResultViewHeight = wordResultViewHeight;
Expand All @@ -336,20 +367,6 @@ - (void)updateWordResultViewHeight:(CGFloat)wordResultViewHeight {
// NSLog(@"%@, result view height: %@", result.serviceType, @(viewHeight));
}

#pragma mark - Public Methods

- (void)updateLoadingAnimation {
[self startOrStopLoadingAnimation:self.result.isLoading];
}

- (void)startOrStopLoadingAnimation:(BOOL)isLoading {
if (isLoading) {
self.errorImageView.hidden = YES;
}
[self.loadingView startLoading:isLoading];
}

#pragma mark -

- (void)updateAllButtonStatus {
[self updateErrorImage];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -834,6 +834,8 @@ - (nullable NSView *)tableView:(NSTableView *)tableView viewForTableColumn:(null
}

EZResultView *resultCell = [self resultCellAtRow:row];
resultCell.windowType = self.windowType;

return resultCell;
}

Expand Down

0 comments on commit 164d9be

Please sign in to comment.