chimper/frontend/
gui.rs

1extern crate conrod_core;
2use conrod_core::{widget, Colorable, Positionable, Sizeable, Borderable, Widget, color};
3extern crate imagepipe;
4
5use crate::frontend::main::Chimper;
6use crate::frontend::main::DisplayableState;
7use crate::frontend::ops;
8
9pub fn draw_gui(chimper: &mut Chimper, ui: &mut conrod_core::Ui) -> bool {
10  let ui = &mut ui.set_widgets();
11
12  let sidewidth = chimper.sidewidth * ((chimper.use_sidepane as u8) as f64);
13  let dragwidth = chimper.dragwidth * ((chimper.use_sidepane as u8) as f64);
14  {
15    let ids = &mut chimper.ids;
16
17    // Adjust settings for fullscreen images
18    let (img_bgcolor, img_padding) = if !chimper.use_sidepane {
19      (color::BLACK, 0.0)
20    } else {
21      (color::CHARCOAL, chimper.imagepadding)
22    };
23
24    // Construct our main `Canvas` tree.
25    widget::Canvas::new().flow_right(&[
26      (ids.imgcanvas, widget::Canvas::new().color(img_bgcolor).border(0.0)),
27      (ids.dragcanvas, widget::Canvas::new().length(dragwidth).color(color::BLACK).border(0.0)),
28      (ids.setcanvas, widget::Canvas::new().length(sidewidth).border(0.0).flow_down(&[
29        (ids.settop, widget::Canvas::new().color(color::GREY).length(100.0).border(0.0)),
30        (ids.setcont, widget::Canvas::new().color(color::GREY).border(0.0)),
31      ])),
32    ]).border(0.0).set(ids.background, ui);
33
34    if let DisplayableState::Present(ref image) = chimper.image {
35      let scale = (image.width as f64)/(image.height as f64);
36      let mut width = (ui.w_of(ids.imgcanvas).unwrap() - img_padding).min(image.width as f64);
37      let mut height = (ui.h_of(ids.imgcanvas).unwrap() - img_padding).min(image.height as f64);
38      if width/height > scale {
39        width = height * scale;
40      } else {
41        height = width / scale;
42      }
43      widget::Image::new(image.id)
44        .w_h(width, height)
45        .middle_of(ids.imgcanvas)
46        .set(ids.raw_image, ui);
47    }
48
49    if sidewidth > 0.0 {
50      for _event in widget::Button::image(chimper.logoid)
51        .w_h(78.0, 88.0)
52        .top_right_with_margin_on(ids.settop, 6.0)
53        .set(ids.chimper, ui)
54      {
55        chimper.sideopt = !chimper.sideopt;
56      }
57
58      if chimper.sideopt {
59        let directory = chimper.directory.as_path();
60        for event in widget::FileNavigator::all(&directory)
61          .color(conrod_core::color::LIGHT_BLUE)
62          .font_size(16)
63          .kid_area_wh_of(ids.setcont)
64          .middle_of(ids.setcont)
65          //.show_hidden_files(true)  // Use this to show hidden files
66          .set(ids.filenav, ui)
67        {
68          match event {
69            conrod_core::widget::file_navigator::Event::ChangeSelection(pbuf) => {
70              if pbuf.len() > 0 {
71                let path = pbuf[0].as_path();
72                if path.is_file() {
73                  log::info!("Loading file {:?}", path);
74                  chimper.file = Some(path.to_str().unwrap().to_string());
75                }
76              }
77            },
78            _ => {},
79          }
80        }
81      }
82    }
83  }
84
85  if sidewidth > 0.0 && !chimper.sideopt {
86    ops::draw_gui(chimper, ui);
87  }
88
89  false
90}