简体   繁体   中英

How to raise a window using fltk-rs

I am trying to "raise" (draw "on top" of other overlapping windows and possibly make "active") a specific window in response to an event. The official FLTK documentation seems to say that the Fl_Window::show() method is the way to do this. However, there is no corresponding WindowExt::show() (or any similar) method in fltk-rs .

There is a WidgetExt::show() method, but it does not raise a window when invoked. There is also a similarly promising but similarly disappointing DoubleWindow::platform_show() method.

What I have gotten to work is to call .hide() and then immediately call .show() on the window in question. This produces the desired effect of raising the window in question to the top of the pile. However, this

  • Seems hacky, right? Like going around one's rear to reach one's elbow.
  • Really does hide then show the window. On my Debian system running i3, this is almost unnoticeable (the flicker might just be standard window-redrawing), but under Windows this is terrible, because .hide() ing the window triggers a short (but oh-so-noticeable) fade-out animation (and .show() ing it a corresponding fade-in animation, plus there might even be a little shrink/grow action happening, too), which is bad user experience and looks like something is malfunctioning.

Here's an example:


use fltk::{
    prelude::*,
    app::App,
    enums::Color,
    frame::Frame,
    window::DoubleWindow,
    button::Button,
};

fn main() {
    let a = App::default();
    
    let mut sub_win = DoubleWindow::default()
        .with_size(128, 128)
        .with_pos(64, 64);
    sub_win.set_border(false);
    sub_win.set_color(Color::Magenta); // So you can see it clearly.
    let _ = Frame::default().with_label("Sub Window")
        .with_size(128, 128)
        .with_pos(0, 0);
    sub_win.end();
    sub_win.show();
    
    let mut main_win = DoubleWindow::default().with_label("Main Window")
        .with_size(256, 128)
        .with_pos(0, 0);
    let mut b0 = Button::default().with_label("won't work")
        .with_size(96, 64)
        .with_pos(20, 32);
    let mut b1 = Button::default().with_label("is hacky")
        .with_size(96, 64)
        .with_pos(130, 32);
    main_win.end();
    main_win.show();
    
    b0.set_callback({
        let mut sub_win = sub_win.clone();
        move |_| {
            sub_win.show();          // The FLTK docs suggest this should work.
            sub_win.platform_show(); // This also disappoints.
        }
    });

    b1.set_callback(move |_| {
        sub_win.hide();          // This combination is what
        sub_win.show();          // actually works.
    });
    
    a.run().unwrap();
}

If anybody knows the magic incantation I seek, I'd appreciate it.

On the C++ end, Fl_Widget::show() is a virtual function that should raise the window, if the variable points to a top-level window (a window with no parent). On the Rust side, I see these lines:

extern "C" {
    pub fn Fl_Window_show(arg1: *mut Fl_Window);
}

which seem to indicate that there is a direct interface for calling Fl_Window::show(), and the same for Fl_Double_Window.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM