feat(linux): document requirements and return gtk::Box

This commit is contained in:
amrbashir 2022-06-06 14:36:22 +02:00
parent c8dcb40d56
commit 6cb2ce1c7a
No known key found for this signature in database
GPG key ID: BBD7A47A2003FF33
3 changed files with 20 additions and 6 deletions

View file

@ -106,9 +106,17 @@ impl Menu {
Submenu(self.0.add_submenu(label, enabled)) Submenu(self.0.add_submenu(label, enabled))
} }
/// Adds this menu to a [`gtk::Window`]. /// Adds this menu to a [`gtk::Window`]
///
/// This method adds a [`gtk::Box`] then adds a [`gtk::MenuBar`] as its first child and returns the [`gtk::Box`].
/// So if more widgets need to be added, then [`gtk::prelude::BoxExt::pack_start`] or
/// similiar methods should be used on the returned [`gtk::Box`].
///
/// ## Safety:
///
/// This should be called before anything is added to the window.
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
pub fn init_for_gtk_window<W>(&self, w: &W) pub fn init_for_gtk_window<W>(&self, w: &W) -> std::rc::Rc<gtk::Box>
where where
W: gtk::prelude::IsA<gtk::Container>, W: gtk::prelude::IsA<gtk::Container>,
{ {

View file

@ -3,7 +3,7 @@
use crate::util::Counter; use crate::util::Counter;
use gtk::{prelude::*, Orientation}; use gtk::{prelude::*, Orientation};
use parking_lot::Mutex; use parking_lot::Mutex;
use std::sync::Arc; use std::{rc::Rc, sync::Arc};
static COUNTER: Counter = Counter::new(); static COUNTER: Counter = Counter::new();
@ -33,7 +33,7 @@ struct InnerMenu {
// multiple times, and thus can't be used in multiple windows, entry // multiple times, and thus can't be used in multiple windows, entry
// keeps a vector of a tuple of `gtk::MenuBar` and `gtk::Box` // keeps a vector of a tuple of `gtk::MenuBar` and `gtk::Box`
// and push to it every time `Menu::init_for_gtk_window` is called. // and push to it every time `Menu::init_for_gtk_window` is called.
gtk_items: Vec<(gtk::MenuBar, gtk::Box)>, gtk_items: Vec<(gtk::MenuBar, Rc<gtk::Box>)>,
} }
pub struct Menu(Arc<Mutex<InnerMenu>>); pub struct Menu(Arc<Mutex<InnerMenu>>);
@ -61,7 +61,7 @@ impl Menu {
Submenu(entry) Submenu(entry)
} }
pub fn init_for_gtk_window<W>(&self, w: &W) pub fn init_for_gtk_window<W>(&self, w: &W) -> Rc<gtk::Box>
where where
W: IsA<gtk::Container>, W: IsA<gtk::Container>,
{ {
@ -73,7 +73,12 @@ impl Menu {
w.add(&vbox); w.add(&vbox);
vbox.show_all(); vbox.show_all();
let vbox = Rc::new(vbox);
let vbox_c = Rc::clone(&vbox);
self.0.lock().gtk_items.push((menu_bar, vbox)); self.0.lock().gtk_items.push((menu_bar, vbox));
vbox_c
} }
} }

View file

@ -1,3 +1,5 @@
#![allow(unused)]
use std::sync::atomic::{AtomicU64, Ordering}; use std::sync::atomic::{AtomicU64, Ordering};
pub struct Counter(AtomicU64); pub struct Counter(AtomicU64);
@ -11,7 +13,6 @@ impl Counter {
self.0.fetch_add(1, Ordering::Relaxed) self.0.fetch_add(1, Ordering::Relaxed)
} }
#[allow(unused)]
pub fn current(&self) -> u64 { pub fn current(&self) -> u64 {
self.0.load(Ordering::Relaxed) self.0.load(Ordering::Relaxed)
} }