-
Notifications
You must be signed in to change notification settings - Fork 17
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
Split sync and async API #50
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
09d962d
improv: move Mode to separate file
edfloreshz 208e8c4
websys: split sync and async api
edfloreshz eb1957c
improv: split sync and async api for macOS
edfloreshz fb1c53d
linux: split sync and async api
edfloreshz a839150
windows: split sync and async api
edfloreshz 83f9ee0
fix: move back to objc2
edfloreshz a4ba7ab
fix: replace dyn with impl for return type
edfloreshz 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
This file contains 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 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,19 @@ | ||
use futures::StreamExt; | ||
|
||
#[tokio::main] | ||
async fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
detect().await; | ||
subscribe().await; | ||
Ok(()) | ||
} | ||
|
||
async fn detect() { | ||
println!("Current mode: {:?}", dark_light::detect().await); | ||
} | ||
|
||
async fn subscribe() { | ||
let mut stream = dark_light::subscribe().await; | ||
while let Some(mode) = stream.next().await { | ||
println!("System mode changed: {:?}", mode); | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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,16 @@ | ||
fn main() -> Result<(), Box<dyn std::error::Error>> { | ||
detect(); | ||
subscribe(); | ||
Ok(()) | ||
} | ||
|
||
fn detect() { | ||
println!("Current mode: {:?}", dark_light::detect()); | ||
} | ||
|
||
fn subscribe() { | ||
let stream = dark_light::subscribe(); | ||
while let Ok(mode) = stream.recv() { | ||
println!("System theme changed: {:?}", mode); | ||
} | ||
} |
This file contains 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 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,21 @@ | ||
/// Enum representing dark mode, light mode, or unspecified. | ||
#[derive(Copy, Clone, PartialEq, Eq, Debug)] | ||
pub enum Mode { | ||
/// Dark mode | ||
Dark, | ||
/// Light mode | ||
Light, | ||
/// Unspecified | ||
Default, | ||
} | ||
|
||
impl Mode { | ||
#[allow(dead_code)] | ||
pub fn from_bool(b: bool) -> Self { | ||
if b { | ||
Mode::Dark | ||
} else { | ||
Mode::Light | ||
} | ||
} | ||
} |
This file contains 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 |
---|---|---|
@@ -1,5 +1,11 @@ | ||
use crate::Mode; | ||
|
||
#[cfg(feature = "sync")] | ||
pub fn detect() -> Mode { | ||
pollster::block_on(super::get_color_scheme()) | ||
} | ||
|
||
#[cfg(not(feature = "sync"))] | ||
pub async fn detect() -> Mode { | ||
super::get_color_scheme().await | ||
} |
This file contains 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 |
---|---|---|
@@ -1,5 +1,5 @@ | ||
pub mod detect; | ||
pub mod notify; | ||
pub mod subscribe; | ||
|
||
use crate::Mode; | ||
|
||
|
This file was deleted.
Oops, something went wrong.
This file contains 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,54 @@ | ||
use std::error::Error; | ||
|
||
use ashpd::desktop::settings::Settings; | ||
use futures::{stream, Stream, StreamExt}; | ||
|
||
use crate::Mode; | ||
|
||
#[cfg(feature = "sync")] | ||
pub fn subscribe() -> std::sync::mpsc::Receiver<Mode> { | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
|
||
std::thread::spawn(move || { | ||
pollster::block_on(async { | ||
let stream = match color_scheme_stream().await { | ||
Ok(stream) => stream, | ||
Err(err) => { | ||
log::error!("Failed to subscribe to color scheme changes: {}", err); | ||
Box::pin(Box::new(stream::empty())) | ||
} | ||
}; | ||
|
||
stream | ||
.for_each(|mode| { | ||
let _ = tx.send(mode); | ||
async {} | ||
}) | ||
.await; | ||
}); | ||
}); | ||
|
||
rx | ||
} | ||
|
||
#[cfg(not(feature = "sync"))] | ||
pub async fn subscribe() -> impl Stream<Item = Mode> + Send { | ||
match color_scheme_stream().await { | ||
Ok(stream) => stream, | ||
Err(err) => { | ||
log::error!("Failed to subscribe to color scheme changes: {}", err); | ||
panic!("Failed to subscribe to color scheme changes: {}", err); | ||
} | ||
} | ||
} | ||
|
||
pub async fn color_scheme_stream() -> Result<impl Stream<Item = Mode> + Send, Box<dyn Error>> { | ||
let initial = stream::once(super::get_color_scheme()).boxed(); | ||
let later_updates = Settings::new() | ||
.await? | ||
.receive_color_scheme_changed() | ||
.await? | ||
.map(Mode::from) | ||
.boxed(); | ||
Ok(Box::pin(initial.chain(later_updates))) | ||
} |
This file contains 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 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 |
---|---|---|
@@ -1,2 +1,29 @@ | ||
pub mod detect; | ||
pub mod notify; | ||
pub mod subscribe; | ||
|
||
use objc2::{class, msg_send}; | ||
use objc2_foundation::{NSObject, NSString}; | ||
|
||
fn is_dark_mode() -> bool { | ||
unsafe { | ||
let user_defaults: *mut NSObject = msg_send![class!(NSUserDefaults), standardUserDefaults]; | ||
let apple_domain = NSString::from_str("Apple Global Domain"); | ||
let dict: *mut NSObject = msg_send![user_defaults, persistentDomainForName:&*apple_domain]; | ||
|
||
if !dict.is_null() { | ||
let style_key = NSString::from_str("AppleInterfaceStyle"); | ||
let style: *mut NSObject = msg_send![dict, objectForKey:&*style_key]; | ||
|
||
if !style.is_null() { | ||
// Compare with "Dark" | ||
let dark_str = NSString::from_str("Dark"); | ||
let is_dark: bool = msg_send![style, isEqualToString:&*dark_str]; | ||
is_dark | ||
} else { | ||
false | ||
} | ||
} else { | ||
false | ||
} | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains 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,38 @@ | ||
use crate::Mode; | ||
|
||
#[cfg(feature = "sync")] | ||
pub fn subscribe() -> std::sync::mpsc::Receiver<Mode> { | ||
let (tx, rx) = std::sync::mpsc::channel(); | ||
let mut last_mode = crate::detect(); | ||
|
||
tx.send(last_mode).unwrap(); | ||
|
||
std::thread::spawn(move || loop { | ||
let current_mode = crate::detect(); | ||
|
||
if current_mode != last_mode { | ||
if tx.send(current_mode).is_err() { | ||
break; | ||
} | ||
last_mode = current_mode; | ||
} | ||
}); | ||
|
||
rx | ||
} | ||
|
||
#[cfg(not(feature = "sync"))] | ||
pub async fn subscribe() -> impl futures::Stream<Item = Mode> { | ||
Box::pin(futures::stream::unfold( | ||
crate::detect().await, | ||
|last_mode| async move { | ||
loop { | ||
let current_mode = crate::detect().await; | ||
|
||
if current_mode != last_mode { | ||
return Some((current_mode, current_mode)); | ||
} | ||
} | ||
}, | ||
)) | ||
} |
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Tip: You can avoid the brittle
msg_send!
here by importingNSUserDefaults
fromobjc2-foundation
(might have to modifyCargo.toml
to include the"NSUserDefaults"
Cargo feature first).