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 android example - calculator #3936

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
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
7 changes: 7 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
19 changes: 19 additions & 0 deletions examples/calculator-android/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# Copyright © Cloris <luhaoyu@live.com>>

Check failure on line 1 in examples/calculator-android/Cargo.toml

View workflow job for this annotation

GitHub Actions / docs / docs

[xtask license_header] reported by reviewdog 🐶 Raw Output: examples/calculator-android/Cargo.toml:1:-# Copyright © Cloris <luhaoyu@live.com>> examples/calculator-android/Cargo.toml:1:+# Copyright © SixtyFPS GmbH <info@slint.dev>
# SPDX-License-Identifier: MIT

[package]
name = "calculator-rs"
version = "0.1.0"
edition = "2021"
authors = ["Cloris <luhaoyu@live.com>"]
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"] }
251 changes: 251 additions & 0 deletions examples/calculator-android/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,251 @@
#[cfg(target_os = "android")]
#[no_mangle]

Check failure on line 2 in examples/calculator-android/src/lib.rs

View workflow job for this annotation

GitHub Actions / docs / docs

[xtask license_header] reported by reviewdog 🐶 Raw Output: examples/calculator-android/src/lib.rs:1:+// Copyright © SixtyFPS GmbH <info@slint.dev> examples/calculator-android/src/lib.rs:2:+// SPDX-License-Identifier: MIT examples/calculator-android/src/lib.rs:3:+
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::<Calculator>().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::<i32>() {
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 <int> 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)}
}
}
}
}
}
Loading