Skip to content

Commit

Permalink
Fix deprecated code
Browse files Browse the repository at this point in the history
  • Loading branch information
ranfdev committed Oct 3, 2024
1 parent 25cd1c6 commit 7b996f7
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 27 deletions.
19 changes: 18 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use gtk::glib;
use gtk::{glib, gio};
use once_cell::sync::Lazy;
use url::Url;

Expand Down Expand Up @@ -57,3 +57,20 @@ pub fn bookmarks_url() -> Url {
pub fn glibctx() -> glib::MainContext {
glib::MainContext::default()
}

pub fn open_uri_externally(uri: &str) {
gtk::UriLauncher::new(&uri).launch(None::<&gtk::Window>, None::<&gio::Cancellable>, |res| {
if let Err(e) = res {
log::error!("error opening external uri {:?}", e);
}
});
}

pub fn open_file_externally(path: &std::path::Path) {
let file = gio::File::for_path(path);
gtk::FileLauncher::new(Some(&file)).launch(None::<&gtk::Window>, None::<&gio::Cancellable>, |res| {
if let Err(e) = res {
log::error!("error opening external file {:?}", e);
}
});
}
2 changes: 1 addition & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ macro_rules! self_action {
{
let this = &$self;
let action = gio::SimpleAction::new($name, None);
action.connect_activate(clone!(@weak this => move |_,_| this.$method()));
action.connect_activate(clone!(#[weak] this, move |_,_| this.$method()));
$self.add_action(&action);
action
}
Expand Down
6 changes: 3 additions & 3 deletions src/widgets/pages/hypertext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,22 +237,22 @@ impl Hypertext {
let motion_ctrl = gtk::EventControllerMotion::new();

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

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

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

Expand Down
12 changes: 6 additions & 6 deletions src/widgets/tab.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ use url::Url;

use super::pages::{self, hypertext};
use crate::common;
use crate::common::glibctx;
use crate::common::{glibctx, open_uri_externally, open_file_externally};
use crate::lossy_text_read::*;
use crate::session_provider::SessionProvider;

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

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

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

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

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

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

this.session().validator().override_trust(url.host_str().unwrap());
Expand Down
40 changes: 24 additions & 16 deletions src/widgets/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ pub mod imp {
fn set_zoom(&self, v: f64) {
let Zoom { value, provider } = &mut *self.zoom.borrow_mut();
*value = v.clamp(1.0 / ZOOM_MAX_FACTOR, ZOOM_MAX_FACTOR);
provider.load_from_data(&format!(
provider.load_from_string(&format!(
"textview {{
font-size: {}rem;
}}",
Expand Down Expand Up @@ -232,26 +232,26 @@ impl Window {

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

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

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

let act_set_clipboard =
gio::SimpleAction::new("set-clipboard", Some(glib::VariantTy::STRING));
act_set_clipboard.connect_activate(clone!(@weak self as this => move |_,v| {
act_set_clipboard.connect_activate(clone!(#[weak(rename_to = this)] self, move |_,v| {
this.set_clipboard(v.unwrap().get::<String>().unwrap().as_str());
this.imp().toast_overlay.add_toast(adw::Toast::new("Copied to clipboard"));
}));
Expand All @@ -267,7 +267,8 @@ impl Window {
imp.scroll_ctrl
.set_flags(gtk::EventControllerScrollFlags::VERTICAL);
imp.scroll_ctrl.connect_scroll(
clone!(@weak self as this => @default-panic, move |ctrl, _, y| {
clone!(#[weak(rename_to = this)] self, #[upgrade_or_panic]
move |ctrl, _, y| {
let up = y < 0.0;
if let Some(gdk::ModifierType::CONTROL_MASK) = ctrl.current_event().map(|e| e.modifier_state()) {
if up {
Expand All @@ -283,7 +284,7 @@ impl Window {
);
imp.mouse_prev_next_ctrl.set_button(0);
imp.mouse_prev_next_ctrl.connect_pressed(
clone!(@weak self as this => @default-panic, move |ctrl, _, _, _| {
clone!(#[weak(rename_to = this)] self, move |ctrl, _, _, _| {
match ctrl.current_button() {
8 => {
this.previous();
Expand All @@ -299,7 +300,8 @@ impl Window {
self.connect_local(
"notify::url",
false,
clone!(@weak self as this => @default-panic, move |_| {
clone!(#[weak(rename_to = this)] self, #[upgrade_or_default]
move |_| {
this.update_domain_color();

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

imp.tab_view.connect_selected_page_notify(
clone!(@weak self as this => @default-panic, move |tab_view| {
clone!(#[weak(rename_to = this)] self, move |tab_view| {
this.page_switched(tab_view);
}),
);
imp.tab_view.connect_close_page(
clone!(@weak self as this => @default-panic, move |tab_view, page| {
clone!(#[weak(rename_to = this)] self,
#[upgrade_or_panic]
move |tab_view, page| {
tab_view.close_page_finish(page, !page.is_pinned());

if tab_view.n_pages() == 0 {
Expand All @@ -329,19 +333,21 @@ impl Window {
}),
);
imp.tab_overview.connect_create_tab(
clone!(@weak self as this => @default-panic, move |_| {
clone!(#[weak(rename_to = this)] self,
#[upgrade_or_panic]
move |_| {
this.new_tab();
this.imp().tab_view.selected_page().unwrap()
}),
);

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

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

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

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

glibctx().spawn_local(clone!(@weak imp => async move {
glibctx().spawn_local(clone!(#[weak] imp, async move {
match Self::append_bookmark(&url).await {
Ok(_) => {
info!("{} saved to bookmarks", url);
Expand Down

0 comments on commit 7b996f7

Please sign in to comment.