wip: applet workspaces

This commit is contained in:
Ashley Wulber 2022-06-08 10:47:16 -04:00
parent 464338585c
commit 2cbaaae069
20 changed files with 669 additions and 0 deletions

View file

@ -0,0 +1,29 @@
use std::{rc::Rc, cell::RefCell};
use gtk4::{Label, ToggleButton, glib, subclass::prelude::*};
use tokio::sync::mpsc;
use once_cell::sync::OnceCell;
use crate::Event;
// Object holding the state
#[derive(Default)]
pub struct WorkspaceButton {
pub tx: Rc<OnceCell<mpsc::Sender<Event>>>,
pub button: Rc<RefCell<ToggleButton>>,
}
// The central trait for subclassing a GObject
#[glib::object_subclass]
impl ObjectSubclass for WorkspaceButton {
const NAME: &'static str = "WorkspaceButton";
type Type = super::WorkspaceButton;
type ParentType = gtk4::Box;
}
// Trait shared by all GObjects
impl ObjectImpl for WorkspaceButton {}
// Trait shared by all widgets
impl WidgetImpl for WorkspaceButton {}
// Trait shared by all buttons
impl BoxImpl for WorkspaceButton {}

View file

@ -0,0 +1,43 @@
mod imp;
use glib::Object;
use gtk4::{glib, prelude::*, subclass::prelude::*, ToggleButton};
use tokio::sync::mpsc;
use crate::{Event, workspace_object::WorkspaceObject};
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 {
pub fn new(tx: mpsc::Sender<Event>) -> Self {
let self_ = Object::new(&[]).expect("Failed to create `WorkspaceButton`.");
let imp = imp::WorkspaceButton::from_instance(&self_);
imp.tx.set(tx).unwrap();
let tb = ToggleButton::with_label("");
self_.append(&tb);
imp.button.replace(tb);
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);
let id = obj.id();
let new_button = ToggleButton::with_label(&format!("{}", id));
new_button.set_active(obj.active());
self.append(&new_button);
new_button.connect_clicked(glib::clone!(@weak imp.tx as tx => move |_| {
let _ = tx.get().unwrap().send(Event::Activate(id));
}));
imp.button.replace(new_button);
}
}