antoyo / relm

Idiomatic, GTK+-based, GUI library, inspired by Elm, written in Rust
MIT License
2.43k stars 78 forks source link

Remove menu bar #197

Closed MGlolenstine closed 4 years ago

MGlolenstine commented 4 years ago

Is there a way to remove the menu bar, which appears after I remove the title bar? Screenshot from 2020-02-26 12-23-30

This is my code and I'm not setting any top-padding anywhere.

The code is just slightly modified treeview.rs example.

use std::io;

use glib::StaticType;
use gtk::{CellLayoutExt, ContainerExt, GtkWindowExt, Inhibit, TreeModelExt, TreeSelectionExt, TreeView, TreeViewExt, WidgetExt, Window, WindowType, prelude::GtkListStoreExtManual, Adjustment};
use relm_derive::Msg;
use relm::{connect, Relm, Update, Widget};

// These two constants stand for the columns of the listmodel and the listview
const VALUE_COL: i32 = 0;
const IS_DIR_COL: i32 = 1;

struct Model {
    text: String,
}

#[derive(Msg)]
enum Msg {
    ItemSelect,
    Quit,
}

struct Win {
    tree_view: TreeView,
    window: Window,
}

impl Update for Win {
    type Model = Model;
    type ModelParam = ();
    type Msg = Msg;

    fn model(_: &Relm<Self>, _: ()) -> Model {
        Model {
            text: "test".to_string()
        }
    }

    fn update(&mut self, event: Msg) {
        match event {
            Msg::ItemSelect => {
                let (list_model, iter) = self.tree_view.get_selection().get_selected().unwrap();
                println!("{:#?}",list_model.get_value(&iter, VALUE_COL).get::<String>().ok().and_then(|x| x).expect("Something went wrong reading the selected value!"))
            },
            Msg::Quit => gtk::main_quit(),
        }
    }
}

impl Widget for Win {
    type Root = Window;

    fn root(&self) -> Self::Root {
        self.window.clone()
    }

    fn view(relm: &Relm<Self>, model: Self::Model) -> Self {
        let window = gtk::Window::new(WindowType::Toplevel);
        let vbox = gtk::ScrolledWindow::new(None::<&Adjustment>, None::<&Adjustment>);
        let tree_view = gtk::TreeView::new();
        let column = gtk::TreeViewColumn::new();
        let cell = gtk::CellRendererText::new();

        window.set_border_width(0);
        window.set_position(gtk::WindowPosition::Center);
        window.set_default_size(480, 272);
        window.set_resizable(false);
        window.set_decorated(false);

        column.pack_start(&cell, true);
        // Assiciate view's column with model's id column
        column.add_attribute(&cell, "text", VALUE_COL);
        tree_view.append_column(&column);

        let store_model = create_and_fill_model(&model.text)
            .expect("create_and_fill_model failed");
        tree_view.set_model(Some(&store_model));

        vbox.add(&tree_view);
        window.add(&vbox);

        window.show_all();

        connect!(relm, tree_view, connect_cursor_changed(_), Msg::ItemSelect);
        connect!(relm, window, connect_delete_event(_, _), return (Some(Msg::Quit), Inhibit(false)));

        Win {
            tree_view,
            window,
        }
    }
}

fn create_and_fill_model(_: &String) -> io::Result<gtk::ListStore> {
    // Single row model
    let model = gtk::ListStore::new(&[String::static_type(), bool::static_type()]);

    // Add the parent directory
    for i in 1..100 {
        model.insert_with_values(None,
                                 &[VALUE_COL as u32, IS_DIR_COL as u32],
                                 &[&format!("Test #{}", i), &true]);
    }
    Ok(model)
}

fn main() {
    Win::run(()).expect("Win::run failed");
}
antoyo commented 4 years ago

I'm not sure which menu bar you're talking about, but if you want to hide the header of the tree view, please use this:

tree_view.set_headers_visible(false);

(By the way, please ask gtk-related questions on the gtk+ or gtk-rs related channel, to only keep relm-related issues here.)

MGlolenstine commented 4 years ago

Yes, sorry... I thought that it was an empty menu bar, but now I see that it's the header of the tree view... Thanks for the information and in the future I'll make sure to report it on other issue trackers than here, if it's not related to the RELM.

Thanks!