rust gtk-rs设置窗口的最大大小

to94eoyn  于 2023-01-17  发布在  其他
关注(0)|答案(2)|浏览(167)

是否可以在gtk-rs中设置窗口的最大大小?在阅读文档时,我可以找到很多选项来设置最小窗口大小和默认窗口大小,但我找不到任何选项来设置最大窗口大小。
看起来确实有一些窗口大小管理的选项,但是我在识别它们应该如何使用时遇到了一些麻烦。当查看小部件的测量特性时(我认为可能可以为所有小部件设置最大大小),有一个到GtkWidget的几何管理部分的链接,但是它是404的
将窗口设置为全屏确实会阻止它在多个屏幕上扩展,但它也会取消菜单栏,导致我不得不强制关闭应用程序才能让它离开屏幕。最大化类似于全屏,但保留了菜单栏。当使用这两个选项中的任何一个时,内容都不会按窗口大小换行。它只是离开屏幕,没有滚动条。
下面的代码示例演示了这个问题,它使用了一个很长的标签,让窗口在多个屏幕上断开,或者如果屏幕足够大,可以容纳标签,则窗口大小不合适。

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button, Orientation};
use std::sync::{Arc, Mutex};
const APP_ID: &str = "org.gtk_rs.HelloWorld3";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Press me!")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let label = gtk::Label::new(Some("this is a very long string that will change the size of the window to a size that is impossible to use..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."));

    let label_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .margin_top(12)
        .margin_bottom(12)
        .build();
    label_box.append(&label);

    label_box.set_visible(false);

    let button_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .margin_top(12)
        .margin_bottom(12)
        .build();
    button_box.append(&button);
    button_box.append(&label_box);

    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&button_box)
        .default_width(360)
        .default_height(720)
        
        .build();

    window.present();
    //window.fullscreen(); // uncomment this to see the fullscreen option.

    button.connect_clicked(move |button| {
        if label_box.get_visible() == false {
            label_box.set_visible(true);
        }
        else {
            // this makes the label invisible but does not revert screen size.
            label_box.set_visible(false);
        }
    });
}

如何限制gtk-rs窗口的大小,或者如何将内容 Package 到窗口大小?如果窗口本身不能限制,是否可以限制小部件的大小?
编辑:我用了一个标签来说明这个问题,因为这是一个简单的方法来说明这个问题。有一个方法来设置标签的最大宽度,但它只适用于标签。我想限制所有小部件的大小。

nkkqxpd9

nkkqxpd91#

我会接受**的建议,使用ScrolledWindow
下面是用ScrolledWindow扩展的代码。child可以是任何Widget

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button, Orientation};

const APP_ID: &str = "org.gtk_rs.HelloWorld3";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Press me!")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let label = gtk::Label::new(Some("this is a very long string that will change the size of the window to a size that is impossible to use..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."));

    let label_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .margin_top(12)
        .margin_bottom(12)
        .build();
    label_box.append(&label);

    label_box.set_visible(false);

    let button_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .margin_top(12)
        .margin_bottom(12)
        .build();
    button_box.append(&button);

    let scrollable_container = gtk::ScrolledWindow::builder()
        .child(&label_box)
        .build();
    button_box.append(&scrollable_container);

    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&button_box)
        .default_width(360)
        .default_height(720)
        
        .build();

    window.present();
    //window.fullscreen(); // uncomment this to see the fullscreen option.

    button.connect_clicked(move |_button| {
        if label_box.get_visible() == false {
            label_box.set_visible(true);
        }
        else {
            // this makes the label invisible but does not revert screen size.
            label_box.set_visible(false);
        }
    });
}
fnvucqvd

fnvucqvd2#

按如下方式使用gtk::ScrolledWindow

use gtk::prelude::*;
use gtk::{Application, ApplicationWindow, Button, Orientation};

const APP_ID: &str = "org.gtk_rs.HelloWorld3";

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_activate(build_ui);
    app.run();
}

fn build_ui(app: &Application) {
    let button = Button::builder()
        .label("Press me!")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let label = gtk::Label::new(Some("this is a very long string that will change the size of the window to a size that is impossible to use..................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................................."));

    // create scrolled window and add label
    let adjustment: Option<&gtk::Adjustment> = None;
    let scrolled_window = gtk::ScrolledWindow::new(adjustment, adjustment);
    scrolled_window.set_policy(gtk::PolicyType::Automatic, gtk::PolicyType::Never);
    scrolled_window.add(&label);

    let label_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .margin_top(12)
        .margin_bottom(12)
        .build();
    label_box.pack_end(&scrolled_window, true, true, 0);

    label_box.set_visible(false);

    let button_box = gtk::Box::builder()
        .orientation(Orientation::Vertical)
        .margin_top(12)
        .margin_bottom(12)
        .build();
    button_box.pack_end(&button, true, true, 0);
    button_box.pack_end(&label_box, true, true, 0);

    let window = ApplicationWindow::builder()
        .application(app)
        .title("My GTK App")
        .child(&button_box)
        .default_width(360)
        .default_height(720)
        .build();

    window.present();
    //window.fullscreen(); // uncomment this to see the fullscreen option.
    window.show_all();
    button.connect_clicked(move |_button| {
        if label_box.get_visible() == false {
            label_box.set_visible(true);
        } else {
            // this makes the label invisible but does not revert screen size.
            label_box.set_visible(false);
        }
    });
}

相关问题