Skip to content

Commit 8ed4b9e

Browse files
committed
Added a web extension to do the scrolling.
1 parent 504e4e0 commit 8ed4b9e

File tree

9 files changed

+597
-31
lines changed

9 files changed

+597
-31
lines changed

Cargo.lock

+25-17
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

+6
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@ version = "0.1.0"
44
authors = ["Antoni Boucher <[email protected]>"]
55

66
[dependencies]
7+
dbus = "^0.4"
8+
dbus-macros = "^0.0.3"
79
docopt = "^0.6"
10+
libc = "^0.2"
811
#mg = "^0.0.4"
912
mg-settings = "^0.0.2"
1013
rustc-serialize = "^0.3"
1114
url = "^1.2"
1215
xdg = "^2.0"
1316

17+
[features]
18+
default = ["webkit2/v2_4"]
19+
1420
[dependencies.gdk]
1521
git = "https://github.com/gtk-rs/gdk"
1622
version = "0.5.0"

LICENSE

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
Copyright (c) 2016 Boucher, Antoni <[email protected]>
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy of
4+
this software and associated documentation files (the "Software"), to deal in
5+
the Software without restriction, including without limitation the rights to
6+
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
7+
the Software, and to permit persons to whom the Software is furnished to do so,
8+
subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
15+
FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
16+
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
17+
IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
18+
CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

src/app.rs

+1
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,7 @@ impl App {
136136
webview.connect_scrolled(move |scroll_percentage| {
137137
let text =
138138
match scroll_percentage {
139+
-1 => "[all]".to_string(),
139140
0 => "[top]".to_string(),
140141
100 => "[bot]".to_string(),
141142
_ => format!("[{}%]", scroll_percentage),

src/main.rs

+12
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@
2020
*/
2121

2222
/*
23+
* FIXME: webview hides when resizing the screen (seems related to the web extension, or when the
24+
* page is not yet loaded, error: WebKitWebProcess: cairo-ft-font.c :669 : _cairo_ft_unscaled_font_lock_face: l'assertion « !unscaled->from_face » a échoué.).
25+
* TODO: show error if communication is not working.
2326
* TODO: follow link.
27+
* TODO: activate insert mode after focusing a text element.
2428
* TODO: support bookmarks with tags.
2529
* TODO: settings (third-party cookies).
2630
* TODO: download manager.
@@ -29,20 +33,28 @@
2933
* TODO: command/open completions.
3034
* TODO: copy/paste URLs.
3135
* TODO: handle network errors.
36+
* TODO: activate selected links with Enter.
37+
* TODO: support marks.
3238
* TODO: preferred languages.
3339
* TODO: store cache.
40+
* TODO: log console message to stdout.
3441
* TODO: NoScript.
3542
* TODO: open textarea in text editor.
3643
* TODO: non-modal javascript alert, prompt and confirm.
3744
* TODO: add option to use light theme variant instead of dark variant.
3845
* TODO: add content to the default config file.
46+
* TODO: switch from dbus to gdbus.
3947
*/
4048

49+
extern crate dbus;
50+
#[macro_use]
51+
extern crate dbus_macros;
4152
extern crate docopt;
4253
extern crate gdk;
4354
extern crate glib;
4455
extern crate gtk;
4556
extern crate gtk_sys;
57+
extern crate libc;
4658
#[macro_use]
4759
extern crate mg;
4860
#[macro_use]

src/webview.rs

+28-14
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,9 @@ use std::cell::{Cell, RefCell};
2424
use std::ops::Deref;
2525
use std::rc::Rc;
2626

27+
use glib::ToVariant;
2728
use gtk::{Inhibit, WidgetExt};
29+
use libc::getpid;
2830
use url::Url;
2931
use webkit2::{self, CookiePersistentStorage, FindController, FindOptions, WebContext, WebViewExt, FIND_OPTIONS_BACKWARDS, FIND_OPTIONS_CASE_INSENSITIVE, FIND_OPTIONS_WRAP_AROUND};
3032
use xdg::BaseDirectories;
@@ -33,10 +35,19 @@ use app::APP_NAME;
3335

3436
const SCROLL_LINE_VERTICAL: i32 = 40;
3537

38+
dbus_interface!("com.titanium.client", interface MessageServer {
39+
fn get_scroll_percentage() -> i64;
40+
fn scroll_bottom();
41+
fn scroll_by(pixels: i64);
42+
fn scroll_top();
43+
});
44+
45+
3646
/// Webkit-based view.
3747
pub struct WebView {
3848
find_controller: FindController,
39-
scrolled_callback: RefCell<Option<Rc<Box<Fn(u8)>>>>,
49+
message_server: MessageServer,
50+
scrolled_callback: RefCell<Option<Rc<Box<Fn(i64)>>>>,
4051
search_backwards: Cell<bool>,
4152
view: webkit2::WebView,
4253
}
@@ -46,6 +57,13 @@ impl WebView {
4657
pub fn new() -> Rc<Self> {
4758
let context = WebContext::get_default().unwrap();
4859
context.set_web_extensions_directory("/usr/local/lib/titanium/web-extensions");
60+
61+
let pid = unsafe { getpid() };
62+
let bus_name = format!("com.titanium.process{}", pid);
63+
let message_server = MessageServer::new(&bus_name);
64+
65+
context.set_web_extensions_initialization_user_data(&bus_name.to_variant());
66+
4967
let view = webkit2::WebView::new_with_context(&context);
5068

5169
let find_controller = view.get_find_controller().unwrap();
@@ -59,6 +77,7 @@ impl WebView {
5977
let webview =
6078
Rc::new(WebView {
6179
find_controller: find_controller,
80+
message_server: message_server,
6281
scrolled_callback: RefCell::new(None),
6382
search_backwards: Cell::new(false),
6483
view: view,
@@ -78,27 +97,22 @@ impl WebView {
7897

7998
/// Clear the selection.
8099
pub fn clear_selection(&self) {
100+
// TODO: write this in the web extension.
81101
self.run_javascript("window.getSelection().empty();");
82102
}
83103

84104
/// Connect the scrolled event.
85-
pub fn connect_scrolled<F: Fn(u8) + 'static>(&self, callback: F) {
105+
pub fn connect_scrolled<F: Fn(i64) + 'static>(&self, callback: F) {
86106
*self.scrolled_callback.borrow_mut() = Some(Rc::new(Box::new(callback)));
87107
}
88108

89109
/// Emit the scrolled event.
90110
pub fn emit_scrolled_event(&self) {
91111
if let Some(ref callback) = *self.scrolled_callback.borrow() {
92112
let callback = callback.clone();
93-
self.run_javascript_with_callback("window.scrollY / (document.body.scrollHeight - window.innerHeight)", move |result| {
94-
if let Ok(result) = result {
95-
let context = result.get_global_context().unwrap();
96-
let value = result.get_value().unwrap();
97-
if let Some(number) = value.to_number(&context) {
98-
callback((number * 100.0).round() as u8);
99-
}
100-
}
101-
});
113+
if let Ok(scroll_percentage) = self.message_server.get_scroll_percentage() {
114+
callback(scroll_percentage);
115+
}
102116
}
103117
}
104118

@@ -121,12 +135,12 @@ impl WebView {
121135

122136
/// Scroll by the specified number of pixels.
123137
fn scroll(&self, pixels: i32) {
124-
self.run_javascript(&format!("window.scrollBy(0, {});", pixels));
138+
self.message_server.scroll_by(pixels as i64).ok();
125139
}
126140

127141
/// Scroll to the bottom of the page.
128142
pub fn scroll_bottom(&self) {
129-
self.run_javascript("window.scrollTo(0, document.body.clientHeight);");
143+
self.message_server.scroll_bottom().ok();
130144
}
131145

132146
/// Scroll down by one line.
@@ -148,7 +162,7 @@ impl WebView {
148162

149163
/// Scroll to the top of the page.
150164
pub fn scroll_top(&self) {
151-
self.run_javascript("window.scrollTo(0, 0);");
165+
self.message_server.scroll_top().ok();
152166
}
153167

154168
/// Scroll up by one line.

0 commit comments

Comments
 (0)