Skip to content

Commit 5a8f713

Browse files
author
Reinissance
committed
doubleTabWidgetsInput
1 parent a55bfe8 commit 5a8f713

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed

src/gui/Widget.m

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,14 @@ - (id)initWithAtomLine:(NSArray *)line andGui:(Gui *)gui {
7070
if(self) {
7171
[self _init];
7272
self.gui = gui;
73+
if ([self.type isEqual:@"Symbol"] ||
74+
[self.type isEqual:@"List"] ||
75+
[self.type isEqual:@"Knob"] ||
76+
[self.type hasPrefix:@"Number"] ||
77+
[self.type hasSuffix:@"Radio"] ||
78+
[self.type hasSuffix:@"Slider"]) {
79+
[self addDoubleTabInputToWidget:self];
80+
}
7381
}
7482
return self;
7583
}
@@ -337,4 +345,54 @@ + (NSString *)stringFromFloat:(double)f withWidth:(int)width {
337345
return string;
338346
}
339347

348+
- (void) addDoubleTabInputToWidget:(Widget*)w {
349+
UITapGestureRecognizer *tab = [[UITapGestureRecognizer alloc]
350+
initWithTarget:self
351+
action:@selector(handleWidgetTap:)];
352+
tab.numberOfTapsRequired = 2;
353+
[w addGestureRecognizer:tab];
354+
}
355+
356+
- (void)handleWidgetTap:(UITapGestureRecognizer *)gestureRecognizer {
357+
Widget *control = (Widget *)gestureRecognizer.view;
358+
if (control) {
359+
[self showInputDialogForWidget:control withNumberPad:!([control.type isEqual:@"Symbol"] || [control.type isEqual:@"List"])];
360+
}
361+
}
362+
363+
- (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
388+
}
389+
}
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];
396+
}
397+
340398
@end

0 commit comments

Comments
 (0)