Skip to content

Commit

Permalink
tabInputsImproved
Browse files Browse the repository at this point in the history
  • Loading branch information
Reinissance committed Jul 4, 2024
1 parent 5a8f713 commit 5ee0001
Showing 1 changed file with 47 additions and 30 deletions.
77 changes: 47 additions & 30 deletions src/gui/Widget.m
Original file line number Diff line number Diff line change
Expand Up @@ -361,38 +361,55 @@ - (void)handleWidgetTap:(UITapGestureRecognizer *)gestureRecognizer {
}

- (void) showInputDialogForWidget: (Widget*)w withNumberPad: (BOOL) pad {
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:@"set value:"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
if (pad) {
textField.keyboardType = UIKeyboardTypeDecimalPad;
}
[textField becomeFirstResponder];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Set"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
UITextField *textField = alertController.textFields.firstObject;
if (textField) {
if (![textField.text isEqual: @""] && ![textField.text isEqual: @"-"]) {
if (pad) {
NSNumber *value = [NSNumber numberWithFloat:[[textField.text stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue]];
[w receiveFloat:[value floatValue] fromSource:@"user"]; // source has no effect here
}
else {
[w receiveSymbol:textField.text fromSource:@"user"]; // source has no effect here
dispatch_async(dispatch_get_main_queue(), ^{ // make shure we dont affect audio
UIAlertController *alertController = [UIAlertController alertControllerWithTitle:nil
message:@"set value:"
preferredStyle:UIAlertControllerStyleAlert];
[alertController addTextFieldWithConfigurationHandler:^(UITextField * _Nonnull textField) {
if (pad) {
textField.keyboardType = UIKeyboardTypeDecimalPad;
// add button to toggle positive/negative
UIButton *minusBtn = [UIButton buttonWithType:UIButtonTypeCustom];
[minusBtn addTarget:self action:@selector(toggleNegative:) forControlEvents:UIControlEventTouchUpInside];
[minusBtn setTitle:@"-/+" forState:UIControlStateNormal];

textField.leftView = minusBtn;
textField.leftViewMode = UITextFieldViewModeAlways;
}
textField.textAlignment = NSTextAlignmentCenter;
[textField becomeFirstResponder];
}];
UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"Cancel"
style:UIAlertActionStyleCancel
handler:nil];
UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"Set"
style:UIAlertActionStyleDefault
handler:^(UIAlertAction * _Nonnull action) {
UITextField *textField = alertController.textFields.firstObject;
if (textField) {
if (![textField.text isEqual: @""] && ![textField.text isEqual: @"-"]) {
if (pad) {
float value = [[textField.text stringByReplacingOccurrencesOfString:@"," withString:@"."] floatValue];
// reflect the widget's min/max range
value = ((value >= w.minValue) && (value <= w.maxValue)) ? value : (value >= w.minValue) ? w.maxValue : w.minValue;
[w receiveFloat:value fromSource:@"user"]; // source has no effect here
}
else {
[w receiveSymbol:textField.text fromSource:@"user"]; // source has no effect here
}
}
}
}
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
}];
[alertController addAction:cancelAction];
[alertController addAction:okAction];
UIViewController *rootViewController = [UIApplication sharedApplication].keyWindow.rootViewController;
[rootViewController presentViewController:alertController animated:YES completion:nil];
});
}

- (void) toggleNegative: (UIButton*) btn {
UITextField *tf = (UITextField*) [btn superview];
tf.text = ([tf.text hasPrefix:@"-"]) ? [tf.text substringFromIndex:1] : [@"-" stringByAppendingString:tf.text];
}

@end

0 comments on commit 5ee0001

Please sign in to comment.