0

I new to rust and Gtk4. I am trying to write a small logout program. I am using Rust and Gtk4. I want to be able to execute the lock screen and close the window. I am have trouble on closing the window. Here is main code and If someone to give me a example adding window close feature. `

fn main() {
    let app = Application::builder().application_id(APP_ID).build();
    app.connect_startup(|_| load_css());
    app.connect_activate(build_ui);
    app.set_accels_for_action("win.close", &["c"]);
    app.run();
}

fn load_css() {
    // Load the CSS file and add it to the provideer
    let provider = CssProvider::new();
    provider.load_from_data(include_bytes!("style.css"));

    // Add the provider to the default screen
    StyleContext::add_provider_for_display(
        &Display::default().expect("Could not connect to a display."),
        &provider,
        gtk::STYLE_PROVIDER_PRIORITY_APPLICATION,
    );
}

fn build_ui(app: &Application) {
    // Images for the Buttons
    let cancel_image = Image::builder()
        .pixel_size(150)
        .file("img/cancel.png")
        .build();

    let logout_image = Image::builder()
        .pixel_size(150)
        .file("img/logout.png")
        .build();

    let reboot_image = Image::builder()
        .pixel_size(150)
        .file("img/reboot.png")
        .build();

    let shutdown_image = Image::builder()
        .pixel_size(150)
        .file("img/shutdown.png")
        .build();

    let lockscreen_image = Image::builder()
        .pixel_size(150)
        .file("img/lock.png")
        .build();

    //Labels for Button
    let cancel_label = Label::builder().label("Cancel (C)").build();

    let logout_label = Label::builder().label("Log Out (L)").build();

    let reboot_label = Label::builder().label("Reboot (R)").build();

    let shutdown_label = Label::builder().label("Shut Down (S)").build();

    let lockscreen_label = Label::builder().label("Lock Screen (X)").build();

    // Buttons for Rs Signtou
    let cancel_button = Button::builder()
        .child(&cancel_image)
        .action_name("win.close")
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let logout_button = Button::builder()
        .child(&logout_image)
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let reboot_button = Button::builder()
        .child(&reboot_image)
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let shutdown_button = Button::builder()
        .child(&shutdown_image)
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let lockscreen_button = Button::builder()
        .child(&lockscreen_image)
        .margin_top(12)
        .margin_bottom(12)
        .margin_start(12)
        .margin_end(12)
        .build();

    let keydown = EventControllerKey::new();
    keydown.connect_key_pressed(|_, key, _, _| {
        match key {
            Key::l => {
                logout();
            }
            Key::r => {
                reboot();
            }
            Key::s => {
                shutdown();
            }
            Key::x => {
                //lockscreen();
            }
            _ => {}
        };
        Inhibit(false)
    });

    fn logout() {
        let get_user = Command::new("whoami")
            .stdout(Stdio::piped())
            .output()
            .expect("Failed to execute Command");

        //Mut the Command
        let mut get_user = String::from_utf8(get_user.stdout).unwrap();
        // Pop remove the new line
        get_user.pop();

        //Command to Log Out User.
        let log = Command::new("pkill")
            .arg("-u")
            .arg(get_user)
            .stdout(Stdio::piped())
            .output()
            .expect("Failed to process Command");
        assert!(log.status.success());
    }

    fn reboot() {
        let reboot_comp = Command::new("reboot")
            .output()
            .expect("Failed to execute Command");

        assert!(reboot_comp.status.success());
    }

    fn shutdown() {
        let shut_down = Command::new("poweroff")
            .output()
            .expect("Failed to execute Command");

        assert!(shut_down.status.success());
    }

    fn lockscreen() {
        let lock_screen = Command::new("betterlockscreen")
            .args(["-l", "dim"])
            .output()
            .expect("Failed to execute Command");

        assert!(lock_screen.status.success());
    }

    let cancel_box = Box::builder().orientation(Orientation::Vertical).build();
    cancel_box.append(&cancel_button);
    cancel_box.append(&cancel_label);

    let logout_box = Box::builder().orientation(Orientation::Vertical).build();
    logout_box.append(&logout_button);
    logout_box.append(&logout_label);

    let reboot_box = Box::builder().orientation(Orientation::Vertical).build();
    reboot_box.append(&reboot_button);
    reboot_box.append(&reboot_label);

    let shutdown_box = Box::builder().orientation(Orientation::Vertical).build();
    shutdown_box.append(&shutdown_button);
    shutdown_box.append(&shutdown_label);

    let lockscreen_box = Box::builder().orientation(Orientation::Vertical).build();
    lockscreen_box.append(&lockscreen_button);
    lockscreen_box.append(&lockscreen_label);

    let main_box = Box::builder()
        .valign(Align::Center)
        .halign(Align::Center)
        .orientation(Orientation::Horizontal)
        .build();

    main_box.append(&cancel_box);
    main_box.append(&logout_box);
    main_box.append(&reboot_box);
    main_box.append(&shutdown_box);
    main_box.append(&lockscreen_box);

    let window = ApplicationWindow::builder()
        .application(app)
        .title("Rs SignOut")
        .fullscreened(true)
        .child(&main_box)
        .build();

    let action_close = SimpleAction::new("close", None);
    action_close.connect_activate(clone!(@weak window => move |_, _| {
        window.close();
    }));

    window.add_action(&action_close);

    window.add_controller(&keydown);

    window.present();
}

When ether click the button or press the x key. I when to activate the lock screen and close the program.

0 Answers0