From 2597e3591b8194c4dc319ef7fd90aea22e386b86 Mon Sep 17 00:00:00 2001 From: rosegoldd <155942159+rosegoldd@users.noreply.github.com> Date: Mon, 3 Jun 2024 15:18:55 -0700 Subject: [PATCH] video and audio support --- napture/Cargo.toml | 4 +- napture/src/b9/css.rs | 51 +++++++++++++-- napture/src/b9/html.rs | 137 ++++++++++++++++++++++++++++++++++++---- napture/src/b9/lua.rs | 86 +++++++++++++++++++++---- napture/test/index.html | 6 +- napture/test/script.lua | 11 ++-- 6 files changed, 254 insertions(+), 41 deletions(-) diff --git a/napture/Cargo.toml b/napture/Cargo.toml index 69df1434..b758e3e7 100644 --- a/napture/Cargo.toml +++ b/napture/Cargo.toml @@ -6,11 +6,11 @@ edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -adw = { version = "0.6.0", package = "libadwaita", features = ["v1_5"] } +adw = { version = "0.6.0", package = "libadwaita", features = ["v1_4"] } chrono = "0.4.38" directories = "5.0.1" glib = "0.19.4" -gtk = { version = "0.8.1", package = "gtk4", features = ["v4_14"] } +gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] } html_parser = "0.7.0" lazy_static = "1.4.0" mlua = { version = "0.9.7", features = ["lua54", "macros", "async", "unstable", "serialize"] } diff --git a/napture/src/b9/css.rs b/napture/src/b9/css.rs index 8db6349f..efe85ff9 100644 --- a/napture/src/b9/css.rs +++ b/napture/src/b9/css.rs @@ -484,6 +484,45 @@ impl Styleable for gtk::Picture { } } +// video +impl Styleable for gtk::Video { + fn style(&self) -> String { + let guard = match CSS_RULES.lock() { + Ok(guard) => guard, + Err(_) => { + println!("FATAL: failed to lock CSS_RULES mutex! Aborting function at GtkVideo."); + return String::new(); + } + }; + + if let Some(css) = guard.as_ref() { + let mut classes = self.css_classes(); + let mut final_css = "".to_string(); + + classes.push(self.css_name()); + + for class in classes { + if let Some(rules) = css.get(&class.to_string()) { + let properties: Properties = get_properties(rules); + + self.set_margin_top(properties.margin_top.parse::().unwrap_or(0)); + self.set_margin_bottom(properties.margin_bottom.parse::().unwrap_or(0)); + self.set_margin_start(properties.margin_left.parse::().unwrap_or(0)); + self.set_margin_end(properties.margin_right.parse::().unwrap_or(0)); + + self.set_opacity(properties.opacity); + + final_css += &compute_styling(class, &properties); + } + } + + final_css + } else { + String::new() + } + } +} + // input impl Styleable for gtk::Entry { fn style(&self) -> String { @@ -507,7 +546,7 @@ impl Styleable for gtk::Entry { let width = properties.width; let height = properties.height; - + if width > 0 || height > 0 { let normalized_width = if width > 0 { width } else { -1 }; let normalized_height = if height > 0 { height } else { -1 }; @@ -558,7 +597,7 @@ impl Styleable for gtk::Button { self.set_margin_bottom(properties.margin_bottom.parse::().unwrap_or(0)); self.set_margin_start(properties.margin_left.parse::().unwrap_or(0)); self.set_margin_end(properties.margin_right.parse::().unwrap_or(0)); - + self.set_opacity(properties.opacity); final_css += &compute_styling(class, &properties); @@ -658,7 +697,7 @@ fn compute_styling(class: GString, properties: &Properties) -> String { if properties.border_style != "none" { borders.push_str(&format!("border-style: {};", properties.border_style)); } - + format!( " .{} {{ @@ -726,8 +765,8 @@ fn get_properties(rules: &[(String, String)]) -> Properties { .parse::() .unwrap_or(0); let opacity = get_rule(rules, "opacity", "1.0") - .parse::() - .unwrap_or(1.0); + .parse::() + .unwrap_or(1.0); Properties { direction, @@ -757,6 +796,6 @@ fn get_properties(rules: &[(String, String)]) -> Properties { padding, gap, font_size, - opacity + opacity, } } diff --git a/napture/src/b9/html.rs b/napture/src/b9/html.rs index 3230f166..08dffc62 100644 --- a/napture/src/b9/html.rs +++ b/napture/src/b9/html.rs @@ -7,9 +7,16 @@ use super::{ lua, }; -use std::{cell::RefCell, fs, rc::Rc, thread}; - -use gtk::{gdk::Display, gdk_pixbuf, gio, glib::Bytes, prelude::*, CssProvider}; +use std::{cell::RefCell, fs, io::Write, rc::Rc, thread}; + +use gtk::{ + gdk::Display, + gdk_pixbuf, + gio::{self, File}, + glib::Bytes, + prelude::*, + CssProvider, +}; use html_parser::{Dom, Element, Node, Result}; use lua::Luable; @@ -87,7 +94,7 @@ pub async fn build_ui( .build(); let mut css: String = css::reset_css(); - + let (head, body) = match parse_html(furl.to_string()).await { Ok(ok) => ok, Err(e) => { @@ -208,7 +215,12 @@ pub async fn build_ui( Ok((html_view, provider)) } -async fn render_head(element: &Element, contents: Option<&Node>, tab: Rc>, furl: &String) { +async fn render_head( + element: &Element, + contents: Option<&Node>, + tab: Rc>, + furl: &String, +) { match element.name.as_str() { "title" => { if let Some(contents) = contents { @@ -480,6 +492,78 @@ fn render_html( wrapper.append(&image); html_view.append(&wrapper); } + "video" => { + let url = match element.attributes.get("src") { + Some(Some(url)) => url.clone(), + _ => { + println!("INFO: