From f2dadee082884cfaad39bab9fe57926e094f7fe1 Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 12 Dec 2024 12:42:47 +0200 Subject: [PATCH] add optional download button for unknown mime types --- src/app/browser/window/tab/item/page.rs | 5 +++- .../browser/window/tab/item/page/content.rs | 8 +++--- .../window/tab/item/page/content/status.rs | 10 +++++-- .../tab/item/page/content/status/mime.rs | 28 ++++++++++++++++--- 4 files changed, 39 insertions(+), 12 deletions(-) diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index 16609fc3..a8fc076d 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -679,7 +679,10 @@ impl Page { }, mime => { // Init children widget - let status = content.to_status_mime(mime); + let status = content.to_status_mime( + mime, + Some((tab_action.clone(), navigation.request.download())) + ); // Update page meta meta.set_status(Status::Failure) diff --git a/src/app/browser/window/tab/item/page/content.rs b/src/app/browser/window/tab/item/page/content.rs index 82b79162..5dfb6ed8 100644 --- a/src/app/browser/window/tab/item/page/content.rs +++ b/src/app/browser/window/tab/item/page/content.rs @@ -6,11 +6,11 @@ use image::Image; use status::Status; use text::Text; -use crate::app::browser::window::{tab::item::Action as TabAction, Action as WindowAction}; +use super::{TabAction, WindowAction}; use gtk::{ gdk::Paintable, gio::{Cancellable, File}, - glib::Uri, + glib::{GString, Uri}, prelude::{BoxExt, IsA, WidgetExt}, Box, Orientation, }; @@ -74,9 +74,9 @@ impl Content { /// Set new `content::Status` component for `Self` with new `status::Mime` issue preset /// /// * action removes previous children component from `Self` - pub fn to_status_mime(&self, mime: &str) -> Status { + pub fn to_status_mime(&self, mime: &str, download: Option<(Rc, GString)>) -> Status { self.clean(); - let status = Status::new_mime(mime); + let status = Status::new_mime(mime, download); self.gobject.append(status.gobject()); status } diff --git a/src/app/browser/window/tab/item/page/content/status.rs b/src/app/browser/window/tab/item/page/content/status.rs index 8a62defc..898bb4c7 100644 --- a/src/app/browser/window/tab/item/page/content/status.rs +++ b/src/app/browser/window/tab/item/page/content/status.rs @@ -4,8 +4,12 @@ mod identity; mod loading; mod mime; +use super::TabAction; use adw::StatusPage; -use gtk::gio::{Cancellable, File}; +use gtk::{ + gio::{Cancellable, File}, + glib::GString, +}; use std::{rc::Rc, time::Duration}; pub struct Status { @@ -38,9 +42,9 @@ impl Status { /// Create new mime issue preset /// /// Useful as placeholder widget for mime issue handlers - pub fn new_mime(mime: &str) -> Self { + pub fn new_mime(mime: &str, download: Option<(Rc, GString)>) -> Self { Self { - gobject: mime::new_gobject(mime), + gobject: mime::new_gobject(mime, download), } } diff --git a/src/app/browser/window/tab/item/page/content/status/mime.rs b/src/app/browser/window/tab/item/page/content/status/mime.rs index 86567f7a..3abde42e 100644 --- a/src/app/browser/window/tab/item/page/content/status/mime.rs +++ b/src/app/browser/window/tab/item/page/content/status/mime.rs @@ -1,11 +1,31 @@ +use super::TabAction; use adw::StatusPage; +use gtk::{glib::GString, prelude::ButtonExt, Align, Button}; +use std::rc::Rc; /// Create new default `GObject` preset for mime issue /// [StatusPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.StatusPage.html) -pub fn new_gobject(mime: &str) -> StatusPage { - StatusPage::builder() - .title("Oops") +pub fn new_gobject(mime: &str, download: Option<(Rc, GString)>) -> StatusPage { + let status_page = StatusPage::builder() .description(format!("Content type `{mime}` not supported!")) .icon_name("dialog-question-symbolic") - .build() + .title("Oops") + .build(); + + if let Some((action, request)) = download { + let button = Button::builder() + .css_classes(["accent"]) + .halign(Align::Center) + .label("Download") + .tooltip_text("Download as file to open with other application") + .build(); + + button.connect_clicked(move |_| { + action.load.activate(Some(request.as_str()), true); + }); + + status_page.set_child(Some(&button)); + } + + status_page }