Browse Source

delete deprecated label components

master
yggverse 2 months ago
parent
commit
c86149a429
  1. 197
      src/app/browser/window/tab/item/label.rs
  2. 80
      src/app/browser/window/tab/item/label/database.rs
  3. 28
      src/app/browser/window/tab/item/label/pin.rs
  4. 40
      src/app/browser/window/tab/item/label/title.rs
  5. 39
      src/app/browser/window/tab/item/label/widget.rs

197
src/app/browser/window/tab/item/label.rs

@ -1,197 +0,0 @@
mod database;
mod pin;
mod title;
mod widget;
use database::Database;
use pin::Pin;
use sqlite::Transaction;
use title::Title;
use widget::Widget;
use gtk::{gdk, glib::GString, prelude::WidgetExt, Box, GestureClick};
use std::sync::{Arc, Mutex};
pub struct Label {
// Components
pin: Arc<Pin>,
title: Arc<Title>,
// GTK
widget: Arc<Widget>,
// Extras
is_pinned: Mutex<bool>,
}
impl Label {
// Construct
pub fn new_arc(name: GString, is_pinned: bool) -> Arc<Self> {
// Init components
let pin = Pin::new_arc(is_pinned);
let title = Title::new_arc();
let widget = Widget::new_arc(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 widget = widget.clone();
let label = label.clone();
move |_| {
// Init primary button listener
let primary_button_controller = GestureClick::new();
// Listen for double primary button click
primary_button_controller.connect_pressed({
let label = label.clone();
move |_, count, _, _| {
if count == 2 {
label.pin(!label.is_pinned());
}
}
});
// Init middle button listener
let middle_button_controller =
GestureClick::builder().button(gdk::BUTTON_MIDDLE).build();
// Listen for single middle button click
middle_button_controller.connect_pressed({
let label = label.clone();
move |_, _, _, _| {
if label.is_pinned() {
label.pin(false); // unpin
} else {
// @TODO close
}
}
});
// Label's parent contain native GTK paddings, that makes click event ignored
// try assign the controller to parent as the solution
match widget.gobject().parent() {
// @TODO check for GtkGizmo type?
Some(parent) => {
parent.add_controller(primary_button_controller);
parent.add_controller(middle_button_controller);
}
// Assign controller to the current widget, drop notice
None => {
widget.gobject().add_controller(primary_button_controller);
widget.gobject().add_controller(middle_button_controller);
println!("Could not assign action to destination, please report");
// @TODO
}
}
}
});
// Result
label
}
// Actions
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
match Database::delete(transaction, &record.id) {
Ok(_) => {
// Delegate clean action to childs
// nothing yet..
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn restore(
&self,
transaction: &Transaction,
app_browser_window_tab_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
self.pin(record.is_pinned);
// Delegate restore action to childs
// nothing yet..
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_id: &i64,
) -> Result<(), String> {
match Database::add(transaction, app_browser_window_tab_id, &self.is_pinned()) {
Ok(_) => {
// Delegate save action to childs
// nothing yet..
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn update(&self, title: Option<&GString>) {
self.title.update(title);
self.widget.update(title);
}
// 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.is_pinned.lock().unwrap().clone()
}
pub fn gobject(&self) -> &Box {
&self.widget.gobject()
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = Database::init(&tx) {
return Err(e.to_string());
}
// Delegate migration to childs
// nothing yet..
// Success
Ok(())
}
}

80
src/app/browser/window/tab/item/label/database.rs

@ -1,80 +0,0 @@
use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
// pub app_browser_window_tab_item_id: i64, not in use
pub is_pinned: bool,
}
pub struct Database {
// nothing yet..
}
impl Database {
pub fn init(tx: &Transaction) -> Result<usize, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_label`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_item_id` INTEGER NOT NULL,
`is_pinned` INTEGER NOT NULL
)",
[],
)
}
pub fn add(
tx: &Transaction,
app_browser_window_tab_item_id: &i64,
is_pinned: &bool,
) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `app_browser_window_tab_item_label` (
`app_browser_window_tab_item_id`,
`is_pinned`
) VALUES (?,?)",
[app_browser_window_tab_item_id, &(*is_pinned as i64)],
)
}
pub fn records(
tx: &Transaction,
app_browser_window_tab_item_id: &i64,
) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_item_id`,
`is_pinned` FROM `app_browser_window_tab_item_label`
WHERE `app_browser_window_tab_item_id` = ?",
)?;
let result = stmt.query_map([app_browser_window_tab_item_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_browser_window_tab_item_id: row.get(1)?, not in use
is_pinned: row.get(2)?,
})
})?;
let mut records = Vec::new();
for record in result {
let table = record?;
records.push(table);
}
Ok(records)
}
pub fn delete(tx: &Transaction, id: &i64) -> Result<usize, Error> {
tx.execute(
"DELETE FROM `app_browser_window_tab_item_label` WHERE `id` = ?",
[id],
)
}
/* not in use
pub fn last_insert_id(tx: &Transaction) -> i64 {
tx.last_insert_rowid()
} */
}

28
src/app/browser/window/tab/item/label/pin.rs

@ -1,28 +0,0 @@
use std::sync::Arc;
use gtk::{prelude::WidgetExt, Image};
pub struct Pin {
gobject: Image,
}
impl Pin {
// Construct
pub fn new_arc(visible: bool) -> Arc<Pin> {
let gobject = Image::builder()
.icon_name("view-pin-symbolic")
.visible(visible)
.build();
Arc::new(Self { gobject })
}
pub fn pin(&self, is_pinned: bool) {
self.gobject().set_visible(is_pinned);
}
// Getters
pub fn gobject(&self) -> &Image {
&self.gobject
}
}

40
src/app/browser/window/tab/item/label/title.rs

@ -1,40 +0,0 @@
use std::sync::Arc;
use gtk::{glib::GString, pango::EllipsizeMode, prelude::WidgetExt, Label};
const DEFAULT_LABEL_TEXT: &str = "New page";
pub struct Title {
gobject: Label,
}
impl Title {
// Construct
pub fn new_arc() -> Arc<Self> {
Arc::new(Self {
gobject: Label::builder()
.label(DEFAULT_LABEL_TEXT)
.ellipsize(EllipsizeMode::End)
.width_chars(16)
.single_line_mode(true)
.build(),
})
}
// Actions
pub fn update(&self, title: Option<&GString>) {
match title {
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 gobject(&self) -> &Label {
&self.gobject
}
}

39
src/app/browser/window/tab/item/label/widget.rs

@ -1,39 +0,0 @@
use std::sync::Arc;
use gtk::{
glib::GString, prelude::BoxExt, prelude::WidgetExt, Align, Box, Image, Label, Orientation,
};
pub struct Widget {
gobject: Box,
}
impl Widget {
// Construct
pub fn new_arc(name: GString, pin: &Image, title: &Label) -> Arc<Self> {
let gobject = Box::builder()
.orientation(Orientation::Horizontal)
.halign(Align::Center)
.name(name)
.tooltip_text(title.text())
.build();
gobject.append(pin);
gobject.append(title);
Arc::new(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