Turn view::port::Port into a gtk::Button subclass

This commit is contained in:
Tom A. Wagner
2021-04-02 14:53:58 +02:00
parent 9b448f0a30
commit 75aa0a30d0
3 changed files with 56 additions and 28 deletions

View File

@@ -1,12 +1,34 @@
use gtk::WidgetExt;
use gtk::{glib, prelude::*, subclass::prelude::*};
use crate::controller::MediaType;
/// Graphical representation of a pipewire port.
pub struct Port {
pub widget: gtk::Button,
pub id: u32,
pub direction: pipewire::port::Direction,
mod imp {
use once_cell::unsync::OnceCell;
use super::*;
/// Graphical representation of a pipewire port.
#[derive(Default)]
pub struct Port {
pub(super) id: OnceCell<u32>,
pub(super) direction: OnceCell<pipewire::port::Direction>,
}
#[glib::object_subclass]
impl ObjectSubclass for Port {
const NAME: &'static str = "Port";
type Type = super::Port;
type ParentType = gtk::Button;
}
impl ObjectImpl for Port {}
impl WidgetImpl for Port {}
impl ButtonImpl for Port {}
}
glib::wrapper! {
pub struct Port(ObjectSubclass<imp::Port>)
@extends gtk::Button, gtk::Widget;
}
impl Port {
@@ -16,20 +38,30 @@ impl Port {
direction: pipewire::port::Direction,
media_type: Option<MediaType>,
) -> Self {
let widget = gtk::Button::with_label(name);
// Create the widget and initialize needed fields
let res: Self = glib::Object::new(&[]).expect("Failed to create Port");
let private = imp::Port::from_instance(&res);
private.id.set(id).expect("Port id already set");
private
.direction
.set(direction)
.expect("Port direction already set");
res.set_child(Some(&gtk::Label::new(Some(name))));
// Color the port according to its media type.
match media_type {
Some(MediaType::Video) => widget.add_css_class("video"),
Some(MediaType::Audio) => widget.add_css_class("audio"),
Some(MediaType::Midi) => widget.add_css_class("midi"),
Some(MediaType::Video) => res.add_css_class("video"),
Some(MediaType::Audio) => res.add_css_class("audio"),
Some(MediaType::Midi) => res.add_css_class("midi"),
None => {}
}
Self {
widget,
id,
direction,
}
res
}
pub fn direction(&self) -> &pipewire::port::Direction {
let private = imp::Port::from_instance(self);
private.direction.get().expect("Port direction is not set")
}
}