Skip to content

Commit

Permalink
v0.1.1 Ready
Browse files Browse the repository at this point in the history
  • Loading branch information
Walker-00 committed Aug 22, 2023
1 parent 4ace590 commit 7f21108
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 35 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "rview"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

[profile.release]
Expand Down
13 changes: 5 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ This `Rview` is the a part of the project `Project SS`.
- [x] Support image picker
- [x] Support image from File
- [x] Support Image from Url
- [ ] Better Error Handling
- [x] Better Error Handling
- [ ] Optimize size
- [ ] Support Multiple Images Open
- [ ] Cli Features
Expand Down Expand Up @@ -58,13 +58,10 @@ cargo install --path .
```

## [Voices From Burma]
As burmeses living in dictatorship is fucking hard.
Currently, Burma is under the Military rule and as a burmese, living under the dictatorship is extremely chellenging.
<br>
We all burmeses are asking for Justice, but all we got are bullets.
But we stayed strong and are fighting against Military Junta's bullets and violence.
<br>
We all burmeses are want our democracy and our leaders back.
We demand Justice and Democracy.
<br>
We all burmeses are fighting back to the Military Junta and Injustice.
<br>
As burmeses we really want other countries to join us, but also we still fighting our own.

And we kindly want to ask for the International support and attention.
109 changes: 83 additions & 26 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,18 @@ use std::{fs::File, io::Read, path::Path};

use eframe::{
egui::{self, CentralPanel, ImageButton, RichText, TextStyle, Window},
epaint::Color32,
run_native, App, CreationContext, NativeOptions,
};
use egui_extras::RetainedImage;
use lazy_static::lazy_static;
use reqwest::header::CONTENT_TYPE;
use rfd::FileDialog;

lazy_static! {
static ref ERRWIN: bool = true;
}

fn main() {
let np = NativeOptions::default();
run_native("Rview", np, Box::new(|cc| Box::new(Rview::new(cc)))).unwrap();
Expand All @@ -17,8 +23,9 @@ fn main() {
struct Rview {
image_seted: bool,
image: String,
image_bytes: Vec<u8>,
image_retained: Option<RetainedImage>,
errwin: bool,
errmsg: String,
}

impl Rview {
Expand All @@ -33,6 +40,14 @@ impl Rview {
impl App for Rview {
fn update(&mut self, ctx: &egui::Context, frame: &mut eframe::Frame) {
CentralPanel::default().show(ctx, |ui| {
Window::new(monospace_text("Oops!"))
.open(&mut self.errwin)
.show(ctx, |ui| {
ui.heading(
monospace_text(format!("Error due To: {}", self.errmsg))
.color(Color32::RED),
);
});
if let Some(image_retained) = &self.image_retained {
if ui
.add(ImageButton::new(
Expand Down Expand Up @@ -77,29 +92,64 @@ impl App for Rview {
if ui.button(monospace_text("Open Image")).clicked() {
let mut bytes = vec![];
if url::Url::parse(&self.image).is_ok() {
bytes = image_req(&self.image).unwrap();
match image_req(&self.image) {
Ok(bytess) => bytes = bytess,
Err(e) => {
self.errwin = true;
self.errmsg = e.to_string();
}
}
} else if Path::new(&self.image).exists() {
let mut file = File::open(&self.image).unwrap();
file.read_to_end(&mut bytes).unwrap();
match File::open(&self.image) {
Ok(mut file) => {
if let Err(e) = file.read_to_end(&mut bytes) {
self.errwin = true;
self.errmsg = e.to_string()
}
}
Err(e) => {
self.errwin = true;
self.errmsg = e.to_string()
}
}
} else {
todo!()
self.errwin = true;
self.errmsg = format!(
"Your Image `{}`, is invalid Url Or File Path!",
self.image
);
}

if !bytes.is_empty() {
self.image_bytes = bytes.clone();
let utf8_bytes = String::from_utf8_lossy(&bytes);
if utf8_bytes.contains("<?xml") || utf8_bytes.contains("<svg") {
self.image_retained = Some(
RetainedImage::from_svg_bytes(&self.image, &bytes)
.unwrap(),
);
match RetainedImage::from_svg_bytes(&self.image, &bytes) {
Ok(image_retained) => {
self.image_retained = Some(image_retained);
}
Err(e) => {
self.errwin = true;
self.errmsg = e.to_string();
}
}
} else {
self.image_retained = Some(
RetainedImage::from_image_bytes(&self.image, &bytes)
.unwrap(),
);
match RetainedImage::from_image_bytes(&self.image, &bytes) {
Ok(image_retained) => {
self.image_retained = Some(image_retained);
}
Err(e) => {
self.errwin = true;
self.errmsg = e.to_string();
}
}
}
self.image_seted = false;
} else {
self.errwin = true;
self.errmsg = format!(
"Reading bytes From Your Image `{}` is Error, Maybe image format is not supported?",
self.image
);
}
}
})
Expand All @@ -109,18 +159,25 @@ impl App for Rview {
}
}

fn image_req(url: &str) -> Result<Vec<u8>, ()> {
let resp = reqwest::blocking::get(url).unwrap();

if let Some(content_type) = resp.headers().get(CONTENT_TYPE) {
if content_type.to_str().unwrap().starts_with("image") {
let bytes = resp.bytes().unwrap().to_vec();
Ok(bytes)
} else {
Err(())
}
} else {
Err(())
fn image_req(url: &str) -> Result<Vec<u8>, String> {
match reqwest::blocking::get(url) {
Ok(resp) => match resp.headers().get(CONTENT_TYPE) {
Some(content_type) => match content_type.to_str() {
Ok(ctstr) => {
if ctstr.starts_with("image") {
match resp.bytes() {
Ok(bytes) => Ok(bytes.to_vec()),
Err(e) => Err(e.to_string()),
}
} else {
Err("Url is Not Image's Url??".to_string())
}
}
Err(e) => Err(e.to_string()),
},
None => Err("Url Is Not Image's Url??".to_string()),
},
Err(e) => Err(e.to_string()),
}
}

Expand Down

0 comments on commit 7f21108

Please sign in to comment.