mirror of
https://gitlab.freedesktop.org/pipewire/helvum
synced 2026-03-15 03:26:10 +08:00
graph: Move link data into new GObject subclass
This commit is contained in:
@@ -25,10 +25,7 @@ use gtk::{
|
|||||||
use log::info;
|
use log::info;
|
||||||
use pipewire::{channel::Sender, spa::Direction};
|
use pipewire::{channel::Sender, spa::Direction};
|
||||||
|
|
||||||
use crate::{
|
use crate::{ui, GtkMessage, MediaType, NodeType, PipewireLink, PipewireMessage};
|
||||||
ui,
|
|
||||||
GtkMessage, MediaType, NodeType, PipewireLink, PipewireMessage,
|
|
||||||
};
|
|
||||||
|
|
||||||
static STYLE: &str = include_str!("style.css");
|
static STYLE: &str = include_str!("style.css");
|
||||||
|
|
||||||
|
|||||||
@@ -14,8 +14,6 @@
|
|||||||
//
|
//
|
||||||
// SPDX-License-Identifier: GPL-3.0-only
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
use super::{Node, Port};
|
|
||||||
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
glib::{self, clone},
|
glib::{self, clone},
|
||||||
graphene,
|
graphene,
|
||||||
@@ -28,6 +26,7 @@ use log::{error, warn};
|
|||||||
|
|
||||||
use std::{cmp::Ordering, collections::HashMap};
|
use std::{cmp::Ordering, collections::HashMap};
|
||||||
|
|
||||||
|
use super::{Link, Node, Port};
|
||||||
use crate::NodeType;
|
use crate::NodeType;
|
||||||
|
|
||||||
const CANVAS_SIZE: f64 = 5000.0;
|
const CANVAS_SIZE: f64 = 5000.0;
|
||||||
@@ -59,7 +58,7 @@ mod imp {
|
|||||||
/// Stores nodes and their positions.
|
/// Stores nodes and their positions.
|
||||||
pub(super) nodes: RefCell<HashMap<u32, (Node, Point)>>,
|
pub(super) nodes: RefCell<HashMap<u32, (Node, Point)>>,
|
||||||
/// Stores the link and whether it is currently active.
|
/// Stores the link and whether it is currently active.
|
||||||
pub(super) links: RefCell<HashMap<u32, (crate::PipewireLink, bool)>>,
|
pub(super) links: RefCell<HashMap<u32, Link>>,
|
||||||
pub hadjustment: RefCell<Option<gtk::Adjustment>>,
|
pub hadjustment: RefCell<Option<gtk::Adjustment>>,
|
||||||
pub vadjustment: RefCell<Option<gtk::Adjustment>>,
|
pub vadjustment: RefCell<Option<gtk::Adjustment>>,
|
||||||
pub zoom_factor: Cell<f64>,
|
pub zoom_factor: Cell<f64>,
|
||||||
@@ -431,13 +430,13 @@ mod imp {
|
|||||||
rgba.alpha().into(),
|
rgba.alpha().into(),
|
||||||
);
|
);
|
||||||
|
|
||||||
for (link, active) in self.links.borrow().values() {
|
for link in self.links.borrow().values() {
|
||||||
// TODO: Do not draw links when they are outside the view
|
// TODO: Do not draw links when they are outside the view
|
||||||
if let Some((from_x, from_y, to_x, to_y)) = self.get_link_coordinates(link) {
|
if let Some((from_x, from_y, to_x, to_y)) = self.get_link_coordinates(link) {
|
||||||
link_cr.move_to(from_x, from_y);
|
link_cr.move_to(from_x, from_y);
|
||||||
|
|
||||||
// Use dashed line for inactive links, full line otherwise.
|
// Use dashed line for inactive links, full line otherwise.
|
||||||
if *active {
|
if link.active() {
|
||||||
link_cr.set_dash(&[], 0.0);
|
link_cr.set_dash(&[], 0.0);
|
||||||
} else {
|
} else {
|
||||||
link_cr.set_dash(&[10.0, 5.0], 0.0);
|
link_cr.set_dash(&[10.0, 5.0], 0.0);
|
||||||
@@ -478,11 +477,10 @@ mod imp {
|
|||||||
///
|
///
|
||||||
/// # Returns
|
/// # Returns
|
||||||
/// `Some((from_x, from_y, to_x, to_y))` if all objects the links refers to exist as widgets.
|
/// `Some((from_x, from_y, to_x, to_y))` if all objects the links refers to exist as widgets.
|
||||||
fn get_link_coordinates(&self, link: &crate::PipewireLink) -> Option<(f64, f64, f64, f64)> {
|
fn get_link_coordinates(&self, link: &Link) -> Option<(f64, f64, f64, f64)> {
|
||||||
let widget = &*self.obj();
|
let widget = &*self.obj();
|
||||||
let nodes = self.nodes.borrow();
|
|
||||||
|
|
||||||
let output_port = &nodes.get(&link.node_from)?.0.get_port(link.port_from)?;
|
let output_port = link.output_port()?;
|
||||||
|
|
||||||
let output_port_padding =
|
let output_port_padding =
|
||||||
(output_port.allocated_width() - output_port.width()) as f64 / 2.0;
|
(output_port.allocated_width() - output_port.width()) as f64 / 2.0;
|
||||||
@@ -493,7 +491,7 @@ mod imp {
|
|||||||
(output_port.height() / 2) as f64,
|
(output_port.height() / 2) as f64,
|
||||||
)?;
|
)?;
|
||||||
|
|
||||||
let input_port = &nodes.get(&link.node_to)?.0.get_port(link.port_to)?;
|
let input_port = link.input_port()?;
|
||||||
|
|
||||||
let input_port_padding =
|
let input_port_padding =
|
||||||
(input_port.allocated_width() - input_port.width()) as f64 / 2.0;
|
(input_port.allocated_width() - input_port.width()) as f64 / 2.0;
|
||||||
@@ -671,16 +669,28 @@ impl GraphView {
|
|||||||
}
|
}
|
||||||
|
|
||||||
pub fn add_link(&self, link_id: u32, link: crate::PipewireLink, active: bool) {
|
pub fn add_link(&self, link_id: u32, link: crate::PipewireLink, active: bool) {
|
||||||
self.imp()
|
let nodes = self.imp().nodes.borrow();
|
||||||
.links
|
|
||||||
.borrow_mut()
|
let output_port = nodes
|
||||||
.insert(link_id, (link, active));
|
.get(&link.node_from)
|
||||||
|
.and_then(|(node, _)| node.get_port(link.port_from));
|
||||||
|
|
||||||
|
let input_port = nodes
|
||||||
|
.get(&link.node_to)
|
||||||
|
.and_then(|(node, _)| node.get_port(link.port_to));
|
||||||
|
|
||||||
|
let link = Link::new();
|
||||||
|
link.set_input_port(input_port.as_ref());
|
||||||
|
link.set_output_port(output_port.as_ref());
|
||||||
|
link.set_active(active);
|
||||||
|
|
||||||
|
self.imp().links.borrow_mut().insert(link_id, link);
|
||||||
self.queue_draw();
|
self.queue_draw();
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn set_link_state(&self, link_id: u32, active: bool) {
|
pub fn set_link_state(&self, link_id: u32, active: bool) {
|
||||||
if let Some((_, state)) = self.imp().links.borrow_mut().get_mut(&link_id) {
|
if let Some(link) = self.imp().links.borrow_mut().get_mut(&link_id) {
|
||||||
*state = active;
|
link.set_active(active);
|
||||||
self.queue_draw();
|
self.queue_draw();
|
||||||
} else {
|
} else {
|
||||||
warn!("Link state changed on unknown link (id={})", link_id);
|
warn!("Link state changed on unknown link (id={})", link_id);
|
||||||
|
|||||||
114
src/ui/graph/link.rs
Normal file
114
src/ui/graph/link.rs
Normal file
@@ -0,0 +1,114 @@
|
|||||||
|
// Copyright 2021 Tom A. Wagner <tom.a.wagner@protonmail.com>
|
||||||
|
//
|
||||||
|
// This program is free software: you can redistribute it and/or modify
|
||||||
|
// it under the terms of the GNU General Public License version 3 as published by
|
||||||
|
// the Free Software Foundation.
|
||||||
|
//
|
||||||
|
// This program is distributed in the hope that it will be useful,
|
||||||
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
||||||
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
||||||
|
// GNU General Public License for more details.
|
||||||
|
//
|
||||||
|
// You should have received a copy of the GNU General Public License
|
||||||
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
//
|
||||||
|
// SPDX-License-Identifier: GPL-3.0-only
|
||||||
|
|
||||||
|
use gtk::{glib, prelude::*, subclass::prelude::*};
|
||||||
|
|
||||||
|
use super::Port;
|
||||||
|
|
||||||
|
mod imp {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
use std::cell::Cell;
|
||||||
|
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
|
||||||
|
#[derive(Default)]
|
||||||
|
pub struct Link {
|
||||||
|
pub output_port: glib::WeakRef<Port>,
|
||||||
|
pub input_port: glib::WeakRef<Port>,
|
||||||
|
pub active: Cell<bool>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[glib::object_subclass]
|
||||||
|
impl ObjectSubclass for Link {
|
||||||
|
const NAME: &'static str = "HelvumLink";
|
||||||
|
type Type = super::Link;
|
||||||
|
type ParentType = glib::Object;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ObjectImpl for Link {
|
||||||
|
fn properties() -> &'static [glib::ParamSpec] {
|
||||||
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
||||||
|
vec![
|
||||||
|
glib::ParamSpecObject::builder::<Port>("output-port")
|
||||||
|
.flags(glib::ParamFlags::READWRITE)
|
||||||
|
.build(),
|
||||||
|
glib::ParamSpecObject::builder::<Port>("input-port")
|
||||||
|
.flags(glib::ParamFlags::READWRITE)
|
||||||
|
.build(),
|
||||||
|
glib::ParamSpecBoolean::builder("active")
|
||||||
|
.default_value(false)
|
||||||
|
.flags(glib::ParamFlags::READWRITE)
|
||||||
|
.build(),
|
||||||
|
]
|
||||||
|
});
|
||||||
|
|
||||||
|
PROPERTIES.as_ref()
|
||||||
|
}
|
||||||
|
|
||||||
|
fn property(&self, _id: usize, pspec: &glib::ParamSpec) -> glib::Value {
|
||||||
|
match pspec.name() {
|
||||||
|
"output-port" => self.output_port.upgrade().to_value(),
|
||||||
|
"input-port" => self.input_port.upgrade().to_value(),
|
||||||
|
"active" => self.active.get().to_value(),
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
fn set_property(&self, _id: usize, value: &glib::Value, pspec: &glib::ParamSpec) {
|
||||||
|
match pspec.name() {
|
||||||
|
"output-port" => self.output_port.set(value.get().unwrap()),
|
||||||
|
"input-port" => self.input_port.set(value.get().unwrap()),
|
||||||
|
"active" => self.active.set(value.get().unwrap()),
|
||||||
|
_ => unimplemented!(),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
glib::wrapper! {
|
||||||
|
pub struct Link(ObjectSubclass<imp::Link>);
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Link {
|
||||||
|
pub fn new() -> Self {
|
||||||
|
glib::Object::new()
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn output_port(&self) -> Option<Port> {
|
||||||
|
self.property("output-port")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_output_port(&self, port: Option<&Port>) {
|
||||||
|
self.set_property("output-port", port);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn input_port(&self) -> Option<Port> {
|
||||||
|
self.property("input-port")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_input_port(&self, port: Option<&Port>) {
|
||||||
|
self.set_property("input-port", port);
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn active(&self) -> bool {
|
||||||
|
self.property("active")
|
||||||
|
}
|
||||||
|
|
||||||
|
pub fn set_active(&self, active: bool) {
|
||||||
|
self.set_property("active", active);
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -20,5 +20,7 @@ mod node;
|
|||||||
pub use node::*;
|
pub use node::*;
|
||||||
mod port;
|
mod port;
|
||||||
pub use port::*;
|
pub use port::*;
|
||||||
|
mod link;
|
||||||
|
pub use link::*;
|
||||||
mod zoomentry;
|
mod zoomentry;
|
||||||
pub use zoomentry::*;
|
pub use zoomentry::*;
|
||||||
@@ -109,11 +109,9 @@ mod imp {
|
|||||||
|
|
||||||
fn properties() -> &'static [glib::ParamSpec] {
|
fn properties() -> &'static [glib::ParamSpec] {
|
||||||
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
static PROPERTIES: Lazy<Vec<glib::ParamSpec>> = Lazy::new(|| {
|
||||||
vec![
|
vec![glib::ParamSpecObject::builder::<GraphView>("zoomed-widget")
|
||||||
glib::ParamSpecObject::builder::<GraphView>("zoomed-widget")
|
.flags(glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT)
|
||||||
.flags(glib::ParamFlags::READWRITE | glib::ParamFlags::CONSTRUCT)
|
.build()]
|
||||||
.build(),
|
|
||||||
]
|
|
||||||
});
|
});
|
||||||
|
|
||||||
PROPERTIES.as_ref()
|
PROPERTIES.as_ref()
|
||||||
|
|||||||
Reference in New Issue
Block a user