Skip to content

Commit

Permalink
refactor(linux): relax gtk constraints on methods (#139)
Browse files Browse the repository at this point in the history
* refactor(linux): relax gtk constraints on methods

* fix test
  • Loading branch information
amrbashir authored Nov 13, 2023
1 parent 9c10715 commit ae316bf
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 42 deletions.
12 changes: 12 additions & 0 deletions .changes/gtk-constraints.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
"muda": "minor"
---

Changed `ContextMenu::show_context_menu_for_gtk_window` to take `gtk::Window` instead of `gtk::ApplicationWindow` and relaxed generic gtk constraints on the following methods:

- `MenuBar::init_for_gtk_window`
- `MenuBar::remove_for_gtk_window`
- `MenuBar::hide_for_gtk_window`
- `MenuBar::show_for_gtk_window`
- `MenuBar::is_visible_on_gtk_window`
- `MenuBar::gtk_menubar_for_gtk_window`
2 changes: 1 addition & 1 deletion examples/tao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ fn show_context_menu(window: &Window, menu: &dyn ContextMenu, position: Option<P
#[cfg(target_os = "windows")]
menu.show_context_menu_for_hwnd(window.hwnd() as _, position);
#[cfg(target_os = "linux")]
menu.show_context_menu_for_gtk_window(window.gtk_window(), position);
menu.show_context_menu_for_gtk_window(window.gtk_window().as_ref(), position);
#[cfg(target_os = "macos")]
menu.show_context_menu_for_nsview(window.ns_view() as _, position);
}
Expand Down
2 changes: 1 addition & 1 deletion examples/wry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ fn show_context_menu(window: &Window, menu: &dyn ContextMenu, position: Option<m
#[cfg(target_os = "windows")]
menu.show_context_menu_for_hwnd(window.hwnd() as _, position);
#[cfg(target_os = "linux")]
menu.show_context_menu_for_gtk_window(window.gtk_window(), position);
menu.show_context_menu_for_gtk_window(window.gtk_window().as_ref(), position);
#[cfg(target_os = "macos")]
menu.show_context_menu_for_nsview(window.ns_view() as _, position);
}
Expand Down
6 changes: 1 addition & 5 deletions src/items/submenu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -233,11 +233,7 @@ impl ContextMenu for Submenu {
}

#[cfg(target_os = "linux")]
fn show_context_menu_for_gtk_window(
&self,
w: &gtk::ApplicationWindow,
position: Option<Position>,
) {
fn show_context_menu_for_gtk_window(&self, w: &gtk::Window, position: Option<Position>) {
self.inner
.borrow_mut()
.show_context_menu_for_gtk_window(w, position)
Expand Down
12 changes: 4 additions & 8 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@
//! # let menu = muda::Menu::new();
//! # let window_hwnd = 0;
//! # #[cfg(target_os = "linux")]
//! # let gtk_window = gtk::ApplicationWindow::builder().build();
//! # let gtk_window = gtk::Window::builder().build();
//! # #[cfg(target_os = "linux")]
//! # let vertical_gtk_box = gtk::Box::new(gtk::Orientation::Vertical, 0);
//! // --snip--
Expand All @@ -96,7 +96,7 @@
//! # let menu = muda::Menu::new();
//! # let window_hwnd = 0;
//! # #[cfg(target_os = "linux")]
//! # let gtk_window = gtk::ApplicationWindow::builder().build();
//! # let gtk_window = gtk::Window::builder().build();
//! # #[cfg(target_os = "macos")]
//! # let nsview = 0 as *mut objc::runtime::Object;
//! // --snip--
Expand Down Expand Up @@ -324,15 +324,11 @@ pub trait ContextMenu {
#[cfg(target_os = "windows")]
fn detach_menu_subclass_from_hwnd(&self, hwnd: isize);

/// Shows this menu as a context menu inside a [`gtk::ApplicationWindow`]
/// Shows this menu as a context menu inside a [`gtk::Window`]
///
/// - `position` is relative to the window top-left corner, if `None`, the cursor position is used.
#[cfg(target_os = "linux")]
fn show_context_menu_for_gtk_window(
&self,
w: &gtk::ApplicationWindow,
position: Option<Position>,
);
fn show_context_menu_for_gtk_window(&self, w: &gtk::Window, position: Option<Position>);

/// Get the underlying gtk menu reserved for context menus.
#[cfg(target_os = "linux")]
Expand Down
29 changes: 11 additions & 18 deletions src/menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,15 +161,15 @@ impl Menu {
self.inner.borrow().items()
}

/// Adds this menu to a [`gtk::ApplicationWindow`]
/// Adds this menu to a [`gtk::Window`]
///
/// - `container`: this is an optional paramter to specify a container for the [`gtk::MenuBar`],
/// it is highly recommended to pass a container, otherwise the menubar will be added directly to the window,
/// which is usually not the desired behavior.
///
/// ## Example:
/// ```no_run
/// let window = gtk::ApplicationWindow::builder().build();
/// let window = gtk::Window::builder().build();
/// let vbox = gtk::Box::new(gtk::Orientation::Vertical, 0);
/// let menu = muda::Menu::new();
/// // -- snip, add your menu items --
Expand All @@ -183,11 +183,9 @@ impl Menu {
#[cfg(target_os = "linux")]
pub fn init_for_gtk_window<W, C>(&self, window: &W, container: Option<&C>) -> crate::Result<()>
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
W: gtk::prelude::IsA<gtk::Container>,
C: gtk::prelude::IsA<gtk::Container>,
C: gtk::prelude::IsA<gtk::Box>,
{
self.inner
.borrow_mut()
Expand Down Expand Up @@ -231,11 +229,10 @@ impl Menu {
self.inner.borrow_mut().haccel()
}

/// Removes this menu from a [`gtk::ApplicationWindow`]
/// Removes this menu from a [`gtk::Window`]
#[cfg(target_os = "linux")]
pub fn remove_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
{
self.inner.borrow_mut().remove_for_gtk_window(window)
Expand All @@ -247,11 +244,11 @@ impl Menu {
self.inner.borrow_mut().remove_for_hwnd(hwnd)
}

/// Hides this menu from a [`gtk::ApplicationWindow`]
/// Hides this menu from a [`gtk::Window`]
#[cfg(target_os = "linux")]
pub fn hide_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
{
self.inner.borrow_mut().hide_for_gtk_window(window)
}
Expand All @@ -262,11 +259,11 @@ impl Menu {
self.inner.borrow().hide_for_hwnd(hwnd)
}

/// Shows this menu on a [`gtk::ApplicationWindow`]
/// Shows this menu on a [`gtk::Window`]
#[cfg(target_os = "linux")]
pub fn show_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
{
self.inner.borrow_mut().show_for_gtk_window(window)
}
Expand All @@ -277,11 +274,11 @@ impl Menu {
self.inner.borrow().show_for_hwnd(hwnd)
}

/// Returns whether this menu visible on a [`gtk::ApplicationWindow`]
/// Returns whether this menu visible on a [`gtk::Window`]
#[cfg(target_os = "linux")]
pub fn is_visible_on_gtk_window<W>(&self, window: &W) -> bool
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
{
self.inner.borrow().is_visible_on_gtk_window(window)
}
Expand All @@ -291,7 +288,7 @@ impl Menu {
/// This is useful to get information about the menubar for example its height.
pub fn gtk_menubar_for_gtk_window<W>(self, window: &W) -> Option<gtk::MenuBar>
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
{
self.inner.borrow().gtk_menubar_for_gtk_window(window)
}
Expand Down Expand Up @@ -339,11 +336,7 @@ impl ContextMenu for Menu {
}

#[cfg(target_os = "linux")]
fn show_context_menu_for_gtk_window(
&self,
window: &gtk::ApplicationWindow,
position: Option<Position>,
) {
fn show_context_menu_for_gtk_window(&self, window: &gtk::Window, position: Option<Position>) {
self.inner
.borrow_mut()
.show_context_menu_for_gtk_window(window, position)
Expand Down
21 changes: 12 additions & 9 deletions src/platform_impl/gtk/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -251,11 +251,9 @@ impl Menu {
container: Option<&C>,
) -> crate::Result<()>
where
W: IsA<gtk::ApplicationWindow>,
W: IsA<gtk::Window>,
W: IsA<gtk::Container>,
C: IsA<gtk::Container>,
C: IsA<gtk::Box>,
{
let id = window.as_ptr() as u32;

Expand Down Expand Up @@ -283,8 +281,14 @@ impl Menu {

// add the menubar to the specified widget, otherwise to the window
if let Some(container) = container {
container.add(menu_bar);
container.reorder_child(menu_bar, 0);
if container.type_().name() == "GtkBox" {
container
.dynamic_cast_ref::<gtk::Box>()
.unwrap()
.pack_start(menu_bar, false, false, 0);
} else {
container.add(menu_bar);
}
} else {
window.add(menu_bar);
}
Expand All @@ -297,7 +301,6 @@ impl Menu {

pub fn remove_for_gtk_window<W>(&mut self, window: &W) -> crate::Result<()>
where
W: IsA<gtk::ApplicationWindow>,
W: IsA<gtk::Window>,
{
let id = window.as_ptr() as u32;
Expand All @@ -321,7 +324,7 @@ impl Menu {

pub fn hide_for_gtk_window<W>(&mut self, window: &W) -> crate::Result<()>
where
W: IsA<gtk::ApplicationWindow>,
W: IsA<gtk::Window>,
{
self.gtk_menubars
.get(&(window.as_ptr() as u32))
Expand All @@ -332,7 +335,7 @@ impl Menu {

pub fn show_for_gtk_window<W>(&self, window: &W) -> crate::Result<()>
where
W: IsA<gtk::ApplicationWindow>,
W: IsA<gtk::Window>,
{
self.gtk_menubars
.get(&(window.as_ptr() as u32))
Expand All @@ -343,7 +346,7 @@ impl Menu {

pub fn is_visible_on_gtk_window<W>(&self, window: &W) -> bool
where
W: IsA<gtk::ApplicationWindow>,
W: IsA<gtk::Window>,
{
self.gtk_menubars
.get(&(window.as_ptr() as u32))
Expand All @@ -353,7 +356,7 @@ impl Menu {

pub fn gtk_menubar_for_gtk_window<W>(&self, window: &W) -> Option<gtk::MenuBar>
where
W: gtk::prelude::IsA<gtk::ApplicationWindow>,
W: gtk::prelude::IsA<gtk::Window>,
{
self.gtk_menubars.get(&(window.as_ptr() as u32)).cloned()
}
Expand Down

0 comments on commit ae316bf

Please sign in to comment.