From 86243722c17a8e16bc6f0bc0c3628f0a1d529ce5 Mon Sep 17 00:00:00 2001 From: Reinissance Date: Thu, 4 Jul 2024 14:32:17 +0200 Subject: [PATCH] tabInputsImproved --- src/gui/Widget.m | 80 ++++++++++++++++++++++++++++++------------------ 1 file changed, 50 insertions(+), 30 deletions(-) diff --git a/src/gui/Widget.m b/src/gui/Widget.m index 9632ab2..793fbe8 100644 --- a/src/gui/Widget.m +++ b/src/gui/Widget.m @@ -361,38 +361,58 @@ - (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]; + // in case the widget's min and max range is set + if ((w.minValue != 0) && (w.maxValue != 0)) { + // 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