Skip to content

Commit

Permalink
Use GtkDrawingArea for our main widget
Browse files Browse the repository at this point in the history
  • Loading branch information
Alecaddd committed Sep 24, 2024
1 parent 4390587 commit 919524a
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 22 deletions.
57 changes: 38 additions & 19 deletions src/layout/main_area.rs → src/layout/drawing_area.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,47 +19,58 @@ extern crate skia_safe;
use crate::canvas::Canvas;

use gtk::glib;
use gtk::prelude::*;
use gtk::subclass::prelude::*;
use gtk::prelude::DrawingAreaExtManual;
use gtk::{cairo, prelude::*, subclass::prelude::*};

mod imp {
use super::*;
use gtk::glib::clone;

#[derive(Debug, Default)]
pub struct MainArea {}
pub struct DrawingArea {}

#[glib::object_subclass]
impl ObjectSubclass for MainArea {
const NAME: &'static str = "MainArea";
type Type = super::MainArea;
type ParentType = gtk::Grid;
impl ObjectSubclass for DrawingArea {
const NAME: &'static str = "DrawingArea";
type ParentType = gtk::DrawingArea;
type Type = super::DrawingArea;
}

impl ObjectImpl for MainArea {
impl ObjectImpl for DrawingArea {
fn constructed(&self) {
self.parent_constructed();

let obj = self.obj();
obj.build_canvas();
}
}
impl WidgetImpl for MainArea {}
impl GridImpl for MainArea {}
impl WidgetImpl for DrawingArea {
fn realize(&self) {
self.parent_realize();

let obj = self.obj();
obj.set_draw_func(clone!(
#[weak]
obj,
move |_, context, _, _| obj.draw(context)
));
}
}
impl DrawingAreaImpl for DrawingArea {}
}

glib::wrapper! {
pub struct MainArea(ObjectSubclass<imp::MainArea>) @extends gtk::Widget, gtk::Grid;
pub struct DrawingArea(ObjectSubclass<imp::DrawingArea>)
@extends gtk::DrawingArea, gtk::Widget, gtk::Buildable;
}

impl Default for MainArea {
fn default() -> Self {
glib::Object::builder()
.property("orientation", gtk::Orientation::Vertical)
.build()
impl DrawingArea {
pub fn new() -> Self {
glib::Object::builder().build()
}
}

impl MainArea {
fn draw(&self, context: &cairo::Context) {}

fn build_canvas(&self) {
let mut canvas = Canvas::new(2560, 1280);
canvas.scale(1.2, 1.2);
Expand All @@ -77,7 +88,15 @@ impl MainArea {
canvas.line_to(270.0, 90.0);
canvas.fill();

// How do we plug this into a gtk grid? Do we need a renderer wrapper?
// let surface = canvas.surface;
// How do we plug skia canvas into the gtk drawing area?
// Do we need to pass through cairo context? (yuck)
// self.attach(&canvas, 1, 1, 1, 1);
}
}

impl Default for DrawingArea {
fn default() -> Self {
DrawingArea::new()
}
}
2 changes: 1 addition & 1 deletion src/layout/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,5 +14,5 @@
* You should have received a copy of the GNU General Public License along with
* Akira. If not, see <https://www.gnu.org/ licenses/>.
*/
pub mod main_area;
pub mod drawing_area;
pub mod sidebar;
4 changes: 2 additions & 2 deletions src/window.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use gtk::{gio, gio::Settings, glib, prelude::*, ApplicationWindow};
use once_cell::sync::OnceCell;

use crate::config::APP_ID;
use crate::layout::main_area;
use crate::layout::drawing_area;
use crate::layout::sidebar::*;

mod imp {
Expand Down Expand Up @@ -158,7 +158,7 @@ impl AppWindow {
pane1.set_end_child(Some(&pane2));
pane1.set_start_child(Some(&options::Options::default()));

pane2.set_start_child(Some(&main_area::MainArea::default()));
pane2.set_start_child(Some(&drawing_area::DrawingArea::new()));
pane2.set_end_child(Some(&layers::Layers::default()));

self.set_child(Some(&pane1));
Expand Down

0 comments on commit 919524a

Please sign in to comment.