Skip to content

Commit

Permalink
Update QR code example with theme selector
Browse files Browse the repository at this point in the history
  • Loading branch information
clarkmoody committed Feb 2, 2024
1 parent 7458053 commit cc9a5fc
Showing 1 changed file with 54 additions and 6 deletions.
60 changes: 54 additions & 6 deletions examples/qr_code/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use iced::color;
use iced::theme::{Palette, Theme};
use iced::widget::qr_code::{self, QRCode};
use iced::widget::{column, container, text, text_input};
use iced::{Alignment, Color, Element, Length, Sandbox, Settings};
use iced::widget::{column, container, radio, row, text, text_input};
use iced::{Alignment, Element, Length, Sandbox, Settings};

pub fn main() -> iced::Result {
QRGenerator::run(Settings::default())
Expand All @@ -10,11 +12,20 @@ pub fn main() -> iced::Result {
struct QRGenerator {
data: String,
qr_code: Option<qr_code::State>,
theme: Theme,
}

#[derive(Debug, PartialEq, Eq, Clone, Copy)]
enum ThemeType {
Light,
Dark,
Custom,
}

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

impl Sandbox for QRGenerator {
Expand All @@ -41,21 +52,54 @@ impl Sandbox for QRGenerator {

self.data = data;
}
Message::ThemeChanged(theme) => {
self.theme = match theme {
ThemeType::Light => Theme::Light,
ThemeType::Dark => Theme::Dark,
ThemeType::Custom => Theme::custom(
String::from("Custom"),
Palette {
background: color!(0x1d1a05),
text: color!(0xfffbbd),
primary: color!(0xca3c25),
success: color!(0x000000),
danger: color!(0x000000),
},
),
};
if self.qr_code.is_some() {
self.qr_code = qr_code::State::new(&self.data).ok();
}
}
}
}

fn view(&self) -> Element<Message> {
let title = text("QR Code Generator")
.size(70)
.style(Color::from([0.5, 0.5, 0.5]));
let title = text("QR Code Generator").size(70);

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

let mut content = column![title, input]
let choose_theme =
[ThemeType::Light, ThemeType::Dark, ThemeType::Custom]
.iter()
.fold(row![text("Theme:")].spacing(10), |row, theme| {
row.push(radio(
format!("{theme:?}"),
*theme,
Some(match self.theme {
Theme::Light => ThemeType::Light,
Theme::Dark => ThemeType::Dark,
Theme::Custom { .. } => ThemeType::Custom,
}),
Message::ThemeChanged,
))
});

let mut content = column![title, input, choose_theme]
.width(700)
.spacing(20)
.align_items(Alignment::Center);
Expand All @@ -72,4 +116,8 @@ impl Sandbox for QRGenerator {
.center_y()
.into()
}

fn theme(&self) -> Theme {
self.theme.clone()
}
}

0 comments on commit cc9a5fc

Please sign in to comment.