From 13dda7c316d30ece96a318d9b521f5c7f13e35a0 Mon Sep 17 00:00:00 2001 From: sleptworld Date: Thu, 27 Apr 2023 18:29:47 +0800 Subject: [PATCH] add backend --- Cargo.lock | 7 ++ Cargo.toml | 3 + build.rs | 7 ++ src/css/style.css | 3 + src/main.rs | 94 +++++++++++++-------------- src/painter/mod.rs | 1 + src/painter/polar_coord.rs | 16 +++++ src/resources/resources.gresource.xml | 6 ++ src/resources/window.ui | 15 +++++ src/window/imp.rs | 50 ++++++++++++++ src/window/mod.rs | 20 ++++++ 11 files changed, 175 insertions(+), 47 deletions(-) create mode 100644 build.rs create mode 100644 src/css/style.css create mode 100644 src/painter/mod.rs create mode 100644 src/painter/polar_coord.rs create mode 100644 src/resources/resources.gresource.xml create mode 100644 src/resources/window.ui create mode 100644 src/window/imp.rs create mode 100644 src/window/mod.rs diff --git a/Cargo.lock b/Cargo.lock index f29b9e9..3ca5d49 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,7 @@ version = "0.1.0" dependencies = [ "cairo-rs", "glib", + "glib-build-tools", "gtk4", "plotters", "plotters-backend", @@ -602,6 +603,12 @@ dependencies = [ "thiserror", ] +[[package]] +name = "glib-build-tools" +version = "0.17.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "8f8480c9ba9cc06aa8d5baf446037f8dc237bee127e9b62080c4db7e293d8ea0" + [[package]] name = "glib-macros" version = "0.17.9" diff --git a/Cargo.toml b/Cargo.toml index 4093075..a266edc 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,3 +13,6 @@ gtk = { version = "0.6.6", package = "gtk4", features = ["v4_8"] } plotters = "0.3.4" plotters-backend = "0.3.4" + +[build-dependencies] +glib-build-tools = "0.17.0" diff --git a/build.rs b/build.rs new file mode 100644 index 0000000..43a125c --- /dev/null +++ b/build.rs @@ -0,0 +1,7 @@ +fn main() { + glib_build_tools::compile_resources( + &["src/resources"], + "src/resources/resources.gresource.xml", + "window.gresource", + ); +} \ No newline at end of file diff --git a/src/css/style.css b/src/css/style.css new file mode 100644 index 0000000..6ca35c8 --- /dev/null +++ b/src/css/style.css @@ -0,0 +1,3 @@ +button { + color: magenta; +} \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index 10728dd..8478e15 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,19 +1,27 @@ -use crate::backend::CairoBackend; -use gtk::gdk::CairoContext; -use gtk::{glib, Application, ApplicationWindow}; +use backend::CairoBackend; +use gtk::gdk::Display; +use gtk::{ + gio, glib, style_context_add_provider_for_display, Application, ApplicationWindow, CssProvider, + StyleContext, +}; use gtk::{prelude::*, DrawingArea}; -use plotters::prelude::*; - -use cairo::Context; - -pub mod backend; +use plotters::prelude::{ChartBuilder, IntoDrawingArea}; +use plotters::style::{BLACK, WHITE}; +// use plotters::prelude::DrawingArea; +use window::Window; +mod backend; +mod painter; +mod window; const APP_ID: &str = "org.gtk_rs.HelloWorld2"; fn main() -> glib::ExitCode { + gio::resources_register_include!("window.gresource").expect("Failed to register resources"); // Create a new application let app = Application::builder().application_id(APP_ID).build(); + app.connect_startup(|_| load_css()); + // Connect to "activate" signal of `app` app.connect_activate(build_ui); @@ -23,50 +31,42 @@ fn main() -> glib::ExitCode { fn build_ui(app: &Application) { // Create a window and set the title - let window = ApplicationWindow::builder() - .application(app) - .title("My GTK App") - .build(); - let draw_area = DrawingArea::new(); + let window = Window::new(app); - draw_area.set_draw_func(|a, cr, width, height| { - draw(cr, 2000, 1000); + let drawing_area = DrawingArea::new(); + + drawing_area.set_draw_func(|a, b, c, d| { + let root = CairoBackend::new(b, (500, 500)) + .expect("fuck") + .into_drawing_area(); + + root.fill(&WHITE); + + let mut chart = ChartBuilder::on(&root) + .margin(20) + .build_cartesian_2d(-2.1f64..0.6f64, -1.2f64..1.2f64).unwrap(); + + chart.configure_mesh().draw().unwrap(); + + }); - window.set_child(Some(&draw_area)); - // Present window + window.set_child(Some(&drawing_area)); + window.set_default_width(1000); + window.set_default_height(800); + window.present(); } -fn draw(ctx: &Context, width: u32, height: u32) -> Result<(), Box> { - // let mut backend = root.fill(&WHITE)?; - let mut root = CairoBackend::new(ctx, (width, height))?.into_drawing_area(); - - let mut chart = ChartBuilder::on(&root) - .caption("y=x^2", ("sans-serif", 50).into_font()) - .margin(5) - .x_label_area_size(30) - .y_label_area_size(30) - .build_cartesian_2d(-1f32..1f32, -0.1f32..1f32)?; - - chart.configure_mesh().draw()?; - - chart - .draw_series(LineSeries::new( - (-50..=50).map(|x| x as f32 / 50.0).map(|x| (x, x * x)), - &RED, - ))? - .label("y = x^2") - .legend(|(x, y)| PathElement::new(vec![(x, y), (x + 20, y)], &RED)); - - chart - .configure_series_labels() - .background_style(&WHITE.mix(0.8)) - .border_style(&BLACK) - .draw()?; - - root.present()?; - - Ok(()) +fn load_css() { + // Load the CSS file and add it to the provider + let provider = CssProvider::new(); + provider.load_from_data(include_str!("css/style.css")); + // Add the provider to the default screen + style_context_add_provider_for_display( + &Display::default().expect("Could not connect to a display."), + &provider, + gtk::STYLE_PROVIDER_PRIORITY_APPLICATION, + ); } diff --git a/src/painter/mod.rs b/src/painter/mod.rs new file mode 100644 index 0000000..10f9ff8 --- /dev/null +++ b/src/painter/mod.rs @@ -0,0 +1 @@ +mod polar_coord; \ No newline at end of file diff --git a/src/painter/polar_coord.rs b/src/painter/polar_coord.rs new file mode 100644 index 0000000..dc529ff --- /dev/null +++ b/src/painter/polar_coord.rs @@ -0,0 +1,16 @@ +use plotters::{coord::ranged1d, prelude::Ranged}; + +#[derive(Clone)] +pub struct PolarCoord2d { + logic_r: R, + logic_a: A, +} + +impl PolarCoord2d { + pub fn new, IntoA: Into>(logic_r: IntoR, logic_a: IntoA) -> Self { + Self { + logic_r: logic_r.into(), + logic_a: logic_a.into(), + } + } +} diff --git a/src/resources/resources.gresource.xml b/src/resources/resources.gresource.xml new file mode 100644 index 0000000..4a9ecb2 --- /dev/null +++ b/src/resources/resources.gresource.xml @@ -0,0 +1,6 @@ + + + + window.ui + + diff --git a/src/resources/window.ui b/src/resources/window.ui new file mode 100644 index 0000000..0e3dae4 --- /dev/null +++ b/src/resources/window.ui @@ -0,0 +1,15 @@ + + + + diff --git a/src/window/imp.rs b/src/window/imp.rs new file mode 100644 index 0000000..7a978a3 --- /dev/null +++ b/src/window/imp.rs @@ -0,0 +1,50 @@ +use glib::subclass::InitializingObject; +use gtk::prelude::*; +use gtk::subclass::prelude::*; +use gtk::{glib, Button, CompositeTemplate}; + +#[derive(CompositeTemplate, Default)] +#[template(resource = "/org/cinrad_g/window.ui")] +pub struct Window { + #[template_child] + pub button: TemplateChild