@@ -361,38 +361,58 @@ - (void)handleWidgetTap:(UITapGestureRecognizer *)gestureRecognizer {
361361}
362362
363363- (void ) showInputDialogForWidget : (Widget*)w withNumberPad : (BOOL ) pad {
364- UIAlertController *alertController = [UIAlertController alertControllerWithTitle: nil
365- message: @" set value:"
366- preferredStyle: UIAlertControllerStyleAlert];
367- [alertController addTextFieldWithConfigurationHandler: ^(UITextField * _Nonnull textField) {
368- if (pad) {
369- textField.keyboardType = UIKeyboardTypeDecimalPad;
370- }
371- [textField becomeFirstResponder ];
372- }];
373- UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: @" Cancel"
374- style: UIAlertActionStyleCancel
375- handler: nil ];
376- UIAlertAction *okAction = [UIAlertAction actionWithTitle: @" Set"
377- style: UIAlertActionStyleDefault
378- handler: ^(UIAlertAction * _Nonnull action) {
379- UITextField *textField = alertController.textFields .firstObject ;
380- if (textField) {
381- if (![textField.text isEqual: @" " ] && ![textField.text isEqual: @" -" ]) {
382- if (pad) {
383- NSNumber *value = [NSNumber numberWithFloat: [[textField.text stringByReplacingOccurrencesOfString: @" ," withString: @" ." ] floatValue ]];
384- [w receiveFloat: [value floatValue ] fromSource: @" user" ]; // source has no effect here
385- }
386- else {
387- [w receiveSymbol: textField.text fromSource: @" user" ]; // source has no effect here
364+ dispatch_async (dispatch_get_main_queue (), ^{ // make shure we dont affect audio
365+ UIAlertController *alertController = [UIAlertController alertControllerWithTitle: nil
366+ message: @" set value:"
367+ preferredStyle: UIAlertControllerStyleAlert];
368+ [alertController addTextFieldWithConfigurationHandler: ^(UITextField * _Nonnull textField) {
369+ if (pad) {
370+ textField.keyboardType = UIKeyboardTypeDecimalPad;
371+ // add button to toggle positive/negative
372+ UIButton *minusBtn = [UIButton buttonWithType: UIButtonTypeCustom];
373+ [minusBtn addTarget: self action: @selector (toggleNegative: ) forControlEvents: UIControlEventTouchUpInside];
374+ [minusBtn setTitle: @" -/+" forState: UIControlStateNormal];
375+
376+ textField.leftView = minusBtn;
377+ textField.leftViewMode = UITextFieldViewModeAlways;
378+ }
379+ textField.textAlignment = NSTextAlignmentCenter;
380+ [textField becomeFirstResponder ];
381+ }];
382+ UIAlertAction *cancelAction = [UIAlertAction actionWithTitle: @" Cancel"
383+ style: UIAlertActionStyleCancel
384+ handler: nil ];
385+ UIAlertAction *okAction = [UIAlertAction actionWithTitle: @" Set"
386+ style: UIAlertActionStyleDefault
387+ handler: ^(UIAlertAction * _Nonnull action) {
388+ UITextField *textField = alertController.textFields .firstObject ;
389+ if (textField) {
390+ if (![textField.text isEqual: @" " ] && ![textField.text isEqual: @" -" ]) {
391+ if (pad) {
392+ float value = [[textField.text stringByReplacingOccurrencesOfString: @" ," withString: @" ." ] floatValue ];
393+ // in case the widget's min and max range is set
394+ if ((w.minValue != 0 ) && (w.maxValue != 0 )) {
395+ // reflect the widget's min/max range
396+ value = ((value >= w.minValue ) && (value <= w.maxValue )) ? value : (value >= w.minValue ) ? w.maxValue : w.minValue ;
397+ }
398+ [w receiveFloat: value fromSource: @" user" ]; // source has no effect here
399+ }
400+ else {
401+ [w receiveSymbol: textField.text fromSource: @" user" ]; // source has no effect here
402+ }
388403 }
389404 }
390- }
391- }];
392- [alertController addAction: cancelAction];
393- [alertController addAction: okAction];
394- UIViewController *rootViewController = [UIApplication sharedApplication ].keyWindow .rootViewController ;
395- [rootViewController presentViewController: alertController animated: YES completion: nil ];
405+ }];
406+ [alertController addAction: cancelAction];
407+ [alertController addAction: okAction];
408+ UIViewController *rootViewController = [UIApplication sharedApplication ].keyWindow .rootViewController ;
409+ [rootViewController presentViewController: alertController animated: YES completion: nil ];
410+ });
411+ }
412+
413+ - (void ) toggleNegative : (UIButton*) btn {
414+ UITextField *tf = (UITextField*) [btn superview ];
415+ tf.text = ([tf.text hasPrefix: @" -" ]) ? [tf.text substringFromIndex: 1 ] : [@" -" stringByAppendingString: tf.text];
396416}
397417
398418@end
0 commit comments