-
-
Notifications
You must be signed in to change notification settings - Fork 25
Add keyboard shortcut feature #160
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ehamiter
wants to merge
2
commits into
cosmic-utils:master
Choose a base branch
from
ehamiter:keyboard-shortcut
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+254
−37
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| //! IPC mechanism for external toggle functionality via file-based signaling. | ||
| //! | ||
| //! This module provides a simple IPC mechanism using a signal file in XDG_RUNTIME_DIR. | ||
| //! When the `--toggle` command is invoked, it writes a timestamp to the signal file. | ||
| //! The timestamp ensures the file content changes on each toggle, which triggers | ||
| //! the file watcher to notify the app to toggle the popup. | ||
|
|
||
| use std::path::PathBuf; | ||
| use std::fs; | ||
| use std::time::SystemTime; | ||
|
|
||
| use cosmic::iced_futures::Subscription; | ||
| use crate::message::AppMsg; | ||
|
|
||
| /// Get the signal file path for IPC toggle functionality. | ||
| /// Returns None if XDG_RUNTIME_DIR is not set. | ||
| pub fn get_signal_file_path() -> Option<PathBuf> { | ||
| std::env::var("XDG_RUNTIME_DIR").ok().map(|runtime_dir| { | ||
| PathBuf::from(runtime_dir).join("cosmic-clipboard-manager-toggle") | ||
| }) | ||
| } | ||
|
|
||
| /// Send a toggle signal by writing a timestamp to the signal file. | ||
| /// The timestamp ensures the file content changes, triggering the file watcher. | ||
| pub fn send_toggle_signal() -> std::io::Result<()> { | ||
| let signal_file = get_signal_file_path().ok_or_else(|| { | ||
| std::io::Error::new( | ||
| std::io::ErrorKind::NotFound, | ||
| "XDG_RUNTIME_DIR not set - cannot send toggle signal" | ||
| ) | ||
| })?; | ||
|
|
||
| // Write current timestamp to signal file. | ||
| // The timestamp value itself isn't used, but ensures the file content changes | ||
| // on each toggle, which triggers the file watcher to detect the change. | ||
| let timestamp = SystemTime::now() | ||
| .duration_since(SystemTime::UNIX_EPOCH) | ||
| .map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))? | ||
| .as_millis() | ||
| .to_string(); | ||
|
|
||
| fs::write(&signal_file, timestamp)?; | ||
| Ok(()) | ||
| } | ||
|
|
||
| /// Create a file watcher subscription that monitors the signal file for changes. | ||
| /// When the file is modified, it sends a CheckSignalFile message to toggle the popup. | ||
| pub fn signal_file_watcher() -> Subscription<AppMsg> { | ||
| use notify::{Watcher, RecursiveMode, Event}; | ||
| use futures::stream; | ||
|
|
||
| Subscription::run_with_id( | ||
| "signal_file_watcher", | ||
| stream::unfold((), |_| async { | ||
| // Only set up watcher if XDG_RUNTIME_DIR is set | ||
| let signal_file = match get_signal_file_path() { | ||
| Some(path) => path, | ||
| None => { | ||
| // If XDG_RUNTIME_DIR is not set, just wait forever | ||
| futures::future::pending::<()>().await; | ||
| return Some((AppMsg::CheckSignalFile, ())); | ||
| } | ||
| }; | ||
|
|
||
| let (tx, mut rx) = tokio::sync::mpsc::channel(1); | ||
|
|
||
| let mut watcher = notify::recommended_watcher(move |res: Result<Event, notify::Error>| { | ||
| if res.is_ok() { | ||
| let _ = tx.blocking_send(()); | ||
| } | ||
| }).ok()?; | ||
|
|
||
| // Watch the signal file's parent directory since the file might not exist yet | ||
| if let Some(parent) = signal_file.parent() { | ||
| let _ = watcher.watch(parent, RecursiveMode::NonRecursive); | ||
|
|
||
| // Wait for file change notification | ||
| rx.recv().await; | ||
| } | ||
|
|
||
| Some((AppMsg::CheckSignalFile, ())) | ||
| }) | ||
| ) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.