From 793d17916483961c6d0389d8314e0b27e082ba51 Mon Sep 17 00:00:00 2001 From: yggverse Date: Tue, 8 Oct 2024 06:16:06 +0300 Subject: [PATCH] use is_pinned mutex as the struct option, begin use arc for new structs by default --- README.md | 1 - src/app/browser/window/tab/item.rs | 2 +- src/app/browser/window/tab/item/label.rs | 41 +++++++++++-------- src/app/browser/window/tab/item/label/pin.rs | 10 ++--- .../browser/window/tab/item/label/title.rs | 8 ++-- .../browser/window/tab/item/label/widget.rs | 6 ++- 6 files changed, 38 insertions(+), 30 deletions(-) diff --git a/README.md b/README.md index c4629ccd..4c88363f 100644 --- a/README.md +++ b/README.md @@ -49,7 +49,6 @@ Guide and protocol draft * contain main `struct` implementation: * at least one constructor that must: * have common for application name: `new` - * return unwrapped (except `Option`, `Result`), activated new `Self` object * grant ownership for new `Self` object created * public `activate` action if the new object can not be activated on construct * public `link` getter for GTK `widget` (parental composition) diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index abae310e..5c535def 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -43,7 +43,7 @@ impl Item { let id = uuid_string_random(); // Init components - let label = Arc::new(Label::new(id.clone(), false)); + let label = Label::new(id.clone(), false); let page = Arc::new(Page::new( id.clone(), diff --git a/src/app/browser/window/tab/item/label.rs b/src/app/browser/window/tab/item/label.rs index 4db883ce..59b52ea2 100644 --- a/src/app/browser/window/tab/item/label.rs +++ b/src/app/browser/window/tab/item/label.rs @@ -10,7 +10,7 @@ use title::Title; use widget::Widget; use gtk::{glib::GString, prelude::WidgetExt, Box, GestureClick}; -use std::sync::Arc; +use std::sync::{Arc, Mutex}; pub struct Label { // Components @@ -18,39 +18,42 @@ pub struct Label { title: Arc, // GTK widget: Arc<Widget>, + // Extras + is_pinned: Mutex<bool>, } impl Label { // Construct - pub fn new(name: GString, is_pinned: bool) -> Label { + pub fn new(name: GString, is_pinned: bool) -> Arc<Self> { // Init components - let pin = Arc::new(Pin::new(is_pinned)); - let title = Arc::new(Title::new()); - - // Init GTK - let widget = Arc::new(Widget::new(name, pin.gobject(), title.gobject())); + let pin = Pin::new(is_pinned); + let title = Title::new(); + let widget = Widget::new(name, pin.gobject(), title.gobject()); + + // Init label struct + let label = Arc::new(Self { + pin: pin.clone(), + title: title.clone(), + widget: widget.clone(), + is_pinned: Mutex::new(is_pinned), + }); // Init events: // Await for widget realize to continue this feature widget.gobject().connect_realize({ - let pin = pin.clone(); - let title = title.clone(); let widget = widget.clone(); + let label = label.clone(); move |_| { // Init GestureClick listener let controller = GestureClick::new(); // Listen for double click controller.connect_pressed({ - let pin = pin.clone(); - let title = title.clone(); + let label = label.clone(); move |_, count, _, _| { if count == 2 { - // Toggle pin @TODO use action? - let is_pinned = !pin.is_pinned(); - pin.pin(is_pinned); - title.pin(is_pinned); + label.pin(!label.is_pinned()); } } }); @@ -73,7 +76,7 @@ impl Label { }); // Result - Self { pin, title, widget } + label } // Actions @@ -143,13 +146,17 @@ impl Label { // Setters pub fn pin(&self, is_pinned: bool) { + // Update Self + *self.is_pinned.lock().unwrap() = is_pinned; + + // Update child components self.pin.pin(is_pinned); self.title.pin(is_pinned); } // Getters pub fn is_pinned(&self) -> bool { - self.pin.is_pinned() // @TODO + self.is_pinned.lock().unwrap().clone() } pub fn gobject(&self) -> &Box { diff --git a/src/app/browser/window/tab/item/label/pin.rs b/src/app/browser/window/tab/item/label/pin.rs index 1af37e1a..2d84fd84 100644 --- a/src/app/browser/window/tab/item/label/pin.rs +++ b/src/app/browser/window/tab/item/label/pin.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use gtk::{prelude::WidgetExt, Image}; pub struct Pin { @@ -6,13 +8,13 @@ pub struct Pin { impl Pin { // Construct - pub fn new(visible: bool) -> Pin { + pub fn new(visible: bool) -> Arc<Pin> { let gobject = Image::builder() .icon_name("view-pin-symbolic") .visible(visible) .build(); - Self { gobject } + Arc::new(Self { gobject }) } pub fn pin(&self, is_pinned: bool) { @@ -20,10 +22,6 @@ impl Pin { } // Getters - 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/item/label/title.rs b/src/app/browser/window/tab/item/label/title.rs index f0c23bc3..6d48bf95 100644 --- a/src/app/browser/window/tab/item/label/title.rs +++ b/src/app/browser/window/tab/item/label/title.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use gtk::{glib::GString, pango::EllipsizeMode, prelude::WidgetExt, Label}; const DEFAULT_LABEL_TEXT: &str = "New page"; @@ -8,15 +10,15 @@ pub struct Title { impl Title { // Construct - pub fn new() -> Self { - Self { + pub fn new() -> Arc<Self> { + Arc::new(Self { gobject: Label::builder() .label(DEFAULT_LABEL_TEXT) .ellipsize(EllipsizeMode::End) .width_chars(16) .single_line_mode(true) .build(), - } + }) } // Actions diff --git a/src/app/browser/window/tab/item/label/widget.rs b/src/app/browser/window/tab/item/label/widget.rs index f5adf239..2dd43ba2 100644 --- a/src/app/browser/window/tab/item/label/widget.rs +++ b/src/app/browser/window/tab/item/label/widget.rs @@ -1,3 +1,5 @@ +use std::sync::Arc; + use gtk::{ glib::GString, prelude::BoxExt, prelude::WidgetExt, Align, Box, Image, Label, Orientation, }; @@ -8,7 +10,7 @@ pub struct Widget { impl Widget { // Construct - pub fn new(name: GString, pin: &Image, title: &Label) -> Self { + pub fn new(name: GString, pin: &Image, title: &Label) -> Arc<Self> { let gobject = Box::builder() .orientation(Orientation::Horizontal) .halign(Align::Center) @@ -19,7 +21,7 @@ impl Widget { gobject.append(pin); gobject.append(title); - Self { gobject } + Arc::new(Self { gobject }) } // Action