From bde421a6eae5d596683fd45754d8fabe8ed49a4b Mon Sep 17 00:00:00 2001 From: Naveen Prashanth Date: Wed, 6 May 2026 12:07:11 +0530 Subject: [PATCH] Use native GskPath GTK4 API --- Cargo.toml | 2 +- src/ui/graph/graph_view.rs | 63 +++++++++++++++----------------------- 2 files changed, 26 insertions(+), 39 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index a9c13f7..d34e3f5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -19,7 +19,7 @@ libspa = "0.9.2" async-channel = "2.5.0" adw = { version = "0.9.1", package = "libadwaita", features = ["v1_9", "gtk_v4_22"] } glib = { version = "0.22.7", features = ["log", "v2_88"] } -gtk4 = "0.11.3" +gtk4 = { version = "0.11.3", features = ["v4_14"] } log = "0.4.29" diff --git a/src/ui/graph/graph_view.rs b/src/ui/graph/graph_view.rs index 9f2f990..75e3ac6 100644 --- a/src/ui/graph/graph_view.rs +++ b/src/ui/graph/graph_view.rs @@ -18,7 +18,7 @@ use adw::{ gio, glib::{self, clone}, gtk::{ - self, cairo, + self, graphene::{self, Point}, gsk, }, @@ -545,36 +545,25 @@ mod imp { fn draw_link( &self, - link_cr: &cairo::Context, + snapshot: >k::Snapshot, output_anchor: &Point, input_anchor: &Point, active: bool, color: &gdk::RGBA, ) { - let output_x: f64 = output_anchor.x().into(); - let output_y: f64 = output_anchor.y().into(); - let input_x: f64 = input_anchor.x().into(); - let input_y: f64 = input_anchor.y().into(); + let output_x = output_anchor.x(); + let output_y = output_anchor.y(); + let input_x = input_anchor.x(); + let input_y = input_anchor.y(); - // Use dashed line for inactive links, full line otherwise. - if active { - link_cr.set_dash(&[], 0.0); - } else { - link_cr.set_dash(&[10.0, 5.0], 0.0); - } - - link_cr.set_source_rgba( - color.red().into(), - color.green().into(), - color.blue().into(), - color.alpha().into(), - ); + let builder = gsk::PathBuilder::new(); + builder.move_to(output_x, output_y); // If the output port is farther right than the input port and they have // a similar y coordinate, apply a y offset to the control points // so that the curve sticks out a bit. let y_control_offset = if output_x > input_x { - f64::max(0.0, 25.0 - (output_y - input_y).abs()) + f32::max(0.0, 25.0 - (output_y - input_y).abs()) } else { 0.0 }; @@ -582,9 +571,8 @@ mod imp { // Place curve control offset by half the x distance between the two points. // This makes the curve scale well for varying distances between the two ports, // especially when the output port is farther right than the input port. - let half_x_dist = f64::abs(output_x - input_x) / 2.0; - link_cr.move_to(output_x, output_y); - link_cr.curve_to( + let half_x_dist = f32::abs(output_x - input_x) / 2.0; + builder.cubic_to( output_x + half_x_dist, output_y - y_control_offset, input_x - half_x_dist, @@ -593,12 +581,18 @@ mod imp { input_y, ); - if let Err(e) = link_cr.stroke() { - warn!("Failed to draw graphview links: {}", e); - }; + let path = builder.to_path(); + let stroke = gsk::Stroke::new(2.0 * self.zoom_factor.get() as f32); + + // Use dashed line for inactive links, full line otherwise. + if !active { + stroke.set_dash(&[10.0, 5.0]); + } + + snapshot.append_stroke(&path, &stroke, color); } - fn draw_dragged_link(&self, port: &Port, link_cr: &cairo::Context, colors: &Colors) { + fn draw_dragged_link(&self, port: &Port, snapshot: >k::Snapshot, colors: &Colors) { let Some(port_anchor) = port.compute_point(&*self.obj(), &port.link_anchor()) else { return; }; @@ -628,17 +622,10 @@ mod imp { let color = &colors.color_for_media_type(MediaType::from_raw(port.media_type())); - self.draw_link(link_cr, output_anchor, input_anchor, false, color); + self.draw_link(snapshot, output_anchor, input_anchor, false, color); } - fn snapshot_links(&self, widget: &super::GraphView, snapshot: >k::Snapshot) { - let (width, height) = (widget.width(), widget.height()); - - let link_cr = - snapshot.append_cairo(&graphene::Rect::new(0.0, 0.0, width as f32, height as f32)); - - link_cr.set_line_width(2.0 * self.zoom_factor.get()); - + fn snapshot_links(&self, _widget: &super::GraphView, snapshot: >k::Snapshot) { let colors = Colors { audio: gdk::RGBA::new(50.0 / 255.0, 100.0 / 255.0, 240.0 / 255.0, 1.0), video: gdk::RGBA::new(200.0 / 255.0, 200.0 / 255.0, 0.0, 1.0), @@ -656,7 +643,7 @@ mod imp { }; self.draw_link( - &link_cr, + snapshot, &output_anchor, &input_anchor, link.active(), @@ -665,7 +652,7 @@ mod imp { } if let Some(port) = self.dragged_port.upgrade() { - self.draw_dragged_link(&port, &link_cr, &colors); + self.draw_dragged_link(&port, snapshot, &colors); } }