Bug 1838365 add some DecodedStream finished logging r=pehrsons,media-playback-reviewe...
[gecko.git] / toolkit / modules / FindBarContent.sys.mjs
blob8b34d93f9d428f0072e2659420682c2ac081c3b7
1 // vim: set ts=2 sw=2 sts=2 tw=80:
2 // This Source Code Form is subject to the terms of the Mozilla Public
3 // License, v. 2.0. If a copy of the MPL was not distributed with this
4 // file, You can obtain one at http://mozilla.org/MPL/2.0/.
6 import { XPCOMUtils } from "resource://gre/modules/XPCOMUtils.sys.mjs";
8 /* Please keep in sync with toolkit/content/widgets/findbar.js */
9 const FIND_NORMAL = 0;
10 const FIND_TYPEAHEAD = 1;
11 const FIND_LINKS = 2;
13 export class FindBarContent {
14   constructor(actor) {
15     this.actor = actor;
17     this.findMode = 0;
18     this.inQuickFind = false;
20     this.addedEventListener = false;
21   }
23   start(event) {
24     this.inPassThrough = true;
25   }
27   startQuickFind(event, autostart = false) {
28     if (!this.addedEventListener) {
29       this.addedEventListener = true;
30       Services.els.addSystemEventListener(
31         this.actor.document.defaultView,
32         "mouseup",
33         this,
34         false
35       );
36     }
38     let mode = FIND_TYPEAHEAD;
39     if (
40       event.charCode == "'".charAt(0) ||
41       (autostart && FindBarContent.typeAheadLinksOnly)
42     ) {
43       mode = FIND_LINKS;
44     }
46     // Set findMode immediately (without waiting for child->parent->child roundtrip)
47     // to ensure we pass any further keypresses, too.
48     this.findMode = mode;
49     this.passKeyToParent(event);
50   }
52   updateState(data) {
53     this.findMode = data.findMode;
54     this.inQuickFind = data.hasQuickFindTimeout;
55     if (data.isOpenAndFocused) {
56       this.inPassThrough = false;
57     }
58   }
60   handleEvent(event) {
61     switch (event.type) {
62       case "keypress":
63         this.onKeypress(event);
64         break;
65       case "mouseup":
66         this.onMouseup(event);
67         break;
68     }
69   }
71   onKeypress(event) {
72     if (this.inPassThrough) {
73       this.passKeyToParent(event);
74     } else if (
75       this.findMode != FIND_NORMAL &&
76       this.inQuickFind &&
77       event.charCode
78     ) {
79       this.passKeyToParent(event);
80     }
81   }
83   passKeyToParent(event) {
84     event.preventDefault();
85     // These are the properties required to dispatch another 'real' event
86     // to the findbar in the parent in _dispatchKeypressEvent in findbar.xml .
87     // If you make changes here, verify that that method can still do its job.
88     const kRequiredProps = [
89       "type",
90       "bubbles",
91       "cancelable",
92       "ctrlKey",
93       "altKey",
94       "shiftKey",
95       "metaKey",
96       "keyCode",
97       "charCode",
98     ];
99     let fakeEvent = {};
100     for (let prop of kRequiredProps) {
101       fakeEvent[prop] = event[prop];
102     }
103     this.actor.sendAsyncMessage("Findbar:Keypress", fakeEvent);
104   }
106   onMouseup(event) {
107     if (this.findMode != FIND_NORMAL) {
108       this.actor.sendAsyncMessage("Findbar:Mouseup", {});
109     }
110   }
113 XPCOMUtils.defineLazyPreferenceGetter(
114   FindBarContent,
115   "typeAheadLinksOnly",
116   "accessibility.typeaheadfind.linksonly"