I have a few buttons, when a button gets clicked it needs to be appended to a display (a TextBox
).
This done by first writing the pressed buttons to a String.
use orbtk::*;
use std::vec::Vec;
use std::cell::RefCell;
use std::rc::Rc;
use std::io::Write;
use std::str::FromStr;
extern crate strum;
#[macro_use]
extern crate strum_macros;
widget!(MainView);
// Enumeration of all possible operators
#[derive(EnumString)]
enum Operator {
#[strum(serialize="+")]
Plus,
#[strum(serialize="-")]
Min,
#[strum(serialize="*")]
Times,
#[strum(serialize="/")]
Divide,
}
impl Template for MainView {
fn template(self, _: Entity, ctx: &mut BuildContext) -> Self {
// String holding pressed keys creating an infix string
// And a TextBox object holding that string
let pressed_keys : Rc<RefCell<String>> = Rc::new(RefCell::new("".to_string()));
let mut screen : Rc<RefCell<TextBox>> =
Rc::new(RefCell::new(
TextBox::create()
.text("")
.attach(Grid::row(0))
.attach(Grid::column(0))
.attach(Grid::column_span(4))
));
// Initializing Grid
let mut grid = Grid::create();
// Configuring grid (amount of rows and columns)
grid = grid
.columns(
Columns::create()
.column("*")
.column("*")
.column("*")
.column("*")
.build()
)
.rows(
Rows::create()
.row(50.0)
.row(50.0)
.row(50.0)
.row(50.0)
.row(50.0)
.row(50.0)
.row(50.0)
.build()
);
//Adding textbox holding entered numbers and operators
grid = grid.child(
screen.borrow_mut().build(ctx)
);
// Adding all buttons from 1-9 to the grid
// in calculator format
let mut counter : u8 = 9;
for i in 1..4 {
for j in 0..3 {
grid = grid.child(
Button::create()
.text(counter.to_string())
.attach(Grid::column(2-j))
.attach(Grid::row(i))
.on_click({
let pressed_keys = pressed_keys.clone();
let screen = screen.clone();
move |_states, _| -> bool {
*pressed_keys.borrow_mut() = format!("{}{}", *pressed_keys.borrow(), counter.to_string());
let screen_text : String = pressed_keys.borrow().clone();
screen.borrow().text(screen_text);
true
}
})
.build(ctx)
);
counter = counter - 1;
}
}
self.name("MainView").child(
grid.build(ctx)
)
}
}
fn main() {
Application::new()
.window(|ctx| {
Window::create()
.title("OrbTk - Calculator")
.position((100.0, 100.0))
.size(420.0, 500.0)
.child(MainView::create().build(ctx))
.build(ctx)
})
.run();
}
Cargo.toml:
[package]
name = "orb_tk_calculator"
version = "0.1.0"
authors = ["lol"]
edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
orbtk = { git = "https://github.com/redox-os/orbtk.git", branch = "develop" }
strum = "0.18.0"
strum_macros = "0.18.0"
This is the error I get:
error[E0507]: cannot move out of dereference of `std::cell::Ref<'_, orbtk_widgets::text_box::TextBox>`
--> src/main.rs:89:33
|
89 | ... screen.borrow().text(screen_text);
| ^^^^^^^^^^^^^^^ move occurs because value has type `orbtk_widgets::text_box::TextBox`, which does not implement the `Copy` trait
I tried dereferencing, borrowing, cloning, and things like *screen.borrow_mut() = (*screen).borrow().text(screen_text);
but none worked. Anyone an idea of how to fix this?