Clippy and stuff

This commit is contained in:
TheTechRobo 2022-08-07 21:55:54 -04:00
parent 3f744762dc
commit 5e10f1feea
3 changed files with 52 additions and 17 deletions

33
Cargo.lock generated
View File

@ -103,6 +103,12 @@ dependencies = [
"smallvec",
]
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "clap"
version = "3.2.14"
@ -262,6 +268,17 @@ dependencies = [
"system-deps",
]
[[package]]
name = "getrandom"
version = "0.2.7"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4eb1a864a501629691edf6c15a593b7a51eebaa1e8468e9ddc623de7c9b58ec6"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "gio"
version = "0.15.12"
@ -621,6 +638,7 @@ dependencies = [
"clap",
"gtk",
"mlua",
"uuidv7",
]
[[package]]
@ -763,6 +781,15 @@ version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "15c61ba63f9235225a22310255a29b806b907c9b8c964bcbd0a2c70f3f2deea7"
[[package]]
name = "uuidv7"
version = "0.1.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3d416315f815f94d52db5db92e1c07561ac8fddc08cc548904a207f0a73073"
dependencies = [
"getrandom",
]
[[package]]
name = "version-compare"
version = "0.1.0"
@ -775,6 +802,12 @@ version = "0.9.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "49874b5167b65d7193b8aba1567f5c7d93d001cafc34600cee003eda787e483f"
[[package]]
name = "wasi"
version = "0.11.0+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
[[package]]
name = "winapi"
version = "0.3.9"

View File

@ -9,6 +9,7 @@ edition = "2021"
clap = {version="3.2.14", features=["derive"]}
gtk = "0.15.5"
mlua = {version="^0.8.1", features=["lua54", "vendored"]}
uuidv7 = "0.1.1"
[target.x86_64-unknown-linux-gnu]
rustflags = ["-C", "link-arg=-fuse-ld=mold"]

View File

@ -1,3 +1,6 @@
#![warn(unused_import_braces)]
#![warn(unused_lifetimes)]
#![warn(unused_qualifications)]
#![feature(once_cell)]
// GTK 3
@ -13,16 +16,9 @@ mod cli; // where the arguments are defined
use mlua::prelude::LuaResult;
use mlua::Lua;
// I really need to not use this,
// but I currently have no idea
// how NOT to. In python you'd
// just use class attributes...
// but here, the borrow checker
// kicks in...
// Allow using one shared Lua context.
// Thanks to https://users.rust-lang.org/t/lazy-static-without-mutexes/23126/7
// for suggesting I go this route! (LazyLock had a Sync requirement, which
// mlua doesn't implement.)
// Originally I thought I should find an alternate method ASAP,
// but then I realised that this is actually a lot easier than
// passing around values. It also makes the code easier to read IMO.
use std::cell::RefCell;
thread_local! {
pub static LUA: RefCell<Lua> = RefCell::new(Lua::new());
@ -31,6 +27,7 @@ thread_local! {
const APPLICATION_ID: &str = "ca.thetechrobo.rad";
#[derive(Debug, Clone)]
#[allow(clippy::enum_variant_names)]
enum CallbackType {
OnModify,
OnClick,
@ -89,7 +86,6 @@ enum Widget {
struct Window {
title: String,
widgets: Vec<Widget>,
//callbacks: RefCell<Lua>,
height: i32,
width: i32,
app: Option<Application>,
@ -104,7 +100,6 @@ fn create_app(title: String, width: i32, height: i32, mut window: Window) -> App
.application_id(APPLICATION_ID)
.build();
app.connect_activate(move |app| {
//let mut window = window.borrow_mut();
// We create the main window.
let lua_script = r#"print("21")"#;
LUA.with(|st| {
@ -131,11 +126,11 @@ fn create_app(title: String, width: i32, height: i32, mut window: Window) -> App
input.connect_changed(|e| {
LUA.with(|st| {
let lua = st.borrow_mut();
lua.load(r#"print("22")"#).exec();
lua.load(r#"print("22")"#).exec().expect("Failed to run Lua");
});
set_title(get_window_from_widget(e.toplevel().unwrap()).unwrap(), "hi");
});
bobox.add(&input);
println!("{}", add_widget(bobox, input));
// Don't forget to make all widgets visible.
win.show_all();
@ -147,7 +142,7 @@ fn create_app(title: String, width: i32, height: i32, mut window: Window) -> App
app
}
fn get_window_from_widget(widget: gtk::Widget) -> Option<gtk::gdk::Window> { // todo: IsA<gtk::Widget> doesn't work, but we should support it
fn get_window_from_widget<T: IsA<gtk::Widget>>(widget: T) -> Option<gtk::gdk::Window> { // todo: IsA<gtk::Widget> doesn't work, but we should support it
widget.toplevel().unwrap().window()
}
@ -155,6 +150,12 @@ fn set_title(window: gtk::gdk::Window, text: &str) {
window.set_title(text);
}
fn add_widget<T: IsA<gtk::Widget>>(cont: gtk::Box, widget: T) -> String {
let uuid: String = uuidv7::create();
cont.add(&widget);
uuid
}
impl Window {
fn init(&mut self, app: Application) {
self.app = Some(app);
@ -193,14 +194,14 @@ fn setup_lua() -> LuaResult<()> {
})?;
lua.globals().set("item", f).unwrap();
Ok(())
});
})?;
Ok(())
}
fn main() -> LuaResult<()> {
setup_lua()?;
let args = cli::Args::parse();
let mut window = Window {
let window = Window {
title: args.title,
widgets: Vec::new(),
width: args.width,