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

QRCode widget #622

Merged
merged 2 commits into from
Nov 20, 2020
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
9 changes: 7 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,17 @@ image = ["iced_wgpu/image"]
svg = ["iced_wgpu/svg"]
# Enables the `Canvas` widget
canvas = ["iced_wgpu/canvas"]
# Enables using system fonts.
# Enables the `QRCode` widget
qr_code = ["iced_wgpu/qr_code"]
# Enables using system fonts
default_system_font = ["iced_wgpu/default_system_font"]
# Enables the `iced_glow` renderer. Overrides `iced_wgpu`
glow = ["iced_glow", "iced_glutin"]
# Enables the `Canvas` widget for `iced_glow`
glow_canvas = ["iced_glow/canvas"]
# Enables using system fonts for `iced_glow`.
# Enables the `QRCode` widget for `iced_glow`
glow_qr_code = ["iced_glow/qr_code"]
# Enables using system fonts for `iced_glow`
glow_default_system_font = ["iced_glow/default_system_font"]
# Enables a debug view in native platforms (press F12)
debug = ["iced_winit/debug"]
Expand Down Expand Up @@ -67,6 +71,7 @@ members = [
"examples/pick_list",
"examples/pokedex",
"examples/progress_bar",
"examples/qr_code",
"examples/scrollable",
"examples/solar_system",
"examples/stopwatch",
Expand Down
9 changes: 9 additions & 0 deletions examples/qr_code/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "qr_code"
version = "0.1.0"
authors = ["Héctor Ramón Jiménez <hector0193@gmail.com>"]
edition = "2018"
publish = false

[dependencies]
iced = { path = "../..", features = ["qr_code"] }
18 changes: 18 additions & 0 deletions examples/qr_code/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
## QR Code Generator

A basic QR code generator that showcases the `QRCode` widget.

The __[`main`]__ file contains all the code of the example.

<div align="center">
<a href="https://gfycat.com/heavyexhaustedaracari">
<img src="https://thumbs.gfycat.com/HeavyExhaustedAracari-size_restricted.gif">
</a>
</div>

You can run it with `cargo run`:
```
cargo run --package qr_code
```

[`main`]: src/main.rs
81 changes: 81 additions & 0 deletions examples/qr_code/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use iced::qr_code::{self, QRCode};
use iced::text_input::{self, TextInput};
use iced::{
Align, Column, Container, Element, Length, Sandbox, Settings, Text,
};

pub fn main() -> iced::Result {
QRGenerator::run(Settings::default())
}

#[derive(Default)]
struct QRGenerator {
data: String,
input: text_input::State,
qr_code: Option<qr_code::State>,
}

#[derive(Debug, Clone)]
enum Message {
DataChanged(String),
}

impl Sandbox for QRGenerator {
type Message = Message;

fn new() -> Self {
QRGenerator {
qr_code: qr_code::State::new("").ok(),
..Self::default()
}
}

fn title(&self) -> String {
String::from("QR Code Generator - Iced")
}

fn update(&mut self, message: Message) {
match message {
Message::DataChanged(mut data) => {
data.truncate(100);

self.qr_code = qr_code::State::new(&data).ok();
self.data = data;
}
}
}

fn view(&mut self) -> Element<Message> {
let title = Text::new("QR Code Generator")
.size(70)
.color([0.5, 0.5, 0.5]);

let input = TextInput::new(
&mut self.input,
"Type the data of your QR code here...",
&self.data,
Message::DataChanged,
)
.size(30)
.padding(15);

let mut content = Column::new()
.width(Length::Units(700))
.spacing(20)
.align_items(Align::Center)
.push(title)
.push(input);

if let Some(qr_code) = self.qr_code.as_mut() {
content = content.push(QRCode::new(qr_code).cell_size(10));
}

Container::new(content)
.width(Length::Fill)
.height(Length::Fill)
.padding(20)
.center_x()
.center_y()
.into()
}
}
1 change: 1 addition & 0 deletions glow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ repository = "https://github.com/hecrj/iced"

[features]
canvas = ["iced_graphics/canvas"]
qr_code = ["iced_graphics/qr_code"]
default_system_font = ["iced_graphics/font-source"]
# Not supported yet!
image = []
Expand Down
8 changes: 8 additions & 0 deletions glow/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ pub mod canvas;
#[doc(no_inline)]
pub use canvas::Canvas;

#[cfg(feature = "qr_code")]
#[cfg_attr(docsrs, doc(cfg(feature = "qr_code")))]
pub mod qr_code;

#[cfg(feature = "qr_code")]
#[doc(no_inline)]
pub use qr_code::QRCode;

pub use iced_native::{Image, Space};

/// A container that distributes its contents vertically.
Expand Down
2 changes: 2 additions & 0 deletions glow/src/widget/qr_code.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
//! Encode and display information in a QR code.
pub use iced_graphics::qr_code::*;
5 changes: 5 additions & 0 deletions graphics/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2018"

[features]
canvas = ["lyon"]
qr_code = ["qrcode", "canvas"]
font-source = ["font-kit"]
font-fallback = []
font-icons = []
Expand All @@ -32,6 +33,10 @@ path = "../style"
version = "0.16"
optional = true

[dependencies.qrcode]
version = "0.12"
optional = true

[dependencies.font-kit]
version = "0.8"
optional = true
Expand Down
8 changes: 8 additions & 0 deletions graphics/src/widget.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,11 @@ pub mod canvas;
#[cfg(feature = "canvas")]
#[doc(no_inline)]
pub use canvas::Canvas;

#[cfg(feature = "qr_code")]
#[cfg_attr(docsrs, doc(cfg(feature = "qr_code")))]
pub mod qr_code;

#[cfg(feature = "qr_code")]
#[doc(no_inline)]
pub use qr_code::QRCode;
Loading