From c22413b9737fed0f40ce60736c0393e184de4c3f Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 11 Dec 2024 07:01:08 +0200 Subject: [PATCH] show icon depending of current entry value --- .../window/tab/item/page/navigation.rs | 1 - .../tab/item/page/navigation/request.rs | 13 +-- .../item/page/navigation/request/widget.rs | 88 ++++++++++--------- .../navigation/request/widget/primary_icon.rs | 46 ++++++++++ 4 files changed, 96 insertions(+), 52 deletions(-) create mode 100644 src/app/browser/window/tab/item/page/navigation/request/widget/primary_icon.rs diff --git a/src/app/browser/window/tab/item/page/navigation.rs b/src/app/browser/window/tab/item/page/navigation.rs index ca6a78d4..8042e366 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -76,7 +76,6 @@ impl Navigation { self.reload.update(!request_text.is_empty()); self.request.update( progress_fraction, - !request_text.is_empty() && request_text.starts_with("gemini"), self.profile .identity .gemini diff --git a/src/app/browser/window/tab/item/page/navigation/request.rs b/src/app/browser/window/tab/item/page/navigation/request.rs index f9008374..9296e326 100644 --- a/src/app/browser/window/tab/item/page/navigation/request.rs +++ b/src/app/browser/window/tab/item/page/navigation/request.rs @@ -25,17 +25,8 @@ impl Request { } // Actions - pub fn update( - &self, - progress_fraction: Option, - is_identity_applicable: bool, - is_identity_active: bool, - ) { - self.widget.update( - progress_fraction, - is_identity_applicable, - is_identity_active, - ); + pub fn update(&self, progress_fraction: Option, is_identity_active: bool) { + self.widget.update(progress_fraction, is_identity_active); } pub fn clean( diff --git a/src/app/browser/window/tab/item/page/navigation/request/widget.rs b/src/app/browser/window/tab/item/page/navigation/request/widget.rs index f34f1827..844742a8 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/widget.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/widget.rs @@ -1,4 +1,7 @@ mod database; +mod primary_icon; + +use primary_icon::PrimaryIcon; use crate::app::browser::{window::tab::item::Action as TabAction, Action as BrowserAction}; use gtk::{ @@ -15,16 +18,6 @@ use std::{ const PLACEHOLDER_TEXT: &str = "URL or search term..."; -const GO_TOOLTIP_TEXT: &str = "Go to the address"; -const GO_ICON_NAME: &str = "pan-end-symbolic"; - -const IDENTITY_TOOLTIP_TEXT: (&str, &str) = ( - "Identity", - "Identity feature not available for this location", -); -const IDENTITY_ICON_NAME: (&str, &str) = - ("avatar-default-symbolic", "applications-system-symbolic"); - // Progress bar animation setup const PROGRESS_ANIMATION_STEP: f64 = 0.05; const PROGRESS_ANIMATION_TIME: u64 = 20; //ms @@ -57,12 +50,6 @@ impl Widget { .hexpand(true) .build(); - entry.set_primary_icon_name(Some(IDENTITY_ICON_NAME.0)); - entry.set_primary_icon_tooltip_text(Some(IDENTITY_TOOLTIP_TEXT.0)); - - entry.set_secondary_icon_name(Some(GO_ICON_NAME)); - entry.set_secondary_icon_tooltip_text(Some(GO_TOOLTIP_TEXT)); - // Connect events entry.connect_icon_release({ let tab_action = tab_action.clone(); @@ -186,32 +173,52 @@ impl Widget { Ok(()) } - pub fn update( - &self, - progress_fraction: Option, - is_identity_applicable: bool, - is_identity_active: bool, - ) { - // Update identity - self.entry - .set_primary_icon_activatable(is_identity_applicable); - self.entry - .set_primary_icon_sensitive(is_identity_applicable); - - if is_identity_applicable { - self.entry.set_primary_icon_name(Some(IDENTITY_ICON_NAME.0)); - self.entry - .set_primary_icon_tooltip_text(Some(IDENTITY_TOOLTIP_TEXT.0)); - } else { - self.entry.set_primary_icon_name(Some(IDENTITY_ICON_NAME.1)); - self.entry - .set_primary_icon_tooltip_text(Some(IDENTITY_TOOLTIP_TEXT.1)); - } - + pub fn update(&self, progress_fraction: Option, is_identity_active: bool) { + // Update primary icon let identity = self.entry.first_child().unwrap(); // @TODO handle identity.remove_css_class("success"); - if is_identity_active { - identity.add_css_class("success"); + + self.entry.set_primary_icon_activatable(false); + self.entry.set_primary_icon_sensitive(false); + + match primary_icon::from(&self.entry.text()) { + PrimaryIcon::Download { + icon_name, + tooltip_text, + } => { + self.entry.set_primary_icon_name(Some(icon_name)); + self.entry.set_primary_icon_tooltip_text(Some(tooltip_text)); + } + PrimaryIcon::Gemini { + icon_name, + tooltip_text, + } => { + self.entry.set_primary_icon_activatable(true); + self.entry.set_primary_icon_sensitive(true); + self.entry.set_primary_icon_name(Some(icon_name)); + if is_identity_active { + self.entry + .set_primary_icon_tooltip_text(Some(tooltip_text.1)); + identity.add_css_class("success"); + } else { + self.entry + .set_primary_icon_tooltip_text(Some(tooltip_text.0)); + } + } + PrimaryIcon::Search { + icon_name, + tooltip_text, + } => { + self.entry.set_primary_icon_name(Some(icon_name)); + self.entry.set_primary_icon_tooltip_text(Some(tooltip_text)); + } + PrimaryIcon::Source { + icon_name, + tooltip_text, + } => { + self.entry.set_primary_icon_name(Some(icon_name)); + self.entry.set_primary_icon_tooltip_text(Some(tooltip_text)); + } } // Update progress @@ -261,6 +268,7 @@ impl Widget { } // Tools + pub fn migrate(tx: &Transaction) -> Result<(), String> { // Migrate self components if let Err(e) = database::init(tx) { diff --git a/src/app/browser/window/tab/item/page/navigation/request/widget/primary_icon.rs b/src/app/browser/window/tab/item/page/navigation/request/widget/primary_icon.rs new file mode 100644 index 00000000..f5a9e44e --- /dev/null +++ b/src/app/browser/window/tab/item/page/navigation/request/widget/primary_icon.rs @@ -0,0 +1,46 @@ +pub enum PrimaryIcon<'a> { + Download { + icon_name: &'a str, + tooltip_text: &'a str, + }, + Gemini { + icon_name: &'a str, + tooltip_text: (&'a str, &'a str), + }, + Search { + icon_name: &'a str, + tooltip_text: &'a str, + }, + Source { + icon_name: &'a str, + tooltip_text: &'a str, + }, +} + +pub fn from(request: &str) -> PrimaryIcon { + if request.starts_with("download:") { + return PrimaryIcon::Download { + icon_name: "document-save-symbolic", + tooltip_text: "Download", + }; + } + + if request.starts_with("source:") { + return PrimaryIcon::Source { + icon_name: "applications-system-symbolic", + tooltip_text: "Source view", + }; + } + + if request.starts_with("gemini:") { + return PrimaryIcon::Gemini { + icon_name: "avatar-default-symbolic", + tooltip_text: ("Guest session", "User session"), + }; + } + + PrimaryIcon::Search { + icon_name: "system-search-symbolic", + tooltip_text: "Search", + } +}