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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
// This file was generated by gir (https://github.com/gtk-rs/gir)
// from gir-files (https://github.com/gtk-rs/gir-files.git)
// from webkit2gtk-gir-files
// DO NOT EDIT

use crate::WebView;
use glib::object::Cast;
use glib::object::IsA;
use glib::signal::connect_raw;
use glib::signal::SignalHandlerId;
use glib::translate::*;
use glib::StaticType;
use glib::ToValue;
use std::boxed::Box as Box_;
use std::fmt;
use std::mem::transmute;

glib::wrapper! {
    /// Controls text search in a [`WebView`][crate::WebView].
    ///
    /// A [`FindController`][crate::FindController] is used to search text in a [`WebView`][crate::WebView]. You
    /// can get a [`WebView`][crate::WebView]<!-- -->'s [`FindController`][crate::FindController] with
    /// [`WebViewExt::find_controller()`][crate::prelude::WebViewExt::find_controller()], and later use it to search
    /// for text using [`FindControllerExt::search()`][crate::prelude::FindControllerExt::search()], or get the
    /// number of matches using [`FindControllerExt::count_matches()`][crate::prelude::FindControllerExt::count_matches()]. The
    /// operations are asynchronous and trigger signals when ready, such as
    /// `signal::FindController::found-text`,
    /// `signal::FindController::failed-to-find-text` or
    /// `signal::FindController::counted-matches`<!-- -->.
    ///
    /// # Implements
    ///
    /// [`FindControllerExt`][trait@crate::prelude::FindControllerExt], [`trait@glib::ObjectExt`]
    #[doc(alias = "WebKitFindController")]
    pub struct FindController(Object<ffi::WebKitFindController, ffi::WebKitFindControllerClass>);

    match fn {
        type_ => || ffi::webkit_find_controller_get_type(),
    }
}

impl FindController {
    pub const NONE: Option<&'static FindController> = None;

    // rustdoc-stripper-ignore-next
    /// Creates a new builder-pattern struct instance to construct [`FindController`] objects.
    ///
    /// This method returns an instance of [`FindControllerBuilder`](crate::builders::FindControllerBuilder) which can be used to create [`FindController`] objects.
    pub fn builder() -> FindControllerBuilder {
        FindControllerBuilder::default()
    }
}

#[derive(Clone, Default)]
// rustdoc-stripper-ignore-next
/// A [builder-pattern] type to construct [`FindController`] objects.
///
/// [builder-pattern]: https://doc.rust-lang.org/1.0.0/style/ownership/builders.html
#[must_use = "The builder must be built to be used"]
pub struct FindControllerBuilder {
    web_view: Option<WebView>,
}

impl FindControllerBuilder {
    // rustdoc-stripper-ignore-next
    /// Create a new [`FindControllerBuilder`].
    pub fn new() -> Self {
        Self::default()
    }

    // rustdoc-stripper-ignore-next
    /// Build the [`FindController`].
    #[must_use = "Building the object from the builder is usually expensive and is not expected to have side effects"]
    pub fn build(self) -> FindController {
        let mut properties: Vec<(&str, &dyn ToValue)> = vec![];
        if let Some(ref web_view) = self.web_view {
            properties.push(("web-view", web_view));
        }
        glib::Object::new::<FindController>(&properties)
    }

    /// The [`WebView`][crate::WebView] this controller is associated to.
    pub fn web_view(mut self, web_view: &impl IsA<WebView>) -> Self {
        self.web_view = Some(web_view.clone().upcast());
        self
    }
}

/// Trait containing all [`struct@FindController`] methods.
///
/// # Implementors
///
/// [`FindController`][struct@crate::FindController]
pub trait FindControllerExt: 'static {
    /// Counts the number of matches for `search_text`.
    ///
    /// Counts the number of matches for `search_text` found in the
    /// [`WebView`][crate::WebView] with the provided `find_options`. The number of
    /// matches will be provided by the
    /// `signal::FindController::counted-matches` signal.
    /// ## `search_text`
    /// the text to look for
    /// ## `find_options`
    /// a bitmask with the [`FindOptions`][crate::FindOptions] used in the search
    /// ## `max_match_count`
    /// the maximum number of matches allowed in the search
    #[doc(alias = "webkit_find_controller_count_matches")]
    fn count_matches(&self, search_text: &str, find_options: u32, max_match_count: u32);

    /// Gets the maximum number of matches to report.
    ///
    /// Gets the maximum number of matches to report during a text
    /// lookup. This number is passed as the last argument of
    /// [`search()`][Self::search()] or
    /// [`count_matches()`][Self::count_matches()].
    ///
    /// # Returns
    ///
    /// the maximum number of matches to report.
    #[doc(alias = "webkit_find_controller_get_max_match_count")]
    #[doc(alias = "get_max_match_count")]
    fn max_match_count(&self) -> u32;

    /// Gets the [`FindOptions`][crate::FindOptions] for the current search.
    ///
    /// Gets a bitmask containing the [`FindOptions`][crate::FindOptions] associated with
    /// the current search.
    ///
    /// # Returns
    ///
    /// a bitmask containing the [`FindOptions`][crate::FindOptions] associated
    /// with the current search.
    #[doc(alias = "webkit_find_controller_get_options")]
    #[doc(alias = "get_options")]
    fn options(&self) -> u32;

    /// Gets the text that `self` is searching for.
    ///
    /// Gets the text that `self` is currently searching
    /// for. This text is passed to either
    /// [`search()`][Self::search()] or
    /// [`count_matches()`][Self::count_matches()].
    ///
    /// # Returns
    ///
    /// the text to look for in the [`WebView`][crate::WebView].
    #[doc(alias = "webkit_find_controller_get_search_text")]
    #[doc(alias = "get_search_text")]
    fn search_text(&self) -> Option<glib::GString>;

    /// Gets the [`WebView`][crate::WebView] this find controller is associated to.
    ///
    /// Do
    /// not dereference the returned instance as it belongs to the
    /// [`FindController`][crate::FindController].
    ///
    /// # Returns
    ///
    /// the [`WebView`][crate::WebView].
    #[doc(alias = "webkit_find_controller_get_web_view")]
    #[doc(alias = "get_web_view")]
    fn web_view(&self) -> Option<WebView>;

    /// Looks for `search_text` associated with `self`.
    ///
    /// Looks for `search_text` in the [`WebView`][crate::WebView] associated with
    /// `self` since the beginning of the document highlighting
    /// up to `max_match_count` matches. The outcome of the search will be
    /// asynchronously provided by the `signal::FindController::found-text`
    /// and `signal::FindController::failed-to-find-text` signals.
    ///
    /// To look for the next or previous occurrences of the same text
    /// with the same find options use [`search_next()`][Self::search_next()]
    /// and/or [`search_previous()`][Self::search_previous()]. The
    /// [`FindController`][crate::FindController] will use the same text and options for the
    /// following searches unless they are modified by another call to this
    /// method.
    ///
    /// Note that if the number of matches is higher than `max_match_count`
    /// then `signal::FindController::found-text` will report `G_MAXUINT` matches
    /// instead of the actual number.
    ///
    /// Callers should call [`search_finish()`][Self::search_finish()] to
    /// finish the current search operation.
    /// ## `search_text`
    /// the text to look for
    /// ## `find_options`
    /// a bitmask with the [`FindOptions`][crate::FindOptions] used in the search
    /// ## `max_match_count`
    /// the maximum number of matches allowed in the search
    #[doc(alias = "webkit_find_controller_search")]
    fn search(&self, search_text: &str, find_options: u32, max_match_count: u32);

    /// Finishes a find operation.
    ///
    /// Finishes a find operation started by
    /// [`search()`][Self::search()]. It will basically unhighlight
    /// every text match found.
    ///
    /// This method will be typically called when the search UI is
    /// closed/hidden by the client application.
    #[doc(alias = "webkit_find_controller_search_finish")]
    fn search_finish(&self);

    /// Looks for the next occurrence of the search text.
    ///
    /// Calling this method before [`search()`][Self::search()] or
    /// [`count_matches()`][Self::count_matches()] is a programming error.
    #[doc(alias = "webkit_find_controller_search_next")]
    fn search_next(&self);

    /// Looks for the previous occurrence of the search text.
    ///
    /// Calling this method before [`search()`][Self::search()] or
    /// [`count_matches()`][Self::count_matches()] is a programming error.
    #[doc(alias = "webkit_find_controller_search_previous")]
    fn search_previous(&self);

    /// The current search text for this [`FindController`][crate::FindController].
    fn text(&self) -> Option<glib::GString>;

    /// This signal is emitted when the [`FindController`][crate::FindController] has
    /// counted the number of matches for a given text after a call
    /// to [`count_matches()`][Self::count_matches()].
    /// ## `match_count`
    /// the number of matches of the search text
    #[doc(alias = "counted-matches")]
    fn connect_counted_matches<F: Fn(&Self, u32) + 'static>(&self, f: F) -> SignalHandlerId;

    /// This signal is emitted when a search operation does not find
    /// any result for the given text. It will be issued if the text
    /// is not found asynchronously after a call to
    /// [`search()`][Self::search()], [`search_next()`][Self::search_next()]
    /// or [`search_previous()`][Self::search_previous()].
    #[doc(alias = "failed-to-find-text")]
    fn connect_failed_to_find_text<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    /// This signal is emitted when a given text is found in the web
    /// page text. It will be issued if the text is found
    /// asynchronously after a call to [`search()`][Self::search()],
    /// [`search_next()`][Self::search_next()] or
    /// [`search_previous()`][Self::search_previous()].
    /// ## `match_count`
    /// the number of matches found of the search text
    #[doc(alias = "found-text")]
    fn connect_found_text<F: Fn(&Self, u32) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "max-match-count")]
    fn connect_max_match_count_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "options")]
    fn connect_options_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;

    #[doc(alias = "text")]
    fn connect_text_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId;
}

impl<O: IsA<FindController>> FindControllerExt for O {
    fn count_matches(&self, search_text: &str, find_options: u32, max_match_count: u32) {
        unsafe {
            ffi::webkit_find_controller_count_matches(
                self.as_ref().to_glib_none().0,
                search_text.to_glib_none().0,
                find_options,
                max_match_count,
            );
        }
    }

    fn max_match_count(&self) -> u32 {
        unsafe { ffi::webkit_find_controller_get_max_match_count(self.as_ref().to_glib_none().0) }
    }

    fn options(&self) -> u32 {
        unsafe { ffi::webkit_find_controller_get_options(self.as_ref().to_glib_none().0) }
    }

    fn search_text(&self) -> Option<glib::GString> {
        unsafe {
            from_glib_none(ffi::webkit_find_controller_get_search_text(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn web_view(&self) -> Option<WebView> {
        unsafe {
            from_glib_none(ffi::webkit_find_controller_get_web_view(
                self.as_ref().to_glib_none().0,
            ))
        }
    }

    fn search(&self, search_text: &str, find_options: u32, max_match_count: u32) {
        unsafe {
            ffi::webkit_find_controller_search(
                self.as_ref().to_glib_none().0,
                search_text.to_glib_none().0,
                find_options,
                max_match_count,
            );
        }
    }

    fn search_finish(&self) {
        unsafe {
            ffi::webkit_find_controller_search_finish(self.as_ref().to_glib_none().0);
        }
    }

    fn search_next(&self) {
        unsafe {
            ffi::webkit_find_controller_search_next(self.as_ref().to_glib_none().0);
        }
    }

    fn search_previous(&self) {
        unsafe {
            ffi::webkit_find_controller_search_previous(self.as_ref().to_glib_none().0);
        }
    }

    fn text(&self) -> Option<glib::GString> {
        glib::ObjectExt::property(self.as_ref(), "text")
    }

    fn connect_counted_matches<F: Fn(&Self, u32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn counted_matches_trampoline<
            P: IsA<FindController>,
            F: Fn(&P, u32) + 'static,
        >(
            this: *mut ffi::WebKitFindController,
            match_count: libc::c_uint,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                FindController::from_glib_borrow(this).unsafe_cast_ref(),
                match_count,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"counted-matches\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    counted_matches_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_failed_to_find_text<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn failed_to_find_text_trampoline<
            P: IsA<FindController>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::WebKitFindController,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(FindController::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"failed-to-find-text\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    failed_to_find_text_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_found_text<F: Fn(&Self, u32) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn found_text_trampoline<
            P: IsA<FindController>,
            F: Fn(&P, u32) + 'static,
        >(
            this: *mut ffi::WebKitFindController,
            match_count: libc::c_uint,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(
                FindController::from_glib_borrow(this).unsafe_cast_ref(),
                match_count,
            )
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"found-text\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    found_text_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_max_match_count_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_max_match_count_trampoline<
            P: IsA<FindController>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::WebKitFindController,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(FindController::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::max-match-count\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_max_match_count_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_options_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_options_trampoline<
            P: IsA<FindController>,
            F: Fn(&P) + 'static,
        >(
            this: *mut ffi::WebKitFindController,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(FindController::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::options\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_options_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }

    fn connect_text_notify<F: Fn(&Self) + 'static>(&self, f: F) -> SignalHandlerId {
        unsafe extern "C" fn notify_text_trampoline<P: IsA<FindController>, F: Fn(&P) + 'static>(
            this: *mut ffi::WebKitFindController,
            _param_spec: glib::ffi::gpointer,
            f: glib::ffi::gpointer,
        ) {
            let f: &F = &*(f as *const F);
            f(FindController::from_glib_borrow(this).unsafe_cast_ref())
        }
        unsafe {
            let f: Box_<F> = Box_::new(f);
            connect_raw(
                self.as_ptr() as *mut _,
                b"notify::text\0".as_ptr() as *const _,
                Some(transmute::<_, unsafe extern "C" fn()>(
                    notify_text_trampoline::<Self, F> as *const (),
                )),
                Box_::into_raw(f),
            )
        }
    }
}

impl fmt::Display for FindController {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        f.write_str("FindController")
    }
}