implement identity button as navigation entry primary icon

This commit is contained in:
yggverse 2024-12-11 05:44:59 +02:00
parent a5415fa4e5
commit 5348417a73
6 changed files with 66 additions and 105 deletions

View file

@ -1,29 +0,0 @@
mod widget;
use widget::Widget;
use crate::app::browser::window::tab::item::Action;
use std::rc::Rc;
pub struct Identity {
action: Rc<Action>,
pub widget: Rc<Widget>,
}
impl Identity {
// Construct
pub fn new(action: Rc<Action>) -> Self {
Self {
action: action.clone(),
widget: Rc::new(Widget::new(action)),
}
}
// Actions
pub fn update(&self, is_auth: bool, is_enabled: bool) {
// Update action status
self.action.ident.simple_action.set_enabled(is_enabled);
// Update widget
self.widget.update(is_auth, is_enabled)
}
}

View file

@ -1,35 +0,0 @@
use crate::app::browser::window::tab::item::Action;
use gtk::{
prelude::{ButtonExt, WidgetExt},
Button,
};
use std::rc::Rc;
pub struct Widget {
pub gobject: Button,
}
impl Widget {
// Construct
pub fn new(action: Rc<Action>) -> Self {
// Init gobject
let gobject = Button::builder()
.icon_name("avatar-default-symbolic")
.tooltip_text("Identity")
//.sensitive(false)
.build();
// Init events @TODO dialog window required
gobject.connect_clicked(move |_| action.ident.activate());
// Return activated `Self`
Self { gobject }
}
// Actions
pub fn update(&self, is_auth: bool, is_enabled: bool) {
self.gobject.set_sensitive(is_enabled);
self.gobject
.set_css_classes(if is_auth { &["success"] } else { &[] });
}
}

View file

@ -25,8 +25,17 @@ impl Request {
}
// Actions
pub fn update(&self, progress_fraction: Option<f64>) {
self.widget.update(progress_fraction);
pub fn update(
&self,
progress_fraction: Option<f64>,
is_identity_applicable: bool,
is_identity_active: bool,
) {
self.widget.update(
progress_fraction,
is_identity_applicable,
is_identity_active,
);
}
pub fn clean(

View file

@ -2,7 +2,6 @@ mod database;
use crate::app::browser::{window::tab::item::Action as TabAction, Action as BrowserAction};
use gtk::{
gio::Icon,
glib::{timeout_add_local, ControlFlow, SourceId},
prelude::{EditableExt, EntryExt, WidgetExt},
Entry, EntryIconPosition, StateFlags,
@ -15,7 +14,16 @@ 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;
@ -49,31 +57,19 @@ impl Widget {
.hexpand(true)
.build();
// Init `go` button feature (if icon available)
let go = match Icon::for_string("pan-end-symbolic") {
Ok(icon) => {
entry.set_secondary_icon_gicon(Some(&icon));
entry.set_secondary_icon_tooltip_text(Some(GO_TOOLTIP_TEXT));
Some(icon)
}
Err(e) => {
println!("{e}"); // drop notice @TODO
None
}
};
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();
let go = go.clone();
move |this, position| match position {
EntryIconPosition::Primary => todo!(),
EntryIconPosition::Secondary => {
if go.is_some() {
tab_action.load.activate(Some(&this.text()), true);
}
}
_ => println!("Undefined icon position"), // drop notice @TODO
EntryIconPosition::Primary => tab_action.ident.activate(),
EntryIconPosition::Secondary => tab_action.load.activate(Some(&this.text()), true),
_ => todo!(), // unexpected
}
});
@ -190,8 +186,36 @@ impl Widget {
Ok(())
}
pub fn update(&self, progress_fraction: Option<f64>) {
// Skip update animation for None value
pub fn update(
&self,
progress_fraction: Option<f64>,
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));
}
let identity = self.entry.first_child().unwrap(); // @TODO handle
identity.remove_css_class("success");
if is_identity_active {
identity.add_css_class("success");
}
// Update progress
// * skip update animation for None value
if let Some(value) = progress_fraction {
// Update shared fraction on new value was changed
if value != self.progress.fraction.replace(value) {

View file

@ -13,7 +13,6 @@ pub struct Widget {
impl Widget {
// Construct
pub fn new(
auth: &impl IsA<gtk::Widget>,
base: &impl IsA<gtk::Widget>,
history: &impl IsA<gtk::Widget>,
reload: &impl IsA<gtk::Widget>,
@ -33,7 +32,6 @@ impl Widget {
gobject.append(reload);
gobject.append(request);
gobject.append(bookmark);
gobject.append(auth);
Self { gobject }
}