|
| 1 | +import GObject from "gi://GObject"; |
| 2 | +import Gdk from "gi://Gdk"; |
| 3 | +import Gtk from "gi://Gtk"; |
| 4 | +import GtkSource from "gi://GtkSource"; |
| 5 | + |
| 6 | +import Template from "./CodeFind.blp" with { type: "uri" }; |
| 7 | + |
| 8 | +class Search extends Gtk.Revealer { |
| 9 | + #previous_search_term = ""; |
| 10 | + #buffer; |
| 11 | + #search_settings; |
| 12 | + #search_context; |
| 13 | + |
| 14 | + constructor(params = {}) { |
| 15 | + super(params); |
| 16 | + |
| 17 | + this.#buffer = this._source_view.buffer; |
| 18 | + |
| 19 | + this.#initSearch(); |
| 20 | + this.#trackOccurrences(); |
| 21 | + this.#connectButtons(); |
| 22 | + this.#addControllers(); |
| 23 | + } |
| 24 | + |
| 25 | + set source_view(obj) { |
| 26 | + this._source_view = obj; |
| 27 | + } |
| 28 | + |
| 29 | + get source_view() { |
| 30 | + return this._source_view; |
| 31 | + } |
| 32 | + |
| 33 | + #initSearch() { |
| 34 | + this.#search_settings = new GtkSource.SearchSettings({ |
| 35 | + case_sensitive: false, |
| 36 | + wrap_around: true, |
| 37 | + }); |
| 38 | + |
| 39 | + this.#search_context = new GtkSource.SearchContext({ |
| 40 | + buffer: this.#buffer, |
| 41 | + settings: this.#search_settings, |
| 42 | + highlight: true, |
| 43 | + }); |
| 44 | + |
| 45 | + this.#search_context.connect("notify::occurrences-count", () => { |
| 46 | + this.#updateInfo(); |
| 47 | + }); |
| 48 | + |
| 49 | + this.#buffer.connect("mark-set", (_buffer, _iter, mark) => { |
| 50 | + const mark_name = mark.get_name(); |
| 51 | + if (mark_name === "insert" || mark_name === "selection_bound") { |
| 52 | + this.#updateInfo(); |
| 53 | + } |
| 54 | + }); |
| 55 | + |
| 56 | + this._text_search_term.connect("notify", () => { |
| 57 | + this.#search_settings.search_text = this._text_search_term.text; |
| 58 | + const [, , iter] = this.#buffer.get_selection_bounds(); |
| 59 | + const [found, ,] = this.#search_context.forward(iter); |
| 60 | + this._button_previous.sensitive = found; |
| 61 | + this._button_next.sensitive = found; |
| 62 | + }); |
| 63 | + } |
| 64 | + |
| 65 | + #trackOccurrences() { |
| 66 | + this.#search_context.connect("notify::occurrences-count", () => { |
| 67 | + this.#updateInfo(); |
| 68 | + }); |
| 69 | + |
| 70 | + this.#buffer.connect("mark-set", (_buffer, _iter, mark) => { |
| 71 | + const mark_name = mark.get_name(); |
| 72 | + if (mark_name === "insert" || mark_name === "selection_bound") { |
| 73 | + this.#updateInfo(); |
| 74 | + } |
| 75 | + }); |
| 76 | + } |
| 77 | + |
| 78 | + #connectButtons() { |
| 79 | + this._button_previous.connect("clicked", () => { |
| 80 | + this.#search(false); |
| 81 | + }); |
| 82 | + |
| 83 | + this._button_next.connect("clicked", () => { |
| 84 | + this.#search(true); |
| 85 | + }); |
| 86 | + } |
| 87 | + |
| 88 | + #addControllers() { |
| 89 | + const controller_key_source_view = new Gtk.EventControllerKey(); |
| 90 | + this._source_view.add_controller(controller_key_source_view); |
| 91 | + controller_key_source_view.connect( |
| 92 | + "key-pressed", |
| 93 | + (_controller, keyval, _keycode, state) => { |
| 94 | + if ( |
| 95 | + state & Gdk.ModifierType.CONTROL_MASK && |
| 96 | + (keyval === Gdk.KEY_f || keyval === Gdk.KEY_F) |
| 97 | + ) { |
| 98 | + const selected_text = this.#getSelectedText(); |
| 99 | + if (selected_text) { |
| 100 | + this._text_search_term.text = selected_text; |
| 101 | + } else { |
| 102 | + this._text_search_term.text = this.#previous_search_term; |
| 103 | + } |
| 104 | + this.reveal_child = true; |
| 105 | + this._text_search_term.grab_focus(); |
| 106 | + } else if (this.reveal_child && keyval === Gdk.KEY_Escape) { |
| 107 | + this.#endSearch(false); |
| 108 | + } else if ( |
| 109 | + this.reveal_child && |
| 110 | + state & Gdk.ModifierType.CONTROL_MASK && |
| 111 | + (keyval === Gdk.KEY_g || keyval === Gdk.KEY_G) |
| 112 | + ) { |
| 113 | + this.#search(!(state & Gdk.ModifierType.SHIFT_MASK)); |
| 114 | + } |
| 115 | + }, |
| 116 | + ); |
| 117 | + |
| 118 | + const controller_key = new Gtk.EventControllerKey(); |
| 119 | + this.add_controller(controller_key); |
| 120 | + |
| 121 | + controller_key.connect( |
| 122 | + "key-pressed", |
| 123 | + (_controller, keyval, _keycode, state) => { |
| 124 | + if ( |
| 125 | + state & Gdk.ModifierType.CONTROL_MASK && |
| 126 | + (keyval === Gdk.KEY_g || keyval === Gdk.KEY_G) |
| 127 | + ) { |
| 128 | + this.#search(!(state & Gdk.ModifierType.SHIFT_MASK)); |
| 129 | + } else if ( |
| 130 | + state & Gdk.ModifierType.SHIFT_MASK && |
| 131 | + keyval === Gdk.KEY_Return |
| 132 | + ) { |
| 133 | + this.#search(false); |
| 134 | + } else if (keyval === Gdk.KEY_Escape) { |
| 135 | + this.#endSearch(true); |
| 136 | + } |
| 137 | + }, |
| 138 | + ); |
| 139 | + |
| 140 | + this._text_search_term.connect("activate", () => { |
| 141 | + this.#search(true); |
| 142 | + }); |
| 143 | + } |
| 144 | + |
| 145 | + #endSearch(grabFocus) { |
| 146 | + this.reveal_child = false; |
| 147 | + this.#previous_search_term = this._text_search_term.text; |
| 148 | + this._text_search_term.text = ""; |
| 149 | + if (grabFocus) { |
| 150 | + this._source_view.grab_focus(); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + #getSelectedText() { |
| 155 | + const [, match_start, match_end] = this.#buffer.get_selection_bounds(); |
| 156 | + return this.#buffer.get_text(match_start, match_end, true); |
| 157 | + } |
| 158 | + |
| 159 | + #search(isForward) { |
| 160 | + let found, match_start, match_end, iter; |
| 161 | + if (isForward) { |
| 162 | + [, , iter] = this.#buffer.get_selection_bounds(); |
| 163 | + [found, match_start, match_end] = this.#search_context.forward(iter); |
| 164 | + } else { |
| 165 | + [, iter] = this.#buffer.get_selection_bounds(); |
| 166 | + [found, match_start, match_end] = this.#search_context.backward(iter); |
| 167 | + } |
| 168 | + if (found) { |
| 169 | + this.#selectOccurence(match_start, match_end); |
| 170 | + } |
| 171 | + } |
| 172 | + |
| 173 | + #selectOccurence(match_start, match_end) { |
| 174 | + this.#buffer.select_range(match_start, match_end); |
| 175 | + this._source_view.scroll_mark_onscreen(this.#buffer.get_insert()); |
| 176 | + } |
| 177 | + |
| 178 | + #updateInfo() { |
| 179 | + if (this._text_search_term.text) { |
| 180 | + const [, match_start, match_end] = this.#buffer.get_selection_bounds(); |
| 181 | + const count = this.#search_context.get_occurrences_count(); |
| 182 | + const index = this.#search_context.get_occurrence_position( |
| 183 | + match_start, |
| 184 | + match_end, |
| 185 | + ); |
| 186 | + |
| 187 | + if (count < 1) { |
| 188 | + this._label_info.label = ""; |
| 189 | + } else if (index === -1) { |
| 190 | + this._label_info.label = `${count} occurences`; |
| 191 | + } else { |
| 192 | + this._label_info.label = `${index} of ${count}`; |
| 193 | + } |
| 194 | + } else { |
| 195 | + this._label_info.label = ""; |
| 196 | + } |
| 197 | + } |
| 198 | +} |
| 199 | + |
| 200 | +export default GObject.registerClass( |
| 201 | + { |
| 202 | + GTypeName: "CodeFind", |
| 203 | + Template, |
| 204 | + InternalChildren: [ |
| 205 | + "text_search_term", |
| 206 | + "label_info", |
| 207 | + "button_previous", |
| 208 | + "button_next", |
| 209 | + ], |
| 210 | + Properties: { |
| 211 | + source_view: GObject.ParamSpec.object( |
| 212 | + "source-view", |
| 213 | + "SourceView", |
| 214 | + "The SourceView that will be searched", |
| 215 | + GObject.ParamFlags.READWRITE | GObject.ParamFlags.CONSTRUCT_ONLY, |
| 216 | + Gtk.Widget.$gtype, |
| 217 | + ), |
| 218 | + }, |
| 219 | + }, |
| 220 | + Search, |
| 221 | +); |
0 commit comments