Skip to content

Commit

Permalink
[Tauri] Press Alt to toggle the window menu (#763)
Browse files Browse the repository at this point in the history
  • Loading branch information
martpie committed Jun 13, 2024
1 parent 619a2e5 commit 09a7557
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 2 deletions.
4 changes: 4 additions & 0 deletions src-tauri/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ fn main() {
tauri_build::try_build(
tauri_build::Attributes::new()
.codegen(tauri_build::CodegenContext::new())
.plugin(
"app-menu",
tauri_build::InlinedPlugin::new().commands(&["toggle"]),
)
.plugin(
"config",
tauri_build::InlinedPlugin::new().commands(&["get_config", "set_config"]),
Expand Down
1 change: 1 addition & 0 deletions src-tauri/capabilities/main.json
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
"menu:allow-popup",
"notification:default",
"window:allow-show",
"app-menu:allow-toggle",
"config:allow-set-config",
"config:allow-get-config",
"cover:allow-get-cover",
Expand Down
3 changes: 3 additions & 0 deletions src-tauri/src/libs/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@ use thiserror::Error;
*/
#[derive(Debug, Error)]
pub enum MuseeksError {
#[error(transparent)]
Tauri(#[from] tauri::Error),

#[error(transparent)]
Lofty(#[from] lofty::LoftyError),

Expand Down
26 changes: 24 additions & 2 deletions src-tauri/src/plugins/app_menu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,34 @@ use tauri::{
Manager, Runtime,
};

use crate::libs::error::AnyResult;
use crate::libs::events::IPCEvent;

#[tauri::command]
pub async fn toggle<R: Runtime>(window: tauri::Window<R>) -> AnyResult<()> {
// On macOS, the menu is global, and thus does not need to be toggled
#[cfg(not(target_os = "macos"))]
{
match window.is_menu_visible() {
Ok(true) => {
window.hide_menu()?;
}
Ok(false) => {
window.show_menu()?;
}
_ => (),
}
}

#[cfg(target_os = "macos")]
drop(window); // Suppress warning about unused variable.

Ok(())
}

pub fn init<R: Runtime>() -> TauriPlugin<R> {
Builder::new("app-menu")
.invoke_handler(tauri::generate_handler![/*popup, toggle*/])
.invoke_handler(tauri::generate_handler![toggle])
.on_window_ready(|window| {
let app_handle = window.app_handle();

Expand Down Expand Up @@ -158,7 +181,6 @@ pub fn init<R: Runtime>() -> TauriPlugin<R> {
window.hide_menu().unwrap();
}

// TODO: hide/show menu with Alt on Linux + Windows
// TODO: support menu events
// https://github.com/tauri-apps/tauri/issues/9060
// window.on_menu_event(|_app_handle, event| {
Expand Down
4 changes: 4 additions & 0 deletions src/components/Events/GlobalKeyBindings.tsx
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { useCallback } from 'react';
import Keybinding from 'react-keybinding-component';
import { useNavigate } from 'react-router-dom';
import { invoke } from '@tauri-apps/api/core';

import { usePlayerAPI } from '../../stores/usePlayerStore';
import { isCtrlKey } from '../../lib/utils-events';
Expand Down Expand Up @@ -40,6 +41,9 @@ function GlobalKeyBindings() {
e.stopPropagation();
playerAPI.jumpTo(player.getCurrentTime() + 10);
break;
case 'Alt':
await invoke('plugin:app-menu|toggle');
break;
default:
break;
}
Expand Down

0 comments on commit 09a7557

Please sign in to comment.