Attachment #8341649: Use Finder instead of a custom find implementation in the Metro browser. for bug #916481

View | Details | Raw Unified | Return to bug 916481
Collapse All | Expand All

(-)a/browser/metro/base/content/browser.js (-2 / +1 lines)
Line     Link Here 
 Lines 58-74   var Browser = { Link Here 
58
58
59
    try {
59
    try {
60
      messageManager.loadFrameScript("chrome://browser/content/Util.js", true);
60
      messageManager.loadFrameScript("chrome://browser/content/Util.js", true);
61
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/Content.js", true);
61
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/Content.js", true);
62
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/FormHelper.js", true);
62
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/FormHelper.js", true);
63
      messageManager.loadFrameScript("chrome://browser/content/library/SelectionPrototype.js", true);
63
      messageManager.loadFrameScript("chrome://browser/content/library/SelectionPrototype.js", true);
64
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/SelectionHandler.js", true);
64
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/SelectionHandler.js", true);
65
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/ContextMenuHandler.js", true);
65
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/ContextMenuHandler.js", true);
66
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/FindHandler.js", true);
67
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/ConsoleAPIObserver.js", true);
66
      messageManager.loadFrameScript("chrome://browser/content/contenthandlers/ConsoleAPIObserver.js", true);
68
    } catch (e) {
67
    } catch (e) {
69
      // XXX whatever is calling startup needs to dump errors!
68
      // XXX whatever is calling startup needs to dump errors!
70
      dump("###########" + e + "\n");
69
      dump("###########" + e + "\n");
71
    }
70
    }
72
71
73
    /* handles dispatching clicks on browser into clicks in content or zooms */
72
    /* handles dispatching clicks on browser into clicks in content or zooms */
74
    Elements.browsers.customDragger = new Browser.MainDragger();
73
    Elements.browsers.customDragger = new Browser.MainDragger();
 Lines 1002-1018   nsBrowserAccess.prototype = { Link Here 
1002
    if (aIID.equals(Ci.nsIBrowserDOMWindow) || aIID.equals(Ci.nsISupports))
1001
    if (aIID.equals(Ci.nsIBrowserDOMWindow) || aIID.equals(Ci.nsISupports))
1003
      return this;
1002
      return this;
1004
    throw Cr.NS_NOINTERFACE;
1003
    throw Cr.NS_NOINTERFACE;
1005
  },
1004
  },
1006
1005
1007
  _getOpenAction: function _getOpenAction(aURI, aOpener, aWhere, aContext) {
1006
  _getOpenAction: function _getOpenAction(aURI, aOpener, aWhere, aContext) {
1008
    let where = aWhere;
1007
    let where = aWhere;
1009
    /*
1008
    /*
1010
     * aWhere: 
1009
     * aWhere:
1011
     * OPEN_DEFAULTWINDOW: default action
1010
     * OPEN_DEFAULTWINDOW: default action
1012
     * OPEN_CURRENTWINDOW: current window/tab
1011
     * OPEN_CURRENTWINDOW: current window/tab
1013
     * OPEN_NEWWINDOW: not allowed, converted to newtab below
1012
     * OPEN_NEWWINDOW: not allowed, converted to newtab below
1014
     * OPEN_NEWTAB: open a new tab
1013
     * OPEN_NEWTAB: open a new tab
1015
     * OPEN_SWITCHTAB: open in an existing tab if it matches, otherwise open
1014
     * OPEN_SWITCHTAB: open in an existing tab if it matches, otherwise open
1016
     * a new tab. afaict we always open these in the current tab.
1015
     * a new tab. afaict we always open these in the current tab.
1017
     */
1016
     */
1018
    if (where == Ci.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW) {
1017
    if (where == Ci.nsIBrowserDOMWindow.OPEN_DEFAULTWINDOW) {
(-)a/browser/metro/base/content/contenthandlers/FindHandler.js (-87 lines)
Line     Link Here 
 Lines 1-87    Link Here 
1
/* This Source Code Form is subject to the terms of the Mozilla Public
2
 * License, v. 2.0. If a copy of the MPL was not distributed with this
3
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4
5
dump("### FindHandler.js loaded\n");
6
7
var FindHandler = {
8
  get _fastFind() {
9
    delete this._fastFind;
10
    this._fastFind = Cc["@mozilla.org/typeaheadfind;1"].createInstance(Ci.nsITypeAheadFind);
11
    this._fastFind.init(docShell);
12
    return this._fastFind;
13
  },
14
15
  init: function findHandlerInit() {
16
    addMessageListener("FindAssist:Find", this);
17
    addMessageListener("FindAssist:Next", this);
18
    addMessageListener("FindAssist:Previous", this);
19
  },
20
21
  receiveMessage: function findHandlerReceiveMessage(aMessage) {
22
    let findResult = Ci.nsITypeAheadFind.FIND_NOTFOUND;
23
    let json = aMessage.json;
24
    switch (aMessage.name) {
25
      case "FindAssist:Find":
26
        findResult = this._fastFind.find(json.searchString, false);
27
        break;
28
29
      case "FindAssist:Previous":
30
        findResult = this._fastFind.findAgain(true, false);
31
        break;
32
33
      case "FindAssist:Next":
34
        findResult = this._fastFind.findAgain(false, false);
35
        break;
36
    }
37
38
    if (findResult == Ci.nsITypeAheadFind.FIND_NOTFOUND) {
39
      sendAsyncMessage("FindAssist:Show", { rect: null, result: findResult });
40
      return;
41
    }
42
43
    if (!this._fastFind.currentWindow)
44
      return;
45
46
    let selection = this._fastFind.currentWindow.getSelection();
47
    if (!selection.rangeCount || selection.isCollapsed) {
48
      // The selection can be into an input or a textarea element
49
      let nodes = content.document.querySelectorAll("input[type='text'], textarea");
50
      for (let i = 0; i < nodes.length; i++) {
51
        let node = nodes[i];
52
        if (node instanceof Ci.nsIDOMNSEditableElement && node.editor) {
53
          selection = node.editor.selectionController.getSelection(Ci.nsISelectionController.SELECTION_NORMAL);
54
          if (selection.rangeCount && !selection.isCollapsed)
55
            break;
56
        }
57
      }
58
    }
59
60
    // Return the bounding selection rect in content coordinates
61
    // including the scroll offset.
62
63
    let offset = ContentScroll.getScrollOffset(content);
64
    for (let frame = this._fastFind.currentWindow; frame != content; frame = frame.parent) {
65
      let rect = frame.frameElement.getBoundingClientRect();
66
      let left = frame.getComputedStyle(frame.frameElement, "").borderLeftWidth;
67
      let top = frame.getComputedStyle(frame.frameElement, "").borderTopWidth;
68
      offset.add(rect.left + parseInt(left), rect.top + parseInt(top));
69
    }
70
71
    let rangeRect = selection.getRangeAt(0).getBoundingClientRect();
72
    let rect = new Rect(offset.x + rangeRect.left, offset.y + rangeRect.top, 
73
                        rangeRect.width, rangeRect.height);
74
75
    // Ensure the potential "scroll" event fired during a search as already fired
76
    let timer = new Util.Timeout(function() {
77
      sendAsyncMessage("FindAssist:Show", {
78
        rect: rect.isEmpty() ? null: rect,
79
        contentHeight: content.document.documentElement.scrollHeight,
80
        result: findResult
81
      });
82
    });
83
    timer.once(0);
84
  }
85
};
86
87
FindHandler.init();
(-)a/browser/metro/base/content/helperui/FindHelperUI.js (-36 / +36 lines)
Line     Link Here 
 Lines 10-27   const kBrowserFindZoomLevelMax = 1; Link Here 
10
var FindHelperUI = {
10
var FindHelperUI = {
11
  type: "find",
11
  type: "find",
12
  commands: {
12
  commands: {
13
    next: "cmd_findNext",
13
    next: "cmd_findNext",
14
    previous: "cmd_findPrevious",
14
    previous: "cmd_findPrevious",
15
    close: "cmd_findClose"
15
    close: "cmd_findClose"
16
  },
16
  },
17
17
18
  _finder: null,
18
  _open: false,
19
  _open: false,
19
  _status: null,
20
  _status: null,
21
  _searchString: "",
20
22
21
  /*
23
  /*
22
   * Properties
24
   * Properties
23
   */
25
   */
24
26
25
  get isActive() {
27
  get isActive() {
26
    return this._open;
28
    return this._open;
27
  },
29
  },
 Lines 45-107   var FindHelperUI = { Link Here 
45
    this._textbox = document.getElementById("findbar-textbox");
47
    this._textbox = document.getElementById("findbar-textbox");
46
    this._container = Elements.findbar;
48
    this._container = Elements.findbar;
47
49
48
    this._cmdPrevious = document.getElementById(this.commands.previous);
50
    this._cmdPrevious = document.getElementById(this.commands.previous);
49
    this._cmdNext = document.getElementById(this.commands.next);
51
    this._cmdNext = document.getElementById(this.commands.next);
50
52
51
    this._textbox.addEventListener("keydown", this);
53
    this._textbox.addEventListener("keydown", this);
52
54
53
    // Listen for find assistant messages from content
54
    messageManager.addMessageListener("FindAssist:Show", this);
55
    messageManager.addMessageListener("FindAssist:Hide", this);
56
57
    // Listen for events where form assistant should be closed
55
    // Listen for events where form assistant should be closed
58
    Elements.tabList.addEventListener("TabSelect", this, true);
56
    Elements.tabList.addEventListener("TabSelect", this, true);
59
    Elements.browsers.addEventListener("URLChanged", this, true);
57
    Elements.browsers.addEventListener("URLChanged", this, true);
60
    window.addEventListener("MozAppbarShowing", this);
58
    window.addEventListener("MozAppbarShowing", this);
61
  },
59
  },
62
60
63
  receiveMessage: function findHelperReceiveMessage(aMessage) {
64
    let json = aMessage.json;
65
    switch(aMessage.name) {
66
      case "FindAssist:Show":
67
        ContextUI.dismiss();
68
        this.status = json.result;
69
        // null rect implies nothing found
70
        if (json.rect) {
71
          this._zoom(Rect.fromRect(json.rect), json.contentHeight);
72
        }
73
        break;
74
75
      case "FindAssist:Hide":
76
        if (this._container.getAttribute("type") == this.type)
77
          this.hide();
78
        break;
79
    }
80
  },
81
82
  handleEvent: function findHelperHandleEvent(aEvent) {
61
  handleEvent: function findHelperHandleEvent(aEvent) {
83
    switch (aEvent.type) {
62
    switch (aEvent.type) {
84
      case "TabSelect":
63
      case "TabSelect":
85
        this.hide();
64
        this.hide();
86
        break;
65
        break;
87
66
88
      case "URLChanged":
67
      case "URLChanged":
89
        if (aEvent.detail && aEvent.target == getBrowser())
68
        if (aEvent.detail && aEvent.target == getBrowser())
90
          this.hide();
69
          this.hide();
91
        break;
70
        break;
92
71
93
      case "keydown":
72
      case "keydown":
94
        if (aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_RETURN) {
73
        if (aEvent.keyCode == Ci.nsIDOMKeyEvent.DOM_VK_RETURN) {
95
          if (aEvent.shiftKey) {
74
          let backwardsSearch = aEvent.shiftKey;
96
            this.goToPrevious();
75
          this.searchAgain(this._searchString, backwardsSearch);
97
          } else {
98
            this.goToNext();
99
          }
100
        }
76
        }
101
        break;
77
        break;
102
78
103
      case "MozAppbarShowing":
79
      case "MozAppbarShowing":
104
        if (aEvent.target != this._container) {
80
        if (aEvent.target != this._container) {
105
          this.hide();
81
          this.hide();
106
        }
82
        }
107
        break;
83
        break;
 Lines 135-179   var FindHelperUI = { Link Here 
135
  hide: function findHelperHide() {
111
  hide: function findHelperHide() {
136
    if (!this._open)
112
    if (!this._open)
137
      return;
113
      return;
138
114
139
    ContentAreaObserver.shiftBrowserDeck(0);
115
    ContentAreaObserver.shiftBrowserDeck(0);
140
116
141
    let onTransitionEnd = () => {
117
    let onTransitionEnd = () => {
142
      this._container.removeEventListener("transitionend", onTransitionEnd, true);
118
      this._container.removeEventListener("transitionend", onTransitionEnd, true);
119
      this._finder.removeResultListener(this);
143
      this._textbox.value = "";
120
      this._textbox.value = "";
144
      this.status = null;
121
      this.status = null;
122
      this._finder = null
145
      this._open = false;
123
      this._open = false;
146
124
147
      // Restore the scroll synchronisation
125
      // Restore the scroll synchronisation
148
      Browser.selectedBrowser.scrollSync = true;
126
      Browser.selectedBrowser.scrollSync = true;
149
    };
127
    };
150
128
151
    this._textbox.blur();
129
    this._textbox.blur();
152
    this._container.addEventListener("transitionend", onTransitionEnd, true);
130
    this._container.addEventListener("transitionend", onTransitionEnd, true);
153
    this._container.dismiss();
131
    this._container.dismiss();
154
  },
132
  },
155
133
134
  search: function findHelperSearch(aValue) {
135
    if (!this._finder) {
136
      this._finder = Browser.selectedBrowser.finder;
137
      this._finder.addResultListener(this);
138
    }
139
    this._searchString = aValue;
140
    if (aValue != "") {
141
      this._finder.fastFind(aValue, false, false);
142
    } else {
143
      this.updateCommands();
144
    }
145
  },
146
147
  searchAgain: function findHelperSearchAgain(aValue, aFindBackwards) {
148
    // This can happen if the user taps next/previous after re-opening the search bar
149
    if (!this._finder) {
150
      this.search(aValue);
151
      return;
152
    }
153
154
    this._finder.findAgain(aFindBackwards, false, false);
155
  },
156
156
  goToPrevious: function findHelperGoToPrevious() {
157
  goToPrevious: function findHelperGoToPrevious() {
157
    Browser.selectedBrowser.messageManager.sendAsyncMessage("FindAssist:Previous", { });
158
    this.searchAgain(this._searchString, true);
158
  },
159
  },
159
160
160
  goToNext: function findHelperGoToNext() {
161
  goToNext: function findHelperGoToNext() {
161
    Browser.selectedBrowser.messageManager.sendAsyncMessage("FindAssist:Next", { });
162
    this.searchAgain(this._searchString, false);
162
  },
163
  },
163
164
164
  search: function findHelperSearch(aValue) {
165
  onFindResult: function(aResult, aFindBackwards, aLinkURL) {
165
    this.updateCommands(aValue);
166
    this._status = aResult;
166
167
    this.updateCommands();
167
    Browser.selectedBrowser.messageManager.sendAsyncMessage("FindAssist:Find", { searchString: aValue });
168
  },
168
  },
169
169
170
  updateCommands: function findHelperUpdateCommands(aValue) {
170
  updateCommands: function findHelperUpdateCommands() {
171
    let disabled = (this._status == Ci.nsITypeAheadFind.FIND_NOTFOUND) || (aValue == "");
171
    let disabled = (this._status == Ci.nsITypeAheadFind.FIND_NOTFOUND) || (this._searchString == "");
172
    this._cmdPrevious.setAttribute("disabled", disabled);
172
    this._cmdPrevious.setAttribute("disabled", disabled);
173
    this._cmdNext.setAttribute("disabled", disabled);
173
    this._cmdNext.setAttribute("disabled", disabled);
174
  },
174
  },
175
175
176
  _zoom: function _findHelperZoom(aElementRect, aContentHeight) {
176
  _zoom: function _findHelperZoom(aElementRect, aContentHeight) {
177
    // The rect we get here is the content rect including scroll offset
177
    // The rect we get here is the content rect including scroll offset
178
    // in the page.
178
    // in the page.
179
179
(-)a/browser/metro/base/jar.mn (-1 lines)
Line     Link Here 
 Lines 43-59   chrome.jar: Link Here 
43
  content/helperui/ChromeSelectionHandler.js   (content/helperui/ChromeSelectionHandler.js)
43
  content/helperui/ChromeSelectionHandler.js   (content/helperui/ChromeSelectionHandler.js)
44
  content/helperui/FormHelperUI.js             (content/helperui/FormHelperUI.js)
44
  content/helperui/FormHelperUI.js             (content/helperui/FormHelperUI.js)
45
  content/helperui/FindHelperUI.js             (content/helperui/FindHelperUI.js)
45
  content/helperui/FindHelperUI.js             (content/helperui/FindHelperUI.js)
46
  content/helperui/ItemPinHelper.js            (content/helperui/ItemPinHelper.js)
46
  content/helperui/ItemPinHelper.js            (content/helperui/ItemPinHelper.js)
47
47
48
  content/contenthandlers/ContextMenuHandler.js (content/contenthandlers/ContextMenuHandler.js)
48
  content/contenthandlers/ContextMenuHandler.js (content/contenthandlers/ContextMenuHandler.js)
49
  content/contenthandlers/SelectionHandler.js  (content/contenthandlers/SelectionHandler.js)
49
  content/contenthandlers/SelectionHandler.js  (content/contenthandlers/SelectionHandler.js)
50
  content/contenthandlers/FormHelper.js        (content/contenthandlers/FormHelper.js)
50
  content/contenthandlers/FormHelper.js        (content/contenthandlers/FormHelper.js)
51
  content/contenthandlers/FindHandler.js       (content/contenthandlers/FindHandler.js)
52
  content/contenthandlers/ConsoleAPIObserver.js (content/contenthandlers/ConsoleAPIObserver.js)
51
  content/contenthandlers/ConsoleAPIObserver.js (content/contenthandlers/ConsoleAPIObserver.js)
53
  content/contenthandlers/Content.js           (content/contenthandlers/Content.js)
52
  content/contenthandlers/Content.js           (content/contenthandlers/Content.js)
54
53
55
  content/library/SelectionPrototype.js        (content/library/SelectionPrototype.js)
54
  content/library/SelectionPrototype.js        (content/library/SelectionPrototype.js)
56
55
57
  content/ContentAreaObserver.js               (content/ContentAreaObserver.js)
56
  content/ContentAreaObserver.js               (content/ContentAreaObserver.js)
58
  content/BrowserTouchHandler.js               (content/BrowserTouchHandler.js)
57
  content/BrowserTouchHandler.js               (content/BrowserTouchHandler.js)
59
* content/WebProgress.js                       (content/WebProgress.js)
58
* content/WebProgress.js                       (content/WebProgress.js)

Return to bug 916481