Browse Source

use separated widget mod

master
yggverse 2 months ago
parent
commit
7d4cf5c1f3
  1. 46
      src/app/browser/window/tab/label.rs
  2. 20
      src/app/browser/window/tab/label/pin.rs
  3. 18
      src/app/browser/window/tab/label/title.rs
  4. 37
      src/app/browser/window/tab/label/widget.rs

46
src/app/browser/window/tab/label.rs

@ -1,41 +1,31 @@ @@ -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<Pin>,
title: Arc<Title>,
// 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 { @@ -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()
}
}

20
src/app/browser/window/tab/label/pin.rs

@ -1,22 +1,30 @@ @@ -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
}
}

18
src/app/browser/window/tab/label/title.rs

@ -1,16 +1,16 @@ @@ -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 { @@ -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
}
}

37
src/app/browser/window/tab/label/widget.rs

@ -0,0 +1,37 @@ @@ -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
}
}
Loading…
Cancel
Save