feat: uint formatter with units

This commit is contained in:
relikd
2025-12-11 15:09:14 +01:00
parent b94dd030b4
commit 5427cb58ee
5 changed files with 27 additions and 6 deletions

View File

@@ -1,4 +1,6 @@
@import Cocoa;
@interface StrictUIntFormatter : NSFormatter
/// Note: must contain `%ld` and is used as formatter string.
@property (nullable, copy) NSString *unit;
@end

View File

@@ -3,11 +3,28 @@
@implementation StrictUIntFormatter
/// Display object as integer formatted string.
- (NSString *)stringForObjectValue:(id)obj {
return [NSString stringWithFormat:@"%d", [[NSString stringWithFormat:@"%@", obj] intValue]];
NSString *str = [NSString stringWithFormat:@"%@", obj];
if (str.length == 0)
return @"";
if (self.unit)
return [NSString stringWithFormat:self.unit, [str integerValue]];
return [NSString stringWithFormat:@"%ld", [str integerValue]];
}
- (NSString *)editingStringForObjectValue:(id)obj {
NSString *str = [NSString stringWithFormat:@"%@", obj];
if (str.length == 0)
return @"";
return [NSString stringWithFormat:@"%ld", [str integerValue]];
}
/// Parse any pasted input as integer.
- (BOOL)getObjectValue:(out id _Nullable __autoreleasing *)obj forString:(NSString *)string errorDescription:(out NSString *__autoreleasing _Nullable *)error {
if (string.length == 0) {
*obj = @"";
} else {
*obj = [[NSNumber numberWithInt:[string intValue]] stringValue];
}
return YES;
}
/// Only digits, no other character allowed

View File

@@ -36,7 +36,7 @@ static inline CGFloat NSMaxWidth(NSView *a, NSView *b) { return Max(NSWidth(a.fr
// UI: TextFields
+ (NSTextField*)label:(NSString*)text;
+ (NSTextField*)inputField:(NSString*)placeholder width:(CGFloat)w;
+ (NSTextField*)integerField:(NSUInteger)placeholder width:(CGFloat)w;
+ (NSTextField*)integerField:(NSString*)placeholder unit:(nullable NSString*)unit width:(CGFloat)w;
+ (NSView*)labelColumn:(NSArray<NSString*>*)labels rowHeight:(CGFloat)h padding:(CGFloat)pad;
// UI: Buttons
+ (NSButton*)button:(NSString*)text;

View File

@@ -29,9 +29,11 @@
}
/// Create input text field which only accepts integer values. (calls `inputField`) `21px` height.
+ (NSTextField*)integerField:(NSUInteger)placeholder width:(CGFloat)w {
NSTextField *input = [self inputField:[NSString stringWithFormat:@"%ld", placeholder] width:w];
/// `field.formatter` is of type `StrictUIntFormatter`.
+ (NSTextField*)integerField:(NSString*)placeholder unit:(nullable NSString*)unit width:(CGFloat)w {
NSTextField *input = [self inputField:placeholder width:w];
input.formatter = [StrictUIntFormatter new];
((StrictUIntFormatter*)input.formatter).unit = unit;
return input;
}

View File

@@ -32,7 +32,7 @@
self.name = [[[NSView inputField:NSLocalizedString(@"Example Title", nil) width:0] placeIn:self x:x yTop:rowHeight] sizeToRight:PAD_S + 18];
self.spinnerName = [[NSView activitySpinner] placeIn:self xRight:1 yTop:rowHeight + 2.5];
// 3. row
self.refreshNum = [[NSView integerField:30 width:85] placeIn:self x:x yTop:2*rowHeight];
self.refreshNum = [[NSView integerField:@"∞" unit:nil width:85] placeIn:self x:x yTop:2*rowHeight];
self.refreshUnit = [[NSView popupButton:120] placeIn:self x:NSMaxX(self.refreshNum.frame) + PAD_M yTop:2*rowHeight];
self.regexConverterButton = [[[[NSView buttonIcon:RSSImageRegexIcon size:19]
action:@selector(openRegexConverter) target:controller]