Skip to content

Commit

Permalink
refactor(KeyBinds): Add code to allow minimize and maximize app with …
Browse files Browse the repository at this point in the history
…same shortcut
  • Loading branch information
lgmarchi committed Jan 23, 2024
1 parent 0348a95 commit 29f025e
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 14 deletions.
21 changes: 14 additions & 7 deletions ui/src/utils/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,15 +141,22 @@ fn RenderGlobalShortCuts<'a>(cx: Scope<'a, GlobalShortcutProps>) -> Element<'a>
if modifiers_and_keys.contains("unknown") {
return None;
}

let global_shortcut = cx.props.global_shortcut.clone();

use_global_shortcut(cx, modifiers_and_keys.as_str(), {
to_owned![command_pressed];
to_owned![command_pressed, global_shortcut];
move || {
debounced_callback(
|| {
command_pressed.with_mut(|i| *i = true);
},
Duration::from_millis(500),
);
if global_shortcut == GlobalShortcut::SetAppVisible {
command_pressed.with_mut(|i| *i = true);
} else {
debounced_callback(
|| {
command_pressed.with_mut(|i| *i = true);
},
Duration::from_millis(500),
);
}
}
});

Expand Down
26 changes: 19 additions & 7 deletions ui/src/utils/keyboard/shortcut_handlers/navigation.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@
use dioxus_core::ScopeState;
use dioxus_desktop::use_window;
use once_cell::sync::Lazy;
use warp::sync::RwLock;

static CALL_COUNT: Lazy<RwLock<u32>> = Lazy::new(|| RwLock::new(0));

pub fn set_app_visible(cx: &ScopeState) {
let window = use_window(cx);
if window.is_visible() || window.is_minimized() {
window.set_minimized(false);
window.set_visible(true);
window.set_focus();
} else {
window.set_minimized(true);
window.set_visible(false);
*CALL_COUNT.write() += 1;

if *CALL_COUNT.read() > 1 {
*CALL_COUNT.write() = 0;
}

if *CALL_COUNT.read() == 1 {
if !window.is_visible() || window.is_minimized() {
window.set_minimized(false);
window.set_visible(true);
window.set_focus();
} else {
window.set_minimized(true);
window.set_visible(false);
}
}
}

0 comments on commit 29f025e

Please sign in to comment.