2022-06-08 10:47:16 -04:00
|
|
|
mod imp;
|
|
|
|
|
|
2022-06-20 14:42:12 -04:00
|
|
|
use crate::{workspace_object::WorkspaceObject, Activate, TX, utils::WorkspaceEvent};
|
2022-06-08 10:47:16 -04:00
|
|
|
use glib::Object;
|
|
|
|
|
use gtk4::{glib, prelude::*, subclass::prelude::*, ToggleButton};
|
|
|
|
|
|
|
|
|
|
glib::wrapper! {
|
|
|
|
|
pub struct WorkspaceButton(ObjectSubclass<imp::WorkspaceButton>)
|
|
|
|
|
@extends gtk4::Box, gtk4::Widget,
|
|
|
|
|
@implements gtk4::Accessible, gtk4::Actionable, gtk4::Buildable, gtk4::ConstraintTarget, gtk4::Orientable;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl WorkspaceButton {
|
2022-06-08 23:51:33 -04:00
|
|
|
pub fn new() -> Self {
|
2022-06-08 10:47:16 -04:00
|
|
|
let self_ = Object::new(&[]).expect("Failed to create `WorkspaceButton`.");
|
|
|
|
|
let imp = imp::WorkspaceButton::from_instance(&self_);
|
|
|
|
|
|
|
|
|
|
let tb = ToggleButton::with_label("");
|
|
|
|
|
self_.append(&tb);
|
|
|
|
|
|
|
|
|
|
imp.button.replace(tb);
|
2022-06-08 23:51:33 -04:00
|
|
|
|
2022-06-08 10:47:16 -04:00
|
|
|
self_
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub fn set_workspace_object(&self, obj: &WorkspaceObject) {
|
|
|
|
|
let imp = imp::WorkspaceButton::from_instance(&self);
|
|
|
|
|
let old_button = imp.button.take();
|
|
|
|
|
self.remove(&old_button);
|
|
|
|
|
|
2022-06-16 17:03:01 -04:00
|
|
|
let is_active = obj.active() == 0;
|
2022-06-08 10:47:16 -04:00
|
|
|
let id = obj.id();
|
2022-06-16 12:23:25 -04:00
|
|
|
let new_button = ToggleButton::with_label(&id);
|
2022-06-16 17:03:01 -04:00
|
|
|
new_button.set_sensitive(!is_active);
|
|
|
|
|
if obj.active() == 0 {
|
|
|
|
|
new_button.add_css_class("active");
|
|
|
|
|
} else if obj.active() == 1 {
|
2022-06-16 14:33:26 -04:00
|
|
|
new_button.add_css_class("alert");
|
2022-06-16 17:03:01 -04:00
|
|
|
} else {
|
|
|
|
|
new_button.add_css_class("inactive");
|
2022-06-16 14:33:26 -04:00
|
|
|
}
|
2022-06-08 10:47:16 -04:00
|
|
|
self.append(&new_button);
|
2022-06-08 23:51:33 -04:00
|
|
|
new_button.connect_clicked(move |_| {
|
2022-06-16 14:33:26 -04:00
|
|
|
let id_clone = id.clone();
|
2022-06-16 17:03:01 -04:00
|
|
|
if !is_active {
|
|
|
|
|
glib::MainContext::default().spawn_local(async move {
|
2022-06-20 14:42:12 -04:00
|
|
|
TX.get().unwrap().send(WorkspaceEvent::Activate(id_clone)).await.unwrap();
|
2022-06-16 17:03:01 -04:00
|
|
|
});
|
|
|
|
|
}
|
2022-06-08 23:51:33 -04:00
|
|
|
});
|
2022-06-08 10:47:16 -04:00
|
|
|
|
|
|
|
|
imp.button.replace(new_button);
|
|
|
|
|
}
|
|
|
|
|
}
|