Skip to content

Commit 7b996f7

Browse files
committed
Fix deprecated code
1 parent 25cd1c6 commit 7b996f7

File tree

5 files changed

+52
-27
lines changed

5 files changed

+52
-27
lines changed

src/common/mod.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use gtk::glib;
1+
use gtk::{glib, gio};
22
use once_cell::sync::Lazy;
33
use url::Url;
44

@@ -57,3 +57,20 @@ pub fn bookmarks_url() -> Url {
5757
pub fn glibctx() -> glib::MainContext {
5858
glib::MainContext::default()
5959
}
60+
61+
pub fn open_uri_externally(uri: &str) {
62+
gtk::UriLauncher::new(&uri).launch(None::<&gtk::Window>, None::<&gio::Cancellable>, |res| {
63+
if let Err(e) = res {
64+
log::error!("error opening external uri {:?}", e);
65+
}
66+
});
67+
}
68+
69+
pub fn open_file_externally(path: &std::path::Path) {
70+
let file = gio::File::for_path(path);
71+
gtk::FileLauncher::new(Some(&file)).launch(None::<&gtk::Window>, None::<&gio::Cancellable>, |res| {
72+
if let Err(e) = res {
73+
log::error!("error opening external file {:?}", e);
74+
}
75+
});
76+
}

src/macros.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ macro_rules! self_action {
44
{
55
let this = &$self;
66
let action = gio::SimpleAction::new($name, None);
7-
action.connect_activate(clone!(@weak this => move |_,_| this.$method()));
7+
action.connect_activate(clone!(#[weak] this, move |_,_| this.$method()));
88
$self.add_action(&action);
99
action
1010
}

src/widgets/pages/hypertext.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -237,22 +237,22 @@ impl Hypertext {
237237
let motion_ctrl = gtk::EventControllerMotion::new();
238238

239239
left_click_ctrl.connect_released(
240-
clone!(@strong this => @default-panic, move |ctrl, _n_press, x, y| {
240+
clone!(#[strong] this, move |ctrl, _n_press, x, y| {
241241
if let Err(e) = this.handle_click(ctrl, x, y) {
242242
log::info!("{}", e);
243243
};
244244
}),
245245
);
246246

247247
right_click_ctrl.connect_pressed(
248-
clone!(@strong this => @default-panic, move |_ctrl, _n_press, x, y| {
248+
clone!(#[strong] this, move |_ctrl, _n_press, x, y| {
249249
if let Err(e) = this.handle_right_click(x, y) {
250250
log::info!("{}", e);
251251
};
252252
}),
253253
);
254254

255-
motion_ctrl.connect_motion(clone!(@strong this => @default-panic,move |_ctrl, x, y| {
255+
motion_ctrl.connect_motion(clone!(#[strong] this, move |_ctrl, x, y| {
256256
let _ = this.handle_motion(x, y);
257257
}));
258258

src/widgets/tab.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ use url::Url;
2222

2323
use super::pages::{self, hypertext};
2424
use crate::common;
25-
use crate::common::glibctx;
25+
use crate::common::{glibctx, open_uri_externally, open_file_externally};
2626
use crate::lossy_text_read::*;
2727
use crate::session_provider::SessionProvider;
2828

@@ -470,7 +470,7 @@ impl Tab {
470470
let downloaded_file_url = format!("file://{}", d_path.as_os_str().to_str().unwrap());
471471
info!("Downloading to {}", downloaded_file_url);
472472
page.imp().open_btn.connect_clicked(move |_| {
473-
gtk::show_uri(None::<&gtk::Window>, &downloaded_file_url, 0);
473+
open_file_externally(&std::path::Path::new(&downloaded_file_url));
474474
});
475475

476476
let ext = file_name.split('.').last();
@@ -594,7 +594,7 @@ impl Tab {
594594
button.set_halign(gtk::Align::Center);
595595
let url_clone = url.clone();
596596
button.connect_clicked(move |_| {
597-
gtk::show_uri(None::<&gtk::Window>, url_clone.as_str(), 0);
597+
open_uri_externally(url_clone.as_str());
598598
});
599599
child.append(&button);
600600

@@ -615,7 +615,7 @@ impl Tab {
615615
p.connect_local(
616616
"open",
617617
false,
618-
clone!(@weak self as this => @default-panic, move |s| {
618+
clone!(#[weak(rename_to = this)] self, #[upgrade_or_panic] move |s| {
619619
let s: String = s[1].get().unwrap();
620620
let url = Url::parse(&s);
621621
if let Ok(url) = url {
@@ -705,7 +705,7 @@ impl Tab {
705705
p.set_icon_name(Some("dialog-error-symbolic"));
706706

707707
let override_btn = gtk::Button::with_label("Trust New Certificate");
708-
override_btn.connect_clicked(clone!(@weak self as this => move |_| {
708+
override_btn.connect_clicked(clone!(#[weak(rename_to = this)] self, move |_| {
709709
let url = Url::parse(&this.url()).unwrap();
710710

711711
this.session().validator().remove_known(url.host_str().unwrap());
@@ -733,7 +733,7 @@ impl Tab {
733733
p.set_icon_name(Some("dialog-error-symbolic"));
734734

735735
let override_btn = gtk::Button::with_label("Continue");
736-
override_btn.connect_clicked(clone!(@weak self as this => move |_| {
736+
override_btn.connect_clicked(clone!(#[weak(rename_to = this)] self, move |_| {
737737
let url = Url::parse(&this.url()).unwrap();
738738

739739
this.session().validator().override_trust(url.host_str().unwrap());

src/widgets/window.rs

+24-16
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ pub mod imp {
122122
fn set_zoom(&self, v: f64) {
123123
let Zoom { value, provider } = &mut *self.zoom.borrow_mut();
124124
*value = v.clamp(1.0 / ZOOM_MAX_FACTOR, ZOOM_MAX_FACTOR);
125-
provider.load_from_data(&format!(
125+
provider.load_from_string(&format!(
126126
"textview {{
127127
font-size: {}rem;
128128
}}",
@@ -232,26 +232,26 @@ impl Window {
232232

233233
let act_open_page = gio::SimpleAction::new("open-omni", Some(glib::VariantTy::STRING));
234234
act_open_page.connect_activate(
235-
clone!(@weak self as this => move |_,v| this.open_omni(v.unwrap().get::<String>().unwrap().as_str())),
235+
clone!(#[weak(rename_to = this)] self, move |_,v| this.open_omni(v.unwrap().get::<String>().unwrap().as_str())),
236236
);
237237
self.add_action(&act_open_page);
238238

239239
let act_open_url = gio::SimpleAction::new("open-url", Some(glib::VariantTy::STRING));
240240
act_open_url.connect_activate(
241-
clone!(@weak self as this => move |_,v| this.open_url_str(v.unwrap().get::<String>().unwrap().as_str())),
241+
clone!(#[weak(rename_to = this)] self, move |_,v| this.open_url_str(v.unwrap().get::<String>().unwrap().as_str())),
242242
);
243243
self.add_action(&act_open_url);
244244

245245
let act_open_in_new_tab =
246246
gio::SimpleAction::new("open-in-new-tab", Some(glib::VariantTy::STRING));
247247
act_open_in_new_tab.connect_activate(
248-
clone!(@weak self as this => move |_,v| this.open_in_new_tab(v.unwrap().get::<String>().unwrap().as_str())),
248+
clone!(#[weak(rename_to = this)] self, move |_,v| this.open_in_new_tab(v.unwrap().get::<String>().unwrap().as_str())),
249249
);
250250
self.add_action(&act_open_in_new_tab);
251251

252252
let act_set_clipboard =
253253
gio::SimpleAction::new("set-clipboard", Some(glib::VariantTy::STRING));
254-
act_set_clipboard.connect_activate(clone!(@weak self as this => move |_,v| {
254+
act_set_clipboard.connect_activate(clone!(#[weak(rename_to = this)] self, move |_,v| {
255255
this.set_clipboard(v.unwrap().get::<String>().unwrap().as_str());
256256
this.imp().toast_overlay.add_toast(adw::Toast::new("Copied to clipboard"));
257257
}));
@@ -267,7 +267,8 @@ impl Window {
267267
imp.scroll_ctrl
268268
.set_flags(gtk::EventControllerScrollFlags::VERTICAL);
269269
imp.scroll_ctrl.connect_scroll(
270-
clone!(@weak self as this => @default-panic, move |ctrl, _, y| {
270+
clone!(#[weak(rename_to = this)] self, #[upgrade_or_panic]
271+
move |ctrl, _, y| {
271272
let up = y < 0.0;
272273
if let Some(gdk::ModifierType::CONTROL_MASK) = ctrl.current_event().map(|e| e.modifier_state()) {
273274
if up {
@@ -283,7 +284,7 @@ impl Window {
283284
);
284285
imp.mouse_prev_next_ctrl.set_button(0);
285286
imp.mouse_prev_next_ctrl.connect_pressed(
286-
clone!(@weak self as this => @default-panic, move |ctrl, _, _, _| {
287+
clone!(#[weak(rename_to = this)] self, move |ctrl, _, _, _| {
287288
match ctrl.current_button() {
288289
8 => {
289290
this.previous();
@@ -299,7 +300,8 @@ impl Window {
299300
self.connect_local(
300301
"notify::url",
301302
false,
302-
clone!(@weak self as this => @default-panic, move |_| {
303+
clone!(#[weak(rename_to = this)] self, #[upgrade_or_default]
304+
move |_| {
303305
this.update_domain_color();
304306

305307
let bar = &this.imp().url_bar;
@@ -313,12 +315,14 @@ impl Window {
313315
);
314316

315317
imp.tab_view.connect_selected_page_notify(
316-
clone!(@weak self as this => @default-panic, move |tab_view| {
318+
clone!(#[weak(rename_to = this)] self, move |tab_view| {
317319
this.page_switched(tab_view);
318320
}),
319321
);
320322
imp.tab_view.connect_close_page(
321-
clone!(@weak self as this => @default-panic, move |tab_view, page| {
323+
clone!(#[weak(rename_to = this)] self,
324+
#[upgrade_or_panic]
325+
move |tab_view, page| {
322326
tab_view.close_page_finish(page, !page.is_pinned());
323327

324328
if tab_view.n_pages() == 0 {
@@ -329,19 +333,21 @@ impl Window {
329333
}),
330334
);
331335
imp.tab_overview.connect_create_tab(
332-
clone!(@weak self as this => @default-panic, move |_| {
336+
clone!(#[weak(rename_to = this)] self,
337+
#[upgrade_or_panic]
338+
move |_| {
333339
this.new_tab();
334340
this.imp().tab_view.selected_page().unwrap()
335341
}),
336342
);
337343

338344
imp.url_bar
339-
.connect_activate(clone!(@weak self as this => @default-panic, move |_sq| {
345+
.connect_activate(clone!(#[weak(rename_to = this)] self, move |_sq| {
340346
this.open_omni(this.imp().url_bar.text().as_str());
341347
}));
342348

343349
adw::StyleManager::default().connect_dark_notify(
344-
clone!(@weak self as this => @default-panic, move |_| {
350+
clone!(#[weak(rename_to = this)] self, move |_| {
345351
this.update_domain_color()
346352
}),
347353
);
@@ -358,7 +364,8 @@ impl Window {
358364
ctrl.set_propagation_phase(gtk::PropagationPhase::Capture);
359365

360366
ctrl.connect_key_pressed(
361-
clone!(@weak self as this => @default-panic, move |_, key, _, modif| {
367+
clone!(#[weak(rename_to = this)] self, #[upgrade_or_panic]
368+
move |_, key, _, modif| {
362369
let action = match (modif.contains(gdk::ModifierType::CONTROL_MASK), key) {
363370
(true, gdk::Key::ISO_Left_Tab) => Some("win.focus-previous-tab"),
364371
(true, gdk::Key::Tab) => Some("win.focus-next-tab"),
@@ -380,7 +387,8 @@ impl Window {
380387
.build();
381388

382389
drop_target.connect_drop(
383-
clone!(@weak self as this => @default-return false, move |_, value, _, _| {
390+
clone!(#[weak(rename_to = this)] self, #[upgrade_or_panic]
391+
move |_, value, _, _| {
384392
if let Ok(files) = value.get::<gdk::FileList>() {
385393
for f in files.files() {
386394
this.open_in_new_tab(&format!("file://{}", f.path().unwrap().to_str().unwrap()));
@@ -626,7 +634,7 @@ impl Window {
626634
let imp = self.imp();
627635
let url = imp.url_bar.text().to_string();
628636

629-
glibctx().spawn_local(clone!(@weak imp => async move {
637+
glibctx().spawn_local(clone!(#[weak] imp, async move {
630638
match Self::append_bookmark(&url).await {
631639
Ok(_) => {
632640
info!("{} saved to bookmarks", url);

0 commit comments

Comments
 (0)