Grid於Rust的Gtk4 binding

出自Tan Kian-ting的維基
於 2023年7月8日 (六) 23:23 由 Tankianting討論 | 貢獻 所做的修訂
跳至導覽 跳至搜尋

按怎佇Gtk4 ê Rust binding 使用Grid(方格仔)tàu元件

如何於Gtk4的Rust binding使用grid(網格)嵌入元件

main.rs

// main.rs
// author
// - gtks-rs' co-authors, list: https://github.com/gtk-rs/gtk4-rs/graphs/contributors
// - Tan, Kian-ting
// License: MIT License (https://opensource.org/license/mit/)
fn main() -> glib::ExitCode{
    let application = Application::builder()
        .application_id("info.kianting.sns.myapp") // app ê id
        .build();

    application.connect_activate(build_ui); // 連結起做ui ê
    application.run() // 紡app
}


// 起做ui
fn build_ui(application: &gtk4::Application) {
    // Create a new window, set its title and default size
    let window = gtk4::ApplicationWindow::new(application);
    window.set_title(Some("Grid Packing"));
    window.set_default_size(200, 120);

    // Here we construct the grid that is going contain our buttons.
    let grid = Grid::builder()
        .margin_start(6) // 倒爿ê邊仔留空白ê闊度
        .margin_end(6) // 正爿ê邊仔留空白ê闊度
        .margin_top(6)// 頂懸ê邊仔留空白ê懸度
        .margin_bottom(6) // 下底ê邊仔留空白ê懸度
        .halign(Align::Center)//水平排列
        .valign(Align::Center)//垂直排列
        .row_spacing(6)// 逐列ê縫留空白ê懸度
        .column_spacing(6)// 逐欄ê縫留空白ê闊度
        .build();

    // Add the grid in the window
    window.set_child(Some(&grid));

    // Create the first button and put it into the grid at (0, 0)
    let button_1 = Button::with_label("Button 1");
    button_1.connect_clicked(move |_| println!("Hello World"));

    // column : 第x (=0,1,...)欄
    // row: 第y(=0,1,...)列
    // width : colspan
    // height : rowspan
    grid.attach(&button_1, 0, 0, 1, 1);

    // Create the second button and put it into the grid at (1, 0)
    let button_2 = Button::with_label("Button 2");
    button_2.connect_clicked(move |_| println!("Hello World"));

    grid.attach(&button_2, 1, 0, 1, 1);

    let button_2 = Button::with_label("Button 2");
    button_2.connect_clicked(move |_| println!("Hello World"));

    grid.attach(&button_2, 2, 0, 1, 1);

    // Create the quit button and put it into the grid at (0, 1)
    let quit_button = Button::with_label("Quit");
    quit_button.connect_clicked(glib::clone!(@weak window => move |_| window.destroy()));

    grid.attach(&quit_button, 0, 1, 2, 1);

    window.present();
}