Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add rust demo grid #571

Merged
merged 4 commits into from
Sep 4, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
53 changes: 53 additions & 0 deletions src/Library/demos/Grid/code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
use crate::workbench;
use glib::clone;
use gtk::glib;
use gtk::prelude::*;
use rand::Rng;
use std::cell::Cell;
use std::rc::Rc;

pub fn main() {
let button_ids = [
"button00", "button01", "button02", "button10", "button11", "button12", "button20",
"button21", "button22",
];

let step = Rc::new(Cell::new(1));
for id in button_ids {
let button: gtk::Button = workbench::builder().object(id).unwrap();
button.connect_clicked(clone!(@strong step => move |button| {
on_clicked(button, step.clone())
}));
}
}

fn on_clicked(button: &gtk::Button, step: Rc<Cell<i32>>) {
// Check access for user action
let image: gtk::Image = button.child().unwrap().downcast().unwrap();
if image.icon_name().is_some() {
return;
}
// Store and show user action
image.set_icon_name(Some("cross-large-symbolic"));
// Calculate pc reaction
let mut pc_is_thinking = true;
while pc_is_thinking {
let random_row: f64 = rand::thread_rng().gen();
let random_col: f64 = rand::thread_rng().gen();
let pc_is_thinking_row = (random_row * 3.0).floor().to_string();
let pc_is_thinking_col = (random_col * 3.0).floor().to_string();
let temp: gtk::Button = workbench::builder()
.object(format!("button{pc_is_thinking_row}{pc_is_thinking_col}"))
.unwrap();
let temp_image: gtk::Image = temp.child().unwrap().downcast().unwrap();
if temp_image.icon_name().is_none() {
// Store and show pc reaction
temp_image.set_icon_name(Some("circle-outline-thick-symbolic"));
pc_is_thinking = false;
step.set(step.get() + 2);
}
if step.get() >= 8 {
pc_is_thinking = false;
}
}
}
2 changes: 1 addition & 1 deletion src/langs/rust/template/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/langs/rust/template/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ edition = "2021"

[dependencies]
libc = "0.2"
rand = "0.8.5"
url = "2.4.0"


Expand Down