Skip to content

Commit

Permalink
fixed line_length
Browse files Browse the repository at this point in the history
  • Loading branch information
groverlynn committed Jul 3, 2023
1 parent a415608 commit d4e2031
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 8 deletions.
37 changes: 29 additions & 8 deletions SquirrelPanel.m
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ @interface SquirrelTheme : NSObject
@property(nonatomic, readonly) CGFloat preeditLinespace;
@property(nonatomic, readonly) CGFloat alpha;
@property(nonatomic, readonly) CGFloat translucency;
@property(nonatomic, readonly) CGFloat lineLength;
@property(nonatomic, readonly) BOOL showPaging;
@property(nonatomic, readonly) BOOL rememberSize;
@property(nonatomic, readonly) BOOL linear;
Expand Down Expand Up @@ -113,6 +114,7 @@ - (void)setCornerRadius:(CGFloat)cornerRadius
preeditLinespace:(CGFloat)preeditLinespace
alpha:(CGFloat)alpha
translucency:(CGFloat)translucency
lineLength:(CGFloat)lineLength
showPaging:(BOOL)showPaging
rememberSize:(BOOL)rememberSize
linear:(BOOL)linear
Expand Down Expand Up @@ -195,6 +197,7 @@ - (void)setCornerRadius:(CGFloat)cornerRadius
preeditLinespace:(CGFloat)preeditLinespace
alpha:(CGFloat)alpha
translucency:(CGFloat)translucency
lineLength:(CGFloat)lineLength
showPaging:(BOOL)showPaging
rememberSize:(BOOL)rememberSize
linear:(BOOL)linear
Expand All @@ -208,6 +211,7 @@ - (void)setCornerRadius:(CGFloat)cornerRadius
_preeditLinespace = preeditLinespace;
_alpha = alpha;
_translucency = translucency;
_lineLength = lineLength;
_showPaging = showPaging;
_rememberSize = rememberSize;
_linear = linear;
Expand Down Expand Up @@ -321,7 +325,7 @@ - (instancetype)initWithFrame:(NSRect)frameRect {
if (@available(macOS 12.0, *)) {
_layoutManager = [[NSTextLayoutManager alloc] init];
_layoutManager.usesFontLeading = NO;
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(NSViewWidthSizable, CGFLOAT_MAX)];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:NSMakeSize(NSViewWidthSizable, CGFLOAT_MAX)];
_layoutManager.textContainer = textContainer;
NSTextContentStorage *textStorage = [[NSTextContentStorage alloc] init];
[textStorage addTextLayoutManager:_layoutManager];
Expand All @@ -339,7 +343,7 @@ - (instancetype)initWithFrame:(NSRect)frameRect {
_textView.layoutManager.backgroundLayoutEnabled = YES;
_textView.layoutManager.usesFontLeading = NO;
_textView.layoutManager.typesetterBehavior = NSTypesetterLatestBehavior;
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithSize:NSMakeSize(NSViewWidthSizable, CGFLOAT_MAX)];
NSTextContainer *textContainer = [[NSTextContainer alloc] initWithContainerSize:NSMakeSize(NSViewWidthSizable, CGFLOAT_MAX)];
[_textView replaceTextContainer:textContainer];
}
_defaultTheme = [[SquirrelTheme alloc] init];
Expand Down Expand Up @@ -1342,6 +1346,9 @@ - (void)getMaxTextWidth {
NSRect screenRect = [_screen visibleFrame];
CGFloat textWidthRatio = MIN(1.0, 1.0 / (theme.vertical ? 4 : 3) + [theme.attrs[NSFontAttributeName] pointSize] / 144.0);
_maxTextWidth = (theme.vertical ? NSHeight(screenRect) : NSWidth(screenRect)) * textWidthRatio - (theme.hilitedCornerRadius + theme.edgeInset.width) * 2;
if (theme.lineLength > 0) {
_maxTextWidth = MIN(theme.lineLength, _maxTextWidth);
}
}

// Get the window size, it will be the dirtyRect in SquirrelView.drawRect
Expand Down Expand Up @@ -1371,11 +1378,16 @@ - (void)show {
bool sweepVertical = NSWidth(_position) > NSHeight(_position);
NSRect contentRect = _view.contentRect;
NSRect maxContentRect = NSInsetRect(contentRect, theme.hilitedCornerRadius, 0);
// remember panel size (fix the top leading anchor of the panel in screen coordiantes)
if (theme.rememberSize) {
if (theme.vertical ? (NSMinY(_position) - NSMinY(screenRect) <= NSHeight(screenRect) * textWidthRatio + kOffsetHeight) :
(sweepVertical ? (NSMinX(_position) - NSMinX(screenRect) > NSWidth(screenRect) * textWidthRatio + kOffsetHeight) :
(NSMinX(_position) + MAX(NSWidth(maxContentRect), _maxSize.width) + theme.hilitedCornerRadius + theme.edgeInset.width > NSMaxX(screenRect)))) {
if (theme.lineLength > 0) { // fixed line length / text width
if (_maxSize.width > 0) { // only applicable to non-status
maxContentRect.size.width = _maxTextWidth;
}
}
if (theme.rememberSize) { // remember panel size (fix the top leading anchor of the panel in screen coordiantes)
if ((theme.vertical ? (NSMinY(_position) - NSMinY(screenRect) <= NSHeight(screenRect) * textWidthRatio + kOffsetHeight) :
(sweepVertical ? (NSMinX(_position) - NSMinX(screenRect) > NSWidth(screenRect) * textWidthRatio + kOffsetHeight) :
(NSMinX(_position) + MAX(NSWidth(maxContentRect), _maxSize.width) + theme.hilitedCornerRadius + theme.edgeInset.width > NSMaxX(screenRect)))) &&
theme.lineLength == 0) {
if (NSWidth(maxContentRect) >= _maxSize.width) {
_maxSize.width = NSWidth(maxContentRect);
} else {
Expand Down Expand Up @@ -1564,6 +1576,9 @@ - (void)showPreedit:(NSString *)preedit

SquirrelTheme *theme = _view.currentTheme;
[self getMaxTextWidth];
if (theme.lineLength > 0) {
_maxSize.width = MIN(theme.lineLength, _maxTextWidth);
}

NSMutableAttributedString *text = [[NSMutableAttributedString alloc] init];
NSRange preeditRange = NSMakeRange(NSNotFound, 0);
Expand Down Expand Up @@ -1851,7 +1866,7 @@ - (void)showStatus:(NSString *)message {
[_view.textView.textStorage setAttributedString:text];
[_view.textView setLayoutOrientation:theme.vertical ? NSTextLayoutOrientationVertical : NSTextLayoutOrientationHorizontal];

_maxSize = NSZeroSize; // disable remember_size for status messages
_maxSize = NSZeroSize; // disable remember_size and fixed line_length for status messages
NSRange emptyRange = NSMakeRange(NSNotFound, 0);
[_view drawViewWith:@[]
highlightedIndex:NSNotFound
Expand Down Expand Up @@ -1997,6 +2012,7 @@ + (void)updateTheme:(SquirrelTheme *)theme withConfig:(SquirrelConfig *)config f
CGFloat lineSpacing = [config getDouble:@"style/line_spacing"];
CGFloat spacing = [config getDouble:@"style/spacing"];
CGFloat baseOffset = [config getDouble:@"style/base_offset"];
CGFloat lineLength = MAX([config getDouble:@"style/line_length"], 0.0);

NSColor *backgroundColor;
NSColor *backgroundImage;
Expand Down Expand Up @@ -2147,6 +2163,10 @@ + (void)updateTheme:(SquirrelTheme *)theme withConfig:(SquirrelConfig *)config f
if (baseOffsetOverridden) {
baseOffset = baseOffsetOverridden.doubleValue;
}
NSNumber *lineLengthOverridden = [config getOptionalDouble:[prefix stringByAppendingString:@"/line_length"]];
if (lineLengthOverridden) {
lineLength = MAX(lineLengthOverridden.doubleValue, 0.0);
}
}

fontSize = fontSize ? fontSize : kDefaultFontSize;
Expand Down Expand Up @@ -2289,6 +2309,7 @@ + (void)updateTheme:(SquirrelTheme *)theme withConfig:(SquirrelConfig *)config f
preeditLinespace:spacing
alpha:(alpha == 0 ? 1.0 : alpha)
translucency:translucency
lineLength:lineLength
showPaging:showPaging
rememberSize:rememberSize
linear:linear
Expand Down
2 changes: 2 additions & 0 deletions data/squirrel.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ style:
line_spacing: 5
# space between preedit and candidates in non-inline mode
spacing: 10
# line length of preedit and candidates (fixed if value > 0, flexible/auto-adjust otherwise)
line_length: 0

#candidate_format: '%c %@'

Expand Down

0 comments on commit d4e2031

Please sign in to comment.