Skip to content

Commit

Permalink
add base64's example
Browse files Browse the repository at this point in the history
  • Loading branch information
Artem V. Ageev committed Jul 20, 2024
1 parent cbdb353 commit ae7a195
Show file tree
Hide file tree
Showing 9 changed files with 175 additions and 21 deletions.
1 change: 1 addition & 0 deletions demos/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

resolver = "2"
members = [
"base64",
"cairo",
"cairo_shadow_button",
"calculator",
Expand Down
10 changes: 10 additions & 0 deletions demos/base64/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "b64converter"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
flemish = { path = "../../" }
base64 = "0.22"
5 changes: 5 additions & 0 deletions demos/base64/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Cairo demo

Use Cairo for custom drawing.

![img](https://github.com/fltk-rs/demos/blob/master/cairo/assets/scrot.png)
108 changes: 108 additions & 0 deletions demos/base64/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
mod model;

use {
flemish::{
app,
button::Button,
color_themes,
enums::{Color, Font},
frame::Frame,
group::Flex,
prelude::*,
text::{TextBuffer, TextEditor, WrapMode},
OnEvent, Sandbox, Settings,
},
model::Model,
};

const PAD: i32 = 10;
const HEIGHT: i32 = PAD * 3;

#[derive(Clone)]
pub enum Message {
Encode,
Decode,
Source(String),
Target(String),
}

fn main() {
Model::new().run(Settings {
size: (640, 360),
ignore_esc_close: true,
resizable: false,
color_map: Some(color_themes::DARK_THEME),
scheme: Some(app::Scheme::Base),
..Default::default()
})
}

impl Sandbox for Model {
type Message = Message;

fn new() -> Self {
Self::default()
}

fn title(&self) -> String {
String::from("FlBase64")
}

fn view(&mut self) {
let mut page = Flex::default_fill().column();
{
page.fixed(&Frame::default(), HEIGHT);
let mut hero = Flex::default();
{
crate::texteditor("Normal text", &self.decode, self.font, self.size)
.on_event(move |text| Message::Source(text.buffer().unwrap().text()));
crate::texteditor("Base64 text", &self.encode, self.font, self.size)
.on_event(move |text| Message::Target(text.buffer().unwrap().text()));
}
hero.end();
hero.set_pad(PAD);
let mut footer = Flex::default();
{
crate::button("Decode", "@<-", &mut footer).on_event(move |_| Message::Decode);
Frame::default();
crate::button("Encode", "@->", &mut footer).on_event(move |_| Message::Encode);
}
footer.end();
page.fixed(&footer, HEIGHT);
}
page.end();
page.set_pad(PAD);
page.set_margin(PAD);
}

fn update(&mut self, message: Message) {
match message {
Message::Source(value) => self.decode = value,
Message::Target(value) => self.encode = value,
Message::Encode => self.encode(),
Message::Decode => self.decode(),
}
}
}

fn texteditor(tooltip: &str, value: &str, font: i32, size: i32) -> TextEditor {
let mut element = TextEditor::default().with_label(tooltip);
element.set_tooltip(tooltip);
element.set_linenumber_width(0);
element.set_buffer(TextBuffer::default());
element.wrap_mode(WrapMode::AtBounds, 0);
element.buffer().unwrap().set_text(value);
element.set_color(Color::from_hex(0x002b36));
element.set_text_color(Color::from_hex(0x93a1a1));
element.set_text_font(Font::by_index(font as usize));
element.set_text_size(size);
element
}

fn button(tooltip: &str, label: &str, flex: &mut Flex) -> Button {
let mut element = Button::default().with_label(label);
element.set_tooltip(tooltip);
element.set_label_size(HEIGHT / 2);
flex.fixed(&element, HEIGHT);
element
}
29 changes: 29 additions & 0 deletions demos/base64/src/model/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
use base64::{engine::general_purpose, Engine};

#[derive(Debug)]
pub struct Model {
pub decode: String,
pub encode: String,
pub font: i32,
pub size: i32,
}

impl Model {
pub fn default() -> Self {
Self {
decode: String::new(),
encode: String::new(),
font: 0,
size: 14,
}
}
pub fn encode(&mut self) {
self.encode = general_purpose::STANDARD.encode(&self.decode);
}
pub fn decode(&mut self) {
self.decode = match general_purpose::STANDARD.decode(&self.encode) {
Ok(decode) => String::from_utf8(decode).unwrap(),
Err(error) => format!("{}", error),
}
}
}
1 change: 1 addition & 0 deletions demos/calculator/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,7 @@ pub fn menu(theme: usize) -> MenuButton {
element.set_color(COLORS[theme][1]);
element.set_text_color(COLORS[theme][0]);
element
.clone()
.on_item_event(
"&Night mode\t",
Shortcut::Ctrl | 'n',
Expand Down
1 change: 1 addition & 0 deletions demos/dialect/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,7 @@ fn textdisplay(tooltip: &str, value: &str, font: i32, size: i32) {
fn menu(flex: &mut Flex) {
let element = MenuButton::default();
element
.clone()
.on_item_event(
"@#fileopen &Open...",
Shortcut::Ctrl | 'o',
Expand Down
39 changes: 19 additions & 20 deletions demos/sudokusolver/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,12 @@ impl Sandbox for Model {
hero.set_pad(0);
let mut footer = Flex::default();
{
Button::default().with_label("Solve").on_event(move |_| Message::Solve);
Button::default().with_label("Clear").on_event(move |_| Message::Clear);
Button::default()
.with_label("Solve")
.on_event(move |_| Message::Solve);
Button::default()
.with_label("Clear")
.on_event(move |_| Message::Clear);
}
footer.end();
footer.set_pad(PAD);
Expand All @@ -98,25 +102,20 @@ fn frame(row: usize, col: usize, value: i32) -> Frame {
};
element.set_frame(FrameType::DownBox);
element.set_label_size(18);
element.handle(move |_, event| {
match event {
Event::Push => match app::event_mouse_button() {
app::MouseButton::Right => {
let mut menu = MenuButton::default()
.with_type(MenuButtonType::Popup3);
menu.set_text_size(18);
menu.add_choice(" 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ");
menu
.clone()
.on_event(move |choice| {
Message::Click((row, col, choice.value() + 1))
}).popup();
true
}
_ => false,
},
element.handle(move |_, event| match event {
Event::Push => match app::event_mouse_button() {
app::MouseButton::Right => {
let mut menu = MenuButton::default().with_type(MenuButtonType::Popup3);
menu.set_text_size(18);
menu.add_choice(" 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 ");
menu.clone()
.on_event(move |choice| Message::Click((row, col, choice.value() + 1)))
.popup();
true
}
_ => false,
}
},
_ => false,
});
element
}
2 changes: 1 addition & 1 deletion demos/sudokusolver/src/model/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl Model {
items = [0; 9];
for i in 0..3 {
for j in 0..3 {
if self.grid[y + i][x + j] > 0 && self.grid[y + i][x + j] < 10 {
if self.grid[y + i][x + j] > 0 && self.grid[y + i][x + j] < 10 {
items[(self.grid[y + i][x + j] - 1) as usize] += 1;
}
}
Expand Down

0 comments on commit ae7a195

Please sign in to comment.