From ae7a195ada03f78827cd4edc00d4b69c5eabb2ce Mon Sep 17 00:00:00 2001 From: "Artem V. Ageev" Date: Sat, 20 Jul 2024 15:14:28 +0300 Subject: [PATCH] add base64's example --- demos/Cargo.toml | 1 + demos/base64/Cargo.toml | 10 +++ demos/base64/README.md | 5 ++ demos/base64/src/main.rs | 108 ++++++++++++++++++++++++++++ demos/base64/src/model/mod.rs | 29 ++++++++ demos/calculator/src/main.rs | 1 + demos/dialect/src/main.rs | 1 + demos/sudokusolver/src/main.rs | 39 +++++----- demos/sudokusolver/src/model/mod.rs | 2 +- 9 files changed, 175 insertions(+), 21 deletions(-) create mode 100644 demos/base64/Cargo.toml create mode 100644 demos/base64/README.md create mode 100644 demos/base64/src/main.rs create mode 100644 demos/base64/src/model/mod.rs diff --git a/demos/Cargo.toml b/demos/Cargo.toml index 7c9f01b..d4997be 100644 --- a/demos/Cargo.toml +++ b/demos/Cargo.toml @@ -2,6 +2,7 @@ resolver = "2" members = [ + "base64", "cairo", "cairo_shadow_button", "calculator", diff --git a/demos/base64/Cargo.toml b/demos/base64/Cargo.toml new file mode 100644 index 0000000..f7ae268 --- /dev/null +++ b/demos/base64/Cargo.toml @@ -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" diff --git a/demos/base64/README.md b/demos/base64/README.md new file mode 100644 index 0000000..0009e0e --- /dev/null +++ b/demos/base64/README.md @@ -0,0 +1,5 @@ +# Cairo demo + +Use Cairo for custom drawing. + +![img](https://github.com/fltk-rs/demos/blob/master/cairo/assets/scrot.png) diff --git a/demos/base64/src/main.rs b/demos/base64/src/main.rs new file mode 100644 index 0000000..2133131 --- /dev/null +++ b/demos/base64/src/main.rs @@ -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 +} diff --git a/demos/base64/src/model/mod.rs b/demos/base64/src/model/mod.rs new file mode 100644 index 0000000..bb708cf --- /dev/null +++ b/demos/base64/src/model/mod.rs @@ -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), + } + } +} diff --git a/demos/calculator/src/main.rs b/demos/calculator/src/main.rs index 90097b6..283cdcb 100644 --- a/demos/calculator/src/main.rs +++ b/demos/calculator/src/main.rs @@ -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', diff --git a/demos/dialect/src/main.rs b/demos/dialect/src/main.rs index 7680798..bcca11e 100644 --- a/demos/dialect/src/main.rs +++ b/demos/dialect/src/main.rs @@ -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', diff --git a/demos/sudokusolver/src/main.rs b/demos/sudokusolver/src/main.rs index d7e4fa8..ec60f51 100644 --- a/demos/sudokusolver/src/main.rs +++ b/demos/sudokusolver/src/main.rs @@ -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); @@ -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 } diff --git a/demos/sudokusolver/src/model/mod.rs b/demos/sudokusolver/src/model/mod.rs index d170371..af08c94 100644 --- a/demos/sudokusolver/src/model/mod.rs +++ b/demos/sudokusolver/src/model/mod.rs @@ -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; } }