Browse Source

update construction api

master
yggverse 1 month ago
parent
commit
cb651496fd
  1. 88
      src/app/browser/window/tab/item/page.rs
  2. 26
      src/app/browser/window/tab/item/page/content.rs
  3. 30
      src/app/browser/window/tab/item/page/content/status.rs
  4. 36
      src/app/browser/window/tab/item/page/content/status/failure.rs
  5. 20
      src/app/browser/window/tab/item/page/content/status/loading.rs

88
src/app/browser/window/tab/item/page.rs

@ -63,10 +63,7 @@ impl Page { @@ -63,10 +63,7 @@ impl Page {
SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type()));
// Init components
let content = Arc::new(Content::new(
action_tab_open.clone(),
action_page_open.clone(),
));
let content = Content::new_arc(action_tab_open.clone(), action_page_open.clone());
let navigation = Navigation::new_arc(
action_tab_page_navigation_base.clone(),
@ -189,11 +186,10 @@ impl Page { @@ -189,11 +186,10 @@ impl Page {
let description = gformat!("Protocol `{scheme}` not supported");
// Update widget
self.content.to_status_failure(
Some(title.as_str()),
Some(description.as_str()),
None,
);
self.content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(description.as_str()));
// Update meta
self.meta.replace(Meta {
@ -524,11 +520,10 @@ impl Page { @@ -524,11 +520,10 @@ impl Page {
};
// Update widget
content.to_status_failure(
Some(title.as_str()),
Some(description.as_str()),
None
);
content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(description.as_str()));
// Update meta
meta.replace(Meta {
@ -551,8 +546,6 @@ impl Page { @@ -551,8 +546,6 @@ impl Page {
=> {
// Final image size unknown, show loading widget
let status = content.to_status_loading(
Some(&"Loading.."),
None,
Some(Duration::from_secs(1)) // show if download time > 1 second
);
@ -594,11 +587,10 @@ impl Page { @@ -594,11 +587,10 @@ impl Page {
let title = gformat!("Oops");
// Update widget
content.to_status_failure(
Some(title.as_str()),
Some(reason.message()),
None
);
content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(reason.message()));
// Update meta
meta.replace(Meta {
@ -623,17 +615,15 @@ impl Page { @@ -623,17 +615,15 @@ impl Page {
};
// Update widget
content.to_status_failure(
Some(title.as_str()),
Some(description.as_str()),
None
);
content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(description.as_str()));
// Update meta
meta.replace(Meta {
status: Some(status),
title: Some(title),
//description: Some(description),
});
}
},
@ -660,11 +650,10 @@ impl Page { @@ -660,11 +650,10 @@ impl Page {
let description = gformat!("Content type not supported");
// Update widget
content.to_status_failure(
Some(title.as_str()),
Some(description.as_str()),
None
);
content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(description.as_str()));
// Update meta
meta.replace(Meta {
@ -696,11 +685,9 @@ impl Page { @@ -696,11 +685,9 @@ impl Page {
);
},
None => {
content.to_status_failure(
Some(&"Oops"),
Some(&"Could not parse redirect meta"),
None
);
content
.to_status_failure()
.set_description(Some("Could not parse redirect meta"));
},
}
@ -760,11 +747,10 @@ impl Page { @@ -760,11 +747,10 @@ impl Page {
};
// Update widget
content.to_status_failure(
Some(title.as_str()),
Some(description.as_str()),
None
);
content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(description.as_str()));
// Update meta
meta.replace(Meta {
@ -785,11 +771,10 @@ impl Page { @@ -785,11 +771,10 @@ impl Page {
let title = gformat!("Oops");
// Update widget
content.to_status_failure(
Some(title.as_str()),
Some(reason.message()),
None
);
content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(reason.message()));
// Update meta
meta.replace(Meta {
@ -809,11 +794,10 @@ impl Page { @@ -809,11 +794,10 @@ impl Page {
let title = gformat!("Oops");
// Update widget
content.to_status_failure(
Some(title.as_str()),
Some(reason.message()),
None
);
content
.to_status_failure()
.set_title(title.as_str())
.set_description(Some(reason.message()));
// Update meta
meta.replace(Meta {

26
src/app/browser/window/tab/item/page/content.rs

@ -13,7 +13,7 @@ use gtk::{ @@ -13,7 +13,7 @@ use gtk::{
prelude::{BoxExt, WidgetExt},
Box, Orientation,
};
use std::time::Duration;
use std::{sync::Arc, time::Duration};
pub struct Content {
// GTK
@ -27,12 +27,12 @@ impl Content { @@ -27,12 +27,12 @@ impl Content {
// Construct
/// Create new container for different components
pub fn new(action_tab_open: SimpleAction, action_page_open: SimpleAction) -> Self {
Self {
pub fn new_arc(action_tab_open: SimpleAction, action_page_open: SimpleAction) -> Arc<Self> {
Arc::new(Self {
gobject: Box::builder().orientation(Orientation::Vertical).build(),
action_tab_open,
action_page_open,
}
})
}
// Actions
@ -50,14 +50,9 @@ impl Content { @@ -50,14 +50,9 @@ impl Content {
/// Set new `content::Status` component for `Self` with new `status::Failure` preset
///
/// * action removes previous children component from `Self`
pub fn to_status_failure(
&self,
title: Option<&str>,
description: Option<&str>,
icon_name: Option<&str>,
) -> Status {
pub fn to_status_failure(&self) -> Status {
self.clean();
let status = Status::new_failure(title, description, icon_name);
let status = Status::new_failure();
self.gobject.append(status.gobject());
status
}
@ -65,14 +60,9 @@ impl Content { @@ -65,14 +60,9 @@ impl Content {
/// Set new `content::Status` component for `Self` with new `status::Loading` preset
///
/// * action removes previous children component from `Self`
pub fn to_status_loading(
&self,
title: Option<&str>,
description: Option<&str>,
show_with_delay: Option<Duration>,
) -> Status {
pub fn to_status_loading(&self, show_with_delay: Option<Duration>) -> Status {
self.clean();
let status = Status::new_loading(title, description, show_with_delay);
let status = Status::new_loading(show_with_delay);
self.gobject.append(status.gobject());
status
}

30
src/app/browser/window/tab/item/page/content/status.rs

@ -14,38 +14,38 @@ impl Status { @@ -14,38 +14,38 @@ impl Status {
/// Create new failure preset
///
/// Useful as placeholder widget for error handlers
pub fn new_failure(
title: Option<&str>,
description: Option<&str>,
icon_name: Option<&str>,
) -> Self {
pub fn new_failure() -> Self {
Self {
gobject: failure::new_gobject_from(title, description, icon_name),
gobject: failure::new_gobject(),
}
}
/// Create new loading preset
///
/// Useful as placeholder widget for async operations
pub fn new_loading(
title: Option<&str>,
description: Option<&str>,
show_with_delay: Option<Duration>,
) -> Self {
pub fn new_loading(show_with_delay: Option<Duration>) -> Self {
Self {
gobject: loading::new_gobject_from(title, description, show_with_delay),
gobject: loading::new_gobject(show_with_delay),
}
}
// Setters
/// Set new description for status component
/// Set new title for `Self`
///
/// Return `Self` reference to apply another functions in chain
pub fn set_title(&self, value: &str) -> &Self {
self.gobject.set_title(value);
&self
}
/// Set new description for `Self`
///
/// Useful for loading widgets to update byte totals and other dynamically changed information
///
/// Return `Self` reference to apply another functions in chain
pub fn set_description(&self, description: Option<&str>) -> &Self {
self.gobject.set_description(description);
pub fn set_description(&self, value: Option<&str>) -> &Self {
self.gobject.set_description(value);
&self
}

36
src/app/browser/window/tab/item/page/content/status/failure.rs

@ -1,31 +1,13 @@ @@ -1,31 +1,13 @@
use adw::StatusPage;
const DEFAULT_TITLE: &str = "Oops";
const DEFAULT_DESCRIPTION: Option<&str> = None;
const DEFAULT_ICON_NAME: Option<&str> = Some("dialog-error");
/// Create new `GObject` preset for failure [StatusPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.StatusPage.html)
pub fn new_gobject_from(
title: Option<&str>,
description: Option<&str>,
icon_name: Option<&str>,
) -> StatusPage {
let gobject = StatusPage::new();
gobject.set_title(match title {
Some(value) => value,
None => DEFAULT_TITLE,
});
gobject.set_description(match description {
Some(value) => Some(value),
None => DEFAULT_DESCRIPTION,
});
gobject.set_icon_name(match icon_name {
Some(value) => Some(value),
None => DEFAULT_ICON_NAME,
});
gobject
const DEFAULT_ICON_NAME: &str = "dialog-error";
/// Create new default `GObject` preset for failure
/// [StatusPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.StatusPage.html)
pub fn new_gobject() -> StatusPage {
StatusPage::builder()
.title(DEFAULT_TITLE)
.icon_name(DEFAULT_ICON_NAME)
.build()
}

20
src/app/browser/window/tab/item/page/content/status/loading.rs

@ -5,15 +5,12 @@ use gtk::{ @@ -5,15 +5,12 @@ use gtk::{
};
use std::time::Duration;
/// 16-64 (px)
const SPINNER_SIZE: i32 = 64;
const SPINNER_SIZE: i32 = 64; // 16-64
const DEFAULT_TITLE: &str = "Loading..";
/// Create new `GObject` preset for loading [StatusPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.StatusPage.html)
pub fn new_gobject_from(
title: Option<&str>,
description: Option<&str>,
show_with_delay: Option<Duration>,
) -> StatusPage {
/// Create new default `GObject` preset for loading
/// [StatusPage](https://gnome.pages.gitlab.gnome.org/libadwaita/doc/main/class.StatusPage.html)
pub fn new_gobject(show_with_delay: Option<Duration>) -> StatusPage {
let gobject = StatusPage::builder()
.child(
&Spinner::builder()
@ -21,14 +18,9 @@ pub fn new_gobject_from( @@ -21,14 +18,9 @@ pub fn new_gobject_from(
.height_request(SPINNER_SIZE)
.build(),
)
.title(DEFAULT_TITLE)
.build();
if let Some(value) = title {
gobject.set_title(value);
}
gobject.set_description(description);
if let Some(duration) = show_with_delay {
gobject.set_visible(false);
timeout_add_local(duration, {

Loading…
Cancel
Save