Home > Software engineering >  FLTK in Rust: Resize image along with its frame
FLTK in Rust: Resize image along with its frame

Time:12-13

I'm trying to write a program that loads up an image, and as the user resizes the window, the image resizes as well, as if it had "height: 100%" in CSS terms, using FLTK in the Rust lang.

This is what I've written:

use fltk::{frame::Frame, window, app, prelude::*, image::JpegImage};

fn main() {
    let app = app::App::default().with_scheme(app::Scheme::Gtk);

    let mut main_win = window::Window::default()
        .with_size(800, 600)
        .center_screen()
        .with_label("PhotoTest");

    let mut img_frame = Frame::default().size_of_parent();
    let img = JpegImage::load("flower.jpg").unwrap();
    img_frame.set_image(Some(img));
    
    img_frame.resize_callback(|s,_,_,_,_| {
        s.image().unwrap().scale(s.w(), s.h(), true, true);
        s.redraw();
    });

    main_win.make_resizable(true);
    main_win.end();
    main_win.show();
    app.run().unwrap();
}

However, the image does not change. This also applies to other operations, like inactive(), it is as if the image is not changed at all. In fact, when using println!("Frame - {}, Image - {}", s.w(), s.image().unwrap().w()) inside the callback, the img_frame` gets a new size, while the size of the image stays the same, showing that the callback is getting its call, it is just not being able to apply it.

These operations work well until the image is moved into the frame, that's when they are ignored. Am I doing something very dumb? Please help.

CodePudding user response:

How about defining the callback function first before adding the image?

CodePudding user response:

1: to have a deactivated image, you must create that yourself, for example by copying your original image and then call desaturate, or whatever look you like. Finally, you assign that to the widget with ‚deimage()‘.

2: FLTK does not resize images automatically. You can override the ‚draw()‘ method of your widget and copy and resize the original image as neede, set it with ‚imang()‘, and call the original ‚draw()‘.

Note that resizing images is a relatively expensive operation in FLTK.

  • Related