Skip to content

Commit

Permalink
Revert "Implement global shortcut on Linux, close #307 (#308)"
Browse files Browse the repository at this point in the history
This reverts commit 9c2841f.
  • Loading branch information
Ngo Iok Ui (Wu Yu Wei) authored Feb 28, 2022
1 parent 8ccdaaa commit 65f9442
Show file tree
Hide file tree
Showing 5 changed files with 387 additions and 179 deletions.
4 changes: 2 additions & 2 deletions examples/global_shortcut.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {
let shortcut_1 = Accelerator::new(SysMods::Shift, KeyCode::ArrowUp);
let shortcut_2 = Accelerator::new(RawMods::AltCtrlMeta, KeyCode::KeyB);
// use string parser to generate accelerator (require `std::str::FromStr`)
let shortcut_3 = Accelerator::from_str("COMMANDORCONTROL+3").unwrap();
let shortcut_3 = Accelerator::from_str("COMMANDORCONTROL+SHIFT+3").unwrap();
let shortcut_4 = Accelerator::from_str("COMMANDORCONTROL+shIfT+DOWN").unwrap();

// save a reference to unregister it later
Expand Down Expand Up @@ -64,7 +64,7 @@ fn main() {
// by example `shortcut_1` will NOT match AcceleratorId::new("SHIFT+UP") as it's
// been created with a struct and the ID is generated automatically
Event::GlobalShortcutEvent(hotkey_id)
if hotkey_id == AcceleratorId::new("COMMANDORCONTROL+3") =>
if hotkey_id == AcceleratorId::new("COMMANDORCONTROL+SHIFT+3") =>
{
println!("Pressed on `shortcut_3`");
}
Expand Down
67 changes: 19 additions & 48 deletions src/platform_impl/linux/event_loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use glib::{source::Priority, Continue, MainContext};
use gtk::{prelude::*, AboutDialog, Inhibit};

use crate::{
accelerator::AcceleratorId,
dpi::{LogicalPosition, LogicalSize},
event::{ElementState, Event, MouseButton, StartCause, WindowEvent},
event_loop::{ControlFlow, EventLoopClosed, EventLoopWindowTarget as RootELW},
Expand All @@ -28,7 +29,6 @@ use crate::{
};

use super::{
global_shortcut::GlobalShortcutEvent,
keyboard,
monitor::MonitorHandle,
window::{WindowId, WindowRequest},
Expand All @@ -44,8 +44,6 @@ pub struct EventLoopWindowTarget<T> {
pub(crate) windows: Rc<RefCell<HashSet<WindowId>>>,
/// Window requests sender
pub(crate) window_requests_tx: glib::Sender<(WindowId, WindowRequest)>,
/// Global shortcut requests sender
pub(crate) shortcut_requests_tx: glib::Sender<GlobalShortcutEvent>,
_marker: std::marker::PhantomData<T>,
}

Expand Down Expand Up @@ -118,16 +116,13 @@ impl<T: 'static> EventLoop<T> {
// Create event loop window target.
let (window_requests_tx, window_requests_rx) = glib::MainContext::channel(Priority::default());
let window_requests_tx_ = window_requests_tx.clone();
let (shortcut_requests_tx, shortcut_requests_rx) =
glib::MainContext::channel(Priority::default());
let display = gdk::Display::default()
.expect("GdkDisplay not found. This usually means `gkt_init` hasn't called yet.");
let window_target = EventLoopWindowTarget {
display,
app,
windows: Rc::new(RefCell::new(HashSet::new())),
window_requests_tx,
shortcut_requests_tx,
_marker: std::marker::PhantomData,
};

Expand All @@ -141,39 +136,6 @@ impl<T: 'static> EventLoop<T> {
Continue(true)
});

// Global Shortcut Event Request
let app = app_.clone();
let event_tx_ = event_tx.clone();
shortcut_requests_rx.attach(Some(&context), move |event| {
match event {
GlobalShortcutEvent::Register((id, key)) => {
let name = format!("{}", id.0);
let action = gio::SimpleAction::new(&name, None);
let event_tx = event_tx_.clone();
action.connect_activate(move |_, _| {
if let Err(e) = event_tx.send(Event::GlobalShortcutEvent(id)) {
log::warn!(
"Failed to send global shortcut event to event channel: {}",
e
);
}
});
app.add_action(&action);
app.set_accels_for_action(&format!("app.{}", name), &[&key]);
}
GlobalShortcutEvent::UnRegister(id) => {
app.remove_action(&format!("{}", id.0));
}
GlobalShortcutEvent::UnRegisterAll => {
for action in app.list_actions() {
app.remove_action(&action);
}
}
}

Continue(true)
});

// Window Request
window_requests_rx.attach(Some(&context), move |(id, request)| {
if let Some(window) = app_.window_by_id(id.0) {
Expand Down Expand Up @@ -605,9 +567,9 @@ impl<T: 'static> EventLoop<T> {

let tx_clone = event_tx.clone();
let keyboard_handler = Rc::new(move |event_key: EventKey, element_state| {
let mut mods = keyboard::get_modifiers(event_key.clone());
// if we have a modifier lets send it
if event_key.is_modifier() {
let mut mods = keyboard::get_modifiers(event_key.clone());
if !mods.is_empty() {
// if we release the modifier tell the world
if ElementState::Released == element_state {
mods = ModifiersState::empty();
Expand Down Expand Up @@ -765,16 +727,25 @@ impl<T: 'static> EventLoop<T> {
menubar.show_all();
}
}
WindowRequest::GlobalHotKey(_hotkey_id) => {}
}
} else if id == WindowId::dummy() {
if let WindowRequest::Menu((None, Some(menu_id))) = request {
if let Err(e) = event_tx.send(Event::MenuEvent {
window_id: None,
menu_id,
origin: MenuType::ContextMenu,
}) {
log::warn!("Failed to send status bar event to event channel: {}", e);
match request {
WindowRequest::GlobalHotKey(hotkey_id) => {
if let Err(e) = event_tx.send(Event::GlobalShortcutEvent(AcceleratorId(hotkey_id))) {
log::warn!("Failed to send global hotkey event to event channel: {}", e);
}
}
WindowRequest::Menu((None, Some(menu_id))) => {
if let Err(e) = event_tx.send(Event::MenuEvent {
window_id: None,
menu_id,
origin: MenuType::ContextMenu,
}) {
log::warn!("Failed to send status bar event to event channel: {}", e);
}
}
_ => {}
}
}
Continue(true)
Expand Down
Loading

0 comments on commit 65f9442

Please sign in to comment.