| 1 |
/* exported FlatsealDocsViewer */ |
| 2 |
|
| 3 |
/* docsViewer.js |
| 4 |
* |
| 5 |
* Copyright 2021 Martin Abente Lahaye |
| 6 |
* |
| 7 |
* This program is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of the GNU General Public License as published by |
| 9 |
* the Free Software Foundation, either version 3 of the License, or |
| 10 |
* (at your option) any later version. |
| 11 |
* |
| 12 |
* This program is distributed in the hope that it will be useful, |
| 13 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 |
* GNU General Public License for more details. |
| 16 |
* |
| 17 |
* You should have received a copy of the GNU General Public License |
| 18 |
* along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 |
*/ |
| 20 |
|
| 21 |
const {GLib, Gtk, GObject, Adw, WebKit} = imports.gi; |
| 22 |
const {WebView} = imports.gi.WebKit; // eslint-disable-line no-unused-vars |
| 23 |
|
| 24 |
const MAX_RESULTS = 10; |
| 25 |
|
| 26 |
var FlatsealDocsViewer = GObject.registerClass({ |
| 27 |
GTypeName: 'FlatsealDocsViewer', |
| 28 |
Template: 'resource:///com/github/tchx84/Flatseal/widgets/docsViewer.ui', |
| 29 |
InternalChildren: [ |
| 30 |
'webview', |
| 31 |
'backButton', |
| 32 |
'forwardButton', |
| 33 |
'previousButton', |
| 34 |
'nextButton', |
| 35 |
'searchButton', |
| 36 |
'searchBar', |
| 37 |
'searchEntry', |
| 38 |
], |
| 39 |
Signals: { |
| 40 |
close: { |
| 41 |
flags: GObject.SignalFlags.RUN_LAST | GObject.SignalFlags.ACTION, |
| 42 |
}, |
| 43 |
}, |
| 44 |
}, class FlatsealDocsViewer extends Adw.Window { |
| 45 |
_init(parent) { |
| 46 |
super._init({}); |
| 47 |
this._setup(parent); |
| 48 |
} |
| 49 |
|
| 50 |
_setup(parent) { |
| 51 |
const [width, height] = parent.get_default_size(); |
| 52 |
this.default_width = width; |
| 53 |
this.default_height = height; |
| 54 |
this.transient_for = parent; |
| 55 |
this.application = parent.application; |
| 56 |
|
| 57 |
const path = GLib.build_filenamev([ |
| 58 |
imports.package.datadir, |
| 59 |
'help', |
| 60 |
'C', |
| 61 |
'flatseal', |
| 62 |
'index.html', |
| 63 |
]); |
| 64 |
|
| 65 |
this._webview.load_uri(`file://${path}`); |
| 66 |
|
| 67 |
/* Force it to use browser history with inner anchors */ |
| 68 |
this._webview.connect('notify::uri', this._loadUri.bind(this)); |
| 69 |
|
| 70 |
/* Use system web browser for external urls */ |
| 71 |
this._webview.connect('decide-policy', this._decidePolicy.bind(this)); |
| 72 |
|
| 73 |
/* Update navigation buttons on every history change */ |
| 74 |
this._webview.connect_after('load-changed', this._updateNavigation.bind(this)); |
| 75 |
this._backButton.connect('clicked', this._goBack.bind(this)); |
| 76 |
this._forwardButton.connect('clicked', this._goForward.bind(this)); |
| 77 |
|
| 78 |
this._searchEntry.connect('search-changed', this._resetSearch.bind(this)); |
| 79 |
this._searchEntry.connect('stop-search', this._cancelSearch.bind(this)); |
| 80 |
this._searchEntry.connect('next-match', this._searchNext.bind(this)); |
| 81 |
this._searchEntry.connect('previous-match', this._searchPrevious.bind(this)); |
| 82 |
|
| 83 |
this._previousButton.connect('clicked', this._searchPrevious.bind(this)); |
| 84 |
this._nextButton.connect('clicked', this._searchNext.bind(this)); |
| 85 |
|
| 86 |
this._searchButton.connect('toggled', this._toggleSearchWithButton.bind(this)); |
| 87 |
this._searchButton.bind_property( |
| 88 |
'active', |
| 89 |
this._searchBar, |
| 90 |
'search-mode-enabled', |
| 91 |
GObject.BindingFlags.BIDIRECTIONAL | GObject.BindingFlags.SYNC_CREATE); |
| 92 |
|
| 93 |
this._searchBar.connect( |
| 94 |
'notify::search-mode-enabled', this._enableSearchController.bind(this)); |
| 95 |
|
| 96 |
this._searchBar.connect_entry(this._searchEntry); |
| 97 |
this._searchBar.set_key_capture_widget(this); |
| 98 |
} |
| 99 |
|
| 100 |
_loadUri() { |
| 101 |
this._webview.load_uri(this._webview.uri); |
| 102 |
} |
| 103 |
|
| 104 |
_decidePolicy(webview, decision, type) { // eslint-disable-line class-methods-use-this |
| 105 |
if (type !== WebKit.PolicyDecisionType.NAVIGATION_ACTION) |
| 106 |
return false; |
| 107 |
|
| 108 |
const action = decision.get_navigation_action(); |
| 109 |
const uri = action.get_request().get_uri(); |
| 110 |
|
| 111 |
if (!uri.startsWith('file')) { |
| 112 |
const launcher = new Gtk.UriLauncher(); |
| 113 |
launcher.uri = uri; |
| 114 |
launcher.launch(this, null, this._openUri); |
| 115 |
|
| 116 |
decision.ignore(); |
| 117 |
return true; |
| 118 |
} |
| 119 |
|
| 120 |
return false; |
| 121 |
} |
| 122 |
|
| 123 |
_openUri(launcher, res) { // eslint-disable-line class-methods-use-this |
| 124 |
try { |
| 125 |
launcher.launch_finish(res); |
| 126 |
} catch (err) { |
| 127 |
logError(err); |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
_updateNavigation() { |
| 132 |
this._backButton.sensitive = this._webview.can_go_back(); |
| 133 |
this._forwardButton.sensitive = this._webview.can_go_forward(); |
| 134 |
} |
| 135 |
|
| 136 |
_goBack() { |
| 137 |
this._webview.go_back(); |
| 138 |
} |
| 139 |
|
| 140 |
_goForward() { |
| 141 |
this._webview.go_forward(); |
| 142 |
} |
| 143 |
|
| 144 |
_toggleSearchWithButton() { |
| 145 |
if (this._searchButton.active) |
| 146 |
this._searchEntry.grab_focus(); |
| 147 |
else |
| 148 |
this._searchButton.grab_focus(); |
| 149 |
} |
| 150 |
|
| 151 |
_enableSearchController() { |
| 152 |
if (this._searchBar.search_mode_enabled) |
| 153 |
this._findController = this._webview.get_find_controller(); |
| 154 |
else |
| 155 |
this._findController.search_finish(); |
| 156 |
} |
| 157 |
|
| 158 |
_cancelSearch() { |
| 159 |
if (this._searchEntry.get_text() === '') |
| 160 |
this._searchBar.search_mode_enabled = false; |
| 161 |
|
| 162 |
this._searchEntry.set_text(''); |
| 163 |
} |
| 164 |
|
| 165 |
_resetSearch() { |
| 166 |
this._findController.search( |
| 167 |
this._searchEntry.text, |
| 168 |
WebKit.FindOptions.CASE_INSENSITIVE | WebKit.FindOptions.WRAP_AROUND, |
| 169 |
MAX_RESULTS); |
| 170 |
} |
| 171 |
|
| 172 |
_searchPrevious() { |
| 173 |
this._findController.search_previous(); |
| 174 |
} |
| 175 |
|
| 176 |
_searchNext() { |
| 177 |
this._findController.search_next(); |
| 178 |
} |
| 179 |
}); |