draft history navigation

This commit is contained in:
yggverse 2024-09-30 21:02:37 +03:00
parent fceb3a75a4
commit f44b48a8ab
5 changed files with 71 additions and 14 deletions

View File

@ -89,10 +89,13 @@ impl Page {
.get::<String>()
.expect("Parameter does not match `String`");
let request = GString::from(uri);
navigation.set_request_text(
&GString::from(uri),
true, // activate (page reload)
&request, true, // activate (page reload)
);
navigation.add_history(request);
}
});

View File

@ -85,6 +85,10 @@ impl Navigation {
self.request.widget().grab_focus();
}
pub fn add_history(&self, request: GString) {
self.history.add(request, true);
}
pub fn update(&self, progress_fraction: Option<f64>) {
self.base.update(self.request.uri());
self.history.update();

View File

@ -17,8 +17,8 @@ pub struct History {
back: Back,
forward: Forward,
// Extras
memory: Vec<Memory>,
index: RefCell<i32>,
memory: RefCell<Vec<Memory>>,
index: RefCell<Option<usize>>,
// GTK
widget: Box,
}
@ -45,10 +45,10 @@ impl History {
widget.append(forward.widget());
// Init memory
let memory = Vec::new();
let memory = RefCell::new(Vec::new());
// Init index
let index = RefCell::new(-1);
let index = RefCell::new(None);
Self {
// Actions
@ -63,9 +63,55 @@ impl History {
}
// Actions
pub fn add(&self, request: GString, follow_to_index: bool) {
// Append new Memory record
self.memory.borrow_mut().push(Memory { request, time: 0 });
if follow_to_index {
// Navigate to the last record appended
self.index.replace(Some(self.memory.borrow().len()));
}
}
pub fn try_back(&self, follow_to_index: bool) -> Option<GString> {
if let Some(index) = self.index.take() {
if let Some(memory) = self.memory.borrow().get(index - 1) {
if follow_to_index {
self.index.replace(Some(index - 1));
}
return Some(memory.request.clone());
}
}
None
}
/* @TODO
pub fn try_current(&self) -> bool {
true
} */
pub fn try_forward(&self, follow_to_index: bool) -> Option<GString> {
if let Some(index) = self.index.take() {
if let Some(memory) = self.memory.borrow().get(index + 1) {
if follow_to_index {
self.index.replace(Some(index + 1));
}
return Some(memory.request.clone());
}
}
None
}
pub fn update(&self) {
self.back.update();
self.forward.update();
match self.try_back(false) {
Some(_) => self.back.update(true),
None => self.back.update(false),
};
match self.try_forward(false) {
Some(_) => self.forward.update(true),
None => self.forward.update(false),
};
}
// Getters

View File

@ -1,6 +1,6 @@
use gtk::{
gio::SimpleAction,
prelude::{ActionExt, ButtonExt},
prelude::{ActionExt, ButtonExt, WidgetExt},
Button,
};
use std::sync::Arc;
@ -37,8 +37,10 @@ impl Back {
}
// Actions
pub fn update(&self) {
// @TODO
pub fn update(&self, status: bool) {
self.action_tab_page_navigation_history_back
.set_enabled(status);
self.widget.set_sensitive(status);
}
// Getters

View File

@ -1,5 +1,5 @@
use gtk::{
prelude::{ActionExt, ButtonExt},
prelude::{ActionExt, ButtonExt, WidgetExt},
{gio::SimpleAction, Button},
};
use std::sync::Arc;
@ -36,8 +36,10 @@ impl Forward {
}
// Actions
pub fn update(&self) {
// @TODO
pub fn update(&self, status: bool) {
self.action_tab_page_navigation_history_forward
.set_enabled(status);
self.widget.set_sensitive(status);
}
// Getters