Skip to content

Commit 891d846

Browse files
authored
Merge pull request #3 from derenv/async_check
Async check
2 parents 4485b11 + 90a0602 commit 891d846

File tree

4 files changed

+135
-85
lines changed

4 files changed

+135
-85
lines changed

src/main.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
mod custom_button;
2323
//use custom_button::CustomButton;
2424
mod subprocess;
25+
mod processor;
2526

2627
use std::ffi::OsStr;
2728

src/processor/imp.rs

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// SPDX-FileCopyrightText: 2022 Deren Vural
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
/*
5+
* Name:
6+
* imp.rs
7+
*
8+
* Description:
9+
* Implementation of our custom GObject class (Processor)
10+
*
11+
* Made:
12+
* 18/09/2022
13+
*
14+
* Made by:
15+
* Deren Vural
16+
*
17+
* Notes:
18+
*
19+
*/
20+
21+
// Imports
22+
use gtk::glib;
23+
use gtk::subclass::prelude::*;
24+
25+
// Object holding the state
26+
#[derive(Default)]
27+
pub struct Processor {
28+
name: String,
29+
base_call: String,
30+
tail_call: String,
31+
call: String,
32+
}
33+
34+
// The central trait for subclassing a GObject
35+
#[glib::object_subclass]
36+
impl ObjectSubclass for Processor {
37+
//Crate+Obj to avoid collisions
38+
const NAME: &'static str = "NvidiaExtensionRustProcessor";
39+
// the actual GObject that will be created
40+
type Type = super::Processor;
41+
// Parent GObject we inherit from
42+
type ParentType = gtk::Widget;
43+
}
44+
45+
// Trait shared by all GObjects
46+
impl ObjectImpl for Processor {}
47+
48+
// Trait shared by all widgets
49+
impl WidgetImpl for Processor {}

src/processor/mod.rs

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
// SPDX-FileCopyrightText: 2022 Deren Vural
2+
// SPDX-License-Identifier: GPL-3.0-or-later
3+
4+
/*
5+
* Name:
6+
* mod.rs
7+
*
8+
* Description:
9+
* Public-facing interface/wrapper for our custom GObject (Processor)
10+
*
11+
* Made:
12+
* 12/09/2022
13+
*
14+
* Made by:
15+
* Deren Vural
16+
*
17+
* Notes:
18+
*
19+
*/
20+
21+
// Custom GObjects
22+
mod imp;
23+
24+
// Imports
25+
use glib::Object;
26+
use gtk::glib;
27+
28+
glib::wrapper! {
29+
pub struct Processor(ObjectSubclass<imp::Processor>)
30+
@extends gtk::Widget,
31+
//@extends gtk::Button, gtk::Widget,
32+
@implements gtk::Accessible, gtk::Actionable, gtk::Buildable, gtk::ConstraintTarget;
33+
}
34+
35+
impl Processor {
36+
pub fn new(name: &str, base_call: &str, tail_call: &str) -> Self {
37+
Object::new(&[
38+
("name", &name),
39+
("base_call", &base_call),
40+
("tail_call", &tail_call)
41+
]).expect("Failed to create `Processor`.")
42+
}
43+
44+
pub fn parse() {
45+
todo!()
46+
}
47+
48+
pub fn process(self) {
49+
todo!()
50+
}
51+
52+
pub fn add_property(self) {
53+
todo!()
54+
}
55+
56+
pub fn get_name(self) -> String {
57+
self.name
58+
}
59+
}

src/subprocess/mod.rs

Lines changed: 26 additions & 85 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
*/
2020

2121
// Imports
22+
use gtk::glib::Bytes;
2223
use gtk::prelude::*;
2324
use gtk::{gio, glib};
2425
use std::ffi::OsStr;
@@ -40,107 +41,50 @@ use std::ffi::OsStr;
4041
* Made by:
4142
* Deren Vural
4243
*
43-
* @param {string[]} argv - a list of string arguments
44-
* @param {Gio.Cancellable} [cancellable] - optional cancellable object
45-
* @returns {Promise<boolean>} - The process success
44+
* Notes:
4645
*
4746
*/
4847
pub fn exec_check(
4948
argv: &[&OsStr],
5049
cancellable: Option<&impl IsA<gio::Cancellable>>,
5150
) -> Result<(), glib::Error> {
5251
// Create subprocess
53-
println!("..creating subprocess"); //DEBUG
5452
match gio::Subprocess::newv(argv, gio::SubprocessFlags::NONE) {
5553
Err(err) => Err(err),
5654
Ok(proc) => {
57-
println!("..Subprocess successfully created!"); //DEBUG
58-
5955
// Run subprocess
60-
61-
// Define callback
62-
//This should be called when the process is done
63-
/*
64-
let callback = |q: Result<(), glib::Error>| {
65-
match q {
66-
Err(err) => {
67-
Err(err)
68-
},
69-
Ok(_out) => {
70-
println!("....process finished");//DEBUG
71-
Ok(())
72-
},
73-
}
74-
};
75-
76-
struct Listener {
77-
done: bool
78-
}
79-
impl Listener {
80-
pub fn on_call(&mut self, done: bool) { self.done = done }
81-
}
82-
struct Caller<'callback> {
83-
callback: Box<dyn FnMut(bool) + 'callback>,
84-
}
85-
impl Caller<'_> {
86-
pub fn call(&mut self) { (self.callback)(true) }
56+
match proc.wait_async(cancellable, |_| ()) {
57+
_ => Ok(()),
8758
}
88-
let mut listener = Listener { done: false };
89-
let mut caller = Caller { callback: Box::new(|x| listener.on_call(x)) };
90-
91-
//fn callback_fn(x: bool){println!("callback bitch!! {}", x)}
92-
//let callback_box = Box::new(|x: bool| callback_fn(x));
93-
94-
//proc.wait_future();//not sure what this does tbh
95-
*/
96-
97-
/*
98-
Do i just not use Results with callback versions?
99-
*/
100-
// This doesn't work
101-
/*
102-
match proc.wait_async(cancellable,None) {
103-
Err(err) => {
104-
Err(err)
105-
},
106-
Ok(_out) => {
107-
println!("....process finished");//DEBUG
108-
Ok(())
109-
},
110-
}
111-
*/
112-
// This also doesn't work
113-
/*
114-
match proc.wait_async(cancellable, callback) {
115-
Err(err) => {
116-
Err(err)
117-
},
118-
Ok(_out) => {
119-
println!("....process finished");//DEBUG
120-
Ok(())
121-
},
122-
}
123-
*/
124-
125-
//*
126-
// This works but holds up main thread..
127-
match proc.wait(cancellable) {
128-
Err(err) => Err(err),
129-
Ok(_out) => {
130-
println!("....process finished"); //DEBUG
131-
Ok(())
132-
}
133-
}
134-
//*/
13559
}
13660
}
13761
}
13862

63+
/*
64+
* Name:
65+
* exec_communicate
66+
*
67+
* Description:
68+
* Execute a command and return any output
69+
*
70+
* If given, @cancellable can be used to stop the process before it finishes.
71+
*
72+
* https://gtk-rs.org/gtk-rs-core/stable/0.14/docs/src/gio/auto/subprocess.rs.html
73+
*
74+
* Made:
75+
* 15/09/2022
76+
*
77+
* Made by:
78+
* Deren Vural
79+
*
80+
* Notes:
81+
*
82+
*/
13983
pub fn exec_communicate(
14084
argv: &[&OsStr],
14185
_input: Option<&str>,
14286
cancellable: Option<&impl IsA<gio::Cancellable>>,
143-
) -> Result<(), glib::Error> {
87+
) -> Result<(Option<Bytes>, Option<Bytes>), glib::Error> {
14488
// Create subprocess
14589
println!("..creating subprocess"); //DEBUG
14690
match gio::Subprocess::newv(argv, gio::SubprocessFlags::NONE) {
@@ -151,10 +95,7 @@ pub fn exec_communicate(
15195
// Run subprocess
15296
match proc.communicate(None, cancellable) {
15397
Err(err) => Err(err),
154-
Ok(_out) => {
155-
println!("....process finished"); //DEBUG
156-
Ok(())
157-
}
98+
Ok(out) => Ok(out),
15899
}
159100
}
160101
}

0 commit comments

Comments
 (0)