Browse Source

implement base button, rename navigation actions

master
yggverse 2 months ago
parent
commit
68898d8da0
  1. 9
      src/browser.rs
  2. 15
      src/browser/main.rs
  3. 21
      src/browser/main/tab.rs
  4. 14
      src/browser/main/tab/page.rs
  5. 7
      src/browser/main/tab/page/navigation.rs
  6. 52
      src/browser/main/tab/page/navigation/base.rs

9
src/browser.rs

@ -60,6 +60,7 @@ impl Browser {
)); ));
let main = Arc::new(Main::new( let main = Arc::new(Main::new(
action_tab_page_navigation_base.clone(),
action_tab_page_navigation_reload.clone(), action_tab_page_navigation_reload.clone(),
action_update.clone(), action_update.clone(),
)); ));
@ -134,19 +135,19 @@ impl Browser {
action_tab_page_navigation_base.connect_activate({ action_tab_page_navigation_base.connect_activate({
let main = main.clone(); let main = main.clone();
move |_, _| { move |_, _| {
// @TODO main.tab_page_navigation_base();
} }
}); });
action_tab_page_navigation_history_back.connect_activate({ action_tab_page_navigation_history_back.connect_activate({
let main = main.clone(); // let main = main.clone();
move |_, _| { move |_, _| {
// @TODO // @TODO
} }
}); });
action_tab_page_navigation_history_forward.connect_activate({ action_tab_page_navigation_history_forward.connect_activate({
let main = main.clone(); // let main = main.clone();
move |_, _| { move |_, _| {
// @TODO // @TODO
} }
@ -155,7 +156,7 @@ impl Browser {
action_tab_page_navigation_reload.connect_activate({ action_tab_page_navigation_reload.connect_activate({
let main = main.clone(); let main = main.clone();
move |_, _| { move |_, _| {
main.tab_page_reload(); main.tab_page_navigation_reload();
} }
}); });

15
src/browser/main.rs

@ -14,11 +14,16 @@ pub struct Main {
impl Main { impl Main {
// Construct // Construct
pub fn new( pub fn new(
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>, action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>, action_update: Arc<SimpleAction>,
) -> Self { ) -> Self {
// Init components // Init components
let tab = Arc::new(Tab::new(action_tab_page_navigation_reload, action_update)); let tab = Arc::new(Tab::new(
action_tab_page_navigation_base,
action_tab_page_navigation_reload,
action_update,
));
tab.activate(tab.clone()); tab.activate(tab.clone());
tab.append(Some(GString::from("gemini://geminiprotocol.net/")), true); // demo tab @TODO replace with session restore feature tab.append(Some(GString::from("gemini://geminiprotocol.net/")), true); // demo tab @TODO replace with session restore feature
@ -36,8 +41,12 @@ impl Main {
self.tab.append(tab_page_navigation_request_text, true); self.tab.append(tab_page_navigation_request_text, true);
} }
pub fn tab_page_reload(&self) { pub fn tab_page_navigation_base(&self) {
self.tab.page_reload(); self.tab.page_navigation_base();
}
pub fn tab_page_navigation_reload(&self) {
self.tab.page_navigation_reload();
} }
pub fn tab_close(&self) { pub fn tab_close(&self) {

21
src/browser/main/tab.rs

@ -17,6 +17,7 @@ pub struct Tab {
// GTK // GTK
widget: Notebook, widget: Notebook,
// Keep action links in memory to not require them on every tab append // Keep action links in memory to not require them on every tab append
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>, action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>, action_update: Arc<SimpleAction>,
// Dynamically allocated reference index // Dynamically allocated reference index
@ -27,6 +28,7 @@ pub struct Tab {
impl Tab { impl Tab {
// Construct // Construct
pub fn new( pub fn new(
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>, action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>, action_update: Arc<SimpleAction>,
) -> Self { ) -> Self {
@ -38,6 +40,7 @@ impl Tab {
// GTK // GTK
widget, widget,
// Define action links // Define action links
action_tab_page_navigation_base,
action_tab_page_navigation_reload, action_tab_page_navigation_reload,
action_update, action_update,
// Init empty HashMap index as no tabs appended yet // Init empty HashMap index as no tabs appended yet
@ -76,6 +79,7 @@ impl Tab {
let page = Arc::new(Page::new( let page = Arc::new(Page::new(
id.clone(), id.clone(),
page_navigation_request_text.clone(), page_navigation_request_text.clone(),
self.action_tab_page_navigation_base.clone(),
self.action_tab_page_navigation_reload.clone(), self.action_tab_page_navigation_reload.clone(),
self.action_update.clone(), self.action_update.clone(),
)); ));
@ -144,14 +148,27 @@ impl Tab {
} }
} }
pub fn page_reload(&self) { pub fn page_navigation_base(&self) {
// Get current page // Get current page
if let Some(page_number) = self.widget.current_page() { if let Some(page_number) = self.widget.current_page() {
// Get default widget to extract it name as the ID for childs // Get default widget to extract it name as the ID for childs
if let Some(widget) = self.widget.nth_page(Some(page_number)) { if let Some(widget) = self.widget.nth_page(Some(page_number)) {
// Get page by widget ID // Get page by widget ID
if let Some(page) = self.pages.borrow().get(&widget.widget_name()) { if let Some(page) = self.pages.borrow().get(&widget.widget_name()) {
page.reload(); page.navigation_base();
}
}
}
}
pub fn page_navigation_reload(&self) {
// Get current page
if let Some(page_number) = self.widget.current_page() {
// Get default widget to extract it name as the ID for childs
if let Some(widget) = self.widget.nth_page(Some(page_number)) {
// Get page by widget ID
if let Some(page) = self.pages.borrow().get(&widget.widget_name()) {
page.navigation_reload();
} }
} }
} }

14
src/browser/main/tab/page.rs

@ -14,7 +14,7 @@ use gtk::{
glib::{gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags}, glib::{gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags},
prelude::{ prelude::{
ActionExt, ActionMapExt, BoxExt, IOStreamExt, InputStreamExtManual, OutputStreamExtManual, ActionExt, ActionMapExt, BoxExt, IOStreamExt, InputStreamExtManual, OutputStreamExtManual,
SocketClientExt, StaticVariantType, WidgetExt, SocketClientExt, StaticVariantType, ToVariant, WidgetExt,
}, },
Box, Orientation, Box, Orientation,
}; };
@ -24,6 +24,7 @@ pub struct Page {
// GTK // GTK
widget: Box, widget: Box,
// Actions // Actions
action_page_open: Arc<SimpleAction>,
// action_tab_page_navigation_reload: Arc<SimpleAction>, // action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>, action_update: Arc<SimpleAction>,
// Components // Components
@ -38,6 +39,7 @@ impl Page {
pub fn new( pub fn new(
name: GString, name: GString,
navigation_request_text: Option<GString>, navigation_request_text: Option<GString>,
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>, action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>, action_update: Arc<SimpleAction>,
) -> Page { ) -> Page {
@ -55,6 +57,7 @@ impl Page {
let content = Arc::new(Content::new(action_page_open.clone())); let content = Arc::new(Content::new(action_page_open.clone()));
let navigation = Arc::new(Navigation::new( let navigation = Arc::new(Navigation::new(
navigation_request_text, navigation_request_text,
action_tab_page_navigation_base.clone(),
action_tab_page_navigation_reload.clone(), action_tab_page_navigation_reload.clone(),
action_update.clone(), action_update.clone(),
)); ));
@ -94,6 +97,7 @@ impl Page {
// GTK // GTK
widget, widget,
// Actions // Actions
action_page_open,
// action_tab_page_navigation_reload, // action_tab_page_navigation_reload,
action_update, action_update,
// Components // Components
@ -109,7 +113,13 @@ impl Page {
self.navigation.grab_request_focus(); self.navigation.grab_request_focus();
} }
pub fn reload(&self) { pub fn navigation_base(&self) {
if let Some(address) = self.navigation.base_address() {
self.action_page_open.activate(Some(&address.to_variant()));
}
}
pub fn navigation_reload(&self) {
// Init globals // Init globals
let request_text = self.navigation.request_text(); let request_text = self.navigation.request_text();

7
src/browser/main/tab/page/navigation.rs

@ -33,11 +33,12 @@ pub struct Navigation {
impl Navigation { impl Navigation {
pub fn new( pub fn new(
request_text: Option<GString>, request_text: Option<GString>,
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_reload: Arc<SimpleAction>, action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>, action_update: Arc<SimpleAction>,
) -> Self { ) -> Self {
// Init components // Init components
let base = Base::new(); let base = Base::new(action_tab_page_navigation_base);
let history = History::new(); let history = History::new();
let reload = Reload::new(action_tab_page_navigation_reload.clone()); let reload = Reload::new(action_tab_page_navigation_reload.clone());
let request = Request::new( let request = Request::new(
@ -102,6 +103,10 @@ impl Navigation {
&self.widget &self.widget
} }
pub fn base_address(&self) -> Option<GString> {
self.base.address()
}
pub fn request_text(&self) -> GString { pub fn request_text(&self) -> GString {
self.request.text() self.request.text()
} }

52
src/browser/main/tab/page/navigation/base.rs

@ -1,35 +1,75 @@
use gtk::{glib::Uri, prelude::WidgetExt, Button}; use gtk::{
gio::SimpleAction,
glib::{gformat, GString, Uri},
prelude::{ActionExt, ButtonExt, WidgetExt},
Button,
};
use std::{cell::RefCell, sync::Arc};
pub struct Base { pub struct Base {
action_tab_page_navigation_base: Arc<SimpleAction>,
uri: RefCell<Option<Uri>>,
widget: Button, widget: Button,
} }
impl Base { impl Base {
// Construct // Construct
pub fn new() -> Self { pub fn new(action_tab_page_navigation_base: Arc<SimpleAction>) -> Self {
// Init widget
let widget = Button::builder() let widget = Button::builder()
.action_name("win.tab_page_base")
.icon_name("go-home-symbolic") .icon_name("go-home-symbolic")
.tooltip_text("Base") .tooltip_text("Base")
.sensitive(false) .sensitive(false)
.build(); .build();
Self { widget } // Init events
widget.connect_clicked({
let action_tab_page_navigation_base = action_tab_page_navigation_base.clone();
move |_| {
action_tab_page_navigation_base.activate(None);
}
});
// Return activated struct
Self {
action_tab_page_navigation_base,
uri: RefCell::new(None),
widget,
}
} }
// Actions // Actions
pub fn update(&self, uri: Option<Uri>) { pub fn update(&self, uri: Option<Uri>) {
let status = match uri { // Update sensitivity
let status = match &uri {
Some(uri) => "/" != uri.path(), Some(uri) => "/" != uri.path(),
None => false, None => false,
}; };
self.widget.action_set_enabled("win.tab_page_base", status); self.action_tab_page_navigation_base.set_enabled(status);
self.widget.set_sensitive(status); self.widget.set_sensitive(status);
// Update parsed cache
self.uri.replace(uri);
} }
// Getters // Getters
pub fn widget(&self) -> &Button { pub fn widget(&self) -> &Button {
&self.widget &self.widget
} }
pub fn address(&self) -> Option<GString> {
if let Some(uri) = self.uri.take() {
let scheme = uri.scheme();
let port = uri.port();
if let Some(host) = uri.host() {
if port.is_positive() {
return Some(gformat!("{scheme}://{host}:{port}/"));
} else {
return Some(gformat!("{scheme}://{host}/"));
}
}
}
None
}
} }

Loading…
Cancel
Save