2

I'd like to use a SimpleActionGroup registered to a Widget that handles SimpleActions. But anyhow every Button that targets to Actions with my custom prefix are grayed out and can't be clicked. However Buttons that are targeting the action prefixes "app" and "win" are working as expected.

Application with two Buttons

Here is the part in my code where my custom prefix is defined.

// Create action group for view
let view_action_group = gio::SimpleActionGroup::new();
view.insert_action_group("view", &view_action_group);

// SimpleAction for view
let view_action = gio::SimpleAction::new("test", None);
view_action.connect_activate(|_, _| println!("view.test"));
view_action_group.add_action(&view_action);

Here I respond to SimpleActions with prefix "app"

// SimpleAction for app
let app_action = gio::SimpleAction::new("test", None);
app_action.connect_activate(|_, _| println!("app.test"));
app.add_action(&app_action);

The SimpleAction itself should be ok, because if I start the application with GTK Inspector I can activate it.

GTK Inspector invoking actions

So what am I missing that the secound Button is not greyed out and responds to my SimpleAction?

Here is the full code:

extern crate gtk;
extern crate gio;

use std::env::args;
use gtk::{GtkApplicationExt, GtkWindowExt, ContainerExt, WidgetExt, HeaderBarExt, ActionableExt};
use gio::{SimpleActionExt, ActionMapExt, ApplicationExt, ApplicationExtManual};

struct Window {
    pub window: gtk::Window,
}

impl Window {
    pub fn new() -> Self {
        // Create Widgets and window
        let header_bar = gtk::HeaderBar::new();
        let window = gtk::Window::new(gtk::WindowType::Toplevel);
        let view = gtk::Box::new(gtk::Orientation::Horizontal, 5);
        let app_button = gtk::Button::new_with_label("App-Action-Button");
        let view_button = gtk::Button::new_with_label("View-Action-Button");

        // Create Action Group for view
        let view_action_group = gio::SimpleActionGroup::new();
        view.insert_action_group("view", &view_action_group);

        // SimpleAction for view
        let view_action = gio::SimpleAction::new("test", None);
        view_action.connect_activate(|_, _| println!("view.test"));
        view_action_group.add_action(&view_action);

        // Set Action for Buttons
        app_button.set_action_name("app.test");
        view_button.set_action_name("view.test");

        // Configure HeaderBar and add Buttons to it
        header_bar.set_show_close_button(true);
        header_bar.pack_start(&app_button);
        header_bar.pack_start(&view_button);

        // Window Stuff
        window.set_titlebar(&header_bar);
        window.add(&view);
        window.show_all();

        Self { window }
    }
}

struct App {
    app: gtk::Application
}

impl App {
    pub fn new () -> Self {

        // Create app and window
        let app = gtk::Application::new(Some("com.example.test"), gio::ApplicationFlags::FLAGS_NONE).unwrap();
        let window = Window::new();

        // SimpleAction for app
        let app_action = gio::SimpleAction::new("test", None);
        app_action.connect_activate(|_, _| println!("app.test"));
        app.add_action(&app_action);

        // add Signal
        let cloned_window = window.window.clone();
        app.connect_activate(move |app| app.add_window(&cloned_window));

        Self { app }
    }

    pub fn run(&self, args: &[String]) {
        self.app.run(args);
    }

}


fn main() {
    let app = App::new();
    app.run(&args().collect::<Vec<_>>());
}
dosch
  • 31
  • 6

0 Answers0