page controller contains lot of code, begin components reorganization before implement new features

This commit is contained in:
yggverse 2025-01-15 03:47:21 +02:00
parent 2cab9e4287
commit a35d86a630
6 changed files with 153 additions and 80 deletions

View file

@ -0,0 +1,67 @@
mod item;
use item::Item;
use gtk::glib::Uri;
use std::cell::{Cell, RefCell};
/// Global limit to prevent infinitive redirection issues
/// * defined value is globally applicable to ALL drivers
/// * every driver implement its own value, according to protocol specification
/// * the `Client` will forcefully break redirection loop when iteration reach this value
pub const LIMIT: usize = 10; // @TODO make optional
pub struct Redirect {
chain: RefCell<Vec<Item>>,
}
impl Default for Redirect {
fn default() -> Self {
Self::new()
}
}
impl Redirect {
// Constructors
/// Create new `Self`
pub fn new() -> Self {
Self {
chain: RefCell::new(Vec::new()),
}
}
// Actions
/// Register new redirect in chain
pub fn add(&self, request: Uri, referrer: Option<Uri>, is_foreground: bool) -> &Self {
self.chain.borrow_mut().push(Item {
request,
referrer,
is_foreground,
is_processed: Cell::new(false),
});
self
}
/// Clear redirect chain
pub fn clear(&self) {
self.chain.borrow_mut().clear()
}
// Getters
/// Get total redirects count in chain
pub fn count(&self) -> usize {
self.chain.borrow().len() + 1
}
/// Get last redirection `Item` copy
pub fn last(&self) -> Option<Item> {
if let Some(redirect) = self.chain.borrow().last() {
if !redirect.is_processed.replace(true) {
return Some(redirect.clone());
}
}
None
}
}

View file

@ -0,0 +1,11 @@
use gtk::glib::Uri;
use std::cell::Cell;
/// Single redirect `Item`
#[derive(Clone)]
pub struct Item {
pub is_foreground: bool,
pub is_processed: Cell<bool>,
pub referrer: Option<Uri>,
pub request: Uri,
}

View file

@ -0,0 +1,36 @@
use std::fmt::{Display, Formatter, Result};
/// Local `Client` status
/// * not same as the Gemini status!
pub enum Status {
/// Ready to use (or cancel from outside)
Cancellable,
/// Operation cancelled, new `Cancellable` required to continue
Cancelled,
/// Redirection count limit reached by protocol driver or global settings
RedirectLimit(usize),
/// New `request` begin
Request(String),
}
impl Display for Status {
fn fmt(&self, f: &mut Formatter) -> Result {
match self {
Self::Cancellable => {
write!(f, "Ready to use (or cancel from outside)")
}
Self::Cancelled => {
write!(
f,
"Operation cancelled, new `Cancellable` required to continue"
)
}
Self::RedirectLimit(count) => {
write!(f, "Redirection count limit ({count}) reached by protocol driver or global settings")
}
Self::Request(value) => {
write!(f, "Request `{value}`...")
}
}
}
}