69 lines
1.9 KiB
Rust
69 lines
1.9 KiB
Rust
use std::borrow::Cow;
|
|
use std::fs::File;
|
|
use std::io::Read;
|
|
use std::sync::{Arc, Mutex};
|
|
|
|
use druid::piet::{Brush, FixedGradient, ImageFormat, InterpolationMode, IntoBrush, PaintBrush};
|
|
use druid::widget::{Align, Flex, Image, Label, Painter, TextBox};
|
|
use druid::{
|
|
AppLauncher, Color, Data, Env, ImageBuf, Lens, LocalizedString, RenderContext, Widget,
|
|
WidgetExt, WindowDesc,
|
|
};
|
|
use image::io::Reader as ImageReader;
|
|
use image::GenericImageView;
|
|
|
|
struct Tab {
|
|
buf: ImageBuf,
|
|
}
|
|
|
|
impl<P: RenderContext> IntoBrush<P> for Tab {
|
|
fn make_brush<'a>(
|
|
&'a self,
|
|
piet: &mut P,
|
|
bbox: impl FnOnce() -> druid::Rect,
|
|
) -> Cow<'a, P::Brush> {
|
|
let ctx = self.to_owned().buf.to_image(piet);
|
|
piet.draw_image(&ctx, bbox(), InterpolationMode::Bilinear);
|
|
Cow::Owned(piet.solid_brush(Color::rgba(0.0, 0.0, 0.0, 1.0)))
|
|
}
|
|
}
|
|
|
|
struct KamintaState {
|
|
tabs: Vec<Tab>,
|
|
}
|
|
|
|
const TEST_IMAGE_PATH: &str = "emma_and_mujika.png";
|
|
|
|
fn main() {
|
|
let mut app_state = KamintaState { tabs: vec![] };
|
|
let img = ImageReader::open(TEST_IMAGE_PATH)
|
|
.unwrap()
|
|
.decode()
|
|
.unwrap();
|
|
app_state.tabs.push(Tab {
|
|
buf: ImageBuf::from_raw(
|
|
img.to_bytes(),
|
|
ImageFormat::Rgb,
|
|
img.dimensions().0.try_into().unwrap(),
|
|
img.dimensions().1.try_into().unwrap(),
|
|
),
|
|
});
|
|
println!["HERE ===================="];
|
|
|
|
let main_window = WindowDesc::new(build_root_widget)
|
|
.title("Kaminta")
|
|
.window_size((800.0, 600.0));
|
|
|
|
AppLauncher::with_window(main_window)
|
|
.launch(Arc::new(Mutex::new(app_state)))
|
|
.expect("failed to launch");
|
|
}
|
|
|
|
fn build_root_widget() -> impl Widget<Arc<Mutex<KamintaState>>> {
|
|
let img = Painter::new(|ctx, data: &Arc<Mutex<KamintaState>>, env| {
|
|
let bounds = ctx.size().to_rect();
|
|
ctx.fill(bounds, data.lock().unwrap().tabs.get(0).unwrap());
|
|
});
|
|
img
|
|
}
|