Skip to content

Commit

Permalink
Refactor current code and use subclasses
Browse files Browse the repository at this point in the history
  • Loading branch information
danirod committed Mar 27, 2024
1 parent 26efe5c commit 817437e
Show file tree
Hide file tree
Showing 6 changed files with 194 additions and 279 deletions.
3 changes: 2 additions & 1 deletion data/prototype.blp
@@ -1,7 +1,7 @@
using Gtk 4.0;
using GtkSource 5;

ApplicationWindow win {
template $CarteroWindow: ApplicationWindow {
Box {
orientation: vertical;

Expand All @@ -24,6 +24,7 @@ ApplicationWindow win {
}

Button send {
clicked => $on_send_request() swapped;
label: 'Enviar';
}
}
Expand Down
168 changes: 0 additions & 168 deletions data/prototype.ui

This file was deleted.

50 changes: 50 additions & 0 deletions src/app.rs
@@ -0,0 +1,50 @@
use glib::Object;
use gtk4::gio;

use crate::win::CarteroWindow;

mod imp {
use glib::subclass::{object::ObjectImpl, types::ObjectSubclass};
use gtk4::prelude::*;
use gtk4::subclass::prelude::ObjectSubclassExt;
use gtk4::subclass::{application::GtkApplicationImpl, prelude::ApplicationImpl};

#[derive(Default)]
pub struct CarteroApplication;

#[glib::object_subclass]
impl ObjectSubclass for CarteroApplication {
const NAME: &'static str = "CarteroApplication";
type Type = super::CarteroApplication;
type ParentType = gtk4::Application;
}

impl ObjectImpl for CarteroApplication {}

impl ApplicationImpl for CarteroApplication {
fn activate(&self) {
self.obj().get_window().present();
}
}

impl GtkApplicationImpl for CarteroApplication {}
}

glib::wrapper! {
pub struct CarteroApplication(ObjectSubclass<imp::CarteroApplication>)
@extends gio::Application, gtk4::Application,
@implements gio::ActionMap, gio::ActionGroup;

}

impl CarteroApplication {
pub fn new() -> Self {
Object::builder()
.property("application-id", "es.danirod.Cartero")
.build()
}

pub fn get_window(&self) -> CarteroWindow {
CarteroWindow::new(&self)
}
}
6 changes: 3 additions & 3 deletions src/components/rowheader.rs
@@ -1,8 +1,8 @@
use glib::GString;
use glib::subclass::InitializingObject;
use gtk4::{prelude::*, CompositeTemplate};
use glib::GString;
use gtk4::subclass::prelude::*;
use gtk4::{ Box, Entry };
use gtk4::{prelude::*, CompositeTemplate};
use gtk4::{Box, Entry};

#[derive(CompositeTemplate, Default)]
#[template(file = "../../data/ui/rowheader.ui")]
Expand Down
113 changes: 6 additions & 107 deletions src/main.rs
@@ -1,113 +1,12 @@
mod app;
mod components;
mod win;

use std::collections::HashMap;

use gtk4::{prelude::*, DropDown, StringObject};
use gtk4::{Application, ApplicationWindow, Box, Builder, Button, Entry, ListBox};
use sourceview5::View;
use components::rowheader::RowHeader;

// Deprecated
#[derive(Debug)]
enum RequestMethod {
Get,
Post,
Put,
Options,
Patch,
Delete,
}

#[derive(Debug)]
struct Request {
method: RequestMethod,
url: String,
body: String,
// Headers (haha, saludos XD)
}

// Please, convert this into an own class.
struct InterfaceState {
method: DropDown,
url: Entry,
request_body: View,
}

fn mock_map() -> HashMap<String, String> {
let mut map = HashMap::new();
map.insert(String::from("Accept"), String::from("text/html"));
map.insert(String::from("User-Agent"), String::from("Cartero/1.0"));
map.insert(String::from("Accept-Encoding"), String::from("bzip"));
map
}

fn create_row(header_name: &str, header_value: &str) -> Box {
let entry_name = Entry::builder().text(header_name).build();
let entry_value = Entry::builder().text(header_value).build();
let entry_box = Box::builder()
.homogeneous(true)
.spacing(10)
.margin_start(10)
.margin_end(10)
.margin_top(5)
.margin_bottom(5)
.build();
entry_box.append(&entry_name);
entry_box.append(&entry_value);
entry_box
}

fn populate_list(list_box: &ListBox, map: &HashMap<String, String>) {
for (name, value) in map.iter() {
let rowheader = RowHeader::new(&name, &value);
list_box.append(&rowheader);
}
}
use app::CarteroApplication;
use gtk4::prelude::*;
pub use win::CarteroWindow;

fn main() -> glib::ExitCode {
let app = Application::builder()
.application_id("es.danirod.Cartero") // change to avoid a copyright take down
.build();

app.connect_activate(|app| {
sourceview5::init();

let ui = include_str!("../data/prototype.ui");
let builder = Builder::from_string(ui);

let request_method = builder.object::<DropDown>("method").unwrap();
let request_url = builder.object::<Entry>("url").unwrap();
let request_body = builder.object::<View>("request_body").unwrap();
let request_state = InterfaceState {
method: request_method,
url: request_url,
request_body,
};

let send = builder.object::<Button>("send").unwrap();
send.connect_clicked(move |_| {
let url = String::from(request_state.url.text());
let method = request_state
.method
.selected_item()
.unwrap()
.downcast::<StringObject>()
.unwrap()
.string();
let (start, end) = request_state.request_body.buffer().bounds();
let body = request_state.request_body.buffer().text(&start, &end, true);
println!("{} {}", method, url);
println!("{}", body);
});

let request_list = builder.object::<ListBox>("request_headers").unwrap();
let headers = mock_map();
populate_list(&request_list, &headers);

let window = builder.object::<ApplicationWindow>("win").unwrap();
window.set_application(Some(app));
window.present();
});

let app = CarteroApplication::new();
app.run()
}

0 comments on commit 817437e

Please sign in to comment.