From 7d4cf5c1f34a86755e50fab46523c47afe63bc9e Mon Sep 17 00:00:00 2001 From: yggverse Date: Sun, 6 Oct 2024 16:17:53 +0300 Subject: [PATCH] use separated widget mod --- src/app/browser/window/tab/label.rs | 46 ++++++++-------------- src/app/browser/window/tab/label/pin.rs | 20 +++++++--- src/app/browser/window/tab/label/title.rs | 18 +++++---- src/app/browser/window/tab/label/widget.rs | 37 +++++++++++++++++ 4 files changed, 78 insertions(+), 43 deletions(-) create mode 100644 src/app/browser/window/tab/label/widget.rs diff --git a/src/app/browser/window/tab/label.rs b/src/app/browser/window/tab/label.rs index 92356dc8..32349b4a 100644 --- a/src/app/browser/window/tab/label.rs +++ b/src/app/browser/window/tab/label.rs @@ -1,41 +1,31 @@ mod pin; mod title; +mod widget; use pin::Pin; use title::Title; +use widget::Widget; -use gtk::{ - glib::GString, - prelude::{BoxExt, WidgetExt}, - Align, Box, Orientation, -}; +use gtk::{glib::GString, Box}; +use std::sync::Arc; pub struct Label { // Components - pin: Pin, - title: Title, - + pin: Arc, + title: Arc, // GTK - widget: Box, + widget: Arc<Widget>, } impl Label { // Construct pub fn new(name: GString, is_pinned: bool) -> Label { // Components - let pin = Pin::new(is_pinned); - let title = Title::new(); + let pin = Arc::new(Pin::new(is_pinned)); + let title = Arc::new(Title::new()); // GTK - let widget = Box::builder() - .orientation(Orientation::Horizontal) - .halign(Align::Center) - .name(name) - .tooltip_text(title.widget().text()) - .build(); - - widget.append(pin.widget()); - widget.append(title.widget()); + let widget = Arc::new(Widget::new(name, pin.gobject(), title.gobject())); // Result Self { pin, title, widget } @@ -43,26 +33,22 @@ impl Label { // Actions pub fn update(&self, title: Option<&GString>) { - match title { - Some(tooltip_text) => self.widget.set_tooltip_text(Some(tooltip_text)), - None => self.widget.set_tooltip_text(None), - } - self.title.update(title); + self.widget.update(title); } // Setters pub fn pin(&self, is_pinned: bool) { - self.pin.widget().set_visible(is_pinned); - self.title.widget().set_visible(!is_pinned); + self.pin.pin(is_pinned); + self.title.pin(is_pinned); } // Getters pub fn is_pinned(&self) -> bool { - self.pin.widget().is_visible() + self.pin.is_pinned() } - pub fn widget(&self) -> &Box { - &self.widget + pub fn gobject(&self) -> &Box { + &self.widget.gobject() } } diff --git a/src/app/browser/window/tab/label/pin.rs b/src/app/browser/window/tab/label/pin.rs index a16727fe..1af37e1a 100644 --- a/src/app/browser/window/tab/label/pin.rs +++ b/src/app/browser/window/tab/label/pin.rs @@ -1,22 +1,30 @@ -use gtk::Image; +use gtk::{prelude::WidgetExt, Image}; pub struct Pin { - widget: Image, + gobject: Image, } impl Pin { // Construct pub fn new(visible: bool) -> Pin { - let widget = Image::builder() + let gobject = Image::builder() .icon_name("view-pin-symbolic") .visible(visible) .build(); - Self { widget } + Self { gobject } + } + + pub fn pin(&self, is_pinned: bool) { + self.gobject().set_visible(is_pinned); } // Getters - pub fn widget(&self) -> &Image { - &self.widget + pub fn is_pinned(&self) -> bool { + self.gobject.is_visible() + } + + pub fn gobject(&self) -> &Image { + &self.gobject } } diff --git a/src/app/browser/window/tab/label/title.rs b/src/app/browser/window/tab/label/title.rs index 63260ad5..f0c23bc3 100644 --- a/src/app/browser/window/tab/label/title.rs +++ b/src/app/browser/window/tab/label/title.rs @@ -1,16 +1,16 @@ -use gtk::{glib::GString, pango::EllipsizeMode, Label}; +use gtk::{glib::GString, pango::EllipsizeMode, prelude::WidgetExt, Label}; const DEFAULT_LABEL_TEXT: &str = "New page"; pub struct Title { - widget: Label, + gobject: Label, } impl Title { // Construct pub fn new() -> Self { Self { - widget: Label::builder() + gobject: Label::builder() .label(DEFAULT_LABEL_TEXT) .ellipsize(EllipsizeMode::End) .width_chars(16) @@ -22,13 +22,17 @@ impl Title { // Actions pub fn update(&self, title: Option<&GString>) { match title { - Some(title) => self.widget.set_text(title), - None => self.widget.set_text(DEFAULT_LABEL_TEXT), + Some(title) => self.gobject.set_text(title), + None => self.gobject.set_text(DEFAULT_LABEL_TEXT), } } + pub fn pin(&self, is_pinned: bool) { + self.gobject.set_visible(!is_pinned); + } + // Getters - pub fn widget(&self) -> &Label { - &self.widget + pub fn gobject(&self) -> &Label { + &self.gobject } } diff --git a/src/app/browser/window/tab/label/widget.rs b/src/app/browser/window/tab/label/widget.rs new file mode 100644 index 00000000..f5adf239 --- /dev/null +++ b/src/app/browser/window/tab/label/widget.rs @@ -0,0 +1,37 @@ +use gtk::{ + glib::GString, prelude::BoxExt, prelude::WidgetExt, Align, Box, Image, Label, Orientation, +}; + +pub struct Widget { + gobject: Box, +} + +impl Widget { + // Construct + pub fn new(name: GString, pin: &Image, title: &Label) -> Self { + let gobject = Box::builder() + .orientation(Orientation::Horizontal) + .halign(Align::Center) + .name(name) + .tooltip_text(title.text()) + .build(); + + gobject.append(pin); + gobject.append(title); + + Self { gobject } + } + + // Action + pub fn update(&self, title: Option<&GString>) { + match title { + Some(tooltip_text) => self.gobject.set_tooltip_text(Some(tooltip_text)), + None => self.gobject.set_tooltip_text(None), + } + } + + // Getters + pub fn gobject(&self) -> &Box { + &self.gobject + } +}