init basic image viewer
This commit is contained in:
commit
e3bb0bf327
9 changed files with 5222 additions and 0 deletions
89
src/main.rs
Normal file
89
src/main.rs
Normal file
|
|
@ -0,0 +1,89 @@
|
|||
use std::env;
|
||||
|
||||
use iced::{
|
||||
Color, Element, Renderer, Subscription, Task, Theme, color, event,
|
||||
keyboard::{self, Key, key},
|
||||
theme, widget,
|
||||
};
|
||||
use image::{EncodableLayout, ImageReader, RgbaImage};
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
enum Message {
|
||||
Event(iced::Event),
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
struct State {
|
||||
image: RgbaImage,
|
||||
image_handle: widget::image::Handle,
|
||||
}
|
||||
impl State {
|
||||
fn new() -> Self {
|
||||
let image = if let Some(path) = env::args().nth(1) {
|
||||
ImageReader::open(path)
|
||||
.unwrap()
|
||||
.decode()
|
||||
.unwrap()
|
||||
.into_rgba8()
|
||||
} else {
|
||||
RgbaImage::new(100, 100)
|
||||
};
|
||||
|
||||
State {
|
||||
image_handle: widget::image::Handle::from_rgba(image.width(), image.height(), unsafe {
|
||||
std::mem::transmute::<_, &'static [u8]>(image.as_bytes())
|
||||
}),
|
||||
image,
|
||||
}
|
||||
}
|
||||
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
||||
widget::image(&self.image_handle).into()
|
||||
}
|
||||
fn update(&mut self, message: Message) -> Task<Message> {
|
||||
match message {
|
||||
Message::Event(iced::Event::Keyboard(keyboard::Event::KeyPressed {
|
||||
key,
|
||||
modifiers,
|
||||
..
|
||||
})) => match key.as_ref() {
|
||||
// input field cycling
|
||||
Key::Named(key::Named::Tab) => {
|
||||
if modifiers.shift() {
|
||||
return widget::operation::focus_previous();
|
||||
} else {
|
||||
return widget::operation::focus_next();
|
||||
}
|
||||
}
|
||||
|
||||
// ignore unused keys
|
||||
_ => {}
|
||||
},
|
||||
|
||||
// ignore unused events
|
||||
Message::Event(_) => {}
|
||||
}
|
||||
|
||||
Task::none()
|
||||
}
|
||||
|
||||
fn subscription(&self) -> Subscription<Message> {
|
||||
event::listen().map(Message::Event)
|
||||
}
|
||||
}
|
||||
|
||||
fn main() -> Result<(), iced::Error> {
|
||||
iced::application(State::new, State::update, State::view)
|
||||
.subscription(State::subscription)
|
||||
.theme(Theme::custom(
|
||||
"custom",
|
||||
theme::Palette {
|
||||
background: color!(0x080808),
|
||||
text: Color::WHITE,
|
||||
primary: color!(0xff00ff),
|
||||
success: color!(0x00ff00),
|
||||
warning: color!(0x880000),
|
||||
danger: color!(0xff0000),
|
||||
},
|
||||
))
|
||||
.run()
|
||||
}
|
||||
Loading…
Reference in a new issue