add view source menu item

This commit is contained in:
yggverse 2024-12-11 16:52:42 +02:00
parent 85735d33e2
commit e66b065fc3
8 changed files with 124 additions and 0 deletions

View File

@ -197,6 +197,14 @@ impl App {
),
&["<Primary>s"],
),
(
format!(
"{}.{}",
browser.window.action.id,
browser.window.action.source.gobject.name()
),
&["<Primary>u"],
),
(
format!(
"{}.{}",

View File

@ -86,6 +86,11 @@ impl Window {
move |position| tab.save_as(position)
});
action.source.connect_activate({
let tab = tab.clone();
move |position| tab.source(position)
});
action.history_back.connect_activate({
let tab = tab.clone();
move |position| {

View File

@ -8,6 +8,7 @@ mod home;
mod pin;
mod reload;
mod save_as;
mod source;
use append::Append;
use bookmark::Bookmark;
@ -19,6 +20,7 @@ use home::Home;
use pin::Pin;
use reload::Reload;
use save_as::SaveAs;
use source::Source;
use gtk::{
gio::SimpleActionGroup,
@ -42,6 +44,7 @@ pub struct Action {
pub pin: Rc<Pin>,
pub reload: Rc<Reload>,
pub save_as: Rc<SaveAs>,
pub source: Rc<Source>,
// Group
pub id: GString,
pub gobject: SimpleActionGroup,
@ -63,6 +66,7 @@ impl Action {
let pin = Rc::new(Pin::new());
let reload = Rc::new(Reload::new());
let save_as = Rc::new(SaveAs::new());
let source = Rc::new(Source::new());
// Generate unique group ID
let id = uuid_string_random();
@ -81,6 +85,7 @@ impl Action {
gobject.add_action(&pin.gobject);
gobject.add_action(&reload.gobject);
gobject.add_action(&save_as.gobject);
gobject.add_action(&source.gobject);
// Done
Self {
@ -94,6 +99,7 @@ impl Action {
pin,
reload,
save_as,
source,
id,
gobject,
}

View File

@ -0,0 +1,64 @@
use gtk::{
gio::SimpleAction,
glib::uuid_string_random,
prelude::{ActionExt, ToVariant},
};
// Defaults
/// C-compatible variant type
const DEFAULT_STATE: i32 = -1;
/// [SimpleAction](https://docs.gtk.org/gio/class.SimpleAction.html) wrapper for `Source` action of `Window` group
pub struct Source {
pub gobject: SimpleAction,
}
impl Source {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
gobject: SimpleAction::new_stateful(
&uuid_string_random(),
None,
&DEFAULT_STATE.to_variant(),
),
}
}
// Actions
/// Change action [state](https://docs.gtk.org/gio/method.SimpleAction.set_state.html)
/// * set `DEFAULT_STATE` on `None`
pub fn change_state(&self, state: Option<i32>) {
self.gobject.change_state(
&match state {
Some(value) => value,
None => DEFAULT_STATE,
}
.to_variant(),
)
}
// Events
/// Define callback function for
/// [SimpleAction::activate](https://docs.gtk.org/gio/signal.SimpleAction.activate.html) signal
pub fn connect_activate(&self, callback: impl Fn(Option<i32>) + 'static) {
self.gobject.connect_activate(move |this, _| {
let state = this
.state()
.expect("State value required")
.get::<i32>()
.expect("Parameter type does not match `i32`");
callback(if state == DEFAULT_STATE {
None
} else {
Some(state)
})
});
}
}

View File

@ -59,6 +59,17 @@ impl Menu {
main_page.append_section(None, &main_page_mark);
// Main > Page > Tools
let main_page_tools = gio::Menu::new();
main_page_tools.append(Some("Source"), Some(&format!(
"{}.{}",
window_action.id,
window_action.source.gobject.name()
)));
main_page.append_section(None, &main_page_tools);
// Main > Page > Navigation
let main_page_navigation = gio::Menu::new();

View File

@ -60,6 +60,7 @@ impl Tab {
action.pin.change_state(state);
action.reload.change_state(state);
action.save_as.change_state(state);
action.source.change_state(state);
}
});
@ -161,6 +162,14 @@ impl Tab {
}
}
// View source for page at given `position`, `None` to use selected page (if available)
pub fn source(&self, page_position: Option<i32>) {
if let Some(item) = self.item(page_position) {
item.page.navigation.request.to_source();
item.page.load(true);
}
}
/// Toggle `Bookmark` in current `Profile` for `Page` at given `position` (current page on `None`)
/// * return `true` on bookmark created, `false` on deleted; `Error` otherwise.
pub fn bookmark(&self, page_position: Option<i32>) -> Result<bool, Error> {

View File

@ -94,6 +94,10 @@ impl Request {
self.widget.entry.set_text(&self.download());
}
pub fn to_source(&self) {
self.widget.entry.set_text(&self.source());
}
// Getters
pub fn uri(&self) -> Option<Uri> {
@ -120,6 +124,10 @@ impl Request {
pub fn download(&self) -> GString {
gformat!("download:{}", self.strip_prefix())
}
pub fn source(&self) -> GString {
gformat!("source:{}", self.strip_prefix())
}
}
// Tools

View File

@ -56,6 +56,19 @@ impl Menu {
main.append_section(None, &main_mark);
let main_tools = gtk::gio::Menu::new();
main_tools.append(
Some("Source"),
Some(&format!(
"{}.{}",
window_action.id,
window_action.source.gobject.name()
)),
);
main.append_section(None, &main_tools);
let navigation = gtk::gio::Menu::new();
navigation.append(