diff --git a/src/common/mod.rs b/src/common/mod.rs index eb3f777..9d4aecf 100644 --- a/src/common/mod.rs +++ b/src/common/mod.rs @@ -1,4 +1,4 @@ -use gtk::glib; +use gtk::{glib, gio}; use once_cell::sync::Lazy; use url::Url; @@ -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::<>k::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::<>k::Window>, None::<&gio::Cancellable>, |res| { + if let Err(e) = res { + log::error!("error opening external file {:?}", e); + } + }); +} diff --git a/src/macros.rs b/src/macros.rs index 770fb75..086928d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -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 } diff --git a/src/widgets/pages/hypertext.rs b/src/widgets/pages/hypertext.rs index 7175542..e788a7c 100644 --- a/src/widgets/pages/hypertext.rs +++ b/src/widgets/pages/hypertext.rs @@ -237,7 +237,7 @@ 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); }; @@ -245,14 +245,14 @@ impl Hypertext { ); 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); })); diff --git a/src/widgets/tab.rs b/src/widgets/tab.rs index 65970ec..7440078 100644 --- a/src/widgets/tab.rs +++ b/src/widgets/tab.rs @@ -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; @@ -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::<>k::Window>, &downloaded_file_url, 0); + open_file_externally(&std::path::Path::new(&downloaded_file_url)); }); let ext = file_name.split('.').last(); @@ -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::<>k::Window>, url_clone.as_str(), 0); + open_uri_externally(url_clone.as_str()); }); child.append(&button); @@ -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 { @@ -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()); @@ -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()); diff --git a/src/widgets/window.rs b/src/widgets/window.rs index ff7af4e..992fba2 100644 --- a/src/widgets/window.rs +++ b/src/widgets/window.rs @@ -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; }}", @@ -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::().unwrap().as_str())), + clone!(#[weak(rename_to = this)] self, move |_,v| this.open_omni(v.unwrap().get::().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::().unwrap().as_str())), + clone!(#[weak(rename_to = this)] self, move |_,v| this.open_url_str(v.unwrap().get::().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::().unwrap().as_str())), + clone!(#[weak(rename_to = this)] self, move |_,v| this.open_in_new_tab(v.unwrap().get::().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::().unwrap().as_str()); this.imp().toast_overlay.add_toast(adw::Toast::new("Copied to clipboard")); })); @@ -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 { @@ -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(); @@ -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; @@ -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 { @@ -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() }), ); @@ -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"), @@ -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::() { for f in files.files() { this.open_in_new_tab(&format!("file://{}", f.path().unwrap().to_str().unwrap())); @@ -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);