mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-02-10 02:14:13 +00:00
use is_pinned mutex as the struct option, begin use arc for new structs by default
This commit is contained in:
parent
41f0452e26
commit
793d179164
@ -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)
|
||||
|
@ -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(),
|
||||
|
@ -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<Title>,
|
||||
// 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());
|
||||
let pin = Pin::new(is_pinned);
|
||||
let title = Title::new();
|
||||
let widget = Widget::new(name, pin.gobject(), title.gobject());
|
||||
|
||||
// Init GTK
|
||||
let widget = Arc::new(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 {
|
||||
|
@ -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
|
||||
}
|
||||
|
@ -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
|
||||
|
@ -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
|
||||
|
Loading…
x
Reference in New Issue
Block a user