Adding window close to the EventControllerKey

I am new to rust and gtk4. I am writing a small logout app… I trying figure out how to call the window close function into the EvenControllerKey. when I press x I want to run the lockscreen and close the Window. Here is my code and if someone can give my an example. I would appreciate it.

fn main() {

// Create a new application
let app = Application::builder().application_id(APP_ID).build();

app.connect_startup(|_| load_css());
// Connect to "activate" signal of `app`
app.connect_activate(build_ui);
app.set_accels_for_action("win.close", &["c"]);
// Run the application
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) {

// Image for Cancel Button
let cancel_image = Image::builder()
    .pixel_size(150)
    .file("/home/lee/img/cancel.png")
    .build();

//Label for Button
let cancel_label = Label::builder().label("Cancel (C)").build();
// Create a button with label and margins
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();

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

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

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

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

let reboot_label = Label::builder()
    .label("Reboot (R)")
    .valign(Align::Start)
    .halign(Align::Center)
    .build();

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

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

let shutdown_label = Label::builder()
    .label("Shut Down (S)")
    .valign(Align::Start)
    .halign(Align::Center)
    .build();

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

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

let lockscreen_label = Label::builder()
    .label("Lock Screen (X)")
    .valign(Align::Start)
    .halign(Align::Center)
    .build();

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

logout_button.connect_clicked(|_| {
    log_out();
});

reboot_button.connect_clicked(|_| {
    reboot();
});

shutdown_button.connect_clicked(|_| {
    shutdown();
});

//lockscreen_button.connect_clicked(|_| {
//    //lockscreen();
//});

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

    //println!("{:?}", key);
    Inhibit(false)
});

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);

struct Context {
    wnd: Option<ApplicationWindow>,
}
let ctx = Context { wnd: None };

let ctx: Rc<RefCell<Context>> = Rc::new(RefCell::new(ctx));

app.connect_activate(clone!(
    @strong ctx =>
    move |app| {
        let mut ctx = ctx.borrow.mut()
        let window = ApplicationWindow::builder()
            .application(app)
            .title("Rs Sign Out")
            .fullscreened(true)
            .child(&main_box)
            .build();
        ctx.wnd = Some(window);
    }
));
// Create a window

fn log_out() {
    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());
}
let windows_clone = window.clone();

//let lockscreen_command = move |_| {
//    let lock_screen = Command::new("betterlockscreen")
//        .args(["-l", "dim"])
//       .output()
//        .expect("Failed to execute Command");
//
//    assert!(lock_screen.status.success());
//    windows_clone.close();
//};

lockscreen_button.connect_clicked(move |_| {
    let lock_screen = Command::new("betterlockscreen")
        .args(["-l", "dim"])
        .output()
        .expect("Failed to execute Command");

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

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

}));

window.add_action(&action_close);

// Add Key Press To the Window
window.add_controller(&keydown);
// Present window
window.present();

}

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.