diff --git a/examples/README.md b/examples/README.md index 7aa612dba83..a48ab045851 100644 --- a/examples/README.md +++ b/examples/README.md @@ -162,6 +162,13 @@ Our implementations of the ["7GUIs"](https://7guis.github.io/7guis/) Tasks. ![Composition of 7GUIs Screenshots](https://user-images.githubusercontent.com/22800467/169002497-5b90e63b-5717-4290-8ac7-c618d9e2a4f1.png "7GUIs") +### [`calculator-android`](./calculator-android/) + +A Rust example that shows how to create a Android App in Slint. + +![Screenshots](https://raw.githubusercontent.com/1595901624/AndroidCalcualator-Slint/main/demo.jpg) + + ### External examples * [Cargo UI](https://github.com/slint-ui/cargo-ui): A rust application that makes use of threads in the background. diff --git a/examples/calculator-android/Cargo.toml b/examples/calculator-android/Cargo.toml new file mode 100644 index 00000000000..9d1f36f9904 --- /dev/null +++ b/examples/calculator-android/Cargo.toml @@ -0,0 +1,19 @@ +# Copyright © Cloris > +# SPDX-License-Identifier: MIT + +[package] +name = "calculator-rs" +version = "0.1.0" +edition = "2021" +authors = ["Cloris "] +license = "MIT" +publish = false +homepage = "https://github.com/slint-ui/slint" +repository = "https://github.com/slint-ui/slint" + +[lib] +crate-type = ["cdylib"] + +[dependencies] +slint = "1.3.0" +i-slint-backend-android-activity = { version = "=1.3.0", features = ["native-activity"] } \ No newline at end of file diff --git a/examples/calculator-android/src/lib.rs b/examples/calculator-android/src/lib.rs new file mode 100644 index 00000000000..eb7027e52a3 --- /dev/null +++ b/examples/calculator-android/src/lib.rs @@ -0,0 +1,251 @@ +#[cfg(target_os = "android")] +#[no_mangle] +fn android_main(app: i_slint_backend_android_activity::AndroidApp) { + use std::cell::RefCell; + use std::rc::Rc; + + slint::platform::set_platform(Box::new( + i_slint_backend_android_activity::AndroidPlatform::new(app), + )) + .unwrap(); + + let app = MainWindow::new().unwrap(); + let weak = app.as_weak(); + + // press first number + let mut num1 = 0; + + // press second number + let mut num2 = 0; + + // operator + let operator = Rc::new(RefCell::new(String::new())); + + app.global::().on_click(move |text| { + let app = weak.unwrap(); + if text == "AC" { + app.set_result(0); + num1 = 0; + num2 = 0; + operator.borrow_mut().clear(); + return; + } + + if text == "+" || text == "-" || text == "x" || text == "÷" { + num1 = app.get_result(); + operator.borrow_mut().clear(); + operator.borrow_mut().push_str(text.as_str()); + app.set_result(0); + return; + } + + if text == "=" { + num2 = app.get_result(); + match operator.borrow().as_str() { + "+" => { + let (result, overflowed) = num1.overflowing_add(num2); + if overflowed { + app.set_result(0); + } else { + app.set_result(result); + } + } + "-" => { + let (result, overflowed) = num1.overflowing_sub(num2); + if overflowed { + app.set_result(0); + } else { + app.set_result(result); + } + } + "x" => { + let (result, overflowed) = num1.overflowing_mul(num2); + if overflowed { + app.set_result(0); + } else { + app.set_result(result); + } + } + "÷" => { + if num2 == 0 { + app.set_result(0); + } else { + app.set_result(num1 / num2); + } + } + _ => {} + } + operator.borrow_mut().clear(); + operator.borrow_mut().push_str("="); + return; + } + + if let Ok(value) = text.parse::() { + let current; + if operator.borrow().as_str() == "=" { + app.set_result(0); + num1 = 0; + current = 0; + operator.borrow_mut().clear(); + } else { + current = app.get_result(); + } + let (result, overflowed) = current.overflowing_mul(10); + if overflowed { + app.set_result(0); + return; + } + let (result, overflowed) = result.overflowing_add(value); + if overflowed { + app.set_result(0); + return; + } + app.set_result(result); + return; + } + }); + app.run().unwrap(); +} + +// UI +slint::slint! { + import { GridBox , Button} from "std-widgets.slint"; + + export global Calculator { + // click event + callback click(string); + } + + export component MainWindow inherits Window { + // Result + in-out property result: 0; + + GridLayout { + padding-top: 100px; + padding-left: 20px; + padding-right: 20px; + spacing: 10px; + + Row { + Text { + colspan: 3; + text: result; + height: 40px; + font-size: 20px; + } + } + + Row { + Button { + colspan: 3; + height: 80px; + text: "AC"; + clicked => { Calculator.click(self.text)} + } + Button { + col: 3; + colspan: 1; + height: 80px; + text: "÷"; + clicked => { Calculator.click(self.text)} + } + } + + Row { + Button { + colspan: 1; + height: 80px; + text: "7"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "8"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "9"; + clicked => { Calculator.click(self.text)} + } + Button { + rowspan: 2; + height: 80px; + text: "x"; + clicked => { Calculator.click(self.text)} + } + } + + Row { + Button { + colspan: 1; + height: 80px; + text: "4"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "5"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "6"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "-"; + clicked => { Calculator.click(self.text)} + } + } + + Row { + Button { + colspan: 1; + height: 80px; + text: "1"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "2"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "3"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 1; + height: 80px; + text: "+"; + clicked => { Calculator.click(self.text)} + } + } + + Row { + Button { + colspan: 1; + height: 80px; + text: "0"; + clicked => { Calculator.click(self.text)} + } + Button { + colspan: 3; + height: 80px; + text: "="; + clicked => { Calculator.click(self.text)} + } + } + } + } +}