summaryrefslogtreecommitdiff
path: root/ios/shared/ios_sharedlo/objective_c/view_controllers/toolbar/MLOFinder.m
blob: 17d1081c297433f6846382f80318062526fab800 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// -*- Mode: ObjC; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
//
// This file is part of the LibreOffice project.
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.

#import "MLOFinder.h"
#import "MLOToolbarButton.h"
#import "MLOButton.h"
#import "MLOMainViewController_Friend.h"
#import "MLOGestureEngine_Impl.h"
#import "MLORenderManager.h"
#import "MLOSubView.h"
#import "MLOResourceImage.h"
#import "mlo_uno.h"

@interface MLOFinder ()
@property MLOSubView * blackBox;
@property UILabel * label;
@property UITextField * textField;
@property NSString * lastSearched;
@property NSDate * findTime;
@property MLOButton * leftButton,* rightButton;
@property NSInteger selectionCount,selectedIndex;
@property MLOMainViewController * mainViewController;
@end

static const NSTimeInterval

    NAVIGATION_BUTTON_FADE_DURATION = 1.0f,
    FIND_DELAY = 1.0,
    FIND_INVOCATION=FIND_DELAY+0.01;

static const CGFloat
    FADE_DURATION = 0.5f,
    FADE_IN_TARGET = 1.0f,
    FADE_OUT_TARGET = 0.0f,

    TEXT_AREA_WIDTH = 150.0f,
    SPACING_FROM_FIND_TOOLBAR_BUTTON=50.0f,
    BLACK_BOX_CORNER_RADIUS=15.0f,

    BLACK_BOX_INNER_PADDING_X = 10.0f,
    BLACK_BOX_INNER_PADDING_Y = 10.0f,
    TEXT_AREA_TO_NAVIAGATION_BUTTONS_SPACING = 5.0f,

    LABEL_HEIGHT = 18.0f,
    LABEL_TO_INPUT_FIELD_PADDING=8.0f,

    INPUT_FIELD_HEIGHT = 20.0f,

    BLACK_BOX_INNER_HEIGHT =LABEL_HEIGHT + LABEL_TO_INPUT_FIELD_PADDING + INPUT_FIELD_HEIGHT,
    BLACK_BOX_HEIGHT=2*BLACK_BOX_INNER_PADDING_Y + BLACK_BOX_INNER_HEIGHT;

@implementation MLOFinder

-(id)initWithToolbarButton:(MLOToolbarButton *) findButton{
    
    self = [super init];
    if(self){

        self.selectedIndex = -1;
        self.selectionCount = -1;
        
        MLOResourceImage * leftButtonImage = [MLOResourceImage left];
        MLOResourceImage * rightButtonImage =[MLOResourceImage right];
        
        CGFloat leftButtonWidth =leftButtonImage.image.size.width;
        CGFloat rightButtonWidth = rightButtonImage.image.size.width;

        CGRect findButtonFrame = findButton.frame;

        CGFloat xOrigin = findButtonFrame.origin.x + findButtonFrame.size.width + SPACING_FROM_FIND_TOOLBAR_BUTTON;
        CGFloat yOrigin = findButtonFrame.origin.y;
        
        CGFloat leftButtonXOrigin = xOrigin + BLACK_BOX_INNER_PADDING_X;

        CGFloat textAreaXOrigin = leftButtonXOrigin +leftButtonWidth + TEXT_AREA_TO_NAVIAGATION_BUTTONS_SPACING;
        CGFloat textAreaYOrigin = yOrigin +BLACK_BOX_INNER_PADDING_Y;

        CGFloat rightButtonXOrigin = textAreaXOrigin + TEXT_AREA_WIDTH + TEXT_AREA_TO_NAVIAGATION_BUTTONS_SPACING;
        
        [self addBlackBoxXOrigin:   xOrigin
                         yOrigin:   yOrigin
                           width:   leftButtonWidth +
                                    rightButtonWidth +
                                    (TEXT_AREA_TO_NAVIAGATION_BUTTONS_SPACING + BLACK_BOX_INNER_PADDING_X)*2 +
                                    TEXT_AREA_WIDTH ];


        [self addLabelXOrigin:textAreaXOrigin yOrigin:textAreaYOrigin];
        [self addTextFieldXOrigin:textAreaXOrigin yOrigin:textAreaYOrigin];

        self.leftButton =
        [self addButtonXOrigin:leftButtonXOrigin
                       yOrigin:textAreaYOrigin
                         image:leftButtonImage
                      selector:@selector(onTapLeft)
                          name:@"left (previous)"];

        self.rightButton =
        [self addButtonXOrigin:rightButtonXOrigin
                       yOrigin:textAreaYOrigin
                         image:rightButtonImage
                      selector:@selector(onTapRight)
                          name:@"right (next)"];

        self.lastSearched = @"";
        self.findTime = [NSDate date];
    }
    return self;
}
-(void) onTapLeft{
    [self onTap:MARK_PREVIOUS];
}
-(void) onTapRight{
    [self onTap:MARK_NEXT];
}

-(void) onTap:(MLOFindSelectionType) type{
    self.selectedIndex = mlo_find(_lastSearched, type);
    [self updateLabel];
    
}

-(void)updateLabel{
    CGFloat navigationAlpha;
    if(_selectionCount>0){
        self.label.text = [NSString stringWithFormat: @"Find (%d of %d)",_selectedIndex + 1,_selectionCount];
        navigationAlpha = 1.0f;

    }else{

        self.label.text = @"Find";
        navigationAlpha = 0.0f;
    }
    
    [self.mainViewController.gestureEngine.renderer renderNow];

    if(_leftButton.alpha != navigationAlpha){
        [UIView animateWithDuration:NAVIGATION_BUTTON_FADE_DURATION animations:^{
            _leftButton.alpha = navigationAlpha;
            _rightButton.alpha = navigationAlpha;

        }];
    }

}

-(MLOButton *) addButtonXOrigin:(CGFloat) xOrigin yOrigin:(CGFloat) yOrigin image:(MLOResourceImage *) image selector:(SEL) selector name:(NSString *) buttonName{
    MLOButton * button = [MLOButton buttonWithImage:image];

    CGFloat height = image.image.size.height;
    
    if(height < BLACK_BOX_INNER_HEIGHT){
        yOrigin += (BLACK_BOX_INNER_HEIGHT - height)/2.0f;
    }

    button.frame = CGRectMake(xOrigin,yOrigin,image.image.size.width,height);
    button.alpha = 0.0f;
    [button addTarget:self action:selector];
    NSLog(@"Created the navigation button %@",buttonName);
    return button;
}

-(void)addBlackBoxXOrigin:(CGFloat) xOrigin yOrigin:(CGFloat)yOrigin width:(CGFloat) width{
    self.blackBox =
    [[MLOSubView alloc]
     initWithFrame: CGRectMake(xOrigin,
                               yOrigin,
                               width,
                               BLACK_BOX_HEIGHT)
     color:         [UIColor blackColor]
     cornerRadius:  BLACK_BOX_CORNER_RADIUS
     alpha:         0.0f];

    NSLog(@"Created finder blackbox");
}

-(void) addLabelXOrigin:(CGFloat) xOrigin yOrigin:(CGFloat) yOrigin{
    self.label = [[UILabel alloc] initWithFrame:CGRectMake(xOrigin,
                                                           yOrigin,
                                                           TEXT_AREA_WIDTH,
                                                           LABEL_HEIGHT)];
    _label.textColor=[UIColor whiteColor];
    _label.backgroundColor = [UIColor clearColor];
    _label.alpha=0.0f;

    [self updateLabel];
}

-(void) addTextFieldXOrigin:(CGFloat) xOrigin yOrigin:(CGFloat) yOrigin{
    self.textField = [[UITextField alloc] initWithFrame:CGRectMake(xOrigin,
                                                                   yOrigin+LABEL_HEIGHT +LABEL_TO_INPUT_FIELD_PADDING,
                                                                   TEXT_AREA_WIDTH,
                                                                   INPUT_FIELD_HEIGHT)];
    _textField.text =@"";
    _textField.textColor = [UIColor blackColor];
    _textField.backgroundColor = [UIColor whiteColor];
    _textField.alpha = 0.0f;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(onTextChanged)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:_textField];
}



-(void)show{
    [self fadeTo:FADE_IN_TARGET];
    [_textField becomeFirstResponder];
}
-(void)hide{
    [self fadeTo:FADE_OUT_TARGET];
    
    [_textField resignFirstResponder];
}

-(void)fadeTo:(CGFloat) alphaTarget{
    if(_blackBox.alpha !=alphaTarget){

        BOOL isFadeNavigationButton = (_selectionCount > 0) || (alphaTarget == 0.0f);
       
        [UIView animateWithDuration:FADE_DURATION animations:^{
             _blackBox.alpha    =   alphaTarget;
            _label.alpha        =   alphaTarget;
            _textField.alpha    =   alphaTarget;

            if(isFadeNavigationButton){
                _leftButton.alpha   =   alphaTarget;
                _rightButton.alpha  =   alphaTarget;
            }
        }];
    }
}


-(void)addToMainViewController:(MLOMainViewController *)mainViewController{
    [mainViewController.canvas addSubview:_blackBox];
    [mainViewController.canvas addSubview:_label];
    [mainViewController.canvas addSubview:_textField];
    [mainViewController.canvas addSubview:_leftButton];
    [mainViewController.canvas addSubview:_rightButton];
    self.mainViewController = mainViewController;
}
- (void)onTextChanged{
    
    self.findTime  = [NSDate dateWithTimeIntervalSinceNow:FIND_DELAY];

    [self performSelector:@selector(invokeFind) withObject:nil afterDelay:FIND_INVOCATION];
    
}

-(void) invokeFind{
    
    if(![[_findTime laterDate:[NSDate date]] isEqualToDate:_findTime]){
        
        NSInteger trimmedLength = [_textField.text  stringByTrimmingCharactersInSet: [NSCharacterSet whitespaceCharacterSet]].length;
        
        if( (_textField.text.length > 0) &&
            (trimmedLength>0) &&
           ![_textField.text isEqualToString:_lastSearched]){
            
            self.lastSearched  = [_textField.text copy];
            
            self.selectionCount = mlo_find(_lastSearched, MARK_FIRST);
            self.selectedIndex = 0;

        }else if(trimmedLength==0){
            
            self.lastSearched =@"";
            self.selectionCount = -1;
            self.selectedIndex = -1;
        }

        [self updateLabel];
    }
}
@end